LiamKhoaLe commited on
Commit
50ed549
·
1 Parent(s): b8d023d

Update Gemini caller

Browse files
Files changed (1) hide show
  1. app.py +19 -6
app.py CHANGED
@@ -114,15 +114,28 @@ def call_gemini(prompt: str, vision_parts=None) -> str:
114
  resp = client.models.generate_content(
115
  model="gemini-2.5-flash-preview-04-17", **kwargs
116
  )
117
- # Join all .text fields in case Gemini responds in multiple parts.
118
  try:
119
- text = "".join(part.text for part in resp.candidates[0].content.parts)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  except Exception as e:
121
- logger.error(f"[LLM] ❌ Failed to parse Gemini response: {e}")
122
  raise RuntimeError("Gemini API response format error")
123
- # Handle response
124
- logger.info(f"[LLM] Response: {text}")
125
- return text.strip()
126
 
127
  @app.post("/voice-transcribe")
128
  async def voice_transcribe(file: UploadFile = File(...)):
 
114
  resp = client.models.generate_content(
115
  model="gemini-2.5-flash-preview-04-17", **kwargs
116
  )
 
117
  try:
118
+ resp = client.models.generate_content(
119
+ model="gemini-2.5-flash-preview-04-17", **kwargs
120
+ )
121
+ # Check for at least one valid candidate
122
+ if not resp.candidates:
123
+ raise RuntimeError("No candidates returned from Gemini")
124
+ # Start at first index
125
+ candidate = resp.candidates[0]
126
+ if candidate.content is None or not hasattr(candidate.content, "parts"):
127
+ raise RuntimeError("Gemini candidate missing content parts")
128
+ # Join all .text fields in case Gemini responds in multiple parts.
129
+ text = "".join(part.text for part in candidate.content.parts if hasattr(part, "text"))
130
+ if not text.strip():
131
+ raise RuntimeError("Gemini response contained empty text")
132
+ # Success
133
+ logger.info(f"[LLM] ✅ Response received: {text[:100]}...")
134
+ return text.strip()
135
+ # Fail
136
  except Exception as e:
137
+ logger.error(f"[LLM] ❌ Gemini API error: {e}")
138
  raise RuntimeError("Gemini API response format error")
 
 
 
139
 
140
  @app.post("/voice-transcribe")
141
  async def voice_transcribe(file: UploadFile = File(...)):