-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
107 lines (85 loc) · 3.45 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
from flask import Flask, render_template, request, jsonify, redirect, Response
from flask_cors import CORS
from functools import wraps
import openai
import base64
import os
import uuid
from werkzeug.utils import secure_filename
import logging
logging.basicConfig(level=logging.DEBUG)
UPLOAD_FOLDER = '/ff/www/uploads'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
openai.api_key = 'KEY'
app = Flask(__name__)
CORS(app)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
USERNAME = 'Username'
PASSWORD = 'Password'
def basic_auth_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
auth = request.authorization
if not auth or not (auth.username == USERNAME and auth.password == PASSWORD):
return Response(
'Could not verify your access level for that URL.\n'
'You have to login with proper credentials.', 401,
{'WWW-Authenticate': 'Basic realm="Login Required"'}
)
return f(*args, **kwargs)
return decorated_function
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
@basic_auth_required
def index():
return redirect("/index.html", code=302)
@app.route("/index.html")
@basic_auth_required
def hello():
return render_template('index.html')
@app.route('/analyze', methods=['POST'])
def analyze():
if request.method == 'OPTIONS':
response = jsonify(success=True)
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type')
response.headers.add('Access-Control-Allow-Methods', 'POST')
return response
if 'file' not in request.files:
return jsonify({"error": "No file part"}), 400
file = request.files['file']
if file.filename == '':
return jsonify({"error": "No selected file"}), 400
if file and allowed_file(file.filename):
filename = str(uuid.uuid4()) + '_' + secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
try:
with open(filepath, "rb") as image_file:
response = openai.chat.completions.create(
model="gpt-4-turbo",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "Is there a bone in the image? If not, simply state This is not a bone. If so, is it intact or broken? Please confirm with a confidence level on a percentage scale. Provide a detailed explanation."},
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64.b64encode(image_file.read()).decode()}"}}
]
}
],
max_tokens=300
)
result = response.choices[0].message.content
return jsonify({"result": result})
except Exception as e:
return jsonify({"error": str(e)}), 500
finally:
if os.path.exists(filepath):
os.remove(filepath)
return jsonify({"error": "Invalid file format"}), 400
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)