Ankit74990 commited on
Commit
48c17bd
·
verified ·
1 Parent(s): f8e24d3

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
4
+
5
+ MODEL_NAME = "Ankit74990/TruthX-DISTILBERT"
6
+
7
+ tokenizer = None
8
+ model = None
9
+
10
+ def load_model():
11
+ global tokenizer, model
12
+ if tokenizer is None:
13
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
14
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
15
+ model.eval()
16
+
17
+ def predict(text):
18
+ load_model()
19
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512, padding=True)
20
+ with torch.no_grad():
21
+ outputs = model(**inputs)
22
+ probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
23
+ fake_prob = probs[0][0].item()
24
+ real_prob = probs[0][1].item()
25
+ label = "Real News" if fake_prob < real_prob else "Fake News"
26
+ confidence = max(fake_prob, real_prob)
27
+ return {"label": label, "confidence": f"{confidence:.2%}", "fake_probability": f"{fake_prob:.2%}", "real_probability": f"{real_prob:.2%}"}
28
+
29
+ demo = gr.Interface(
30
+ fn=predict,
31
+ inputs="text",
32
+ outputs=gr.Label(label="Prediction Result"),
33
+ title="TruthX - Fake News Detector",
34
+ description="Enter a news article or headline to determine if it's real or fake news.",
35
+ examples=[
36
+ ["Breaking: Scientists discover new cure for cancer"],
37
+ ["Government announces new policy changes for education"],
38
+ ["Massive scandal revealed involving major corporation"],
39
+ ]
40
+ )
41
+
42
+ if __name__ == "__main__":
43
+ demo.launch()