Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
import base64
|
|
|
|
|
|
|
| 2 |
from fastapi import FastAPI, UploadFile, File, HTTPException, Request
|
| 3 |
from fastapi.responses import JSONResponse, HTMLResponse
|
| 4 |
from fastapi.templating import Jinja2Templates
|
| 5 |
|
| 6 |
import google.generativeai as genai
|
| 7 |
-
import httpx
|
| 8 |
|
| 9 |
# Configure the Google Generative AI API key.
|
| 10 |
genai.configure(api_key="AIzaSyDcYyq3w21iwipYn17wCAQo3AYWhUIGDSI")
|
|
@@ -12,11 +13,17 @@ genai.configure(api_key="AIzaSyDcYyq3w21iwipYn17wCAQo3AYWhUIGDSI")
|
|
| 12 |
app = FastAPI()
|
| 13 |
templates = Jinja2Templates(directory="templates")
|
| 14 |
|
| 15 |
-
def
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
# Create a prompt
|
| 20 |
prompt = """analyse question , analyse option , and give me question , it's options and answer in json format
|
| 21 |
|
| 22 |
fixed json format
|
|
@@ -36,12 +43,12 @@ below is example of json format ,the json structure is same like below
|
|
| 36 |
|
| 37 |
only json format nothing else"""
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
model = genai.GenerativeModel(model_name="gemini-2.0-flash")
|
| 41 |
-
response = model.generate_content(
|
| 42 |
-
{'mime_type': 'image/jpeg', 'data': image_data},
|
| 43 |
-
prompt
|
| 44 |
-
])
|
| 45 |
return response.text
|
| 46 |
|
| 47 |
@app.get("/", response_class=HTMLResponse)
|
|
@@ -49,15 +56,20 @@ async def index(request: Request):
|
|
| 49 |
return templates.TemplateResponse("index.html", {"request": request})
|
| 50 |
|
| 51 |
@app.post("/upload", response_class=JSONResponse)
|
| 52 |
-
async def
|
| 53 |
-
if not
|
| 54 |
-
raise HTTPException(status_code=400, detail="
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
| 56 |
image_bytes = await file.read()
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
return {"result": result}
|
| 59 |
except Exception as e:
|
| 60 |
-
return JSONResponse(status_code=500, content={"detail": f"Error processing the image: {str(e)}"})
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
| 63 |
import uvicorn
|
|
|
|
| 1 |
import base64
|
| 2 |
+
from typing import List, Tuple
|
| 3 |
+
|
| 4 |
from fastapi import FastAPI, UploadFile, File, HTTPException, Request
|
| 5 |
from fastapi.responses import JSONResponse, HTMLResponse
|
| 6 |
from fastapi.templating import Jinja2Templates
|
| 7 |
|
| 8 |
import google.generativeai as genai
|
|
|
|
| 9 |
|
| 10 |
# Configure the Google Generative AI API key.
|
| 11 |
genai.configure(api_key="AIzaSyDcYyq3w21iwipYn17wCAQo3AYWhUIGDSI")
|
|
|
|
| 13 |
app = FastAPI()
|
| 14 |
templates = Jinja2Templates(directory="templates")
|
| 15 |
|
| 16 |
+
def generate_content_from_images(images: List[Tuple[str, bytes]]) -> str:
|
| 17 |
+
"""
|
| 18 |
+
Given a list of tuples (mime_type, image_bytes), encode each image
|
| 19 |
+
in base64, build a list of image dicts, append the fixed prompt,
|
| 20 |
+
and call the Gemini model.
|
| 21 |
+
"""
|
| 22 |
+
image_data_list = []
|
| 23 |
+
for mime_type, image_bytes in images:
|
| 24 |
+
image_data = base64.b64encode(image_bytes).decode("utf-8")
|
| 25 |
+
image_data_list.append({'mime_type': mime_type, 'data': image_data})
|
| 26 |
|
|
|
|
| 27 |
prompt = """analyse question , analyse option , and give me question , it's options and answer in json format
|
| 28 |
|
| 29 |
fixed json format
|
|
|
|
| 43 |
|
| 44 |
only json format nothing else"""
|
| 45 |
|
| 46 |
+
payload = image_data_list.copy()
|
| 47 |
+
payload.append(prompt)
|
| 48 |
+
|
| 49 |
+
# Use your exact code snippet.
|
| 50 |
model = genai.GenerativeModel(model_name="gemini-2.0-flash")
|
| 51 |
+
response = model.generate_content(payload)
|
|
|
|
|
|
|
|
|
|
| 52 |
return response.text
|
| 53 |
|
| 54 |
@app.get("/", response_class=HTMLResponse)
|
|
|
|
| 56 |
return templates.TemplateResponse("index.html", {"request": request})
|
| 57 |
|
| 58 |
@app.post("/upload", response_class=JSONResponse)
|
| 59 |
+
async def upload_images(files: List[UploadFile] = File(...)):
|
| 60 |
+
if not files:
|
| 61 |
+
raise HTTPException(status_code=400, detail="No files uploaded.")
|
| 62 |
+
images = []
|
| 63 |
+
for file in files:
|
| 64 |
+
if not file.content_type.startswith("image/"):
|
| 65 |
+
raise HTTPException(status_code=400, detail="Only image files are accepted.")
|
| 66 |
image_bytes = await file.read()
|
| 67 |
+
images.append((file.content_type, image_bytes))
|
| 68 |
+
try:
|
| 69 |
+
result = generate_content_from_images(images)
|
| 70 |
return {"result": result}
|
| 71 |
except Exception as e:
|
| 72 |
+
return JSONResponse(status_code=500, content={"detail": f"Error processing the image(s): {str(e)}"})
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
| 75 |
import uvicorn
|