Kackle commited on
Commit
6a5b033
·
verified ·
1 Parent(s): f95d95e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -13
app.py CHANGED
@@ -83,24 +83,30 @@ class GeminiModelAdapter:
83
  def generate(self, *args, **kwargs):
84
  kwargs.pop('stop_sequences', None) # Remove unsupported argument for Gemini
85
  result = self.model.generate_content(*args, **kwargs)
86
- # Always return a string, not a ChatMessage or Gemini object
 
87
  if hasattr(result, "text"):
88
- print(f"[DEBUG] Gemini raw text output: {result.text}")
89
  return result.text
90
- elif isinstance(result, str):
91
- print(f"[DEBUG] Gemini raw string output: {result}")
92
- return result
93
- elif hasattr(result, "candidates") and result.candidates:
94
- # For some Gemini APIs, the text is in candidates[0].content.parts[0].text
 
 
 
 
 
 
95
  try:
96
  text = result.candidates[0].content.parts[0].text
97
- print(f"[DEBUG] Gemini raw candidate output: {text}")
98
  return text
99
- except Exception:
100
- pass
101
- # Fallback: convert to string
102
- print(f"[DEBUG] Gemini unknown response type: {type(result)}; value: {result}")
103
- return str(result)
104
 
105
  # --- Basic Agent Definition ---
106
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
83
  def generate(self, *args, **kwargs):
84
  kwargs.pop('stop_sequences', None) # Remove unsupported argument for Gemini
85
  result = self.model.generate_content(*args, **kwargs)
86
+ print(f"[DEBUG] Gemini raw result type: {type(result)}; value: {result}")
87
+ # Try all known ways to extract text
88
  if hasattr(result, "text"):
89
+ print(f"[DEBUG] Gemini .text: {result.text}")
90
  return result.text
91
+ if hasattr(result, "content"):
92
+ content = getattr(result, "content")
93
+ if isinstance(content, str):
94
+ print(f"[DEBUG] Gemini .content (str): {content}")
95
+ return content
96
+ if hasattr(content, "parts"):
97
+ parts = getattr(content, "parts")
98
+ if parts and hasattr(parts[0], "text"):
99
+ print(f"[DEBUG] Gemini .content.parts[0].text: {parts[0].text}")
100
+ return parts[0].text
101
+ if hasattr(result, "candidates") and result.candidates:
102
  try:
103
  text = result.candidates[0].content.parts[0].text
104
+ print(f"[DEBUG] Gemini .candidates[0].content.parts[0].text: {text}")
105
  return text
106
+ except Exception as e:
107
+ print(f"[DEBUG] Gemini .candidates extraction failed: {e}")
108
+ # Fallback: raise error for debugging
109
+ raise TypeError(f"GeminiModelAdapter.generate() could not extract text from result of type {type(result)}: {result}")
 
110
 
111
  # --- Basic Agent Definition ---
112
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------