varshasharma01 commited on
Commit
31b9335
·
verified ·
1 Parent(s): 6cc4e1a

Update src/main.py

Browse files
Files changed (1) hide show
  1. 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 groq import Groq
 
5
 
6
  app = FastAPI()
7
 
8
  # Get the key from environment variables
9
- GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
10
- client = Groq(api_key=GROQ_API_KEY)
 
 
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 {GROQ_API_KEY}":
19
  raise HTTPException(status_code=401, detail="Unauthorized")
20
-
21
  try:
22
- completion = client.chat.completions.create(
23
- # Using the vision-capable model
24
- model="llama-3.2-11b-vision-preview",
25
- messages=[
26
- {
27
- "role": "user",
28
- "content": [
29
- {"type": "text", "text": "Explain this IPL data chart. What are the key insights?"},
30
- {
31
- "type": "image_url",
32
- "image_url": {"url": f"data:image/png;base64,{request.base64_image}"}
33
- }
34
- ]
35
- }
36
  ]
37
  )
38
- return {"explanation": completion.choices[0].message.content}
 
 
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))