shukdevdatta123 commited on
Commit
8cc3aa8
Β·
verified Β·
1 Parent(s): 76e550a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +266 -30
app.py CHANGED
@@ -53,7 +53,10 @@ def tavily_search(
53
  payload["days"] = days
54
 
55
  if include_raw_content != "false":
56
- payload["include_raw_content"] = include_raw_content if include_raw_content != "true" else True
 
 
 
57
 
58
  if include_domains.strip():
59
  payload["include_domains"] = [domain.strip() for domain in include_domains.split(",")]
@@ -75,27 +78,262 @@ def tavily_search(
75
  if response.status_code == 200:
76
  result = response.json()
77
 
78
- # Format the response for better readability
79
- formatted_result = "πŸ” **Tavily Search Results**\n\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
 
81
  if "answer" in result and result["answer"]:
82
- formatted_result += f"πŸ“‹ **Answer:**\n{result['answer']}\n\n"
 
 
 
 
 
83
 
84
- if "results" in result:
85
- formatted_result += f"πŸ“Š **Found {len(result['results'])} results:**\n\n"
 
 
 
 
86
 
87
  for i, item in enumerate(result["results"], 1):
88
- formatted_result += f"**{i}. {item.get('title', 'No Title')}**\n"
89
- formatted_result += f"πŸ”— URL: {item.get('url', 'No URL')}\n"
90
- formatted_result += f"πŸ“ Content: {item.get('content', 'No content available')[:500]}...\n"
91
- if item.get('score'):
92
- formatted_result += f"⭐ Score: {item['score']}\n"
93
- formatted_result += "\n" + "─" * 50 + "\n\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
 
95
- # Add raw JSON for debugging
96
- formatted_result += "\n\nπŸ”§ **Raw JSON Response:**\n```json\n" + json.dumps(result, indent=2) + "\n```"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
- return formatted_result
99
 
100
  else:
101
  return f"❌ Error: HTTP {response.status_code}\n{response.text}"
@@ -299,10 +537,10 @@ with gr.Blocks(title="Tavily Search API", theme=gr.themes.Soft()) as app:
299
  )
300
 
301
  include_raw_content = gr.Dropdown(
302
- choices=["false", "true", "markdown", "text"],
303
- value="false",
304
  label="Include Raw Content",
305
- info="Include cleaned HTML content"
306
  )
307
 
308
  with gr.Row():
@@ -331,12 +569,10 @@ with gr.Blocks(title="Tavily Search API", theme=gr.themes.Soft()) as app:
331
  with gr.Column(scale=2):
332
  # Results
333
  gr.Markdown("## πŸ“Š Search Results")
334
- results = gr.Textbox(
335
  label="Results",
336
- lines=25,
337
- max_lines=50,
338
- show_copy_button=True,
339
- placeholder="Results will appear here after searching..."
340
  )
341
 
342
  # Examples
@@ -353,7 +589,7 @@ with gr.Blocks(title="Tavily Search API", theme=gr.themes.Soft()) as app:
353
  "None", # time_range
354
  7, # days
355
  True, # include_answer
356
- "false", # include_raw_content
357
  False, # include_images
358
  False, # include_image_descriptions
359
  "", # include_domains
@@ -370,7 +606,7 @@ with gr.Blocks(title="Tavily Search API", theme=gr.themes.Soft()) as app:
370
  "week",
371
  3,
372
  True,
373
- "markdown",
374
  False,
375
  False,
376
  "",
@@ -387,7 +623,7 @@ with gr.Blocks(title="Tavily Search API", theme=gr.themes.Soft()) as app:
387
  "None",
388
  7,
389
  True,
390
- "text",
391
  False,
392
  False,
393
  "github.com, stackoverflow.com, medium.com",
@@ -404,7 +640,7 @@ with gr.Blocks(title="Tavily Search API", theme=gr.themes.Soft()) as app:
404
  "month",
405
  14,
406
  True,
407
- "false",
408
  False,
409
  False,
410
  "",
@@ -438,7 +674,7 @@ with gr.Blocks(title="Tavily Search API", theme=gr.themes.Soft()) as app:
438
  "None",
439
  7,
440
  True,
441
- "markdown",
442
  True,
443
  True,
444
  "apple.com, techcrunch.com, theverge.com",
@@ -455,7 +691,7 @@ with gr.Blocks(title="Tavily Search API", theme=gr.themes.Soft()) as app:
455
  "day",
456
  1,
457
  True,
458
- "text",
459
  False,
460
  False,
461
  "fifa.com, espn.com, bbc.com",
@@ -472,7 +708,7 @@ with gr.Blocks(title="Tavily Search API", theme=gr.themes.Soft()) as app:
472
  "year",
473
  7,
474
  True,
475
- "markdown",
476
  False,
477
  False,
478
  "arxiv.org, nature.com, science.org",
 
53
  payload["days"] = days
54
 
55
  if include_raw_content != "false":
56
+ if include_raw_content == "html":
57
+ payload["include_raw_content"] = True
58
+ else:
59
+ payload["include_raw_content"] = include_raw_content
60
 
61
  if include_domains.strip():
62
  payload["include_domains"] = [domain.strip() for domain in include_domains.split(",")]
 
78
  if response.status_code == 200:
79
  result = response.json()
80
 
81
+ # Format the response as clean HTML
82
+ html_result = """
83
+ <!DOCTYPE html>
84
+ <html>
85
+ <head>
86
+ <meta charset="UTF-8">
87
+ <title>Tavily Search Results</title>
88
+ <style>
89
+ body {
90
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
91
+ line-height: 1.6;
92
+ color: #333;
93
+ max-width: 1200px;
94
+ margin: 0 auto;
95
+ padding: 20px;
96
+ background: #f8f9fa;
97
+ }
98
+ .container {
99
+ background: white;
100
+ border-radius: 12px;
101
+ padding: 30px;
102
+ box-shadow: 0 4px 6px rgba(0,0,0,0.1);
103
+ }
104
+ .header {
105
+ border-bottom: 3px solid #4CAF50;
106
+ padding-bottom: 20px;
107
+ margin-bottom: 30px;
108
+ }
109
+ .search-title {
110
+ color: #2c3e50;
111
+ font-size: 28px;
112
+ font-weight: 700;
113
+ margin: 0;
114
+ display: flex;
115
+ align-items: center;
116
+ gap: 10px;
117
+ }
118
+ .search-query {
119
+ color: #7f8c8d;
120
+ font-size: 16px;
121
+ margin-top: 8px;
122
+ font-style: italic;
123
+ }
124
+ .answer-section {
125
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
126
+ color: white;
127
+ padding: 25px;
128
+ border-radius: 10px;
129
+ margin-bottom: 30px;
130
+ box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
131
+ }
132
+ .answer-title {
133
+ font-size: 20px;
134
+ font-weight: 600;
135
+ margin-bottom: 15px;
136
+ display: flex;
137
+ align-items: center;
138
+ gap: 8px;
139
+ }
140
+ .answer-content {
141
+ font-size: 16px;
142
+ line-height: 1.7;
143
+ }
144
+ .results-section {
145
+ margin-top: 30px;
146
+ }
147
+ .results-header {
148
+ font-size: 22px;
149
+ font-weight: 600;
150
+ color: #2c3e50;
151
+ margin-bottom: 20px;
152
+ display: flex;
153
+ align-items: center;
154
+ gap: 8px;
155
+ }
156
+ .result-item {
157
+ background: #fff;
158
+ border: 1px solid #e1e8ed;
159
+ border-radius: 8px;
160
+ padding: 20px;
161
+ margin-bottom: 20px;
162
+ transition: all 0.3s ease;
163
+ box-shadow: 0 2px 4px rgba(0,0,0,0.05);
164
+ }
165
+ .result-item:hover {
166
+ transform: translateY(-2px);
167
+ box-shadow: 0 8px 25px rgba(0,0,0,0.1);
168
+ border-color: #4CAF50;
169
+ }
170
+ .result-title {
171
+ font-size: 18px;
172
+ font-weight: 600;
173
+ color: #1a73e8;
174
+ margin-bottom: 8px;
175
+ text-decoration: none;
176
+ }
177
+ .result-title:hover {
178
+ text-decoration: underline;
179
+ }
180
+ .result-url {
181
+ color: #34a853;
182
+ font-size: 14px;
183
+ margin-bottom: 12px;
184
+ word-break: break-all;
185
+ }
186
+ .result-content {
187
+ color: #5f6368;
188
+ line-height: 1.6;
189
+ margin-bottom: 12px;
190
+ }
191
+ .result-score {
192
+ background: #e8f5e8;
193
+ color: #2d5a2d;
194
+ padding: 4px 12px;
195
+ border-radius: 20px;
196
+ font-size: 12px;
197
+ font-weight: 500;
198
+ display: inline-block;
199
+ }
200
+ .divider {
201
+ height: 1px;
202
+ background: linear-gradient(90deg, transparent, #ddd, transparent);
203
+ margin: 30px 0;
204
+ }
205
+ .metadata {
206
+ background: #f8f9fa;
207
+ border-radius: 8px;
208
+ padding: 20px;
209
+ margin-top: 30px;
210
+ border-left: 4px solid #4CAF50;
211
+ }
212
+ .metadata-title {
213
+ font-weight: 600;
214
+ color: #2c3e50;
215
+ margin-bottom: 10px;
216
+ }
217
+ .json-container {
218
+ background: #2d3748;
219
+ color: #e2e8f0;
220
+ padding: 20px;
221
+ border-radius: 8px;
222
+ overflow-x: auto;
223
+ font-family: 'Monaco', 'Consolas', monospace;
224
+ font-size: 12px;
225
+ line-height: 1.5;
226
+ max-height: 400px;
227
+ overflow-y: auto;
228
+ }
229
+ .image-section {
230
+ margin-top: 20px;
231
+ }
232
+ .image-grid {
233
+ display: grid;
234
+ grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
235
+ gap: 15px;
236
+ margin-top: 15px;
237
+ }
238
+ .image-item {
239
+ border-radius: 8px;
240
+ overflow: hidden;
241
+ box-shadow: 0 2px 8px rgba(0,0,0,0.1);
242
+ }
243
+ .image-item img {
244
+ width: 100%;
245
+ height: 150px;
246
+ object-fit: cover;
247
+ }
248
+ .image-description {
249
+ padding: 10px;
250
+ background: white;
251
+ font-size: 12px;
252
+ color: #666;
253
+ }
254
+ </style>
255
+ </head>
256
+ <body>
257
+ <div class="container">
258
+ <div class="header">
259
+ <h1 class="search-title">πŸ” Tavily Search Results</h1>
260
+ <div class="search-query">Query: "{}"</div>
261
+ </div>
262
+ """.format(query.replace('"', '&quot;'))
263
 
264
+ # Add answer section if available
265
  if "answer" in result and result["answer"]:
266
+ html_result += f"""
267
+ <div class="answer-section">
268
+ <div class="answer-title">πŸ“‹ AI Generated Answer</div>
269
+ <div class="answer-content">{result['answer']}</div>
270
+ </div>
271
+ """
272
 
273
+ # Add search results
274
+ if "results" in result and result["results"]:
275
+ html_result += f"""
276
+ <div class="results-section">
277
+ <div class="results-header">πŸ“Š Found {len(result['results'])} Results</div>
278
+ """
279
 
280
  for i, item in enumerate(result["results"], 1):
281
+ title = item.get('title', 'No Title').replace('<', '&lt;').replace('>', '&gt;')
282
+ url = item.get('url', 'No URL')
283
+ content = item.get('content', 'No content available')[:800] + '...'
284
+ content = content.replace('<', '&lt;').replace('>', '&gt;')
285
+ score = item.get('score', 0)
286
+
287
+ html_result += f"""
288
+ <div class="result-item">
289
+ <a href="{url}" target="_blank" class="result-title">{i}. {title}</a>
290
+ <div class="result-url">{url}</div>
291
+ <div class="result-content">{content}</div>
292
+ {f'<span class="result-score">⭐ Score: {score:.3f}</span>' if score else ''}
293
+ </div>
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
+ <div class="image-section">
302
+ <div class="results-header">πŸ–ΌοΈ Related Images</div>
303
+ <div class="image-grid">
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('<', '&lt;').replace('>', '&gt;')
308
+ html_result += f"""
309
+ <div class="image-item">
310
+ <img src="{img_url}" alt="{img_title}" loading="lazy">
311
+ <div class="image-description">{img_title}</div>
312
+ </div>
313
+ """
314
+ html_result += "</div></div>"
315
 
316
+ # Add metadata
317
+ html_result += f"""
318
+ <div class="divider"></div>
319
+ <div class="metadata">
320
+ <div class="metadata-title">πŸ”§ Response Metadata</div>
321
+ <div><strong>Results Count:</strong> {len(result.get('results', []))}</div>
322
+ <div><strong>Query:</strong> {query}</div>
323
+ <div><strong>Search Depth:</strong> {search_depth}</div>
324
+ <div><strong>Topic:</strong> {topic}</div>
325
+ </div>
326
+
327
+ <div class="metadata">
328
+ <div class="metadata-title">πŸ“„ Raw JSON Response</div>
329
+ <div class="json-container">{json.dumps(result, indent=2).replace('<', '&lt;').replace('>', '&gt;')}</div>
330
+ </div>
331
+ </div>
332
+ </body>
333
+ </html>
334
+ """
335
 
336
+ return html_result
337
 
338
  else:
339
  return f"❌ Error: HTTP {response.status_code}\n{response.text}"
 
537
  )
538
 
539
  include_raw_content = gr.Dropdown(
540
+ choices=["false", "html", "markdown", "text"],
541
+ value="html",
542
  label="Include Raw Content",
543
+ info="HTML: Clean styled format, Markdown: MD format, Text: Plain text"
544
  )
545
 
546
  with gr.Row():
 
569
  with gr.Column(scale=2):
570
  # Results
571
  gr.Markdown("## πŸ“Š Search Results")
572
+ results = gr.HTML(
573
  label="Results",
574
+ show_label=False,
575
+ elem_id="search_results"
 
 
576
  )
577
 
578
  # Examples
 
589
  "None", # time_range
590
  7, # days
591
  True, # include_answer
592
+ "html", # include_raw_content
593
  False, # include_images
594
  False, # include_image_descriptions
595
  "", # include_domains
 
606
  "week",
607
  3,
608
  True,
609
+ "html",
610
  False,
611
  False,
612
  "",
 
623
  "None",
624
  7,
625
  True,
626
+ "html",
627
  False,
628
  False,
629
  "github.com, stackoverflow.com, medium.com",
 
640
  "month",
641
  14,
642
  True,
643
+ "html",
644
  False,
645
  False,
646
  "",
 
674
  "None",
675
  7,
676
  True,
677
+ "html",
678
  True,
679
  True,
680
  "apple.com, techcrunch.com, theverge.com",
 
691
  "day",
692
  1,
693
  True,
694
+ "html",
695
  False,
696
  False,
697
  "fifa.com, espn.com, bbc.com",
 
708
  "year",
709
  7,
710
  True,
711
+ "html",
712
  False,
713
  False,
714
  "arxiv.org, nature.com, science.org",