-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
63 lines (48 loc) · 1.44 KB
/
app.py
File metadata and controls
63 lines (48 loc) · 1.44 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
import requests
import os
from flask import Flask, request, jsonify, render_template
from dotenv import load_dotenv
load_dotenv()
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
GEMINI_ENDPOINT = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent"
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/gemini', methods=['POST'])
def gemini():
data = request.get_json()
category = data.get("category", "unknown")
prompt = f"""
A food image was classified as: {category}
You are a smart food advisor AI. Please:
1. Describe this category
2. Say whether it's healthy or not
3. Suggest 2 better alternatives if it's unhealthy
4. Recommend one simple meal using it
Keep it friendly and add emojis!
"""
payload = {
"contents": [
{
"parts": [{"text": prompt}]
}
]
}
headers = {
"Content-Type": "application/json",
"X-goog-api-key": GEMINI_API_KEY
}
response = requests.post(
f"{GEMINI_ENDPOINT}",
headers=headers,
json=payload
)
gemini_output = response.json()
try:
text = gemini_output["candidates"][0]["content"]["parts"][0]["text"]
except Exception:
text = "Sorry, something went wrong with Gemini."
return jsonify({'advice': text})
if __name__ == '__main__':
app.run(debug=True)