-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
236 lines (195 loc) · 7.65 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import requests, os, uuid, json
from dotenv import load_dotenv
load_dotenv()
import sys
sys.path.append('./src')
from flask import Flask, request, render_template, redirect, abort, jsonify, flash, url_for
from flask_cors import CORS
from src.flickr8k import *
from src.flickr30k import *
from src.VitGpt2ImageCaption import *
import warnings
warnings.filterwarnings("ignore")
from sqlalchemy import or_
import numpy as np
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import tensorflow as tf
from werkzeug.utils import secure_filename
from flask import send_from_directory
from keras.preprocessing import image
import matplotlib.pyplot as plt
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def create_app(test_config=None):
# Create and configure the app
app = Flask(__name__)
app.config.from_pyfile('settings.py')
#setup_db(app)
CORS(app, resources={r"/api/*": {"origins": "*"}})
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Headers', 'Content-Type, Authorization,true')
response.headers.add('Access-Control-Allow-Methods', 'GET, POST, PATCH, DELETE, OPTIONS')
return response
@app.route("/")
def landing_page():
return render_template("pages/index.html")
@app.route("/caption")
def caption_page():
return render_template("pages/caption.html")
@app.route('/caption', methods=['POST'])
def index_post():
# Read the values from the form
original_text = request.form['text']
target_language = request.form['language']
# Load the values from .env
key = os.environ['KEY']
endpoint = os.environ['ENDPOINT']
location = os.environ['LOCATION']
# Indicate that we want to translate and the API version (3.0) and the target language
path = '/translate?api-version=3.0'
# Add the target language parameter
target_language_parameter = '&to=' + target_language
# Create the full URL
constructed_url = endpoint + path + target_language_parameter
# Set up the header information, which includes our subscription key
headers = {
'Ocp-Apim-Subscription-Key': key,
'Ocp-Apim-Subscription-Region': location,
'Content-type': 'application/json',
'X-ClientTraceId': str(uuid.uuid4())
}
# Create the body of the request with the text to be translated
body = [{ 'text': original_text }]
# Make the call using post
translator_request = requests.post(constructed_url, headers=headers, json=body)
# Retrieve the JSON response
translator_response = translator_request.json()
# Retrieve the translation
translated_text = translator_response[0]['translations'][0]['text']
# Call render template, passing the translated text,
# original text, and target language to the template
return render_template(
'pages/translate.html',
translated_text=translated_text,
original_text=original_text,
target_language=target_language
)
@app.route("/flickr8k", methods=["POST"])
def upload_file():
# check if the post request has the file part
if request.method == 'POST':
if 'image' not in request.files:
flash('No file part')
image = request.files['image']
# if user does not select file, browser also
# submit an empty part without filename
if image.filename == '':
flash('No File Selected')
if image and allowed_file(image.filename):
filename = secure_filename(image.filename)
image.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
caption8k = caption_this_image(path)
return jsonify({
'image' : path,
'description' : caption8k
})
return jsonify({
'success': False
}), 405
@app.route("/flickr30k", methods=["POST"])
def up_file():
# check if the post request has the file part
if request.method == 'POST':
if 'image' not in request.files:
flash('No file part')
image = request.files['image']
# if user does not select file, browser also
# submit an empty part without filename
if image.filename == '':
flash('No File Selected')
if image and allowed_file(image.filename):
filename = secure_filename(image.filename)
image.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
caption30k = runModel(path)
return jsonify({
'image' : path,
'description' : caption30k
})
return jsonify({
'success': False
}), 405
@app.route("/VitGpt2ImageCaption", methods=["POST"])
def upload_f():
# check if the post request has the file part
if request.method == 'POST':
if 'image' not in request.files:
flash('No file part')
image = request.files['image']
# if user does not select file, browser also
# submit an empty part without filename
if image.filename == '':
flash('No File Selected')
if image and allowed_file(image.filename):
filename = secure_filename(image.filename)
image.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
VitGpt2ImageCaption = predict_step([path])
# Extract the string from the input list
input_string = VitGpt2ImageCaption[0]
# Remove the square brackets and single quotes from the input string
captiongpt = input_string
return jsonify({
'image' : path,
'description' : captiongpt
})
return jsonify({
'success': False
}), 405
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)
@app.errorhandler(400)
def bad_request(error):
return jsonify({
'success': False,
'error': 400,
'message': 'bad request'
}), 400
@app.errorhandler(404)
def not_found(error):
return render_template('pages/errors/404.html', data={
'success': False,
'error': 404,
'message': 'Page Not Be Found'
}), 404
@app.errorhandler(405)
def method_not_allowed(error):
return jsonify({
'success': False,
'error': 405,
'message': 'method not allowed'
}), 405
@app.errorhandler(422)
def unprocessable(error):
return jsonify({
"success": False,
"error": 422,
"message": "unprocessable"
}), 422
@app.errorhandler(500)
def internal_server_error(error):
return jsonify({
'success': False,
'error': 500,
'message': 'internal server errors'
}), 500
return app
app = create_app()
if __name__ == '__main__':
app.run(host='127.0.0.1', port=4040, debug=True)