Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
@@ -1,781 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import requests
|
3 |
-
import json
|
4 |
-
from typing import Optional, List
|
5 |
-
|
6 |
-
def tavily_search(
|
7 |
-
api_key: str,
|
8 |
-
query: str,
|
9 |
-
topic: str = "general",
|
10 |
-
search_depth: str = "basic",
|
11 |
-
chunks_per_source: int = 3,
|
12 |
-
max_results: int = 5,
|
13 |
-
time_range: Optional[str] = None,
|
14 |
-
days: int = 7,
|
15 |
-
include_answer: bool = True,
|
16 |
-
include_raw_content: str = "false",
|
17 |
-
include_images: bool = False,
|
18 |
-
include_image_descriptions: bool = False,
|
19 |
-
include_domains: str = "",
|
20 |
-
exclude_domains: str = "",
|
21 |
-
country: Optional[str] = None
|
22 |
-
):
|
23 |
-
"""
|
24 |
-
Perform a Tavily search with the given parameters.
|
25 |
-
"""
|
26 |
-
if not api_key.strip():
|
27 |
-
return "❌ Error: Please provide a valid Tavily API key."
|
28 |
-
|
29 |
-
if not query.strip():
|
30 |
-
return "❌ Error: Please provide a search query."
|
31 |
-
|
32 |
-
url = "https://api.tavily.com/search"
|
33 |
-
|
34 |
-
# Prepare the payload
|
35 |
-
payload = {
|
36 |
-
"query": query,
|
37 |
-
"topic": topic,
|
38 |
-
"search_depth": search_depth,
|
39 |
-
"max_results": max_results,
|
40 |
-
"include_answer": include_answer,
|
41 |
-
"include_images": include_images,
|
42 |
-
"include_image_descriptions": include_image_descriptions,
|
43 |
-
}
|
44 |
-
|
45 |
-
# Add optional parameters
|
46 |
-
if search_depth == "advanced":
|
47 |
-
payload["chunks_per_source"] = chunks_per_source
|
48 |
-
|
49 |
-
if time_range and time_range != "None":
|
50 |
-
payload["time_range"] = time_range
|
51 |
-
|
52 |
-
if topic == "news" and days > 0:
|
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(",")]
|
63 |
-
|
64 |
-
if exclude_domains.strip():
|
65 |
-
payload["exclude_domains"] = [domain.strip() for domain in exclude_domains.split(",")]
|
66 |
-
|
67 |
-
if country and country != "None":
|
68 |
-
payload["country"] = country
|
69 |
-
|
70 |
-
headers = {
|
71 |
-
"Authorization": f"Bearer {api_key}",
|
72 |
-
"Content-Type": "application/json"
|
73 |
-
}
|
74 |
-
|
75 |
-
try:
|
76 |
-
response = requests.post(url, json=payload, headers=headers, timeout=30)
|
77 |
-
|
78 |
-
if response.status_code == 200:
|
79 |
-
result = response.json()
|
80 |
-
|
81 |
-
# Format the response as clean HTML with fixed width
|
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: 100%; /* Changed from 1200px to 100% */
|
93 |
-
width: 100%; /* Added fixed width */
|
94 |
-
margin: 0; /* Changed from 0 auto to 0 */
|
95 |
-
padding: 15px; /* Reduced from 20px */
|
96 |
-
background: #f8f9fa;
|
97 |
-
box-sizing: border-box; /* Added box-sizing */
|
98 |
-
overflow-x: hidden; /* Prevent horizontal scroll */
|
99 |
-
}}
|
100 |
-
.container {{
|
101 |
-
background: white;
|
102 |
-
border-radius: 12px;
|
103 |
-
padding: 20px; /* Reduced from 30px */
|
104 |
-
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
105 |
-
max-width: 100%; /* Added max-width */
|
106 |
-
overflow: hidden; /* Prevent overflow */
|
107 |
-
}}
|
108 |
-
.header {{
|
109 |
-
border-bottom: 3px solid #4CAF50;
|
110 |
-
padding-bottom: 15px; /* Reduced from 20px */
|
111 |
-
margin-bottom: 20px; /* Reduced from 30px */
|
112 |
-
}}
|
113 |
-
.search-title {{
|
114 |
-
color: #2c3e50;
|
115 |
-
font-size: 24px; /* Reduced from 28px */
|
116 |
-
font-weight: 700;
|
117 |
-
margin: 0;
|
118 |
-
display: flex;
|
119 |
-
align-items: center;
|
120 |
-
gap: 10px;
|
121 |
-
}}
|
122 |
-
.search-query {{
|
123 |
-
color: #7f8c8d;
|
124 |
-
font-size: 14px; /* Reduced from 16px */
|
125 |
-
margin-top: 8px;
|
126 |
-
font-style: italic;
|
127 |
-
word-break: break-word; /* Added word break */
|
128 |
-
}}
|
129 |
-
.answer-section {{
|
130 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
131 |
-
color: white;
|
132 |
-
padding: 20px; /* Reduced from 25px */
|
133 |
-
border-radius: 10px;
|
134 |
-
margin-bottom: 20px; /* Reduced from 30px */
|
135 |
-
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
|
136 |
-
}}
|
137 |
-
.answer-title {{
|
138 |
-
font-size: 18px; /* Reduced from 20px */
|
139 |
-
font-weight: 600;
|
140 |
-
margin-bottom: 12px; /* Reduced from 15px */
|
141 |
-
display: flex;
|
142 |
-
align-items: center;
|
143 |
-
gap: 8px;
|
144 |
-
}}
|
145 |
-
.answer-content {{
|
146 |
-
font-size: 14px; /* Reduced from 16px */
|
147 |
-
line-height: 1.6; /* Reduced from 1.7 */
|
148 |
-
word-break: break-word; /* Added word break */
|
149 |
-
}}
|
150 |
-
.results-section {{
|
151 |
-
margin-top: 20px; /* Reduced from 30px */
|
152 |
-
}}
|
153 |
-
.results-header {{
|
154 |
-
font-size: 20px; /* Reduced from 22px */
|
155 |
-
font-weight: 600;
|
156 |
-
color: #2c3e50;
|
157 |
-
margin-bottom: 15px; /* Reduced from 20px */
|
158 |
-
display: flex;
|
159 |
-
align-items: center;
|
160 |
-
gap: 8px;
|
161 |
-
}}
|
162 |
-
.result-item {{
|
163 |
-
background: #fff;
|
164 |
-
border: 1px solid #e1e8ed;
|
165 |
-
border-radius: 8px;
|
166 |
-
padding: 15px; /* Reduced from 20px */
|
167 |
-
margin-bottom: 15px; /* Reduced from 20px */
|
168 |
-
transition: all 0.3s ease;
|
169 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
|
170 |
-
overflow: hidden; /* Prevent overflow */
|
171 |
-
}}
|
172 |
-
.result-item:hover {{
|
173 |
-
transform: translateY(-2px);
|
174 |
-
box-shadow: 0 8px 25px rgba(0,0,0,0.1);
|
175 |
-
border-color: #4CAF50;
|
176 |
-
}}
|
177 |
-
.result-title {{
|
178 |
-
font-size: 16px; /* Reduced from 18px */
|
179 |
-
font-weight: 600;
|
180 |
-
color: #1a73e8;
|
181 |
-
margin-bottom: 8px;
|
182 |
-
text-decoration: none;
|
183 |
-
word-break: break-word; /* Added word break */
|
184 |
-
display: block;
|
185 |
-
}}
|
186 |
-
.result-title:hover {{
|
187 |
-
text-decoration: underline;
|
188 |
-
}}
|
189 |
-
.result-url {{
|
190 |
-
color: #34a853;
|
191 |
-
font-size: 12px; /* Reduced from 14px */
|
192 |
-
margin-bottom: 10px; /* Reduced from 12px */
|
193 |
-
word-break: break-all;
|
194 |
-
overflow: hidden;
|
195 |
-
text-overflow: ellipsis;
|
196 |
-
white-space: nowrap; /* Added to prevent wrapping */
|
197 |
-
}}
|
198 |
-
.result-content {{
|
199 |
-
color: #5f6368;
|
200 |
-
line-height: 1.5; /* Reduced from 1.6 */
|
201 |
-
margin-bottom: 10px; /* Reduced from 12px */
|
202 |
-
font-size: 13px; /* Added smaller font size */
|
203 |
-
word-break: break-word; /* Added word break */
|
204 |
-
max-height: 150px; /* Added max height */
|
205 |
-
overflow: hidden; /* Added overflow hidden */
|
206 |
-
}}
|
207 |
-
.result-score {{
|
208 |
-
background: #e8f5e8;
|
209 |
-
color: #2d5a2d;
|
210 |
-
padding: 3px 10px; /* Reduced padding */
|
211 |
-
border-radius: 20px;
|
212 |
-
font-size: 11px; /* Reduced from 12px */
|
213 |
-
font-weight: 500;
|
214 |
-
display: inline-block;
|
215 |
-
}}
|
216 |
-
.divider {{
|
217 |
-
height: 1px;
|
218 |
-
background: linear-gradient(90deg, transparent, #ddd, transparent);
|
219 |
-
margin: 20px 0; /* Reduced from 30px */
|
220 |
-
}}
|
221 |
-
.metadata {{
|
222 |
-
background: #f8f9fa;
|
223 |
-
border-radius: 8px;
|
224 |
-
padding: 15px; /* Reduced from 20px */
|
225 |
-
margin-top: 20px; /* Reduced from 30px */
|
226 |
-
border-left: 4px solid #4CAF50;
|
227 |
-
font-size: 13px; /* Added smaller font size */
|
228 |
-
}}
|
229 |
-
.metadata-title {{
|
230 |
-
font-weight: 600;
|
231 |
-
color: #2c3e50;
|
232 |
-
margin-bottom: 8px; /* Reduced from 10px */
|
233 |
-
font-size: 14px; /* Added font size */
|
234 |
-
}}
|
235 |
-
.image-section {{
|
236 |
-
margin-top: 15px; /* Reduced from 20px */
|
237 |
-
}}
|
238 |
-
.image-grid {{
|
239 |
-
display: grid;
|
240 |
-
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); /* Reduced from 200px */
|
241 |
-
gap: 10px; /* Reduced from 15px */
|
242 |
-
margin-top: 10px; /* Reduced from 15px */
|
243 |
-
}}
|
244 |
-
.image-item {{
|
245 |
-
border-radius: 8px;
|
246 |
-
overflow: hidden;
|
247 |
-
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
|
248 |
-
}}
|
249 |
-
.image-item img {{
|
250 |
-
width: 100%;
|
251 |
-
height: 120px; /* Reduced from 150px */
|
252 |
-
object-fit: cover;
|
253 |
-
}}
|
254 |
-
.image-description {{
|
255 |
-
padding: 8px; /* Reduced from 10px */
|
256 |
-
background: white;
|
257 |
-
font-size: 11px; /* Reduced from 12px */
|
258 |
-
color: #666;
|
259 |
-
word-break: break-word; /* Added word break */
|
260 |
-
}}
|
261 |
-
|
262 |
-
/* Responsive adjustments */
|
263 |
-
@media (max-width: 768px) {{
|
264 |
-
.container {{ padding: 10px; }}
|
265 |
-
.search-title {{ font-size: 20px; }}
|
266 |
-
.result-item {{ padding: 10px; }}
|
267 |
-
.image-grid {{ grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); }}
|
268 |
-
}}
|
269 |
-
</style>
|
270 |
-
</head>
|
271 |
-
<body>
|
272 |
-
<div class="container">
|
273 |
-
<div class="header">
|
274 |
-
<h1 class="search-title">🔍 Tavily Search Results</h1>
|
275 |
-
<div class="search-query">Query: "{query.replace('"', '"')}"</div>
|
276 |
-
</div>"""
|
277 |
-
|
278 |
-
# Add answer section if available
|
279 |
-
if "answer" in result and result["answer"]:
|
280 |
-
html_result += f"""
|
281 |
-
<div class="answer-section">
|
282 |
-
<div class="answer-title">📋 AI Generated Answer</div>
|
283 |
-
<div class="answer-content">{result['answer']}</div>
|
284 |
-
</div>"""
|
285 |
-
|
286 |
-
# Add search results
|
287 |
-
if "results" in result and result["results"]:
|
288 |
-
html_result += f"""
|
289 |
-
<div class="results-section">
|
290 |
-
<div class="results-header">📊 Found {len(result['results'])} Results</div>"""
|
291 |
-
|
292 |
-
for i, item in enumerate(result["results"], 1):
|
293 |
-
title = item.get('title', 'No Title').replace('<', '<').replace('>', '>')
|
294 |
-
url = item.get('url', 'No URL')
|
295 |
-
content = item.get('content', 'No content available')[:600] + '...' # Reduced from 800
|
296 |
-
content = content.replace('<', '<').replace('>', '>')
|
297 |
-
score = item.get('score', 0)
|
298 |
-
|
299 |
-
html_result += f"""
|
300 |
-
<div class="result-item">
|
301 |
-
<a href="{url}" target="_blank" class="result-title">{i}. {title}</a>
|
302 |
-
<div class="result-url">{url}</div>
|
303 |
-
<div class="result-content">{content}</div>
|
304 |
-
{f'<span class="result-score">⭐ Score: {score:.3f}</span>' if score else ''}
|
305 |
-
</div>"""
|
306 |
-
|
307 |
-
html_result += "</div>"
|
308 |
-
|
309 |
-
# Add images if available
|
310 |
-
if "images" in result and result.get("images"):
|
311 |
-
html_result += """
|
312 |
-
<div class="image-section">
|
313 |
-
<div class="results-header">🖼️ Related Images</div>
|
314 |
-
<div class="image-grid">"""
|
315 |
-
for img in result["images"][:6]: # Limit to 6 images
|
316 |
-
img_url = img.get('url', '')
|
317 |
-
img_title = img.get('title', 'Image').replace('<', '<').replace('>', '>')
|
318 |
-
html_result += f"""
|
319 |
-
<div class="image-item">
|
320 |
-
<img src="{img_url}" alt="{img_title}" loading="lazy">
|
321 |
-
<div class="image-description">{img_title}</div>
|
322 |
-
</div>"""
|
323 |
-
html_result += "</div></div>"
|
324 |
-
|
325 |
-
# Add metadata (without Raw JSON section)
|
326 |
-
html_result += f"""
|
327 |
-
<div class="divider"></div>
|
328 |
-
<div class="metadata">
|
329 |
-
<div class="metadata-title">🔧 Response Metadata</div>
|
330 |
-
<div><strong>Results Count:</strong> {len(result.get('results', []))}</div>
|
331 |
-
<div><strong>Query:</strong> {query}</div>
|
332 |
-
<div><strong>Search Depth:</strong> {search_depth}</div>
|
333 |
-
<div><strong>Topic:</strong> {topic}</div>
|
334 |
-
</div>
|
335 |
-
</div>
|
336 |
-
</body>
|
337 |
-
</html>"""
|
338 |
-
|
339 |
-
return html_result
|
340 |
-
|
341 |
-
else:
|
342 |
-
return f"❌ Error: HTTP {response.status_code}\n{response.text}"
|
343 |
-
|
344 |
-
except requests.exceptions.Timeout:
|
345 |
-
return "❌ Error: Request timed out. Please try again."
|
346 |
-
except requests.exceptions.RequestException as e:
|
347 |
-
return f"❌ Error: {str(e)}"
|
348 |
-
except Exception as e:
|
349 |
-
return f"❌ Unexpected error: {str(e)}"
|
350 |
-
|
351 |
-
# Define the country options
|
352 |
-
country_options = [
|
353 |
-
"None", "afghanistan", "albania", "algeria", "andorra", "angola", "argentina",
|
354 |
-
"armenia", "australia", "austria", "azerbaijan", "bahamas", "bahrain", "bangladesh",
|
355 |
-
"barbados", "belarus", "belgium", "belize", "benin", "bhutan", "bolivia",
|
356 |
-
"bosnia and herzegovina", "botswana", "brazil", "brunei", "bulgaria", "burkina faso",
|
357 |
-
"burundi", "cambodia", "cameroon", "canada", "cape verde", "central african republic",
|
358 |
-
"chad", "chile", "china", "colombia", "comoros", "congo", "costa rica", "croatia",
|
359 |
-
"cuba", "cyprus", "czech republic", "denmark", "djibouti", "dominican republic",
|
360 |
-
"ecuador", "egypt", "el salvador", "equatorial guinea", "eritrea", "estonia",
|
361 |
-
"ethiopia", "fiji", "finland", "france", "gabon", "gambia", "georgia", "germany",
|
362 |
-
"ghana", "greece", "guatemala", "guinea", "haiti", "honduras", "hungary", "iceland",
|
363 |
-
"india", "indonesia", "iran", "iraq", "ireland", "israel", "italy", "jamaica",
|
364 |
-
"japan", "jordan", "kazakhstan", "kenya", "kuwait", "kyrgyzstan", "latvia", "lebanon",
|
365 |
-
"lesotho", "liberia", "libya", "liechtenstein", "lithuania", "luxembourg", "madagascar",
|
366 |
-
"malawi", "malaysia", "maldives", "mali", "malta", "mauritania", "mauritius", "mexico",
|
367 |
-
"moldova", "monaco", "mongolia", "montenegro", "morocco", "mozambique", "myanmar",
|
368 |
-
"namibia", "nepal", "netherlands", "new zealand", "nicaragua", "niger", "nigeria",
|
369 |
-
"north korea", "north macedonia", "norway", "oman", "pakistan", "panama",
|
370 |
-
"papua new guinea", "paraguay", "peru", "philippines", "poland", "portugal", "qatar",
|
371 |
-
"romania", "russia", "rwanda", "saudi arabia", "senegal", "serbia", "singapore",
|
372 |
-
"slovakia", "slovenia", "somalia", "south africa", "south korea", "south sudan",
|
373 |
-
"spain", "sri lanka", "sudan", "sweden", "switzerland", "syria", "taiwan",
|
374 |
-
"tajikistan", "tanzania", "thailand", "togo", "trinidad and tobago", "tunisia",
|
375 |
-
"turkey", "turkmenistan", "uganda", "ukraine", "united arab emirates",
|
376 |
-
"united kingdom", "united states", "uruguay", "uzbekistan", "venezuela", "vietnam",
|
377 |
-
"yemen", "zambia", "zimbabwe"
|
378 |
-
]
|
379 |
-
|
380 |
-
# Create the Gradio interface with fixed layout
|
381 |
-
with gr.Blocks(title="Tavily Search API", theme=gr.themes.Citrus(), css="""
|
382 |
-
/* Custom CSS to prevent interface squeezing */
|
383 |
-
.gradio-container {
|
384 |
-
max-width: none !important;
|
385 |
-
}
|
386 |
-
.contain {
|
387 |
-
max-width: none !important;
|
388 |
-
}
|
389 |
-
#search_results {
|
390 |
-
max-height: 600px !important;
|
391 |
-
overflow-y: auto !important;
|
392 |
-
border: 1px solid #e0e0e0 !important;
|
393 |
-
border-radius: 8px !important;
|
394 |
-
}
|
395 |
-
""") as app:
|
396 |
-
gr.Markdown("# 🔍 Tavily Search API Interface")
|
397 |
-
gr.Markdown("Search the web using Tavily's powerful search API with customizable parameters.")
|
398 |
-
|
399 |
-
# Add documentation accordion
|
400 |
-
with gr.Accordion("📖 API Documentation & Parameter Options", open=False):
|
401 |
-
gr.Markdown("""
|
402 |
-
## Available Options
|
403 |
-
### **topic**: The category of the search
|
404 |
-
- **news**: Useful for retrieving real-time updates, particularly about politics, sports, and major current events covered by mainstream media sources
|
405 |
-
- **general**: For broader, more general-purpose searches that may include a wide range of sources
|
406 |
-
|
407 |
-
**Available options**: `general`, `news`
|
408 |
-
### **search_depth**: The depth of the search
|
409 |
-
- **advanced**: Tailored to retrieve the most relevant sources and content snippets for your query (costs 2 API Credits)
|
410 |
-
- **basic**: Provides generic content snippets from each source (costs 1 API Credit)
|
411 |
-
|
412 |
-
**Available options**: `basic`, `advanced`
|
413 |
-
### **chunks_per_source**: Content snippets control
|
414 |
-
Chunks are short content snippets (maximum 500 characters each) pulled directly from the source. Use this to define the maximum number of relevant chunks returned per source and to control the content length. Chunks will appear in the content field as: `<chunk 1> [...] <chunk 2> [...] <chunk 3>`.
|
415 |
-
|
416 |
-
**Note**: Available only when `search_depth` is `advanced`
|
417 |
-
|
418 |
-
**Required range**: 1 ≤ x ≤ 3
|
419 |
-
### **max_results**: Maximum number of search results
|
420 |
-
The maximum number of search results to return.
|
421 |
-
|
422 |
-
**Required range**: 0 ≤ x ≤ 20
|
423 |
-
### **time_range**: Time filtering
|
424 |
-
The time range back from the current date to filter results. Useful when looking for sources that have published data.
|
425 |
-
|
426 |
-
**Available options**: `day`, `week`, `month`, `year`, `d`, `w`, `m`, `y`
|
427 |
-
### **days**: Days back for news searches
|
428 |
-
Number of days back from the current date to include. Available only if topic is `news`.
|
429 |
-
|
430 |
-
**Required range**: x ≥ 1
|
431 |
-
### **include_answer**: LLM-generated answer
|
432 |
-
Include an LLM-generated answer to the provided query.
|
433 |
-
- `basic` or `true`: Returns a quick answer
|
434 |
-
- `advanced`: Returns a more detailed answer
|
435 |
-
### **include_raw_content**: Raw HTML content
|
436 |
-
Include the cleaned and parsed HTML content of each search result.
|
437 |
-
- `markdown` or `true`: Returns search result content in markdown format
|
438 |
-
- `text`: Returns the plain text from the results (may increase latency)
|
439 |
-
### **include_images**: Image search
|
440 |
-
Also perform an image search and include the results in the response.
|
441 |
-
### **include_image_descriptions**: Image descriptions
|
442 |
-
When `include_images` is true, also add a descriptive text for each image.
|
443 |
-
### **include_domains**: Domain inclusion
|
444 |
-
A list of domains to specifically include in the search results.
|
445 |
-
### **exclude_domains**: Domain exclusion
|
446 |
-
A list of domains to specifically exclude from the search results.
|
447 |
-
### **country**: Country-specific boosting
|
448 |
-
Boost search results from a specific country. This will prioritize content from the selected country in the search results. Available only if topic is `general`.
|
449 |
-
|
450 |
-
**Available countries**: afghanistan, albania, algeria, andorra, angola, argentina, armenia, australia, austria, azerbaijan, bahamas, bahrain, bangladesh, barbados, belarus, belgium, belize, benin, bhutan, bolivia, bosnia and herzegovina, botswana, brazil, brunei, bulgaria, burkina faso, burundi, cambodia, cameroon, canada, cape verde, central african republic, chad, chile, china, colombia, comoros, congo, costa rica, croatia, cuba, cyprus, czech republic, denmark, djibouti, dominican republic, ecuador, egypt, el salvador, equatorial guinea, eritrea, estonia, ethiopia, fiji, finland, france, gabon, gambia, georgia, germany, ghana, greece, guatemala, guinea, haiti, honduras, hungary, iceland, india, indonesia, iran, iraq, ireland, israel, italy, jamaica, japan, jordan, kazakhstan, kenya, kuwait, kyrgyzstan, latvia, lebanon, lesotho, liberia, libya, liechtenstein, lithuania, luxembourg, madagascar, malawi, malaysia, maldives, mali, malta, mauritania, mauritius, mexico, moldova, monaco, mongolia, montenegro, morocco, mozambique, myanmar, namibia, nepal, netherlands, new zealand, nicaragua, niger, nigeria, north korea, north macedonia, norway, oman, pakistan, panama, papua new guinea, paraguay, peru, philippines, poland, portugal, qatar, romania, russia, rwanda, saudi arabia, senegal, serbia, singapore, slovakia, slovenia, somalia, south africa, south korea, south sudan, spain, sri lanka, sudan, sweden, switzerland, syria, taiwan, tajikistan, tanzania, thailand, togo, trinidad and tobago, tunisia, turkey, turkmenistan, uganda, ukraine, united arab emirates, united kingdom, united states, uruguay, uzbekistan, venezuela, vietnam, yemen, zambia, zimbabwe
|
451 |
-
""")
|
452 |
-
|
453 |
-
# API Configuration
|
454 |
-
gr.Markdown("## 🔑 API Configuration")
|
455 |
-
api_key = gr.Textbox(
|
456 |
-
label="Tavily API Key",
|
457 |
-
placeholder="Enter your Tavily API key here...",
|
458 |
-
type="password",
|
459 |
-
info="Your API key will be used to authenticate requests to Tavily."
|
460 |
-
)
|
461 |
-
|
462 |
-
# Search Parameters
|
463 |
-
gr.Markdown("## 🎯 Search Parameters")
|
464 |
-
query = gr.Textbox(
|
465 |
-
label="Search Query",
|
466 |
-
placeholder="e.g., 'who is Leo Messi?'",
|
467 |
-
info="The search query you want to execute."
|
468 |
-
)
|
469 |
-
|
470 |
-
with gr.Row():
|
471 |
-
topic = gr.Dropdown(
|
472 |
-
choices=["general", "news"],
|
473 |
-
value="general",
|
474 |
-
label="Topic",
|
475 |
-
info="general: broad searches, news: real-time updates"
|
476 |
-
)
|
477 |
-
|
478 |
-
search_depth = gr.Dropdown(
|
479 |
-
choices=["basic", "advanced"],
|
480 |
-
value="basic",
|
481 |
-
label="Search Depth",
|
482 |
-
info="basic: 1 credit, advanced: 2 credits"
|
483 |
-
)
|
484 |
-
|
485 |
-
with gr.Row():
|
486 |
-
max_results = gr.Slider(
|
487 |
-
minimum=1,
|
488 |
-
maximum=20,
|
489 |
-
value=5,
|
490 |
-
step=1,
|
491 |
-
label="Max Results",
|
492 |
-
info="Maximum number of search results to return"
|
493 |
-
)
|
494 |
-
|
495 |
-
chunks_per_source = gr.Slider(
|
496 |
-
minimum=1,
|
497 |
-
maximum=3,
|
498 |
-
value=3,
|
499 |
-
step=1,
|
500 |
-
label="Chunks per Source",
|
501 |
-
info="Only applies to advanced search"
|
502 |
-
)
|
503 |
-
|
504 |
-
# Advanced Options
|
505 |
-
gr.Markdown("## ⚙️ Advanced Options")
|
506 |
-
|
507 |
-
with gr.Row():
|
508 |
-
time_range = gr.Dropdown(
|
509 |
-
choices=["None", "day", "week", "month", "year"],
|
510 |
-
value="None",
|
511 |
-
label="Time Range",
|
512 |
-
info="Filter results by time period"
|
513 |
-
)
|
514 |
-
|
515 |
-
days = gr.Number(
|
516 |
-
value=7,
|
517 |
-
minimum=1,
|
518 |
-
label="Days (News only)",
|
519 |
-
info="Number of days back for news searches"
|
520 |
-
)
|
521 |
-
|
522 |
-
with gr.Row():
|
523 |
-
include_answer = gr.Checkbox(
|
524 |
-
value=True,
|
525 |
-
label="Include Answer",
|
526 |
-
info="Include LLM-generated answer"
|
527 |
-
)
|
528 |
-
|
529 |
-
include_images = gr.Checkbox(
|
530 |
-
value=False,
|
531 |
-
label="Include Images",
|
532 |
-
info="Perform image search"
|
533 |
-
)
|
534 |
-
|
535 |
-
include_image_descriptions = gr.Checkbox(
|
536 |
-
value=False,
|
537 |
-
label="Include Image Descriptions",
|
538 |
-
info="Add descriptions for images"
|
539 |
-
)
|
540 |
-
|
541 |
-
include_raw_content = gr.Dropdown(
|
542 |
-
choices=["false", "html", "markdown", "text"],
|
543 |
-
value="html",
|
544 |
-
label="Include Raw Content",
|
545 |
-
info="HTML: Clean styled format, Markdown: MD format, Text: Plain text"
|
546 |
-
)
|
547 |
-
|
548 |
-
with gr.Row():
|
549 |
-
include_domains = gr.Textbox(
|
550 |
-
label="Include Domains",
|
551 |
-
placeholder="example.com, another.com",
|
552 |
-
info="Comma-separated list of domains to include"
|
553 |
-
)
|
554 |
-
|
555 |
-
exclude_domains = gr.Textbox(
|
556 |
-
label="Exclude Domains",
|
557 |
-
placeholder="example.com, another.com",
|
558 |
-
info="Comma-separated list of domains to exclude"
|
559 |
-
)
|
560 |
-
|
561 |
-
country = gr.Dropdown(
|
562 |
-
choices=country_options,
|
563 |
-
value="None",
|
564 |
-
label="Country",
|
565 |
-
info="Boost results from specific country (general topic only)"
|
566 |
-
)
|
567 |
-
|
568 |
-
# Search Button
|
569 |
-
search_btn = gr.Button("🔍 Search", variant="primary", size="lg")
|
570 |
-
|
571 |
-
# Results Section with fixed height
|
572 |
-
gr.Markdown("## 📊 Search Results")
|
573 |
-
results = gr.HTML(
|
574 |
-
label="Results",
|
575 |
-
show_label=False,
|
576 |
-
elem_id="search_results"
|
577 |
-
)
|
578 |
-
|
579 |
-
# Examples
|
580 |
-
gr.Markdown("## 💡 Comprehensive Test Examples")
|
581 |
-
gr.Examples(
|
582 |
-
[
|
583 |
-
# Basic general search
|
584 |
-
[
|
585 |
-
"Who is Leo Messi?", # query
|
586 |
-
"general", # topic
|
587 |
-
"basic", # search_depth
|
588 |
-
3, # chunks_per_source
|
589 |
-
5, # max_results
|
590 |
-
"None", # time_range
|
591 |
-
7, # days
|
592 |
-
True, # include_answer
|
593 |
-
"html", # include_raw_content
|
594 |
-
False, # include_images
|
595 |
-
False, # include_image_descriptions
|
596 |
-
"", # include_domains
|
597 |
-
"", # exclude_domains
|
598 |
-
"None" # country
|
599 |
-
],
|
600 |
-
# Advanced news search with time filter
|
601 |
-
[
|
602 |
-
"Latest artificial intelligence breakthroughs",
|
603 |
-
"news",
|
604 |
-
"advanced",
|
605 |
-
3,
|
606 |
-
3,
|
607 |
-
"week",
|
608 |
-
3,
|
609 |
-
True,
|
610 |
-
"html",
|
611 |
-
False,
|
612 |
-
False,
|
613 |
-
"",
|
614 |
-
"",
|
615 |
-
"None"
|
616 |
-
],
|
617 |
-
# Python tutorials with domain inclusion
|
618 |
-
[
|
619 |
-
"Python machine learning tutorials",
|
620 |
-
"general",
|
621 |
-
"advanced",
|
622 |
-
2,
|
623 |
-
8,
|
624 |
-
"None",
|
625 |
-
7,
|
626 |
-
True,
|
627 |
-
"html",
|
628 |
-
False,
|
629 |
-
False,
|
630 |
-
"github.com, stackoverflow.com, medium.com",
|
631 |
-
"",
|
632 |
-
"None"
|
633 |
-
],
|
634 |
-
# Climate change news with exclusions
|
635 |
-
[
|
636 |
-
"Climate change policy updates",
|
637 |
-
"news",
|
638 |
-
"basic",
|
639 |
-
3,
|
640 |
-
4,
|
641 |
-
"month",
|
642 |
-
14,
|
643 |
-
True,
|
644 |
-
"html",
|
645 |
-
False,
|
646 |
-
False,
|
647 |
-
"",
|
648 |
-
"facebook.com, twitter.com, reddit.com",
|
649 |
-
"None"
|
650 |
-
],
|
651 |
-
# Country-specific search
|
652 |
-
[
|
653 |
-
"Best restaurants and food culture",
|
654 |
-
"general",
|
655 |
-
"basic",
|
656 |
-
3,
|
657 |
-
6,
|
658 |
-
"None",
|
659 |
-
7,
|
660 |
-
True,
|
661 |
-
"false",
|
662 |
-
True,
|
663 |
-
True,
|
664 |
-
"",
|
665 |
-
"",
|
666 |
-
"france"
|
667 |
-
],
|
668 |
-
# Technology news with images
|
669 |
-
[
|
670 |
-
"iPhone 15 features and specs",
|
671 |
-
"general",
|
672 |
-
"advanced",
|
673 |
-
3,
|
674 |
-
5,
|
675 |
-
"None",
|
676 |
-
7,
|
677 |
-
True,
|
678 |
-
"html",
|
679 |
-
True,
|
680 |
-
True,
|
681 |
-
"apple.com, techcrunch.com, theverge.com",
|
682 |
-
"",
|
683 |
-
"united states"
|
684 |
-
],
|
685 |
-
# Sports news recent
|
686 |
-
[
|
687 |
-
"FIFA World Cup 2024 results",
|
688 |
-
"news",
|
689 |
-
"advanced",
|
690 |
-
3,
|
691 |
-
7,
|
692 |
-
"day",
|
693 |
-
1,
|
694 |
-
True,
|
695 |
-
"html",
|
696 |
-
False,
|
697 |
-
False,
|
698 |
-
"fifa.com, espn.com, bbc.com",
|
699 |
-
"",
|
700 |
-
"None"
|
701 |
-
],
|
702 |
-
# Academic research
|
703 |
-
[
|
704 |
-
"quantum computing research papers 2024",
|
705 |
-
"general",
|
706 |
-
"advanced",
|
707 |
-
3,
|
708 |
-
10,
|
709 |
-
"year",
|
710 |
-
7,
|
711 |
-
True,
|
712 |
-
"html",
|
713 |
-
False,
|
714 |
-
False,
|
715 |
-
"arxiv.org, nature.com, science.org",
|
716 |
-
"wikipedia.org",
|
717 |
-
"None"
|
718 |
-
],
|
719 |
-
# Stock market news
|
720 |
-
[
|
721 |
-
"Tesla stock price analysis",
|
722 |
-
"news",
|
723 |
-
"basic",
|
724 |
-
3,
|
725 |
-
5,
|
726 |
-
"week",
|
727 |
-
7,
|
728 |
-
True,
|
729 |
-
"false",
|
730 |
-
False,
|
731 |
-
False,
|
732 |
-
"yahoo.com, bloomberg.com, marketwatch.com",
|
733 |
-
"",
|
734 |
-
"None"
|
735 |
-
],
|
736 |
-
# Health and medical
|
737 |
-
[
|
738 |
-
"COVID-19 vaccine effectiveness studies",
|
739 |
-
"general",
|
740 |
-
"advanced",
|
741 |
-
3,
|
742 |
-
8,
|
743 |
-
"month",
|
744 |
-
7,
|
745 |
-
True,
|
746 |
-
"text",
|
747 |
-
False,
|
748 |
-
False,
|
749 |
-
"who.int, cdc.gov, pubmed.ncbi.nlm.nih.gov",
|
750 |
-
"facebook.com, instagram.com",
|
751 |
-
"None"
|
752 |
-
]
|
753 |
-
],
|
754 |
-
inputs=[
|
755 |
-
query, topic, search_depth, chunks_per_source, max_results,
|
756 |
-
time_range, days, include_answer, include_raw_content, include_images,
|
757 |
-
include_image_descriptions, include_domains, exclude_domains, country
|
758 |
-
],
|
759 |
-
label="🚀 Click on any example to load complete test configurations"
|
760 |
-
)
|
761 |
-
|
762 |
-
# Connect the search button
|
763 |
-
search_btn.click(
|
764 |
-
fn=tavily_search,
|
765 |
-
inputs=[
|
766 |
-
api_key, query, topic, search_depth, chunks_per_source, max_results,
|
767 |
-
time_range, days, include_answer, include_raw_content, include_images,
|
768 |
-
include_image_descriptions, include_domains, exclude_domains, country
|
769 |
-
],
|
770 |
-
outputs=results
|
771 |
-
)
|
772 |
-
|
773 |
-
# Footer
|
774 |
-
gr.Markdown("---")
|
775 |
-
gr.Markdown("🔗 **Get your Tavily API key at:** [tavily.com](https://tavily.com)")
|
776 |
-
|
777 |
-
# Launch the app
|
778 |
-
if __name__ == "__main__":
|
779 |
-
app.launch(
|
780 |
-
share=True
|
781 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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