Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -84,7 +84,7 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
84 |
@lru_cache(maxsize=100)
|
85 |
def cached_search(query):
|
86 |
try:
|
87 |
-
print(f"Performing search for: {query[:
|
88 |
result = search_tool(query)
|
89 |
print(f"Search successful, returned {len(result)} characters")
|
90 |
return result
|
@@ -145,10 +145,10 @@ class BasicAgent:
|
|
145 |
)
|
146 |
|
147 |
def __call__(self, question: str) -> str:
|
148 |
-
print(f"Agent received question: {question[:
|
149 |
try:
|
150 |
final_answer = self.process_question(question)
|
151 |
-
print(f"Agent returning answer: {final_answer[:
|
152 |
return final_answer
|
153 |
except Exception as e:
|
154 |
print(f"Agent error: {str(e)}")
|
@@ -218,13 +218,13 @@ class BasicAgent:
|
|
218 |
|
219 |
def _extract_key_info(self, search_results, question):
|
220 |
# Basic check for empty results
|
221 |
-
if not search_results or len(search_results) <
|
222 |
return "No relevant information found."
|
223 |
|
224 |
# For YouTube transcripts, extract the most relevant portion
|
225 |
if "Transcript from YouTube video" in search_results:
|
226 |
# Split by sentences but keep limited context
|
227 |
-
max_chars =
|
228 |
if len(search_results) > max_chars:
|
229 |
# Take a portion from the middle of the transcript for better relevance
|
230 |
start_idx = search_results.find("\n") + 1 # Skip the first line which is the header
|
@@ -235,18 +235,18 @@ class BasicAgent:
|
|
235 |
# For search results
|
236 |
# Split results into sentences and find most relevant
|
237 |
sentences = search_results.split('. ')
|
238 |
-
if len(sentences) <=
|
239 |
-
return search_results[:
|
240 |
|
241 |
# Try to find sentences with keywords from question
|
242 |
-
keywords = [w for w in question.lower().split() if len(w) >
|
243 |
relevant_sentences = [] # NEW LINE
|
244 |
|
245 |
for sentence in sentences:
|
246 |
sentence_lower = sentence.lower()
|
247 |
if any(keyword in sentence_lower for keyword in keywords):
|
248 |
relevant_sentences.append(sentence)
|
249 |
-
if len(relevant_sentences) >=
|
250 |
break
|
251 |
|
252 |
# If we found relevant sentences, use them
|
@@ -254,12 +254,12 @@ class BasicAgent:
|
|
254 |
return '. '.join(relevant_sentences)
|
255 |
|
256 |
# Fallback to first few sentences
|
257 |
-
return '. '.join(sentences[:
|
258 |
|
259 |
def _formulate_direct_answer(self, relevant_info, question):
|
260 |
|
261 |
if not self.model:
|
262 |
-
return f"
|
263 |
|
264 |
if self.model.startswith('gemini'):
|
265 |
try:
|
@@ -274,7 +274,7 @@ class BasicAgent:
|
|
274 |
Relevant information: {relevant_info}
|
275 |
|
276 |
Instructions:
|
277 |
-
1. Read the question and think about what you need to answer it. Stick to the question. If you need search results, use the search results. If not, just answer the question directly and ignore the search results.
|
278 |
2. If the question is not comprehensible, try reading each letter backwards, from the last character in the last word, to the first letter of the first word. Read carefully, all the way to the beginning. If the backwards text turns out to be an instruction, just follow the instruction and provide a direct answer. Don't provide comments.
|
279 |
3. If the question is still not comprehensible, try seeing if it is in another language.
|
280 |
4. Think about whether you need to elaborate on the information. For example, if you know that John and Jane are kids of Joan, you know Joan has at least two kids. In other words, if you don't have a number that is asked of you, see if you can count to produce an answer.
|
@@ -301,7 +301,7 @@ class BasicAgent:
|
|
301 |
|
302 |
|
303 |
def _get_fallback_answer(self, question):
|
304 |
-
return f"
|
305 |
|
306 |
|
307 |
|
|
|
84 |
@lru_cache(maxsize=100)
|
85 |
def cached_search(query):
|
86 |
try:
|
87 |
+
print(f"Performing search for: {query[:5000]}...")
|
88 |
result = search_tool(query)
|
89 |
print(f"Search successful, returned {len(result)} characters")
|
90 |
return result
|
|
|
145 |
)
|
146 |
|
147 |
def __call__(self, question: str) -> str:
|
148 |
+
print(f"Agent received question: {question[:500]}...")
|
149 |
try:
|
150 |
final_answer = self.process_question(question)
|
151 |
+
print(f"Agent returning answer: {final_answer[:500]}...")
|
152 |
return final_answer
|
153 |
except Exception as e:
|
154 |
print(f"Agent error: {str(e)}")
|
|
|
218 |
|
219 |
def _extract_key_info(self, search_results, question):
|
220 |
# Basic check for empty results
|
221 |
+
if not search_results or len(search_results) < 5:
|
222 |
return "No relevant information found."
|
223 |
|
224 |
# For YouTube transcripts, extract the most relevant portion
|
225 |
if "Transcript from YouTube video" in search_results:
|
226 |
# Split by sentences but keep limited context
|
227 |
+
max_chars = 5000 # Keep a reasonable chunk size
|
228 |
if len(search_results) > max_chars:
|
229 |
# Take a portion from the middle of the transcript for better relevance
|
230 |
start_idx = search_results.find("\n") + 1 # Skip the first line which is the header
|
|
|
235 |
# For search results
|
236 |
# Split results into sentences and find most relevant
|
237 |
sentences = search_results.split('. ')
|
238 |
+
if len(sentences) <= 100:
|
239 |
+
return search_results[:8000]
|
240 |
|
241 |
# Try to find sentences with keywords from question
|
242 |
+
keywords = [w for w in question.lower().split() if len(w) > 100]
|
243 |
relevant_sentences = [] # NEW LINE
|
244 |
|
245 |
for sentence in sentences:
|
246 |
sentence_lower = sentence.lower()
|
247 |
if any(keyword in sentence_lower for keyword in keywords):
|
248 |
relevant_sentences.append(sentence)
|
249 |
+
if len(relevant_sentences) >= 100:
|
250 |
break
|
251 |
|
252 |
# If we found relevant sentences, use them
|
|
|
254 |
return '. '.join(relevant_sentences)
|
255 |
|
256 |
# Fallback to first few sentences
|
257 |
+
return '. '.join(sentences[:100])
|
258 |
|
259 |
def _formulate_direct_answer(self, relevant_info, question):
|
260 |
|
261 |
if not self.model:
|
262 |
+
return f"{relevant_info}"
|
263 |
|
264 |
if self.model.startswith('gemini'):
|
265 |
try:
|
|
|
274 |
Relevant information: {relevant_info}
|
275 |
|
276 |
Instructions:
|
277 |
+
1. Read the question and think about what you need to answer it. Stick to the question. If you need search results, use the search results. If not, just answer the question directly and ignore the search results. Do not use search results if you can answer the question without using them.
|
278 |
2. If the question is not comprehensible, try reading each letter backwards, from the last character in the last word, to the first letter of the first word. Read carefully, all the way to the beginning. If the backwards text turns out to be an instruction, just follow the instruction and provide a direct answer. Don't provide comments.
|
279 |
3. If the question is still not comprehensible, try seeing if it is in another language.
|
280 |
4. Think about whether you need to elaborate on the information. For example, if you know that John and Jane are kids of Joan, you know Joan has at least two kids. In other words, if you don't have a number that is asked of you, see if you can count to produce an answer.
|
|
|
301 |
|
302 |
|
303 |
def _get_fallback_answer(self, question):
|
304 |
+
return f"I cannot provide a specific answer to your question about {question.split()[0:3]}..."
|
305 |
|
306 |
|
307 |
|