File size: 18,704 Bytes
7789450
 
 
544844c
7789450
 
 
 
 
544844c
7789450
 
 
 
59995c4
 
1d1200f
 
59995c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
767bc54
59995c4
 
 
 
 
 
 
 
cc19c7c
59995c4
 
 
767bc54
59995c4
 
 
 
 
 
 
 
 
 
7803593
 
 
 
 
 
 
 
 
 
 
 
 
7789450
 
 
767bc54
 
 
7789450
767bc54
7789450
 
 
 
 
767bc54
 
 
 
 
7789450
 
 
767bc54
7789450
 
767bc54
544844c
7789450
 
 
544844c
 
 
 
 
 
 
 
7789450
767bc54
7789450
 
 
767bc54
7789450
 
 
 
 
 
 
 
 
 
 
767bc54
 
544844c
 
767bc54
 
7789450
767bc54
7789450
767bc54
544844c
 
767bc54
 
 
544844c
 
767bc54
7789450
767bc54
 
 
7789450
 
544844c
 
 
767bc54
7789450
767bc54
7789450
767bc54
7789450
767bc54
7789450
 
767bc54
7789450
59995c4
 
767bc54
 
 
59995c4
7789450
767bc54
 
 
7789450
544844c
 
767bc54
7789450
 
 
767bc54
7789450
 
767bc54
 
 
 
 
 
7789450
 
767bc54
 
 
 
 
 
 
 
 
 
 
7789450
 
544844c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7789450
 
 
 
 
 
7803593
767bc54
7789450
767bc54
 
 
 
7789450
 
 
 
767bc54
 
 
 
 
7789450
 
 
 
 
 
 
 
 
767bc54
 
 
 
 
 
 
 
 
 
 
 
 
 
7789450
767bc54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7789450
767bc54
 
 
 
 
 
 
7803593
767bc54
 
 
 
 
 
 
7789450
 
59995c4
 
 
 
7789450
767bc54
 
 
 
 
7789450
 
7803593
7789450
 
 
 
 
 
 
 
 
 
 
 
544844c
 
7789450
 
 
767bc54
7789450
7803593
767bc54
 
 
544844c
 
 
 
 
 
 
 
7789450
 
 
 
767bc54
 
59995c4
767bc54
7789450
 
 
 
 
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
import streamlit as st
import os
import requests
from typing import Dict, Optional
import time
from datetime import datetime

# Page configuration
st.set_page_config(
    page_title="VeriFact - Claim Verifier",
    page_icon="πŸ”",
    layout="wide",
    initial_sidebar_state="expanded"
)

