Update src/main.py
Browse files- src/main.py +25 -27
src/main.py
CHANGED
|
@@ -1,46 +1,44 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
from fastapi import FastAPI, HTTPException, Header
|
| 3 |
from pydantic import BaseModel
|
| 4 |
-
from
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
# Get the key from environment variables
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
|
| 12 |
class ImageRequest(BaseModel):
|
| 13 |
base64_image: str
|
| 14 |
|
| 15 |
@app.post("/explain-chart")
|
| 16 |
async def explain_chart(request: ImageRequest, authorization: str = Header(None)):
|
| 17 |
-
|
| 18 |
-
if not authorization or authorization != f"Bearer {
|
| 19 |
raise HTTPException(status_code=401, detail="Unauthorized")
|
| 20 |
-
|
| 21 |
try:
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
"image_url": {"url": f"data:image/png;base64,{request.base64_image}"}
|
| 33 |
-
}
|
| 34 |
-
]
|
| 35 |
-
}
|
| 36 |
]
|
| 37 |
)
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
except Exception as e:
|
| 40 |
print(f"Error detail: {e}")
|
| 41 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 42 |
-
|
| 43 |
-
# after clicking the button in the Streamlit app, it will send a POST request to this endpoint
|
| 44 |
-
# with the base64 image, and the FastAPI server will process it and return the AI-generated explanation.
|
| 45 |
-
# this is happening in the background, so the user experience is seamless.
|
| 46 |
-
|
|
|
|
| 1 |
import os
|
| 2 |
+
import io
|
| 3 |
+
import base64
|
| 4 |
from fastapi import FastAPI, HTTPException, Header
|
| 5 |
from pydantic import BaseModel
|
| 6 |
+
from google import genai
|
| 7 |
+
from PIL import Image
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
# Get the key from environment variables
|
| 12 |
+
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 13 |
+
|
| 14 |
+
# Initialize Gemini Client
|
| 15 |
+
client = genai.Client(api_key=GEMINI_API_KEY)
|
| 16 |
|
| 17 |
class ImageRequest(BaseModel):
|
| 18 |
base64_image: str
|
| 19 |
|
| 20 |
@app.post("/explain-chart")
|
| 21 |
async def explain_chart(request: ImageRequest, authorization: str = Header(None)):
|
| 22 |
+
# Security check (matching the key sent from Streamlit)
|
| 23 |
+
if not authorization or authorization != f"Bearer {GEMINI_API_KEY}":
|
| 24 |
raise HTTPException(status_code=401, detail="Unauthorized")
|
| 25 |
+
|
| 26 |
try:
|
| 27 |
+
# 1. Decode the base64 string back into an image
|
| 28 |
+
image_data = base64.b64decode(request.base64_image)
|
| 29 |
+
img = Image.open(io.BytesIO(image_data))
|
| 30 |
+
|
| 31 |
+
# 2. Call Gemini 1.5 Flash (optimized for vision/charts)
|
| 32 |
+
response = client.models.generate_content(
|
| 33 |
+
model="gemini-1.5-flash",
|
| 34 |
+
contents=[
|
| 35 |
+
"You are an expert IPL data analyst. Explain this chart and provide the key insights in concise bullet points.",
|
| 36 |
+
img
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
]
|
| 38 |
)
|
| 39 |
+
|
| 40 |
+
return {"explanation": response.text}
|
| 41 |
+
|
| 42 |
except Exception as e:
|
| 43 |
print(f"Error detail: {e}")
|
| 44 |
+
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|