File size: 36,701 Bytes
93fb7cb
 
 
 
 
 
 
 
c01e49c
 
 
 
 
 
 
93fb7cb
 
 
 
81a5137
 
93fb7cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81a5137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93fb7cb
 
 
 
 
 
 
 
 
 
 
c01e49c
 
 
 
f7478ee
c01e49c
 
 
f7478ee
c01e49c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93fb7cb
c01e49c
93fb7cb
c01e49c
 
 
 
f7478ee
c01e49c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7478ee
 
 
 
 
 
 
c01e49c
 
f7478ee
c01e49c
 
 
 
93fb7cb
c01e49c
 
 
 
 
 
 
93fb7cb
c01e49c
93fb7cb
 
c01e49c
 
 
 
 
 
 
 
 
93fb7cb
 
c01e49c
93fb7cb
c01e49c
 
 
93fb7cb
c01e49c
93fb7cb
c01e49c
93fb7cb
 
 
c01e49c
 
 
 
 
 
 
93fb7cb
c01e49c
 
 
 
93fb7cb
c01e49c
 
 
93fb7cb
c01e49c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93fb7cb
c01e49c
93fb7cb
c01e49c
93fb7cb
 
c01e49c
 
 
 
 
 
 
 
 
93fb7cb
 
c01e49c
93fb7cb
c01e49c
 
 
 
93fb7cb
 
c01e49c
93fb7cb
 
c01e49c
 
 
 
 
 
 
 
 
 
 
93fb7cb
 
 
 
c01e49c
93fb7cb
 
 
 
 
 
 
 
 
 
 
 
81a5137
 
 
93fb7cb
 
c01e49c
 
 
93fb7cb
c01e49c
 
 
 
 
 
93fb7cb
 
 
c01e49c
 
 
 
 
 
 
 
 
 
 
 
 
 
93fb7cb
 
 
 
 
 
 
 
 
 
c01e49c
93fb7cb
 
 
 
 
81a5137
 
 
 
93fb7cb
 
c01e49c
93fb7cb
81a5137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93fb7cb
 
 
 
 
 
 
 
 
 
 
 
 
c01e49c
 
 
 
93fb7cb
c01e49c
 
 
 
93fb7cb
 
 
 
 
c01e49c
93fb7cb
 
c01e49c
93fb7cb
 
 
 
c01e49c
93fb7cb
81a5137
 
 
 
 
 
 
 
 
 
 
 
 
 
93fb7cb
81a5137
93fb7cb
81a5137
 
93fb7cb
81a5137
93fb7cb
 
 
 
 
 
 
 
 
 
 
 
 
c01e49c
93fb7cb
 
 
 
 
 
c01e49c
93fb7cb
 
 
 
 
81a5137
 
 
c01e49c
93fb7cb
 
 
 
c01e49c
 
93fb7cb
c01e49c
 
93fb7cb
81a5137
 
 
 
 
 
 
 
 
93fb7cb
c01e49c
81a5137
c01e49c
81a5137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7478ee
81a5137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
982639c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81a5137
982639c
81a5137
 
 
 
 
 
982639c
81a5137
 
 
 
 
982639c
81a5137
c01e49c
93fb7cb
 
 
 
 
 
 
 
 
81a5137
 
 
 
 
 
 
93fb7cb
81a5137
 
93fb7cb
 
 
 
 
 
 
 
c01e49c
93fb7cb
c01e49c
93fb7cb
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
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
import gradio as gr
import requests
from bs4 import BeautifulSoup
from openai import OpenAI
import json
import re
from urllib.parse import urljoin, urlparse
import time
import urllib3
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
import ssl

# Disable SSL warnings
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

