Spaces:
Running
Running
from fastapi import FastAPI, File, UploadFile, HTTPException | |
from fastapi.responses import JSONResponse | |
from typing import Any | |
import google.generativeai as genai | |
#from dotenv import load_dotenv | |
import io | |
from PIL import Image | |
import os | |
app = FastAPI() | |
API_KEY=os.getenv('GEMINI_API_KEY') | |
genai.configure(api_key=API_KEY) | |
def get_gemini_response(image: bytes) -> Any: | |
image_stream = io.BytesIO(image) | |
image = Image.open(image_stream) | |
model = genai.GenerativeModel('gemini-1.5-flash', | |
generation_config={"response_mime_type": "application/json"}) | |
inputtext = '''System: You are a dietitian, Please check below image and share calorific value of each dish in metric system. Also explain how | |
much you should eat at one time for healthy diet. Response should be as per below json format for each dish separately. | |
{"Dish_Name": , | |
"calorific_value": , | |
"Healthy serving_size": }. | |
If unable to identify the dish then respond with {"Dish_Name": Unable to identify the dish}.''' | |
response = model.generate_content([inputtext, image]) | |
return response.text | |
async def analyze_dish(image: UploadFile = File(...)): | |
try: | |
image_bytes = await image.read() | |
result = get_gemini_response(image_bytes) | |
return JSONResponse(content=result) | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=str(e)) | |
if __name__ == "__main__": | |
import uvicorn | |
uvicorn.run(app, host="0.0.0.0", port=7860) |