Update app.py
Browse files
app.py
CHANGED
@@ -79,203 +79,199 @@ def tavily_search(
|
|
79 |
result = response.json()
|
80 |
|
81 |
# Format the response as clean HTML
|
82 |
-
html_result = """
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
</div>
|
262 |
-
""".format(query.replace('"', '"'))
|
263 |
|
264 |
# Add answer section if available
|
265 |
if "answer" in result and result["answer"]:
|
266 |
html_result += f"""
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
"""
|
272 |
|
273 |
# Add search results
|
274 |
if "results" in result and result["results"]:
|
275 |
html_result += f"""
|
276 |
-
|
277 |
-
|
278 |
-
"""
|
279 |
|
280 |
for i, item in enumerate(result["results"], 1):
|
281 |
title = item.get('title', 'No Title').replace('<', '<').replace('>', '>')
|
@@ -285,53 +281,49 @@ def tavily_search(
|
|
285 |
score = item.get('score', 0)
|
286 |
|
287 |
html_result += f"""
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
"""
|
295 |
|
296 |
html_result += "</div>"
|
297 |
|
298 |
# Add images if available
|
299 |
if "images" in result and result.get("images"):
|
300 |
html_result += """
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
"""
|
305 |
for img in result["images"][:6]: # Limit to 6 images
|
306 |
img_url = img.get('url', '')
|
307 |
img_title = img.get('title', 'Image').replace('<', '<').replace('>', '>')
|
308 |
html_result += f"""
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
"""
|
314 |
html_result += "</div></div>"
|
315 |
|
316 |
# Add metadata
|
317 |
html_result += f"""
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
"""
|
335 |
|
336 |
return html_result
|
337 |
|
|
|
79 |
result = response.json()
|
80 |
|
81 |
# Format the response as clean HTML
|
82 |
+
html_result = f"""<!DOCTYPE html>
|
83 |
+
<html>
|
84 |
+
<head>
|
85 |
+
<meta charset="UTF-8">
|
86 |
+
<title>Tavily Search Results</title>
|
87 |
+
<style>
|
88 |
+
body {{
|
89 |
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
90 |
+
line-height: 1.6;
|
91 |
+
color: #333;
|
92 |
+
max-width: 1200px;
|
93 |
+
margin: 0 auto;
|
94 |
+
padding: 20px;
|
95 |
+
background: #f8f9fa;
|
96 |
+
}}
|
97 |
+
.container {{
|
98 |
+
background: white;
|
99 |
+
border-radius: 12px;
|
100 |
+
padding: 30px;
|
101 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
102 |
+
}}
|
103 |
+
.header {{
|
104 |
+
border-bottom: 3px solid #4CAF50;
|
105 |
+
padding-bottom: 20px;
|
106 |
+
margin-bottom: 30px;
|
107 |
+
}}
|
108 |
+
.search-title {{
|
109 |
+
color: #2c3e50;
|
110 |
+
font-size: 28px;
|
111 |
+
font-weight: 700;
|
112 |
+
margin: 0;
|
113 |
+
display: flex;
|
114 |
+
align-items: center;
|
115 |
+
gap: 10px;
|
116 |
+
}}
|
117 |
+
.search-query {{
|
118 |
+
color: #7f8c8d;
|
119 |
+
font-size: 16px;
|
120 |
+
margin-top: 8px;
|
121 |
+
font-style: italic;
|
122 |
+
}}
|
123 |
+
.answer-section {{
|
124 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
125 |
+
color: white;
|
126 |
+
padding: 25px;
|
127 |
+
border-radius: 10px;
|
128 |
+
margin-bottom: 30px;
|
129 |
+
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
|
130 |
+
}}
|
131 |
+
.answer-title {{
|
132 |
+
font-size: 20px;
|
133 |
+
font-weight: 600;
|
134 |
+
margin-bottom: 15px;
|
135 |
+
display: flex;
|
136 |
+
align-items: center;
|
137 |
+
gap: 8px;
|
138 |
+
}}
|
139 |
+
.answer-content {{
|
140 |
+
font-size: 16px;
|
141 |
+
line-height: 1.7;
|
142 |
+
}}
|
143 |
+
.results-section {{
|
144 |
+
margin-top: 30px;
|
145 |
+
}}
|
146 |
+
.results-header {{
|
147 |
+
font-size: 22px;
|
148 |
+
font-weight: 600;
|
149 |
+
color: #2c3e50;
|
150 |
+
margin-bottom: 20px;
|
151 |
+
display: flex;
|
152 |
+
align-items: center;
|
153 |
+
gap: 8px;
|
154 |
+
}}
|
155 |
+
.result-item {{
|
156 |
+
background: #fff;
|
157 |
+
border: 1px solid #e1e8ed;
|
158 |
+
border-radius: 8px;
|
159 |
+
padding: 20px;
|
160 |
+
margin-bottom: 20px;
|
161 |
+
transition: all 0.3s ease;
|
162 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
|
163 |
+
}}
|
164 |
+
.result-item:hover {{
|
165 |
+
transform: translateY(-2px);
|
166 |
+
box-shadow: 0 8px 25px rgba(0,0,0,0.1);
|
167 |
+
border-color: #4CAF50;
|
168 |
+
}}
|
169 |
+
.result-title {{
|
170 |
+
font-size: 18px;
|
171 |
+
font-weight: 600;
|
172 |
+
color: #1a73e8;
|
173 |
+
margin-bottom: 8px;
|
174 |
+
text-decoration: none;
|
175 |
+
}}
|
176 |
+
.result-title:hover {{
|
177 |
+
text-decoration: underline;
|
178 |
+
}}
|
179 |
+
.result-url {{
|
180 |
+
color: #34a853;
|
181 |
+
font-size: 14px;
|
182 |
+
margin-bottom: 12px;
|
183 |
+
word-break: break-all;
|
184 |
+
}}
|
185 |
+
.result-content {{
|
186 |
+
color: #5f6368;
|
187 |
+
line-height: 1.6;
|
188 |
+
margin-bottom: 12px;
|
189 |
+
}}
|
190 |
+
.result-score {{
|
191 |
+
background: #e8f5e8;
|
192 |
+
color: #2d5a2d;
|
193 |
+
padding: 4px 12px;
|
194 |
+
border-radius: 20px;
|
195 |
+
font-size: 12px;
|
196 |
+
font-weight: 500;
|
197 |
+
display: inline-block;
|
198 |
+
}}
|
199 |
+
.divider {{
|
200 |
+
height: 1px;
|
201 |
+
background: linear-gradient(90deg, transparent, #ddd, transparent);
|
202 |
+
margin: 30px 0;
|
203 |
+
}}
|
204 |
+
.metadata {{
|
205 |
+
background: #f8f9fa;
|
206 |
+
border-radius: 8px;
|
207 |
+
padding: 20px;
|
208 |
+
margin-top: 30px;
|
209 |
+
border-left: 4px solid #4CAF50;
|
210 |
+
}}
|
211 |
+
.metadata-title {{
|
212 |
+
font-weight: 600;
|
213 |
+
color: #2c3e50;
|
214 |
+
margin-bottom: 10px;
|
215 |
+
}}
|
216 |
+
.json-container {{
|
217 |
+
background: #2d3748;
|
218 |
+
color: #e2e8f0;
|
219 |
+
padding: 20px;
|
220 |
+
border-radius: 8px;
|
221 |
+
overflow-x: auto;
|
222 |
+
font-family: 'Monaco', 'Consolas', monospace;
|
223 |
+
font-size: 12px;
|
224 |
+
line-height: 1.5;
|
225 |
+
max-height: 400px;
|
226 |
+
overflow-y: auto;
|
227 |
+
}}
|
228 |
+
.image-section {{
|
229 |
+
margin-top: 20px;
|
230 |
+
}}
|
231 |
+
.image-grid {{
|
232 |
+
display: grid;
|
233 |
+
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
|
234 |
+
gap: 15px;
|
235 |
+
margin-top: 15px;
|
236 |
+
}}
|
237 |
+
.image-item {{
|
238 |
+
border-radius: 8px;
|
239 |
+
overflow: hidden;
|
240 |
+
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
|
241 |
+
}}
|
242 |
+
.image-item img {{
|
243 |
+
width: 100%;
|
244 |
+
height: 150px;
|
245 |
+
object-fit: cover;
|
246 |
+
}}
|
247 |
+
.image-description {{
|
248 |
+
padding: 10px;
|
249 |
+
background: white;
|
250 |
+
font-size: 12px;
|
251 |
+
color: #666;
|
252 |
+
}}
|
253 |
+
</style>
|
254 |
+
</head>
|
255 |
+
<body>
|
256 |
+
<div class="container">
|
257 |
+
<div class="header">
|
258 |
+
<h1 class="search-title">π Tavily Search Results</h1>
|
259 |
+
<div class="search-query">Query: "{query.replace('"', '"')}"</div>
|
260 |
+
</div>"""
|
|
|
|
|
261 |
|
262 |
# Add answer section if available
|
263 |
if "answer" in result and result["answer"]:
|
264 |
html_result += f"""
|
265 |
+
<div class="answer-section">
|
266 |
+
<div class="answer-title">π AI Generated Answer</div>
|
267 |
+
<div class="answer-content">{result['answer']}</div>
|
268 |
+
</div>"""
|
|
|
269 |
|
270 |
# Add search results
|
271 |
if "results" in result and result["results"]:
|
272 |
html_result += f"""
|
273 |
+
<div class="results-section">
|
274 |
+
<div class="results-header">π Found {len(result['results'])} Results</div>"""
|
|
|
275 |
|
276 |
for i, item in enumerate(result["results"], 1):
|
277 |
title = item.get('title', 'No Title').replace('<', '<').replace('>', '>')
|
|
|
281 |
score = item.get('score', 0)
|
282 |
|
283 |
html_result += f"""
|
284 |
+
<div class="result-item">
|
285 |
+
<a href="{url}" target="_blank" class="result-title">{i}. {title}</a>
|
286 |
+
<div class="result-url">{url}</div>
|
287 |
+
<div class="result-content">{content}</div>
|
288 |
+
{f'<span class="result-score">β Score: {score:.3f}</span>' if score else ''}
|
289 |
+
</div>"""
|
|
|
290 |
|
291 |
html_result += "</div>"
|
292 |
|
293 |
# Add images if available
|
294 |
if "images" in result and result.get("images"):
|
295 |
html_result += """
|
296 |
+
<div class="image-section">
|
297 |
+
<div class="results-header">πΌοΈ Related Images</div>
|
298 |
+
<div class="image-grid">"""
|
|
|
299 |
for img in result["images"][:6]: # Limit to 6 images
|
300 |
img_url = img.get('url', '')
|
301 |
img_title = img.get('title', 'Image').replace('<', '<').replace('>', '>')
|
302 |
html_result += f"""
|
303 |
+
<div class="image-item">
|
304 |
+
<img src="{img_url}" alt="{img_title}" loading="lazy">
|
305 |
+
<div class="image-description">{img_title}</div>
|
306 |
+
</div>"""
|
|
|
307 |
html_result += "</div></div>"
|
308 |
|
309 |
# Add metadata
|
310 |
html_result += f"""
|
311 |
+
<div class="divider"></div>
|
312 |
+
<div class="metadata">
|
313 |
+
<div class="metadata-title">π§ Response Metadata</div>
|
314 |
+
<div><strong>Results Count:</strong> {len(result.get('results', []))}</div>
|
315 |
+
<div><strong>Query:</strong> {query}</div>
|
316 |
+
<div><strong>Search Depth:</strong> {search_depth}</div>
|
317 |
+
<div><strong>Topic:</strong> {topic}</div>
|
318 |
+
</div>
|
319 |
+
|
320 |
+
<div class="metadata">
|
321 |
+
<div class="metadata-title">π Raw JSON Response</div>
|
322 |
+
<div class="json-container">{json.dumps(result, indent=2).replace('<', '<').replace('>', '>')}</div>
|
323 |
+
</div>
|
324 |
+
</div>
|
325 |
+
</body>
|
326 |
+
</html>"""
|
|
|
327 |
|
328 |
return html_result
|
329 |
|