File size: 1,091 Bytes
c678f12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import requests
import os

HF_API_KEY = os.environ.get("HF_API_KEY")
MODEL = "mrm8488/bert-tiny-finetuned-fake-news-detection"
API_URL = f"https://api-inference.huggingface.co/models/{MODEL}"
HEADERS = {"Authorization": f"Bearer {HF_API_KEY}"}

def fact_check(claim: str):
    claim = claim.strip()
    if not claim:
        return "Please type a statement first."
    r = requests.post(API_URL, headers=HEADERS, json={"inputs": claim})
    try:
        data = r.json()
    except Exception:
        return f"API error: {r.text}"
    if isinstance(data, list) and data and data[0]:
        item = data[0][0]
        label = item.get("label", "N/A")
        score = round(item.get("score", 0) * 100, 2)
        return f"Result: {label}\nConfidence: {score}%"
    return f"Unexpected response: {data}"

iface = gr.Interface(
    fn=fact_check,
    inputs=gr.components.Textbox(lines=4, placeholder="Type a statement..."),
    outputs="text",
    title="AI Fact Checker",
    description="Type a statement and press Submit."
)

if __name__ == "__main__":
    iface.launch()