Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -114,15 +114,30 @@ Answer:"""
|
|
114 |
# Perform search
|
115 |
search_results = self.search(question)
|
116 |
|
|
|
|
|
|
|
|
|
117 |
if not search_results:
|
118 |
return "No search results found. Let me try to answer based on my knowledge:\n\n" + self._answer_with_llm(question)
|
119 |
|
120 |
-
# Format search results
|
121 |
formatted_results = []
|
122 |
for i, result in enumerate(search_results[:3]): # Use top 3 results
|
123 |
-
|
124 |
-
|
125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
formatted_results.append(f"Result {i+1}:\nTitle: {title}\nContent: {snippet}\nSource: {link}")
|
128 |
|
@@ -151,12 +166,17 @@ Answer:"""
|
|
151 |
|
152 |
except Exception as e:
|
153 |
# Fallback to simple search result formatting
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
|
|
|
|
|
|
|
|
|
|
160 |
|
161 |
except Exception as e:
|
162 |
return f"Search failed: {e}. Let me try to answer based on my knowledge:\n\n" + self._answer_with_llm(question)
|
@@ -421,6 +441,7 @@ def clear_cache():
|
|
421 |
return "Cache cleared successfully.", None
|
422 |
|
423 |
|
|
|
424 |
# --- Enhanced Gradio Interface ---
|
425 |
with gr.Blocks(title="Intelligent Agent with Conditional Search") as demo:
|
426 |
gr.Markdown("# Intelligent Agent with Conditional Search")
|
|
|
114 |
# Perform search
|
115 |
search_results = self.search(question)
|
116 |
|
117 |
+
if self.debug:
|
118 |
+
print(f"Search results type: {type(search_results)}")
|
119 |
+
print(f"Search results: {search_results}")
|
120 |
+
|
121 |
if not search_results:
|
122 |
return "No search results found. Let me try to answer based on my knowledge:\n\n" + self._answer_with_llm(question)
|
123 |
|
124 |
+
# Format search results - handle different result formats
|
125 |
formatted_results = []
|
126 |
for i, result in enumerate(search_results[:3]): # Use top 3 results
|
127 |
+
if isinstance(result, dict):
|
128 |
+
title = result.get("title", "No title")
|
129 |
+
snippet = result.get("snippet", "").strip()
|
130 |
+
link = result.get("link", "")
|
131 |
+
elif isinstance(result, str):
|
132 |
+
# If result is a string, use it directly
|
133 |
+
title = f"Result {i+1}"
|
134 |
+
snippet = result
|
135 |
+
link = ""
|
136 |
+
else:
|
137 |
+
# Handle other formats
|
138 |
+
title = f"Result {i+1}"
|
139 |
+
snippet = str(result)
|
140 |
+
link = ""
|
141 |
|
142 |
formatted_results.append(f"Result {i+1}:\nTitle: {title}\nContent: {snippet}\nSource: {link}")
|
143 |
|
|
|
166 |
|
167 |
except Exception as e:
|
168 |
# Fallback to simple search result formatting
|
169 |
+
if search_results and len(search_results) > 0:
|
170 |
+
top_result = search_results[0]
|
171 |
+
if isinstance(top_result, dict):
|
172 |
+
title = top_result.get("title", "No title")
|
173 |
+
snippet = top_result.get("snippet", "").strip()
|
174 |
+
link = top_result.get("link", "")
|
175 |
+
return f"**{title}**\n\n{snippet}\n\nSource: {link}"
|
176 |
+
else:
|
177 |
+
return f"Search result: {str(top_result)}"
|
178 |
+
else:
|
179 |
+
return "Search completed but no usable results found."
|
180 |
|
181 |
except Exception as e:
|
182 |
return f"Search failed: {e}. Let me try to answer based on my knowledge:\n\n" + self._answer_with_llm(question)
|
|
|
441 |
return "Cache cleared successfully.", None
|
442 |
|
443 |
|
444 |
+
|
445 |
# --- Enhanced Gradio Interface ---
|
446 |
with gr.Blocks(title="Intelligent Agent with Conditional Search") as demo:
|
447 |
gr.Markdown("# Intelligent Agent with Conditional Search")
|