Upload main.py
Browse files- src/main.py +41 -0
src/main.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
from fastapi import FastAPI, HTTPException
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from groq import Groq
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
client = Groq(api_key="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")
|
| 8 |
+
|
| 9 |
+
class ImageRequest(BaseModel):
|
| 10 |
+
base64_image: str
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@app.post("/explain-chart")
|
| 14 |
+
async def explain_chart(request: ImageRequest):
|
| 15 |
+
try:
|
| 16 |
+
completion = client.chat.completions.create(
|
| 17 |
+
|
| 18 |
+
model="meta-llama/llama-4-scout-17b-16e-instruct",
|
| 19 |
+
messages=[
|
| 20 |
+
{
|
| 21 |
+
"role": "user",
|
| 22 |
+
"content": [
|
| 23 |
+
{"type": "text", "text": "Explain this IPL data chart. What are the key insights?"},
|
| 24 |
+
{
|
| 25 |
+
"type": "image_url",
|
| 26 |
+
"image_url": {"url": f"data:image/png;base64,{request.base64_image}"}
|
| 27 |
+
}
|
| 28 |
+
]
|
| 29 |
+
}
|
| 30 |
+
]
|
| 31 |
+
)
|
| 32 |
+
return {"explanation": completion.choices[0].message.content}
|
| 33 |
+
except Exception as e:
|
| 34 |
+
|
| 35 |
+
print(f"Error detail: {e}")
|
| 36 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 37 |
+
|
| 38 |
+
# after clicking the button in the Streamlit app, it will send a POST request to this endpoint
|
| 39 |
+
# with the base64 image, and the FastAPI server will process it and return the AI-generated explanation.
|
| 40 |
+
# this is happening in the background, so the user experience is seamless.
|
| 41 |
+
|