class WebScrapingTool:
    def __init__(self):
        self.client = None
        self.scraped_data = None  # Store scraped data for fact-checking
        self.analysis_result = None  # Store analysis result for fact-checking
        self.system_prompt = """You are a specialized web data extraction assistant. Your core purpose is to browse and analyze the content of web pages based on user instructions, and return structured or unstructured information from the provided URL. Your capabilities include:
1. Navigating and reading web page content from a given URL.
2. Extracting textual content including headings, paragraphs, lists, and metadata.
3. Identifying and extracting HTML tables and presenting them in a clean, structured format.
4. Creating new, custom tables based on user queries by processing, reorganizing, or filtering the content found on the source page.
You must always follow these guidelines:
- Accurately extract and summarize both structured (tables, lists) and unstructured (paragraphs, articles) content.
- Clearly separate different types of data (e.g., summaries, tables, bullet points).
- When extracting textual content:
  - Maintain original meaning, structure, and tone.
  - Capture all relevant sections based on user instructions (e.g., only the "Overview" or "Methodology" sections).
- When extracting tables:
  - Preserve headers and align row data correctly.
  - Identify and differentiate multiple tables, if present.
- When creating custom tables:
  - Include only the relevant columns as per the user request.
  - Sort, filter, and reorganize data accordingly.
  - Use clear and consistent headers.
You must not hallucinate or infer data not present on the page. If content is missing, unclear, or restricted, say so explicitly.
Always respond based on the actual content from the provided link. If the page fails to load or cannot be accessed, inform the user immediately.
Your role is to act as an intelligent browser and data interpreter β€” able to read and reshape any web content to meet user needs."""

        self.factcheck_prompt = """You are an expert fact-checker and critical analysis assistant. Your role is to thoroughly examine AI-generated analysis results against the original source material to verify accuracy, identify potential errors, and assess the reliability of the analysis.

Your fact-checking responsibilities include:
1. **Accuracy Verification**: Compare each claim, statistic, and piece of information in the analysis against the original source content.
2. **Completeness Assessment**: Determine if important information was missed or if the analysis covers all relevant aspects.
3. **Error Detection**: Identify factual errors, misinterpretations, or misrepresentations of the source material.
4. **Context Verification**: Ensure that information is presented in proper context and not taken out of context.
5. **Consistency Check**: Verify that the analysis is internally consistent and doesn't contain contradictions.

For your fact-checking analysis, provide:
- **ACCURACY SCORE**: Rate the overall accuracy on a scale of 1-10 (10 being perfectly accurate)
- **KEY FINDINGS**: List what was correctly analyzed
- **ERRORS IDENTIFIED**: Point out any inaccuracies, misrepresentations, or missing information
- **VERIFICATION STATUS**: For each major claim, indicate whether it's VERIFIED, PARTIALLY VERIFIED, or CANNOT VERIFY
- **RECOMMENDATIONS**: Suggest improvements or corrections needed

Be thorough, objective, and provide specific examples when pointing out discrepancies. If the analysis is accurate, acknowledge its quality. If there are issues, be clear about what needs correction."""

    def setup_client(self, api_key):
        """Initialize OpenAI client with OpenRouter"""
        try:
            self.client = OpenAI(
                base_url="https://openrouter.ai/api/v1",
                api_key=api_key,
            )
            return True, "API client initialized successfully!"
        except Exception as e:
            return False, f"Failed to initialize API client: {str(e)}"

    def create_session(self):
        """Create a robust session with retry strategy and proper headers"""
        session = requests.Session()
        
        # Define retry strategy with fixed parameter name
        retry_strategy = Retry(
            total=3,
            status_forcelist=[429, 500, 502, 503, 504],
            allowed_methods=["HEAD", "GET", "OPTIONS"],  # Fixed: changed from method_whitelist
            backoff_factor=1
        )
        
        # Mount adapter with retry strategy
        adapter = HTTPAdapter(max_retries=retry_strategy)
        session.mount("http://", adapter)
        session.mount("https://", adapter)
        
        # Set comprehensive headers to mimic real browser
        session.headers.update({
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
            'Accept-Language': 'en-US,en;q=0.9',
            'Accept-Encoding': 'gzip, deflate, br',
            'DNT': '1',
            'Connection': 'keep-alive',
            'Upgrade-Insecure-Requests': '1',
            'Sec-Fetch-Dest': 'document',
            'Sec-Fetch-Mode': 'navigate',
            'Sec-Fetch-Site': 'none',
            'Sec-Fetch-User': '?1',
            'Cache-Control': 'max-age=0'
        })
        
        return session

    def scrape_webpage(self, url):
        """Scrape webpage content with enhanced error handling and timeouts"""
        try:
            session = self.create_session()
            
            # Multiple timeout attempts with increasing duration
            timeout_attempts = [15, 30, 45]
            response = None
            
            for timeout in timeout_attempts:
                try:
                    print(f"Attempting to fetch {url} with {timeout}s timeout...")
                    
                    response = session.get(
                        url, 
                        timeout=timeout,
                        verify=False,  # Disable SSL verification for problematic sites
                        allow_redirects=True,
                        stream=False
                    )
                    
                    response.raise_for_status()
                    break
                    
                except requests.exceptions.Timeout:
                    if timeout == timeout_attempts[-1]:  # Last attempt
                        return {
                            'success': False,
                            'error': f"Connection timed out after multiple attempts. The website may be slow or blocking automated requests."
                        }
                    continue
                except requests.exceptions.SSLError:
                    # Try with different SSL context
                    try:
                        response = session.get(
                            url, 
                            timeout=timeout,
                            verify=False,
                            allow_redirects=True
                        )
                        response.raise_for_status()
                        break
                    except:
                        continue
                except requests.exceptions.RequestException as e:
                    if timeout == timeout_attempts[-1]:  # Last attempt
                        return {
                            'success': False,
                            'error': f"Request failed: {str(e)}"
                        }
                    continue
            
            # Check if we got a response
            if response is None:
                return {
                    'success': False,
                    'error': "Failed to establish connection after multiple attempts"
                }
            
            # Check content type
            content_type = response.headers.get('content-type', '').lower()
            if 'text/html' not in content_type and 'text/plain' not in content_type:
                return {
                    'success': False,
                    'error': f"Invalid content type: {content_type}. Expected HTML content."
                }
            
            # Parse HTML content
            soup = BeautifulSoup(response.content, 'html.parser')
            
            # Remove unwanted elements
            for element in soup(["script", "style", "nav", "footer", "header", "aside", "noscript", "iframe"]):
                element.decompose()
            
            # Remove elements with common ad/tracking classes
            ad_classes = ['ad', 'advertisement', 'banner', 'popup', 'modal', 'cookie', 'newsletter']
            for class_name in ad_classes:
                for element in soup.find_all(class_=re.compile(class_name, re.I)):
                    element.decompose()
            
            # Extract text content
            text_content = soup.get_text(separator=' ', strip=True)
            
            # Clean up text - remove extra whitespace
            text_content = re.sub(r'\s+', ' ', text_content)
            text_content = text_content.strip()
            
            # Extract tables with improved structure
            tables = []
            for i, table in enumerate(soup.find_all('table')):
                table_data = []
                headers = []
                
                # Try to find headers in various ways
                header_row = table.find('thead')
                if header_row:
                    header_row = header_row.find('tr')
                else:
                    header_row = table.find('tr')
                
                if header_row:
                    headers = []
                    for th in header_row.find_all(['th', 'td']):
                        header_text = th.get_text(strip=True)
                        headers.append(header_text if header_text else f"Column_{len(headers)+1}")
                
                # Extract all rows (skip header if it was already processed)
                rows = table.find_all('tr')
                start_idx = 1 if header_row and header_row in rows else 0
                
                for row in rows[start_idx:]:
                    cells = row.find_all(['td', 'th'])
                    if cells:
                        row_data = []
                        for cell in cells:
                            cell_text = cell.get_text(strip=True)
                            row_data.append(cell_text)
                        
                        if row_data and any(cell.strip() for cell in row_data):  # Skip empty rows
                            table_data.append(row_data)
                
                if table_data:
                    # Ensure headers match data columns
                    max_cols = max(len(row) for row in table_data) if table_data else 0
                    if len(headers) < max_cols:
                        headers.extend([f"Column_{i+1}" for i in range(len(headers), max_cols)])
                    elif len(headers) > max_cols:
                        headers = headers[:max_cols]
                    
                    tables.append({
                        'id': i + 1,
                        'headers': headers,
                        'data': table_data[:50]  # Limit rows to prevent overwhelming
                    })
            
            # Extract metadata
            title = soup.title.string.strip() if soup.title and soup.title.string else "No title found"
            
            # Extract meta description
            meta_desc = ""
            desc_tag = soup.find('meta', attrs={'name': 'description'})
            if desc_tag and desc_tag.get('content'):
                meta_desc = desc_tag['content'].strip()
            
            return {
                'success': True,
                'text': text_content[:20000],  # Limit text length
                'tables': tables,
                'title': title,
                'meta_description': meta_desc,
                'url': url,
                'content_length': len(text_content)
            }
            
        except requests.exceptions.ConnectionError as e:
            return {
                'success': False,
                'error': f"Connection failed: {str(e)}. The website may be down or blocking requests."
            }
        except requests.exceptions.HTTPError as e:
            return {
                'success': False,
                'error': f"HTTP Error {e.response.status_code}: {e.response.reason}"
            }
        except requests.exceptions.RequestException as e:
            return {
                'success': False,
                'error': f"Request failed: {str(e)}"
            }
        except Exception as e:
            return {
                'success': False,
                'error': f"Unexpected error while processing webpage: {str(e)}"
            }

    def analyze_content(self, scraped_data, user_query, api_key):
        """Analyze scraped content using DeepSeek V3"""
        if not self.client:
            success, message = self.setup_client(api_key)
            if not success:
                return f"Error: {message}"
        
        if not scraped_data['success']:
            return f"Error scraping webpage: {scraped_data['error']}"
        
        # Store scraped data for fact-checking
        self.scraped_data = scraped_data
        
        # Prepare content for AI analysis
        content_text = f"""
WEBPAGE ANALYSIS REQUEST
========================
URL: {scraped_data['url']}
Title: {scraped_data['title']}
Content Length: {scraped_data['content_length']} characters
Tables Found: {len(scraped_data['tables'])}
META DESCRIPTION:
{scraped_data['meta_description']}
MAIN CONTENT:
{scraped_data['text']}
"""
        
        if scraped_data['tables']:
            content_text += f"\n\nSTRUCTURED DATA - {len(scraped_data['tables'])} TABLE(S) FOUND:\n"
            content_text += "=" * 50 + "\n"
            
            for table in scraped_data['tables']:
                content_text += f"\nTABLE {table['id']}:\n"
                content_text += f"Headers: {' | '.join(table['headers'])}\n"
                content_text += "-" * 50 + "\n"
                
                for i, row in enumerate(table['data'][:10]):  # Show first 10 rows
                    content_text += f"Row {i+1}: {' | '.join(str(cell) for cell in row)}\n"
                
                if len(table['data']) > 10:
                    content_text += f"... and {len(table['data']) - 10} more rows\n"
                content_text += "\n"
        
        try:
            completion = self.client.chat.completions.create(
                extra_headers={
                    "HTTP-Referer": "https://gradio-web-scraper.com",
                    "X-Title": "AI Web Scraping Tool",
                },
                model="deepseek/deepseek-chat-v3-0324:free",
                messages=[
                    {"role": "system", "content": self.system_prompt},
                    {"role": "user", "content": f"{content_text}\n\nUSER REQUEST:\n{user_query}\n\nPlease analyze the above webpage content and fulfill the user's request. Be thorough and accurate."}
                ],
                temperature=0.1,
                max_tokens=4000
            )
            
            result = completion.choices[0].message.content
            # Store analysis result for fact-checking
            self.analysis_result = result
            return result
            
        except Exception as e:
            return f"Error analyzing content with AI: {str(e)}"

    def fact_check_analysis(self, api_key):
        """Fact-check the analysis results using DeepSeek R1"""
        if not self.client:
            success, message = self.setup_client(api_key)
            if not success:
                return f"Error: {message}"
        
        if not self.scraped_data or not self.analysis_result:
            return "❌ No analysis results to fact-check. Please run an analysis first."
        
        # Prepare content for fact-checking
        factcheck_content = f"""
FACT-CHECKING TASK
==================

ORIGINAL SOURCE MATERIAL:
-------------------------
URL: {self.scraped_data['url']}
Title: {self.scraped_data['title']}
Content Length: {self.scraped_data['content_length']} characters

SOURCE TEXT:
{self.scraped_data['text']}
"""
        
        if self.scraped_data['tables']:
            factcheck_content += f"\n\nSOURCE TABLES ({len(self.scraped_data['tables'])} found):\n"
            factcheck_content += "=" * 50 + "\n"
            
            for table in self.scraped_data['tables']:
                factcheck_content += f"\nTABLE {table['id']}:\n"
                factcheck_content += f"Headers: {' | '.join(table['headers'])}\n"
                factcheck_content += "-" * 50 + "\n"
                
                for i, row in enumerate(table['data'][:15]):  # Show more rows for fact-checking
                    factcheck_content += f"Row {i+1}: {' | '.join(str(cell) for cell in row)}\n"
                
                if len(table['data']) > 15:
                    factcheck_content += f"... and {len(table['data']) - 15} more rows\n"
                factcheck_content += "\n"
        
        factcheck_content += f"""

AI ANALYSIS TO VERIFY:
======================
{self.analysis_result}

FACT-CHECKING INSTRUCTIONS:
===========================
Please thoroughly fact-check the AI analysis above against the original source material. Verify every claim, statistic, and piece of information. Provide a comprehensive fact-checking report."""
        
        try:
            completion = self.client.chat.completions.create(
                extra_headers={
                    "HTTP-Referer": "https://gradio-web-scraper.com",
                    "X-Title": "AI Web Scraping Tool - Fact Checker",
                },
                extra_body={},
                model="deepseek/deepseek-r1:free",
                messages=[
                    {"role": "system", "content": self.factcheck_prompt},
                    {"role": "user", "content": factcheck_content}
                ]
            )
            
            return completion.choices[0].message.content
            
        except Exception as e:
            return f"Error fact-checking with DeepSeek R1: {str(e)}"

