File size: 31,460 Bytes
9d0ab90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8cc3aa8
 
 
 
9d0ab90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8cc3aa8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d0ab90
8cc3aa8
9d0ab90
8cc3aa8
 
 
 
 
 
9d0ab90
8cc3aa8
 
 
 
 
 
9d0ab90
 
8cc3aa8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d0ab90
8cc3aa8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d0ab90
8cc3aa8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d0ab90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76e550a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d0ab90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8cc3aa8
 
9d0ab90
8cc3aa8
9d0ab90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8cc3aa8
9d0ab90
8cc3aa8
 
9d0ab90
 
 
76e550a
9d0ab90
 
76e550a
 
 
 
 
 
 
 
 
 
8cc3aa8
76e550a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8cc3aa8
76e550a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8cc3aa8
76e550a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8cc3aa8
76e550a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8cc3aa8
76e550a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8cc3aa8
76e550a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8cc3aa8
76e550a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d0ab90
76e550a
9d0ab90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd4f75f
9d0ab90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
import gradio as gr
import requests
import json
from typing import Optional, List

def tavily_search(
    api_key: str,
    query: str,
    topic: str = "general",
    search_depth: str = "basic",
    chunks_per_source: int = 3,
    max_results: int = 5,
    time_range: Optional[str] = None,
    days: int = 7,
    include_answer: bool = True,
    include_raw_content: str = "false",
    include_images: bool = False,
    include_image_descriptions: bool = False,
    include_domains: str = "",
    exclude_domains: str = "",
    country: Optional[str] = None
):
    """
    Perform a Tavily search with the given parameters.
    """
    if not api_key.strip():
        return "❌ Error: Please provide a valid Tavily API key."
    
    if not query.strip():
        return "❌ Error: Please provide a search query."
    
    url = "https://api.tavily.com/search"
    
    # Prepare the payload
    payload = {
        "query": query,
        "topic": topic,
        "search_depth": search_depth,
        "max_results": max_results,
        "include_answer": include_answer,
        "include_images": include_images,
        "include_image_descriptions": include_image_descriptions,
    }
    
    # Add optional parameters
    if search_depth == "advanced":
        payload["chunks_per_source"] = chunks_per_source
    
    if time_range and time_range != "None":
        payload["time_range"] = time_range
    
    if topic == "news" and days > 0:
        payload["days"] = days
    
    if include_raw_content != "false":
        if include_raw_content == "html":
            payload["include_raw_content"] = True
        else:
            payload["include_raw_content"] = include_raw_content
    
    if include_domains.strip():
        payload["include_domains"] = [domain.strip() for domain in include_domains.split(",")]
    
    if exclude_domains.strip():
        payload["exclude_domains"] = [domain.strip() for domain in exclude_domains.split(",")]
    
    if country and country != "None":
        payload["country"] = country
    
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    
    try:
        response = requests.post(url, json=payload, headers=headers, timeout=30)
        
        if response.status_code == 200:
            result = response.json()
            
            # Format the response as clean HTML
            html_result = """
            <!DOCTYPE html>
            <html>
            <head>
                <meta charset="UTF-8">
                <title>Tavily Search Results</title>
                <style>
                    body {
                        font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
                        line-height: 1.6;
                        color: #333;
                        max-width: 1200px;
                        margin: 0 auto;
                        padding: 20px;
                        background: #f8f9fa;
                    }
                    .container {
                        background: white;
                        border-radius: 12px;
                        padding: 30px;
                        box-shadow: 0 4px 6px rgba(0,0,0,0.1);
                    }
                    .header {
                        border-bottom: 3px solid #4CAF50;
                        padding-bottom: 20px;
                        margin-bottom: 30px;
                    }
                    .search-title {
                        color: #2c3e50;
                        font-size: 28px;
                        font-weight: 700;
                        margin: 0;
                        display: flex;
                        align-items: center;
                        gap: 10px;
                    }
                    .search-query {
                        color: #7f8c8d;
                        font-size: 16px;
                        margin-top: 8px;
                        font-style: italic;
                    }
                    .answer-section {
                        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                        color: white;
                        padding: 25px;
                        border-radius: 10px;
                        margin-bottom: 30px;
                        box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
                    }
                    .answer-title {
                        font-size: 20px;
                        font-weight: 600;
                        margin-bottom: 15px;
                        display: flex;
                        align-items: center;
                        gap: 8px;
                    }
                    .answer-content {
                        font-size: 16px;
                        line-height: 1.7;
                    }
                    .results-section {
                        margin-top: 30px;
                    }
                    .results-header {
                        font-size: 22px;
                        font-weight: 600;
                        color: #2c3e50;
                        margin-bottom: 20px;
                        display: flex;
                        align-items: center;
                        gap: 8px;
                    }
                    .result-item {
                        background: #fff;
                        border: 1px solid #e1e8ed;
                        border-radius: 8px;
                        padding: 20px;
                        margin-bottom: 20px;
                        transition: all 0.3s ease;
                        box-shadow: 0 2px 4px rgba(0,0,0,0.05);
                    }
                    .result-item:hover {
                        transform: translateY(-2px);
                        box-shadow: 0 8px 25px rgba(0,0,0,0.1);
                        border-color: #4CAF50;
                    }
                    .result-title {
                        font-size: 18px;
                        font-weight: 600;
                        color: #1a73e8;
                        margin-bottom: 8px;
                        text-decoration: none;
                    }
                    .result-title:hover {
                        text-decoration: underline;
                    }
                    .result-url {
                        color: #34a853;
                        font-size: 14px;
                        margin-bottom: 12px;
                        word-break: break-all;
                    }
                    .result-content {
                        color: #5f6368;
                        line-height: 1.6;
                        margin-bottom: 12px;
                    }
                    .result-score {
                        background: #e8f5e8;
                        color: #2d5a2d;
                        padding: 4px 12px;
                        border-radius: 20px;
                        font-size: 12px;
                        font-weight: 500;
                        display: inline-block;
                    }
                    .divider {
                        height: 1px;
                        background: linear-gradient(90deg, transparent, #ddd, transparent);
                        margin: 30px 0;
                    }
                    .metadata {
                        background: #f8f9fa;
                        border-radius: 8px;
                        padding: 20px;
                        margin-top: 30px;
                        border-left: 4px solid #4CAF50;
                    }
                    .metadata-title {
                        font-weight: 600;
                        color: #2c3e50;
                        margin-bottom: 10px;
                    }
                    .json-container {
                        background: #2d3748;
                        color: #e2e8f0;
                        padding: 20px;
                        border-radius: 8px;
                        overflow-x: auto;
                        font-family: 'Monaco', 'Consolas', monospace;
                        font-size: 12px;
                        line-height: 1.5;
                        max-height: 400px;
                        overflow-y: auto;
                    }
                    .image-section {
                        margin-top: 20px;
                    }
                    .image-grid {
                        display: grid;
                        grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
                        gap: 15px;
                        margin-top: 15px;
                    }
                    .image-item {
                        border-radius: 8px;
                        overflow: hidden;
                        box-shadow: 0 2px 8px rgba(0,0,0,0.1);
                    }
                    .image-item img {
                        width: 100%;
                        height: 150px;
                        object-fit: cover;
                    }
                    .image-description {
                        padding: 10px;
                        background: white;
                        font-size: 12px;
                        color: #666;
                    }
                </style>
            </head>
            <body>
                <div class="container">
                    <div class="header">
                        <h1 class="search-title">πŸ” Tavily Search Results</h1>
                        <div class="search-query">Query: "{}"</div>
                    </div>
            """.format(query.replace('"', '&quot;'))
            
            # Add answer section if available
            if "answer" in result and result["answer"]:
                html_result += f"""
                    <div class="answer-section">
                        <div class="answer-title">πŸ“‹ AI Generated Answer</div>
                        <div class="answer-content">{result['answer']}</div>
                    </div>
                """
            
            # Add search results
            if "results" in result and result["results"]:
                html_result += f"""
                    <div class="results-section">
                        <div class="results-header">πŸ“Š Found {len(result['results'])} Results</div>
                """
                
                for i, item in enumerate(result["results"], 1):
                    title = item.get('title', 'No Title').replace('<', '&lt;').replace('>', '&gt;')
                    url = item.get('url', 'No URL')
                    content = item.get('content', 'No content available')[:800] + '...'
                    content = content.replace('<', '&lt;').replace('>', '&gt;')
                    score = item.get('score', 0)
                    
                    html_result += f"""
                        <div class="result-item">
                            <a href="{url}" target="_blank" class="result-title">{i}. {title}</a>
                            <div class="result-url">{url}</div>
                            <div class="result-content">{content}</div>
                            {f'<span class="result-score">⭐ Score: {score:.3f}</span>' if score else ''}
                        </div>
                    """
                
                html_result += "</div>"
            
            # Add images if available
            if "images" in result and result.get("images"):
                html_result += """
                    <div class="image-section">
                        <div class="results-header">πŸ–ΌοΈ Related Images</div>
                        <div class="image-grid">
                """
                for img in result["images"][:6]:  # Limit to 6 images
                    img_url = img.get('url', '')
                    img_title = img.get('title', 'Image').replace('<', '&lt;').replace('>', '&gt;')
                    html_result += f"""
                        <div class="image-item">
                            <img src="{img_url}" alt="{img_title}" loading="lazy">
                            <div class="image-description">{img_title}</div>
                        </div>
                    """
                html_result += "</div></div>"
            
            # Add metadata
            html_result += f"""
                    <div class="divider"></div>
                    <div class="metadata">
                        <div class="metadata-title">πŸ”§ Response Metadata</div>
                        <div><strong>Results Count:</strong> {len(result.get('results', []))}</div>
                        <div><strong>Query:</strong> {query}</div>
                        <div><strong>Search Depth:</strong> {search_depth}</div>
                        <div><strong>Topic:</strong> {topic}</div>
                    </div>
                    
                    <div class="metadata">
                        <div class="metadata-title">πŸ“„ Raw JSON Response</div>
                        <div class="json-container">{json.dumps(result, indent=2).replace('<', '&lt;').replace('>', '&gt;')}</div>
                    </div>
                </div>
            </body>
            </html>
            """
            
            return html_result
            
        else:
            return f"❌ Error: HTTP {response.status_code}\n{response.text}"
            
    except requests.exceptions.Timeout:
        return "❌ Error: Request timed out. Please try again."
    except requests.exceptions.RequestException as e:
        return f"❌ Error: {str(e)}"
    except Exception as e:
        return f"❌ Unexpected error: {str(e)}"

