Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 2 |
+
import os
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
|
| 10 |
+
|
| 11 |
+
if not GOOGLE_API_KEY:
|
| 12 |
+
raise ValueError("GOOGLE_API_KEY environment variable not set")
|
| 13 |
+
|
| 14 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
| 15 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 16 |
+
|
| 17 |
+
@app.post("/generate-caption/")
|
| 18 |
+
async def generate_caption(file: UploadFile = File(...)):
|
| 19 |
+
try:
|
| 20 |
+
image_data = await file.read()
|
| 21 |
+
image = Image.open(BytesIO(image_data))
|
| 22 |
+
|
| 23 |
+
response = model.generate_content(image)
|
| 24 |
+
return {"caption": response.text}
|
| 25 |
+
except Exception as e:
|
| 26 |
+
raise HTTPException(status_code=500, detail=str(e))
|