Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -49,22 +49,27 @@ def search_web_duckduckgo(query: str) -> str:
|
|
49 |
response.raise_for_status()
|
50 |
|
51 |
data = response.json()
|
52 |
-
|
53 |
results = []
|
54 |
|
55 |
if data.get('AbstractText'):
|
56 |
results.append(f"Summary: {data['AbstractText']}")
|
57 |
|
58 |
if data.get('RelatedTopics'):
|
59 |
-
results.append("\nRelated
|
60 |
for i, topic in enumerate(data['RelatedTopics'][:3], 1):
|
61 |
if isinstance(topic, dict) and topic.get('Text'):
|
62 |
results.append(f"{i}. {topic['Text']}")
|
|
|
|
|
|
|
|
|
63 |
|
64 |
if data.get('Definition'):
|
65 |
results.append(f"\nDefinition: {data['Definition']}")
|
66 |
|
67 |
-
|
|
|
|
|
68 |
except Exception as e:
|
69 |
logger.error(f"Web search API error: {str(e)}")
|
70 |
return search_web_scraper(query)
|
@@ -94,7 +99,7 @@ def search_web_scraper(query: str) -> str:
|
|
94 |
for i, link in enumerate(result_links, 1):
|
95 |
title = link.get_text(strip=True)
|
96 |
if title:
|
97 |
-
results.append(f"{i}. {title}")
|
98 |
|
99 |
if results:
|
100 |
return f"Search Results for '{query}':\n" + "\n".join(results)
|
@@ -168,4 +173,9 @@ demo3 = gr.Interface(
|
|
168 |
demo = gr.TabbedInterface([demo1, demo2, demo3], ["Knowledge Base", "Web Search", "Formatter"])
|
169 |
|
170 |
if __name__ == "__main__":
|
171 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
49 |
response.raise_for_status()
|
50 |
|
51 |
data = response.json()
|
|
|
52 |
results = []
|
53 |
|
54 |
if data.get('AbstractText'):
|
55 |
results.append(f"Summary: {data['AbstractText']}")
|
56 |
|
57 |
if data.get('RelatedTopics'):
|
58 |
+
results.append("\nRelated Topics:")
|
59 |
for i, topic in enumerate(data['RelatedTopics'][:3], 1):
|
60 |
if isinstance(topic, dict) and topic.get('Text'):
|
61 |
results.append(f"{i}. {topic['Text']}")
|
62 |
+
elif hasattr(topic, 'Topics'):
|
63 |
+
for subtopic in topic['Topics'][:3]:
|
64 |
+
if subtopic.get('Text'):
|
65 |
+
results.append(f"{i}. {subtopic['Text']}")
|
66 |
|
67 |
if data.get('Definition'):
|
68 |
results.append(f"\nDefinition: {data['Definition']}")
|
69 |
|
70 |
+
if not results or len(''.join(results).strip()) < 20:
|
71 |
+
return search_web_scraper(query)
|
72 |
+
return "\n".join(results)
|
73 |
except Exception as e:
|
74 |
logger.error(f"Web search API error: {str(e)}")
|
75 |
return search_web_scraper(query)
|
|
|
99 |
for i, link in enumerate(result_links, 1):
|
100 |
title = link.get_text(strip=True)
|
101 |
if title:
|
102 |
+
results.append(f"{i}. {title} (https://{link['href']})" if link.get('href') else f"{i}. {title}")
|
103 |
|
104 |
if results:
|
105 |
return f"Search Results for '{query}':\n" + "\n".join(results)
|
|
|
173 |
demo = gr.TabbedInterface([demo1, demo2, demo3], ["Knowledge Base", "Web Search", "Formatter"])
|
174 |
|
175 |
if __name__ == "__main__":
|
176 |
+
demo.launch(
|
177 |
+
server_name="0.0.0.0",
|
178 |
+
server_port=7860,
|
179 |
+
ssl_verify=False,
|
180 |
+
enable_queue=True
|
181 |
+
)
|