# FIXED CSS - Much better colors and visibility!
st.markdown("""
<style>
    /* Main background */
    .main {
        background-color: #ffffff;
    }
    
    /* Headers with good contrast */
    .main-header {
        font-size: 3rem;
        font-weight: bold;
        color: #1a1a1a;
        text-align: center;
        margin-bottom: 0.5rem;
        text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
    }
    
    .sub-header {
        text-align: center;
        color: #444444;
        margin-bottom: 2rem;
        font-size: 1.2rem;
    }
    
    /* TRUE verdict - Green with dark text */
    .verdict-true {
        background: linear-gradient(135deg, #d4edda 0%, #c3e6cb 100%);
        border-left: 6px solid #28a745;
        padding: 1.5rem;
        border-radius: 8px;
        margin: 1rem 0;
        box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        color: #155724 !important;
    }
    
    .verdict-true h3, .verdict-true p, .verdict-true strong {
        color: #155724 !important;
    }
    
    /* FALSE verdict - Red with dark text */
    .verdict-false {
        background: linear-gradient(135deg, #f8d7da 0%, #f5c6cb 100%);
        border-left: 6px solid #dc3545;
        padding: 1.5rem;
        border-radius: 8px;
        margin: 1rem 0;
        box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        color: #721c24 !important;
    }
    
    .verdict-false h3, .verdict-false p, .verdict-false strong {
        color: #721c24 !important;
    }
    
    /* MIXED verdict - Yellow with dark text */
    .verdict-mixed {
        background: linear-gradient(135deg, #fff3cd 0%, #ffeaa7 100%);
        border-left: 6px solid #ffc107;
        padding: 1.5rem;
        border-radius: 8px;
        margin: 1rem 0;
        box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        color: #856404 !important;
    }
    
    .verdict-mixed h3, .verdict-mixed p, .verdict-mixed strong {
        color: #856404 !important;
    }
    
    /* UNVERIFIED - Blue with dark text */
    .verdict-unverified {
        background: linear-gradient(135deg, #e7f3ff 0%, #cfe2ff 100%);
        border-left: 6px solid #0d6efd;
        padding: 1.5rem;
        border-radius: 8px;
        margin: 1rem 0;
        box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        color: #084298 !important;
    }
    
    .verdict-unverified h3, .verdict-unverified p, .verdict-unverified strong {
        color: #084298 !important;
    }
    
    /* Links */
    .verdict-true a, .verdict-false a, .verdict-mixed a, .verdict-unverified a {
        color: #0066cc !important;
        text-decoration: underline;
        font-weight: bold;
    }
    
    /* Stats boxes */
    .stat-box {
        background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
        padding: 2rem 1rem;
        border-radius: 12px;
        text-align: center;
        margin: 0.5rem;
        border: 2px solid #dee2e6;
        box-shadow: 0 4px 6px rgba(0,0,0,0.1);
    }
    
    /* Fix input & textarea visibility */
    textarea, input {
        background-color: #f8f9fa !important;
        color: #000000 !important;
        border: 2px solid #ced4da !important;
        border-radius: 8px !important;
        font-size: 16px !important;
    }

    textarea::placeholder, input::placeholder {
        color: #6c757d !important;
    }

    .stat-box h2 {
        margin: 0 !important;
        font-size: 2.5rem !important;
    }
    
    .stat-box p {
        color: #495057 !important;
        font-weight: 600;
        font-size: 0.9rem;
    }
    /* Section titles (bold black headings) */
    .section-title {
        font-size: 1.7rem;
        font-weight: 800;
        color: #000000 !important;       /* force black */
        margin-top: 2rem;
        margin-bottom: 1rem;
    }
    
    .section-title strong {
        color: #000000 !important;       /* force black for strong text */
        font-weight: 900;
    }
</style>
""", unsafe_allow_html=True)