# Define the country options
country_options = [
    "None", "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"
]

# Create the Gradio interface
with gr.Blocks(title="Tavily Search API", theme=gr.themes.Soft()) as app:
    gr.Markdown("# πŸ” Tavily Search API Interface")
    gr.Markdown("Search the web using Tavily's powerful search API with customizable parameters.")
    
    # Add documentation accordion
    with gr.Accordion("πŸ“– API Documentation & Parameter Options", open=False):
        gr.Markdown("""
        ## Available Options

        ### **topic**: The category of the search
        - **news**: Useful for retrieving real-time updates, particularly about politics, sports, and major current events covered by mainstream media sources
        - **general**: For broader, more general-purpose searches that may include a wide range of sources
        
        **Available options**: `general`, `news`

        ### **search_depth**: The depth of the search
        - **advanced**: Tailored to retrieve the most relevant sources and content snippets for your query (costs 2 API Credits)
        - **basic**: Provides generic content snippets from each source (costs 1 API Credit)
        
        **Available options**: `basic`, `advanced`

        ### **chunks_per_source**: Content snippets control
        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>`. 
        
        **Note**: Available only when `search_depth` is `advanced`
        
        **Required range**: 1 ≀ x ≀ 3

        ### **max_results**: Maximum number of search results
        The maximum number of search results to return.
        
        **Required range**: 0 ≀ x ≀ 20

        ### **time_range**: Time filtering
        The time range back from the current date to filter results. Useful when looking for sources that have published data.
        
        **Available options**: `day`, `week`, `month`, `year`, `d`, `w`, `m`, `y`

        ### **days**: Days back for news searches
        Number of days back from the current date to include. Available only if topic is `news`.
        
        **Required range**: x β‰₯ 1

        ### **include_answer**: LLM-generated answer
        Include an LLM-generated answer to the provided query.
        - `basic` or `true`: Returns a quick answer
        - `advanced`: Returns a more detailed answer

        ### **include_raw_content**: Raw HTML content
        Include the cleaned and parsed HTML content of each search result.
        - `markdown` or `true`: Returns search result content in markdown format
        - `text`: Returns the plain text from the results (may increase latency)

        ### **include_images**: Image search
        Also perform an image search and include the results in the response.

        ### **include_image_descriptions**: Image descriptions
        When `include_images` is true, also add a descriptive text for each image.

        ### **include_domains**: Domain inclusion
        A list of domains to specifically include in the search results.

        ### **exclude_domains**: Domain exclusion
        A list of domains to specifically exclude from the search results.

        ### **country**: Country-specific boosting
        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`.
        
        **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
        """)
    
    with gr.Row():
        with gr.Column(scale=1):
            # API Configuration
            gr.Markdown("## πŸ”‘ API Configuration")
            api_key = gr.Textbox(
                label="Tavily API Key",
                placeholder="Enter your Tavily API key here...",
                type="password",
                info="Your API key will be used to authenticate requests to Tavily."
            )
            
            # Search Parameters
            gr.Markdown("## 🎯 Search Parameters")
            query = gr.Textbox(
                label="Search Query",
                placeholder="e.g., 'who is Leo Messi?'",
                info="The search query you want to execute."
            )
            
            with gr.Row():
                topic = gr.Dropdown(
                    choices=["general", "news"],
                    value="general",
                    label="Topic",
                    info="general: broad searches, news: real-time updates"
                )
                
                search_depth = gr.Dropdown(
                    choices=["basic", "advanced"],
                    value="basic",
                    label="Search Depth",
                    info="basic: 1 credit, advanced: 2 credits"
                )
            
            with gr.Row():
                max_results = gr.Slider(
                    minimum=1,
                    maximum=20,
                    value=5,
                    step=1,
                    label="Max Results",
                    info="Maximum number of search results to return"
                )
                
                chunks_per_source = gr.Slider(
                    minimum=1,
                    maximum=3,
                    value=3,
                    step=1,
                    label="Chunks per Source",
                    info="Only applies to advanced search"
                )
            
            # Advanced Options
            gr.Markdown("## βš™οΈ Advanced Options")
            
            with gr.Row():
                time_range = gr.Dropdown(
                    choices=["None", "day", "week", "month", "year"],
                    value="None",
                    label="Time Range",
                    info="Filter results by time period"
                )
                
                days = gr.Number(
                    value=7,
                    minimum=1,
                    label="Days (News only)",
                    info="Number of days back for news searches"
                )
            
            with gr.Row():
                include_answer = gr.Checkbox(
                    value=True,
                    label="Include Answer",
                    info="Include LLM-generated answer"
                )
                
                include_images = gr.Checkbox(
                    value=False,
                    label="Include Images",
                    info="Perform image search"
                )
                
                include_image_descriptions = gr.Checkbox(
                    value=False,
                    label="Include Image Descriptions",
                    info="Add descriptions for images"
                )
            
            include_raw_content = gr.Dropdown(
                choices=["false", "html", "markdown", "text"],
                value="html",
                label="Include Raw Content",
                info="HTML: Clean styled format, Markdown: MD format, Text: Plain text"
            )
            
            with gr.Row():
                include_domains = gr.Textbox(
                    label="Include Domains",
                    placeholder="example.com, another.com",
                    info="Comma-separated list of domains to include"
                )
                
                exclude_domains = gr.Textbox(
                    label="Exclude Domains",
                    placeholder="example.com, another.com",
                    info="Comma-separated list of domains to exclude"
                )
            
            country = gr.Dropdown(
                choices=country_options,
                value="None",
                label="Country",
                info="Boost results from specific country (general topic only)"
            )
            
            # Search Button
            search_btn = gr.Button("πŸ” Search", variant="primary", size="lg")
        
        with gr.Column(scale=2):
            # Results
            gr.Markdown("## πŸ“Š Search Results")
            results = gr.HTML(
                label="Results",
                show_label=False,
                elem_id="search_results"
            )
    
    # Examples
    gr.Markdown("## πŸ’‘ Comprehensive Test Examples")
    gr.Examples(
        [
            # Basic general search
            [
                "Who is Leo Messi?",  # query
                "general",  # topic
                "basic",  # search_depth
                3,  # chunks_per_source
                5,  # max_results
                "None",  # time_range
                7,  # days
                True,  # include_answer
                "html",  # include_raw_content
                False,  # include_images
                False,  # include_image_descriptions
                "",  # include_domains
                "",  # exclude_domains
                "None"  # country
            ],
            # Advanced news search with time filter
            [
                "Latest artificial intelligence breakthroughs",
                "news",
                "advanced",
                3,
                3,
                "week",
                3,
                True,
                "html",
                False,
                False,
                "",
                "",
                "None"
            ],
            # Python tutorials with domain inclusion
            [
                "Python machine learning tutorials",
                "general",
                "advanced",
                2,
                8,
                "None",
                7,
                True,
                "html",
                False,
                False,
                "github.com, stackoverflow.com, medium.com",
                "",
                "None"
            ],
            # Climate change news with exclusions
            [
                "Climate change policy updates",
                "news",
                "basic",
                3,
                4,
                "month",
                14,
                True,
                "html",
                False,
                False,
                "",
                "facebook.com, twitter.com, reddit.com",
                "None"
            ],
            # Country-specific search
            [
                "Best restaurants and food culture",
                "general",
                "basic",
                3,
                6,
                "None",
                7,
                True,
                "false",
                True,
                True,
                "",
                "",
                "france"
            ],
            # Technology news with images
            [
                "iPhone 15 features and specs",
                "general",
                "advanced",
                3,
                5,
                "None",
                7,
                True,
                "html",
                True,
                True,
                "apple.com, techcrunch.com, theverge.com",
                "",
                "united states"
            ],
            # Sports news recent
            [
                "FIFA World Cup 2024 results",
                "news",
                "advanced",
                3,
                7,
                "day",
                1,
                True,
                "html",
                False,
                False,
                "fifa.com, espn.com, bbc.com",
                "",
                "None"
            ],
            # Academic research
            [
                "quantum computing research papers 2024",
                "general",
                "advanced",
                3,
                10,
                "year",
                7,
                True,
                "html",
                False,
                False,
                "arxiv.org, nature.com, science.org",
                "wikipedia.org",
                "None"
            ],
            # Stock market news
            [
                "Tesla stock price analysis",
                "news",
                "basic",
                3,
                5,
                "week",
                7,
                True,
                "false",
                False,
                False,
                "yahoo.com, bloomberg.com, marketwatch.com",
                "",
                "None"
            ],
            # Health and medical
            [
                "COVID-19 vaccine effectiveness studies",
                "general",
                "advanced",
                3,
                8,
                "month",
                7,
                True,
                "text",
                False,
                False,
                "who.int, cdc.gov, pubmed.ncbi.nlm.nih.gov",
                "facebook.com, instagram.com",
                "None"
            ]
        ],
        inputs=[
            query, topic, search_depth, chunks_per_source, max_results,
            time_range, days, include_answer, include_raw_content, include_images,
            include_image_descriptions, include_domains, exclude_domains, country
        ],
        label="πŸš€ Click on any example to load complete test configurations"
    )
    
    # Connect the search button
    search_btn.click(
        fn=tavily_search,
        inputs=[
            api_key, query, topic, search_depth, chunks_per_source, max_results,
            time_range, days, include_answer, include_raw_content, include_images,
            include_image_descriptions, include_domains, exclude_domains, country
        ],
        outputs=results
    )
    
    # Footer
    gr.Markdown("---")
    gr.Markdown("πŸ”— **Get your Tavily API key at:** [tavily.com](https://tavily.com)")

# Launch the app
if __name__ == "__main__":
    app.launch(
        share=True
    )