Spaces:
Sleeping
Sleeping
Update gemini_agent.py
Browse files- gemini_agent.py +120 -8
gemini_agent.py
CHANGED
|
@@ -41,12 +41,92 @@ class GeminiAgent:
|
|
| 41 |
if '.xlsx' in question or '.xls' in question or 'excel' in question.lower():
|
| 42 |
return await self._handle_excel_question(question)
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
# Regular text-based question
|
| 45 |
return await self._handle_text_question(question)
|
| 46 |
|
| 47 |
except Exception as e:
|
| 48 |
print(f"Error processing question: {e}")
|
| 49 |
return "Unable to process request."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
async def _handle_video_question(self, question: str) -> str:
|
| 52 |
"""Handle questions that require video analysis"""
|
|
@@ -163,7 +243,7 @@ Provide only the direct answer. If it's a quote, give just the quoted text. If i
|
|
| 163 |
async def _handle_text_question(self, question: str) -> str:
|
| 164 |
"""Handle regular text-based questions"""
|
| 165 |
prompt = ""
|
| 166 |
-
#
|
| 167 |
def is_explicit_retrieval_question(question):
|
| 168 |
q = question.lower()
|
| 169 |
return (
|
|
@@ -173,17 +253,36 @@ Provide only the direct answer. If it's a quote, give just the quoted text. If i
|
|
| 173 |
"duckduckgo" in q or
|
| 174 |
"web search" in q
|
| 175 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
wiki_context = ""
|
| 177 |
ddg_context = ""
|
| 178 |
-
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
| 180 |
try:
|
| 181 |
wiki_context = self.wiki_tool.run(question)
|
|
|
|
| 182 |
except Exception as e:
|
| 183 |
print(f"Wikipedia tool failed: {e}")
|
| 184 |
-
|
|
|
|
| 185 |
try:
|
| 186 |
ddg_context = self.ddg_tool.run(question)
|
|
|
|
| 187 |
except Exception as e:
|
| 188 |
print(f"DuckDuckGo tool failed: {e}")
|
| 189 |
# Handle attached file questions with enhanced prompts
|
|
@@ -226,10 +325,23 @@ Provide only the direct answer. If it's a quote, give just the quoted text. If i
|
|
| 226 |
# Prepend context to the prompt if available and likely relevant
|
| 227 |
def is_good_context(context):
|
| 228 |
return context and not any(x in context.lower() for x in ["not found", "no results", "does not contain information"])
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
# Use the constructed prompt for all cases
|
| 235 |
await self._rate_limit()
|
|
|
|
| 41 |
if '.xlsx' in question or '.xls' in question or 'excel' in question.lower():
|
| 42 |
return await self._handle_excel_question(question)
|
| 43 |
|
| 44 |
+
# Check if question is about actors, TV shows, or movies
|
| 45 |
+
if self._is_actor_or_show_question(question):
|
| 46 |
+
return await self._handle_actor_show_question(question)
|
| 47 |
+
|
| 48 |
# Regular text-based question
|
| 49 |
return await self._handle_text_question(question)
|
| 50 |
|
| 51 |
except Exception as e:
|
| 52 |
print(f"Error processing question: {e}")
|
| 53 |
return "Unable to process request."
|
| 54 |
+
|
| 55 |
+
def _is_actor_or_show_question(self, question: str) -> bool:
|
| 56 |
+
"""Determine if a question is about actors, TV shows, or movies"""
|
| 57 |
+
q = question.lower()
|
| 58 |
+
actor_show_patterns = [
|
| 59 |
+
"who played", "who did", "who was the actor", "who was the actress",
|
| 60 |
+
"what role", "what character", "what part",
|
| 61 |
+
"which actor", "which actress",
|
| 62 |
+
"in the movie", "in the show", "in the series", "in the film",
|
| 63 |
+
"version of", "language version", "dubbed version"
|
| 64 |
+
]
|
| 65 |
+
return any(pattern in q for pattern in actor_show_patterns)
|
| 66 |
+
|
| 67 |
+
async def _handle_actor_show_question(self, question: str) -> str:
|
| 68 |
+
"""Handle questions about actors, TV shows, and movies with enhanced search"""
|
| 69 |
+
print(f"Processing actor/show question: {question[:50]}...")
|
| 70 |
+
|
| 71 |
+
# Always try both Wikipedia and DuckDuckGo for these questions
|
| 72 |
+
wiki_context = ""
|
| 73 |
+
ddg_context = ""
|
| 74 |
+
|
| 75 |
+
try:
|
| 76 |
+
wiki_context = self.wiki_tool.run(question)
|
| 77 |
+
print("Wikipedia search completed")
|
| 78 |
+
except Exception as e:
|
| 79 |
+
print(f"Wikipedia tool failed: {e}")
|
| 80 |
+
|
| 81 |
+
try:
|
| 82 |
+
ddg_context = self.ddg_tool.run(question)
|
| 83 |
+
print("DuckDuckGo search completed")
|
| 84 |
+
except Exception as e:
|
| 85 |
+
print(f"DuckDuckGo tool failed: {e}")
|
| 86 |
+
|
| 87 |
+
# Combine contexts if available
|
| 88 |
+
combined_context = ""
|
| 89 |
+
if wiki_context and not any(x in wiki_context.lower() for x in ["not found", "no results", "does not contain"]):
|
| 90 |
+
combined_context += f"Wikipedia context: {wiki_context}\n\n"
|
| 91 |
+
if ddg_context and not any(x in ddg_context.lower() for x in ["not found", "no results", "does not contain"]):
|
| 92 |
+
combined_context += f"Web search context: {ddg_context}\n\n"
|
| 93 |
+
|
| 94 |
+
# Create a specialized prompt for actor/show questions
|
| 95 |
+
prompt = f"""Based on the following context, answer this question about an actor or TV show:
|
| 96 |
+
|
| 97 |
+
{combined_context}
|
| 98 |
+
|
| 99 |
+
Question: {question}
|
| 100 |
+
|
| 101 |
+
Provide ONLY the specific name or information requested. No explanations or additional context.
|
| 102 |
+
If the answer is a person's name, provide ONLY their first name as requested."""
|
| 103 |
+
|
| 104 |
+
await self._rate_limit()
|
| 105 |
+
response = self.model.generate_content(
|
| 106 |
+
prompt,
|
| 107 |
+
generation_config=genai.types.GenerationConfig(
|
| 108 |
+
max_output_tokens=50,
|
| 109 |
+
temperature=0.0
|
| 110 |
+
)
|
| 111 |
+
)
|
| 112 |
+
answer = response.text.strip()
|
| 113 |
+
|
| 114 |
+
# Clean up the answer to extract just the name or information
|
| 115 |
+
# Remove common prefixes
|
| 116 |
+
prefixes = ['The answer is', 'Based on', 'According to', 'The actor is', 'The actress is']
|
| 117 |
+
for prefix in prefixes:
|
| 118 |
+
if answer.lower().startswith(prefix.lower()):
|
| 119 |
+
answer = answer[len(prefix):].strip()
|
| 120 |
+
if answer.startswith(','):
|
| 121 |
+
answer = answer[1:].strip()
|
| 122 |
+
|
| 123 |
+
# If the question asks for just a first name, extract it
|
| 124 |
+
if "give only the first name" in question.lower() or "only the first name" in question.lower():
|
| 125 |
+
name_parts = answer.split()
|
| 126 |
+
if name_parts:
|
| 127 |
+
answer = name_parts[0].rstrip(',.')
|
| 128 |
+
|
| 129 |
+
return answer
|
| 130 |
|
| 131 |
async def _handle_video_question(self, question: str) -> str:
|
| 132 |
"""Handle questions that require video analysis"""
|
|
|
|
| 243 |
async def _handle_text_question(self, question: str) -> str:
|
| 244 |
"""Handle regular text-based questions"""
|
| 245 |
prompt = ""
|
| 246 |
+
# Check for different types of questions that need retrieval
|
| 247 |
def is_explicit_retrieval_question(question):
|
| 248 |
q = question.lower()
|
| 249 |
return (
|
|
|
|
| 253 |
"duckduckgo" in q or
|
| 254 |
"web search" in q
|
| 255 |
)
|
| 256 |
+
|
| 257 |
+
def is_factual_question(question):
|
| 258 |
+
q = question.lower()
|
| 259 |
+
# Check for factual question patterns about people, shows, movies, etc.
|
| 260 |
+
factual_patterns = [
|
| 261 |
+
"who played", "who did", "who was", "who is",
|
| 262 |
+
"what role", "what character", "what part",
|
| 263 |
+
"which actor", "which actress",
|
| 264 |
+
"in the movie", "in the show", "in the series", "in the film",
|
| 265 |
+
"version of"
|
| 266 |
+
]
|
| 267 |
+
return any(pattern in q for pattern in factual_patterns)
|
| 268 |
+
|
| 269 |
wiki_context = ""
|
| 270 |
ddg_context = ""
|
| 271 |
+
|
| 272 |
+
# Use retrieval for explicit web/Wikipedia questions OR factual questions about actors/shows
|
| 273 |
+
if is_explicit_retrieval_question(question) or is_factual_question(question):
|
| 274 |
+
# For factual questions, always try both Wikipedia and DuckDuckGo
|
| 275 |
+
if is_factual_question(question) or "wikipedia" in question.lower():
|
| 276 |
try:
|
| 277 |
wiki_context = self.wiki_tool.run(question)
|
| 278 |
+
print(f"Wikipedia search completed for: {question[:50]}...")
|
| 279 |
except Exception as e:
|
| 280 |
print(f"Wikipedia tool failed: {e}")
|
| 281 |
+
|
| 282 |
+
if is_factual_question(question) or "duckduckgo" in question.lower() or "web search" in question.lower():
|
| 283 |
try:
|
| 284 |
ddg_context = self.ddg_tool.run(question)
|
| 285 |
+
print(f"DuckDuckGo search completed for: {question[:50]}...")
|
| 286 |
except Exception as e:
|
| 287 |
print(f"DuckDuckGo tool failed: {e}")
|
| 288 |
# Handle attached file questions with enhanced prompts
|
|
|
|
| 325 |
# Prepend context to the prompt if available and likely relevant
|
| 326 |
def is_good_context(context):
|
| 327 |
return context and not any(x in context.lower() for x in ["not found", "no results", "does not contain information"])
|
| 328 |
+
|
| 329 |
+
# For factual questions about actors/shows, try to use both Wikipedia and web search results
|
| 330 |
+
if is_factual_question(question):
|
| 331 |
+
combined_context = ""
|
| 332 |
+
if wiki_context and is_good_context(wiki_context):
|
| 333 |
+
combined_context += f"Wikipedia context: {wiki_context}\n\n"
|
| 334 |
+
if ddg_context and is_good_context(ddg_context):
|
| 335 |
+
combined_context += f"Web search context: {ddg_context}\n\n"
|
| 336 |
+
|
| 337 |
+
if combined_context:
|
| 338 |
+
prompt = f"Use the following context to answer the question accurately. Focus on finding the exact name or information requested:\n{combined_context}\n{prompt}"
|
| 339 |
+
else:
|
| 340 |
+
# For non-factual questions, use the first good context available
|
| 341 |
+
if wiki_context and is_good_context(wiki_context):
|
| 342 |
+
prompt = f"Use the following Wikipedia context to answer the question:\n{wiki_context}\n\n{prompt}"
|
| 343 |
+
elif ddg_context and is_good_context(ddg_context):
|
| 344 |
+
prompt = f"Use the following web search context to answer the question:\n{ddg_context}\n\n{prompt}"
|
| 345 |
|
| 346 |
# Use the constructed prompt for all cases
|
| 347 |
await self._rate_limit()
|