def create_interface():
    tool = WebScrapingTool()
    
    def process_request(api_key, url, user_query):
        if not api_key.strip():
            return "❌ Please enter your OpenRouter API key"
        
        if not url.strip():
            return "❌ Please enter a valid URL"
        
        if not user_query.strip():
            return "❌ Please enter your analysis query"
        
        # Validate URL format
        if not url.startswith(('http://', 'https://')):
            url = 'https://' + url
        
        # Add progress updates
        yield "πŸ”„ Initializing web scraper..."
        time.sleep(0.5)
        
        yield "🌐 Fetching webpage content (this may take a moment)..."
        
        # Scrape webpage
        scraped_data = tool.scrape_webpage(url)
        
        if not scraped_data['success']:
            yield f"❌ Scraping Failed: {scraped_data['error']}"
            return
        
        yield f"βœ… Successfully scraped webpage!\nπŸ“„ Title: {scraped_data['title']}\nπŸ“Š Found {len(scraped_data['tables'])} tables\nπŸ“ Content: {scraped_data['content_length']} characters\n\nπŸ€– Analyzing content with DeepSeek V3..."
        
        # Analyze content
        result = tool.analyze_content(scraped_data, user_query, api_key)
        
        yield f"βœ… Analysis Complete!\n{'='*50}\n\n{result}"
    
    def fact_check_request(api_key):
        if not api_key.strip():
            return "❌ Please enter your OpenRouter API key"
        
        yield "πŸ” Starting fact-check with DeepSeek R1..."
        time.sleep(0.5)
        
        yield "🧠 Analyzing accuracy and verifying claims..."
        
        # Perform fact-checking
        factcheck_result = tool.fact_check_analysis(api_key)
        
        yield f"βœ… Fact-Check Complete!\n{'='*50}\n\n{factcheck_result}"
    
    # Create Gradio interface
    with gr.Blocks(title="AI Web Scraping Tool with Fact-Checking", theme=gr.themes.Soft()) as app:
        gr.Markdown("""
        # πŸ€– AI Web Scraping Tool with Fact-Checking
        ### Powered by DeepSeek V3 & DeepSeek R1 via OpenRouter
        
        Extract and analyze web content using advanced AI, then fact-check the results for accuracy and reliability.
        """)
        
        with gr.Row():
            with gr.Column(scale=2):
                api_key_input = gr.Textbox(
                    label="πŸ”‘ OpenRouter API Key",
                    placeholder="Enter your OpenRouter API key here...",
                    type="password",
                    info="Get your free API key from openrouter.ai"
                )
                
                url_input = gr.Textbox(
                    label="🌐 Website URL",
                    placeholder="https://example.com or just example.com",
                    info="Enter the URL you want to scrape and analyze"
                )
                
                query_input = gr.Textbox(
                    label="πŸ“ Analysis Query",
                    placeholder="What do you want to extract? (e.g., 'Extract main points and create a summary table')",
                    lines=4,
                    info="Describe what information you want to extract from the webpage"
                )
                
                with gr.Row():
                    analyze_btn = gr.Button("πŸš€ Analyze Website", variant="primary", size="lg")
                    factcheck_btn = gr.Button("πŸ” Fact-Check Results", variant="secondary", size="lg")
                
                with gr.Row():
                    clear_btn = gr.Button("πŸ—‘οΈ Clear All", variant="secondary")
            
            with gr.Column(scale=3):
                output = gr.Textbox(
                    label="πŸ“Š Analysis Results",
                    lines=25,
                    max_lines=40,
                    show_copy_button=True,
                    interactive=False,
                    placeholder="Results will appear here after analysis..."
                )
                
                factcheck_output = gr.Textbox(
                    label="πŸ” Fact-Check Report",
                    lines=20,
                    max_lines=40,
                    show_copy_button=True,
                    interactive=False,
                    placeholder="Fact-check results will appear here after clicking 'Fact-Check Results'..."
                )
        
        # Tips and Examples
        with gr.Accordion("πŸ’‘ Usage Tips & Fact-Checking Guide", open=False):
            gr.Markdown("""
            ## πŸ”„ **How to Use the Fact-Checking Feature:**
            
            1. **First**: Enter your API key, URL, and analysis query
            2. **Second**: Click "πŸš€ Analyze Website" to get initial results
            3. **Third**: Click "πŸ” Fact-Check Results" to verify accuracy with DeepSeek R1
            
            ## 🎯 **What the Fact-Checker Does:**
            
            ### **Accuracy Verification**
            - Compares every claim in the analysis against the original source
            - Identifies factual errors and misrepresentations
            - Verifies numerical data and statistics
            
            ### **Completeness Assessment**
            - Checks if important information was missed
            - Evaluates coverage of all relevant aspects
            - Identifies gaps in the analysis
            
            ### **Context Verification**
            - Ensures information isn't taken out of context
            - Verifies proper interpretation of source material
            - Checks for misleading presentations
            
            ### **Quality Scoring**
            - Provides accuracy scores (1-10 scale)
            - Lists verified vs. unverified claims
            - Offers specific recommendations for improvement
            
            ## πŸ§ͺ **Best Practices for Fact-Checking:**
            
            ### **Ideal Test Cases:**
            ```
            URL: https://en.wikipedia.org/wiki/List_of_countries_by_population
            Query: Create a table showing the top 10 most populous countries with their exact population figures
            ```
            *Perfect for fact-checking numerical accuracy*
            
            ```
            URL: https://www.who.int/news-room/fact-sheets
            Query: Extract key health statistics and create a summary of global health metrics
            ```
            *Great for verifying official statistics*
            
            ```
            URL: https://finance.yahoo.com/quote/AAPL
            Query: Extract Apple's current stock price, market cap, and financial metrics
            ```
            *Excellent for checking real-time financial data accuracy*
            
            ## 🎯 **Example Analysis Queries for Fact-Checking:**
            
            ### **Data-Heavy Content**
            - *"Extract all numerical data and organize it in a table format"*
            - *"Create a comparison table of different countries' GDP figures"*
            - *"List the top 10 items with their exact values from the source"*
            
            ### **Statistical Information**
            - *"Summarize key statistics with specific numbers and percentages"*
            - *"Extract survey results and present the exact figures"*
            - *"Create a timeline with specific dates and events"*
            
            ### **Complex Analysis**
            - *"Compare different viewpoints and cite specific quotes"*
            - *"Extract cause-and-effect relationships mentioned in the article"*
            - *"Summarize research findings with methodology details"*
            
            ## πŸ” **What Gets Fact-Checked:**
            
            βœ… **Verified Items:**
            - Exact quotes and citations
            - Numerical data and statistics
            - Dates, names, and factual claims
            - Table data accuracy
            - Mathematical calculations
            
            ⚠️ **Flagged Issues:**
            - Misquoted information
            - Incorrect numbers or percentages
            - Missing context or nuance
            - Overgeneralized statements
            - Unsupported conclusions
            
            ## 🚨 **Red Flags the Fact-Checker Catches:**
            
            - **Hallucinated Data**: Information not present in the source
            - **Misattributed Quotes**: Quotes assigned to wrong sources
            - **Mathematical Errors**: Incorrect calculations or summaries
            - **Context Loss**: Information presented without proper context
            - **Incomplete Extraction**: Missing important details from tables
            
            ## πŸ’‘ **Tips for Better Fact-Checking:**
            
            1. **Use Specific Queries**: More specific requests = better fact-checking
            2. **Test with Known Data**: Start with sites where you know the content
            3. **Check Complex Tables**: Tables are great for testing accuracy
            4. **Verify Names & Dates**: These are common error points
            5. **Cross-Reference**: Compare with multiple sources when possible
            
            ## πŸ”¬ **Advanced Fact-Checking Tests:**
            
            ### **Financial Data Test**
            ```
            URL: https://finance.yahoo.com/quote/MSFT
            Query: Create a detailed financial summary table with exact figures for Microsoft stock
            Expected: Fact-checker should verify all numbers match the source exactly
            ```
            
            ### **Statistical Data Test**
            ```
            URL: https://www.census.gov/quickfacts/fact/table/US
            Query: Extract US population demographics with specific percentages
            Expected: Fact-checker should confirm all demographic percentages are accurate
            ```
            
            ### **Historical Data Test**
            ```
            URL: https://en.wikipedia.org/wiki/List_of_Presidents_of_the_United_States
            Query: Create a table of the last 10 US presidents with their exact terms of office
            Expected: Fact-checker should verify all dates and names are correct
            ```

            ## πŸ§ͺ **Test Scenarios**
            
            ### **1. News & Media Sites**
            ```
            URL: https://www.bbc.com/news
            Query: Extract the top 5 news headlines with their summaries and create a table with columns: Headline, Category, Summary
            ```
            
            ```
            URL: https://edition.cnn.com
            Query: Find all breaking news items and organize them by topic/region in a structured format
            ```
            
            ### **2. Financial Data Sites**
            ```
            URL: https://finance.yahoo.com/quote/AAPL
            Query: Extract Apple stock information including current price, daily change, market cap, and any financial metrics into a summary table
            ```
            
            ```
            URL: https://www.marketwatch.com/investing/stock/tsla
            Query: Create a table with Tesla's key financial metrics: price, change, volume, market cap, P/E ratio
            ```
            
            ### **3. E-commerce & Product Pages**
            ```
            URL: https://www.amazon.com/dp/B08N5WRWNW
            Query: Extract product details including name, price, ratings, key features, and specifications in a structured format
            ```
            
            ```
            URL: https://www.ebay.com/itm/123456789
            Query: Extract item details, price, seller information, and shipping details into a comparison-ready table
            ```
            
            ### **4. Educational & Reference Sites**
            ```
            URL: https://en.wikipedia.org/wiki/Artificial_intelligence
            Query: Extract the main definition, history timeline, and applications of AI. Create separate sections for each topic.
            ```
            
            ```
            URL: https://en.wikipedia.org/wiki/List_of_countries_by_population
            Query: Extract the population data table and create a new table showing top 10 most populous countries with their population and growth rate
            ```
            
            ### **5. Government & Official Statistics**
            ```
            URL: https://www.who.int/emergencies/diseases/novel-coronavirus-2019/situation-reports
            Query: Extract the latest COVID-19 statistics and create a summary table with key global figures
            ```
            
            ```
            URL: https://www.census.gov/quickfacts
            Query: Extract key demographic statistics for the United States and organize them into categories: Population, Economy, Geography
            ```
            
            ### **6. Technology & Business News**
            ```
            URL: https://techcrunch.com
            Query: Find the latest startup funding news and create a table with: Company Name, Funding Amount, Investors, Industry
            ```
            
            ```
            URL: https://www.reuters.com/technology
            Query: Extract top technology news and summarize each story in 2-3 sentences with key points
            ```
            
            ### **7. Scientific & Research Sites**
            ```
            URL: https://www.nature.com/articles
            Query: Extract recent scientific article titles, authors, and abstracts. Create a summary table organized by research field
            ```
            
            ```
            URL: https://pubmed.ncbi.nlm.nih.gov/trending
            Query: Find trending medical research topics and create a list with brief descriptions of each study's findings
            ```
            
            ### **8. Sports & Entertainment**
            ```
            URL: https://www.espn.com/nba/standings
            Query: Extract NBA team standings and create a table with: Team, Wins, Losses, Win Percentage, Conference Position
            ```
            
            ```
            URL: https://www.imdb.com/chart/top
            Query: Extract the top 10 movies from IMDb's top 250 list with ratings, year, and brief description
            ```
            
            ### **9. Weather & Environmental Data**
            ```
            URL: https://weather.com/weather/today
            Query: Extract current weather conditions and forecast data. Create a summary with temperature, conditions, and weekly outlook
            ```
            
            ### **10. Real Estate & Property**
            ```
            URL: https://www.zillow.com/homes/for_sale
            Query: Extract property listings with prices, locations, square footage, and key features into a comparison table
            ```
            
            ## 🎯 **Quick Test Samples (Copy & Paste Ready)**
            
            ### **Simple Test:**
            ```
            URL: https://httpbin.org/html
            Query: Extract all text content and identify the page structure
            ```
            
            ### **Table Extraction Test:**
            ```
            URL: https://www.w3schools.com/html/html_tables.asp
            Query: Find all HTML tables on this page and convert them to a structured format with proper headers
            ```
            
            ### **Complex Analysis Test:**
            ```
            URL: https://www.sec.gov/edgar/browse/?CIK=320193
            Query: Extract Apple Inc.'s recent SEC filings and create a table with: Filing Date, Document Type, Description
            ```
            
            ### **International Site Test:**
            ```
            URL: https://www.bbc.co.uk/weather
            Query: Extract UK weather information and create a regional breakdown of current conditions
            ```
            
            ## 🎯 **Interpreting Fact-Check Results:**
            
            ### **Accuracy Scores:**
            - **9-10**: Highly accurate, minimal issues
            - **7-8**: Generally accurate with minor corrections needed
            - **5-6**: Moderate accuracy, several issues to address
            - **3-4**: Low accuracy, significant problems found
            - **1-2**: Poor accuracy, major fact-checking failures
            
            ### **Verification Status:**
            - **βœ… VERIFIED**: Claim matches source exactly
            - **⚠️ PARTIALLY VERIFIED**: Claim is mostly correct but lacks nuance
            - **❌ CANNOT VERIFY**: Claim not supported by source material
            - **🚨 CONTRADICTED**: Claim directly contradicts source
            
            Remember: The fact-checker is designed to be thorough and critical. Even high-quality analyses may receive suggestions for improvement!
            """)
        
        # Event handlers
        analyze_btn.click(
            fn=process_request,
            inputs=[api_key_input, url_input, query_input],
            outputs=output,
            show_progress=True
        )
        
        factcheck_btn.click(
            fn=fact_check_request,
            inputs=[api_key_input],
            outputs=factcheck_output,
            show_progress=True
        )
        
        clear_btn.click(
            fn=lambda: ("", "", "", "", ""),
            outputs=[api_key_input, url_input, query_input, output, factcheck_output]
        )
    
    return app

if __name__ == "__main__":
    # Create and launch the app
    app = create_interface()
    
    # Launch with enhanced configuration
    app.launch(
        share=True
    )