# ==============================
# Google Fact Check API Class
# ==============================
class GoogleFactCheckAPI:
    """Google Fact Check Tools API Integration"""
    
    def __init__(self, api_key: Optional[str] = None):
        self.api_key = api_key or os.environ.get('GOOGLE_API_KEY')
        self.base_url = "https://factchecktools.googleapis.com/v1alpha1/claims:search"
        if not self.api_key:
            st.markdown("""
            <div class="verdict-mixed">
                <p>⚠️ Google API Key not found. Add it in Settings β†’ Repository secrets</p>
            </div>
            """, unsafe_allow_html=True)
    
    def verify_claim(self, claim: str, language: str = "en") -> Dict:
        if not self.api_key:
            return {"status": "error", "message": "❌ API key not configured. Please add GOOGLE_API_KEY in Hugging Face Secrets."}
        
        try:
            params = {"key": self.api_key, "query": claim, "languageCode": language, "pageSize": 10}
            response = requests.get(self.base_url, params=params, timeout=15)
            response.raise_for_status()
            data = response.json()
            
            if "claims" not in data or len(data["claims"]) == 0:
                simplified_query = self._simplify_claim(claim)
                if simplified_query != claim:
                    params["query"] = simplified_query
                    response = requests.get(self.base_url, params=params, timeout=15)
                    response.raise_for_status()
                    data = response.json()
            
            if "claims" not in data or len(data["claims"]) == 0:
                return {"status": "no_results", "message": f"No fact-checks found for: '{claim}'"}
            
            results = []
            for claim_data in data["claims"]:
                for review in claim_data.get("claimReview", []):
                    results.append({
                        "claim_text": claim_data.get("text", ""),
                        "claimant": claim_data.get("claimant", "Unknown"),
                        "claim_date": claim_data.get("claimDate", "Unknown"),
                        "publisher": review.get("publisher", {}).get("name", "Unknown"),
                        "url": review.get("url", ""),
                        "title": review.get("title", ""),
                        "rating": review.get("textualRating", "Unknown"),
                        "language": review.get("languageCode", "en")
                    })
            
            return {"status": "success", "claim": claim, "results": results, "count": len(results)}
        
        except requests.exceptions.HTTPError as e:
            if e.response.status_code == 403:
                return {"status": "error", "message": "❌ API key is invalid or Fact Check API is not enabled. Check Google Cloud Console."}
            return {"status": "error", "message": f"❌ HTTP Error: {str(e)}"}
        except requests.exceptions.RequestException as e:
            return {"status": "error", "message": f"❌ Network error: {str(e)}"}
        except Exception as e:
            return {"status": "error", "message": f"❌ Unexpected error: {str(e)}"}
    
    def _simplify_claim(self, claim: str) -> str:
        stop_words = ['the', 'a', 'an', 'is', 'are', 'was', 'were', 'be', 'been', 'being',
                      'have', 'has', 'had', 'do', 'does', 'did', 'will', 'would', 'should',
                      'could', 'may', 'might', 'can']
        words = claim.lower().split()
        key_words = [w for w in words if w not in stop_words and len(w) > 2]
        return ' '.join(key_words[:5]) if key_words else claim

# ==============================
# Helper Functions
# ==============================
def determine_verdict_color(rating: str) -> str:
    rating_lower = rating.lower()
    true_keywords = ["true", "correct", "accurate", "mostly true", "verified", "confirmed"]
    false_keywords = ["false", "incorrect", "inaccurate", "mostly false", "debunked", "fake", "pants on fire"]
    mixed_keywords = ["mixture", "mixed", "partially", "misleading", "unproven", "undetermined"]
    if any(k in rating_lower for k in true_keywords):
        return "verdict-true"
    elif any(k in rating_lower for k in false_keywords):
        return "verdict-false"
    elif any(k in rating_lower for k in mixed_keywords):
        return "verdict-mixed"
    return "verdict-unverified"

def get_verdict_emoji(rating: str) -> str:
    return {"verdict-true":"βœ…","verdict-false":"❌","verdict-mixed":"⚠️","verdict-unverified":"❓"}.get(determine_verdict_color(rating),"❓")

def classify_rating(rating: str) -> str:
    r = rating.lower()
    if any(k in r for k in ["false","fake","pants on fire"]): return "false"
    if any(k in r for k in ["true","correct","accurate"]): return "true"
    if any(k in r for k in ["mixed","misleading","partially","half","mostly"]): return "mixed"
    return "unverified"

