-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
59 lines (47 loc) · 1.81 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
import tempfile
import requests
import subprocess # This import might not be necessary
from flask import Flask
from flask import request
import flask.json as flask_json
from gptv import process_image
app = Flask(__name__)
@app.route('/', methods=['POST'])
def get_jpg_and_execute(): # Use this function to POST a binary to the app
# Get the binary data from the POST request
file = request.files['image']
# Write the binary data to a temporary file
tf = tempfile.NamedTemporaryFile(delete=False)
tf.write(file.stream.read())
tf.close()
# Pass the temporary file path to the process_image function
oimg1 = process_image(tf.name)
json = oimg1.model_dump_json()
# Replace '"n/a"' with 'null' in the JSON response
json = json.replace('"n/a"', 'null')
response = app.response_class(
response=json,
mimetype='application/json'
)
return response
"""
#Code below can be used as replacement if the image is posted via URL instead of a binary
def get_jpg_and_execute():
try:
# Get the image URL from the POST request data
data = request.get_json()
image_url = data['url'] # Replace 'url' with the actual key in the JSON data
# Download the image and save it to a temporary file
response = requests.get(image_url)
tf = tempfile.NamedTemporaryFile(delete=False)
tf.write(response.content)
tf.close()
# Pass the temporary file path to the process_image function
oimg1 = process_image(tf.name)
static_string = f"Processed image data: {oimg1}"
except Exception as e:
static_string = f"Error processing image: {e}"
return static_string
"""
if __name__ == '__main__':
app.run(host='0.0.0.0', port='5000', debug=False) # Disable debug mode for production