Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from fastapi import FastAPI, File, UploadFile
|
2 |
import google.generativeai as genai
|
3 |
from PIL import Image
|
4 |
import io
|
@@ -9,8 +9,11 @@ import uvicorn
|
|
9 |
|
10 |
app = FastAPI()
|
11 |
|
12 |
-
#
|
13 |
-
|
|
|
|
|
|
|
14 |
|
15 |
@app.get("/")
|
16 |
def read_root():
|
@@ -21,7 +24,7 @@ async def root_head():
|
|
21 |
return {} # Empty response for HEAD requests
|
22 |
|
23 |
@app.post("/analyze")
|
24 |
-
async def analyze_food_image(file: UploadFile = File(
|
25 |
"""Analyzes food items in the image using Gemini 1.5 Flash"""
|
26 |
|
27 |
# Read the uploaded image file
|
@@ -76,10 +79,10 @@ async def analyze_food_image(file: UploadFile = File(...)):
|
|
76 |
"""
|
77 |
|
78 |
# Get response from API
|
79 |
-
response = model.generate_content([prompt,
|
80 |
|
81 |
# Debugging: Print the raw response
|
82 |
-
print("Raw Response from Gemini:
|
83 |
|
84 |
# Extract JSON response
|
85 |
try:
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
2 |
import google.generativeai as genai
|
3 |
from PIL import Image
|
4 |
import io
|
|
|
9 |
|
10 |
app = FastAPI()
|
11 |
|
12 |
+
# Secure API key retrieval
|
13 |
+
api_key = "AIzaSyAlvHXHO7xFFjfEyaNOmyZvn9FFPPJdIx4"
|
14 |
+
if not api_key:
|
15 |
+
raise RuntimeError("GOOGLE_API_KEY is not set in the environment variables.")
|
16 |
+
genai.configure(api_key=api_key)
|
17 |
|
18 |
@app.get("/")
|
19 |
def read_root():
|
|
|
24 |
return {} # Empty response for HEAD requests
|
25 |
|
26 |
@app.post("/analyze")
|
27 |
+
async def analyze_food_image(file: UploadFile = File()):
|
28 |
"""Analyzes food items in the image using Gemini 1.5 Flash"""
|
29 |
|
30 |
# Read the uploaded image file
|
|
|
79 |
"""
|
80 |
|
81 |
# Get response from API
|
82 |
+
response = model.generate_content([prompt, img]) # Pass image correctly
|
83 |
|
84 |
# Debugging: Print the raw response
|
85 |
+
print(f"Raw Response from Gemini: {response}")
|
86 |
|
87 |
# Extract JSON response
|
88 |
try:
|