# ==============================
# Main App
# ==============================
def main():
    st.markdown('<div class="main-header">πŸ” VeriFact</div>', unsafe_allow_html=True)
    st.markdown('<div class="sub-header">AI-Powered Fact Checking β€’ Built by Areeba Fatima</div>', unsafe_allow_html=True)

    # Sidebar
    with st.sidebar:
        st.header("βš™οΈ Configuration")
        api_key_input = st.text_input("Google API Key (Optional)", type="password", help="Leave empty to use Hugging Face Secrets")
        st.markdown("---")
        
        st.markdown("""
        <div class="verdict-unverified">
            <p>ℹ️ <strong>VeriFact</strong> verifies claims using Google's Fact Check Tools API against
            databases from PolitiFact, Snopes, FactCheck.org, and 50+ other sources.</p>
        </div>
        """, unsafe_allow_html=True)
        
        st.markdown("---")
        st.markdown("""
        <div class="verdict-unverified">
            <p>ℹ️ <strong>How to Use:</strong></p>
            <ul>
                <li>Enter any claim</li>
                <li>Click Verify</li>
                <li>Review results</li>
                <li>Check sources</li>
            </ul>
        </div>
        """, unsafe_allow_html=True)
        
        st.markdown("---")
        with st.expander("πŸ“ Example Claims to Try"):
            st.code("""
βœ… TRUE:
β€’ Water boils at 100Β°C at sea level
β€’ Pakistan gained independence in 1947
β€’ The Earth orbits the Sun

❌ FALSE:
β€’ The Earth is flat
β€’ Vaccines cause autism
β€’ 5G causes COVID-19

⚠️ MIXED:
β€’ Coffee is good for health
β€’ Organic food is healthier
            """)
    
    # Initialize API
    api_key = api_key_input if api_key_input else None
    fact_checker = GoogleFactCheckAPI(api_key)
    
    # Main content
    st.markdown('<div class="section-title"><strong>πŸ“ Enter Your Claim</strong></div>', unsafe_allow_html=True)
    claim_text = st.text_area("Claim to Verify", height=120, placeholder="Example: COVID-19 vaccines are safe and effective", help="Enter any factual claim you want to verify")
    
    col1, col2, col3 = st.columns([2,2,3])
    with col1: verify_button = st.button("πŸ” Verify Claim", type="primary", use_container_width=True)
    with col2: clear_button = st.button("πŸ—‘οΈ Clear", use_container_width=True)
    if clear_button: st.rerun()
    
    # Process verification
    if verify_button:
        if not claim_text.strip():
            st.markdown("""
            <div class="verdict-mixed">
                <p>⚠️ Please enter a claim to verify.</p>
            </div>
            """, unsafe_allow_html=True)
        else:
            with st.spinner("πŸ”Ž Searching fact-check databases..."):
                start_time = time.time()
                results = fact_checker.verify_claim(claim_text)
                end_time = time.time()
            
            st.markdown("---")
            
            if results["status"] == "error":
                st.markdown(f"""
                <div class="verdict-false">
                    <p>❌ {results['message']}</p>
                </div>
                <div class="verdict-unverified">
                    <p>ℹ️ <strong>Troubleshooting:</strong></p>
                    <ul>
                        <li>Make sure you added <code>GOOGLE_API_KEY</code> in Settings β†’ Repository secrets</li>
                        <li>Verify the API key is correct</li>
                        <li>Ensure Fact Check Tools API is enabled in Google Cloud Console</li>
                    </ul>
                </div>
                """, unsafe_allow_html=True)
            
            elif results["status"] == "no_results":
                st.markdown(f"""
                <div class="verdict-mixed">
                    <p>⚠️ {results['message']}</p>
                </div>
                <div class="verdict-unverified">
                    <p>ℹ️ <strong>Why no results?</strong></p>
                    <ul>
                        <li>This specific claim may not have been fact-checked yet</li>
                        <li>Try rephrasing (simpler is better)</li>
                        <li>Try well-known claims like "The Earth is flat"</li>
                        <li>Check spelling</li>
                    </ul>
                    <p><strong>Note:</strong> Not all claims have fact-checks available. The database contains 
                    claims that have been verified by major fact-checking organizations.</p>
                </div>
                """, unsafe_allow_html=True)
            
            elif results["status"] == "success":
                st.markdown(f"""
                <div class="verdict-true">
                    <h3>βœ… Found {results['count']} fact-check(s)</h3>
                    <p>⏱ Time taken: {end_time - start_time:.2f}s</p>
                </div>
                """, unsafe_allow_html=True)
                
                st.markdown('<div class="section-title"><strong>πŸ“Š Results</strong></div>', unsafe_allow_html=True)
                st.markdown("""
                <div class="verdict-unverified">
                    <p>ℹ️ <strong>Important:</strong> VeriFact does not perform original fact-checking. 
                    It retrieves claims that have already been verified by professional, 
                    published fact-checking organizations approved by Google.</p>
                </div>
                """, unsafe_allow_html=True)
                
                ratings = [r["rating"] for r in results["results"]]
                classified = [classify_rating(r) for r in ratings]
                true_count = classified.count("true")
                false_count = classified.count("false")
                mixed_count = classified.count("mixed")
                
                col1, col2, col3, col4 = st.columns(4)
                with col1: st.markdown(f"""<div class="stat-box"><h2 style="color:#28a745;">βœ… {true_count}</h2><p>True/Accurate</p></div>""", unsafe_allow_html=True)
                with col2: st.markdown(f"""<div class="stat-box"><h2 style="color:#dc3545;">❌ {false_count}</h2><p>False/Incorrect</p></div>""", unsafe_allow_html=True)
                with col3: st.markdown(f"""<div class="stat-box"><h2 style="color:#ffc107;">⚠️ {mixed_count}</h2><p>Mixed/Misleading</p></div>""", unsafe_allow_html=True)
                with col4: st.markdown(f"""<div class="stat-box"><h2 style="color:#6c757d;">πŸ“° {results['count']}</h2><p>Total Sources</p></div>""", unsafe_allow_html=True)
                
                st.markdown("---")
                st.markdown('<div class="section-title"><strong>πŸ” Detailed Fact-Check Results</strong></div>', unsafe_allow_html=True)
                
                for idx, result in enumerate(results["results"], 1):
                    verdict_class = determine_verdict_color(result["rating"])
                    emoji = get_verdict_emoji(result["rating"])
                    st.markdown(f"""
                    <div class="{verdict_class}">
                        <h3>{emoji} Fact-Check #{idx}</h3>
                        <p><strong>Rating:</strong> {result["rating"]}</p>
                        <p><strong>Publisher:</strong> {result["publisher"]}</p>
                        <p><strong>Claim:</strong> {result["claim_text"]}</p>
                        <p><strong>Claimant:</strong> {result["claimant"]}</p>
                        <p><strong>Date:</strong> {result["claim_date"]}</p>
                        <p><strong>Title:</strong> {result["title"]}</p>
                        <p><strong>Source:</strong> <a href="{result["url"]}" target="_blank">View Full Fact-Check β†’</a></p>
                    </div>
                    """, unsafe_allow_html=True)
                
                # Export Report
                st.markdown("---")
                st.markdown('<div class="section-title"><strong>πŸ“„ Report</strong></div>', unsafe_allow_html=True)
                export_text = f"VERIFACT - CLAIM VERIFICATION REPORT\nGenerated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\nCLAIM: {claim_text}\n\nSUMMARY:\n- Total Fact-Checks: {results['count']}\n- True/Accurate: {true_count}\n- False/Incorrect: {false_count}\n- Mixed/Misleading: {mixed_count}\n\nDETAILED RESULTS:\n"
                for idx, result in enumerate(results["results"],1):
                    export_text += f"\n{idx}. {result['publisher']}\n   Rating: {result['rating']}\n   URL: {result['url']}\n   Claimant: {result['claimant']}\n   Date: {result['claim_date']}\n   Title: {result['title']}\n"
                
                st.download_button(
                    label="πŸ“„ Download Report",
                    data=export_text,
                    file_name=f"verifact_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt",
                    mime="text/plain",
                    use_container_width=True
                )
    
    # Footer
    st.markdown("---")
    st.markdown("""
    <div style="text-align:center;color:#666;padding:2rem 0;">
        <p style="font-size:1.1rem;"><strong>VeriFact</strong> | Powered by Google Fact Check Tools API</p>
        <p>Built by <strong>Areeba Fatima</strong> with Python & Streamlit</p>
        <p style="font-size:0.85rem;color:#999;">⚠️ Always verify important claims from multiple sources. This tool is for informational purposes.</p>
    </div>
    """, unsafe_allow_html=True)

if __name__ == "__main__":
    main()