Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -37,9 +37,48 @@ class BasicAgent:
|
|
37 |
print(f"Agent returning answer: {final_answer[:50]}...")
|
38 |
return final_answer
|
39 |
|
40 |
-
def process_question
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
|
45 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
|
37 |
print(f"Agent returning answer: {final_answer[:50]}...")
|
38 |
return final_answer
|
39 |
|
40 |
+
def process_question(self, question:str) -> str:
|
41 |
+
try:
|
42 |
+
search_results = cached_search(question) if self.tools and search_tool in self.tools else "No search results available."
|
43 |
+
|
44 |
+
# Extract key information and provide direct answer
|
45 |
+
relevant_info = self._extract_key_info(search_results, question)
|
46 |
+
direct_answer = self._formulate_direct_answer(relevant_info, question)
|
47 |
+
|
48 |
+
return direct_answer
|
49 |
+
except Exception as e:
|
50 |
+
if "too many requests" in str(e).lower():
|
51 |
+
time.sleep(2)
|
52 |
+
try:
|
53 |
+
search_results = cached_search(question)
|
54 |
+
relevant_info = self._extract_key_info(search_results, question)
|
55 |
+
return self._formulate_direct_answer(relevant_info, question)
|
56 |
+
except:
|
57 |
+
return self._get_fallback_answer(question)
|
58 |
+
return self._get_fallback_answer(question)
|
59 |
+
|
60 |
+
def _extract_key_info(self, search_results, question):
|
61 |
+
# Split results into sentences and find most relevant
|
62 |
+
sentences = search_results.split('. ')
|
63 |
+
if len(sentences) <= 3:
|
64 |
+
return search_results[:250] # If few sentences, return first portion
|
65 |
+
|
66 |
+
# Try to find sentence with keywords from question
|
67 |
+
keywords = [w for w in question.lower().split() if len(w) > 3]
|
68 |
+
for sentence in sentences:
|
69 |
+
sentence_lower = sentence.lower()
|
70 |
+
if any(keyword in sentence_lower for keyword in keywords):
|
71 |
+
return sentence
|
72 |
+
|
73 |
+
# Fallback to first few sentences
|
74 |
+
return '. '.join(sentences[:2])
|
75 |
+
|
76 |
+
def _formulate_direct_answer(self, relevant_info, question):
|
77 |
+
# Format as direct, concise answer
|
78 |
+
return relevant_info
|
79 |
+
|
80 |
+
def _get_fallback_answer(self, question):
|
81 |
+
return f"Based on the information available, I cannot provide a specific answer to your question about {question.split()[0:3]}..."
|
82 |
|
83 |
|
84 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|