-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
96 lines (90 loc) · 3.05 KB
/
app.py
File metadata and controls
96 lines (90 loc) · 3.05 KB
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
from flask import Flask, request, redirect, url_for, jsonify, render_template_string
import base64
import requests
from dotenv import load_dotenv
import os
load_dotenv()
app = Flask(__name__)
HTML_FORM = '''
<!DOCTYPE html>
<html>
<head>
<title>Upload Image for Food Analysis</title>
</head>
<body>
<h1>Upload an image to get the calorie breakdown</h1>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="image" required>
<input type="submit" value="Upload Image">
</form>
</body>
</html>
'''
@app.route('/')
def index():
return HTML_FORM
def encode_image_to_base64(image_file):
"""Encode an image file to Base64."""
return base64.b64encode(image_file.read()).decode('utf-8')
def call_gpt4_vision_api(base64_image, api_key):
"""Make an API call to GPT-4 with a custom prompt for food identification."""
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
payload = {
"model": "gpt-4-vision-preview",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Identify the food items in this image and provide their calorie breakdown. no long sentences just structure them in a table. also give the total calorie"
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
],
"max_tokens": 300
}
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
return response.json()
@app.route('/upload', methods=['POST'])
def upload_file():
if 'image' not in request.files:
return redirect(request.url)
file = request.files['image']
if file.filename == '':
return redirect(request.url)
if file:
base64_image = encode_image_to_base64(file)
api_key = os.getenv('OPENAI_API_KEY')
response = call_gpt4_vision_api(base64_image, api_key)
try:
#extract the table data from the response
table_data = response.get('choices', [{}])[0].get('message', {}).get('content', '')
#convert the markdown table to HTML table
table_html = table_data.replace(" | ", "</td><td>").replace("\n", "</td></tr><tr><td>").replace("---", "").strip()
table_html = f"<table><tr><td>{table_html}</td></tr></table>"
#return the HTML table
return render_template_string(f'''<!DOCTYPE html>
<html>
<head>
<title>Food Analysis Result</title>
</head>
<body>
<h1>Food Items and Calorie Information</h1>
{table_html}
<a href="/">Upload another image</a>
</body>
</html>''')
except Exception as e:
return jsonify({"error": str(e)})
if __name__ == '__main__':
app.run(debug=True)