ma4389 commited on
Commit
f10dbd8
·
verified ·
1 Parent(s): 746aee5

Upload 3 files

Browse files
Files changed (3) hide show
  1. New Text Document.txt +3 -0
  2. app.py +32 -0
  3. best_model (4).pth +3 -0
New Text Document.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch
2
+ transformers
3
+ gradio
app.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
3
+ import gradio as gr
4
+
5
+ # Load tokenizer and model
6
+ tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
7
+ model = DistilBertForSequenceClassification.from_pretrained('distilbert-base-uncased', num_labels=2)
8
+ model.load_state_dict(torch.load('best_model (4).pth', map_location=torch.device('cpu')))
9
+ model.eval()
10
+
11
+ # Prediction function
12
+ def classify_news(text):
13
+ inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True, max_length=512)
14
+ with torch.no_grad():
15
+ outputs = model(**inputs)
16
+ probs = torch.nn.functional.softmax(outputs.logits, dim=1)
17
+ predicted_class = torch.argmax(probs, dim=1).item()
18
+ labels = ["Fake", "True"]
19
+ return {labels[0]: float(probs[0][0]), labels[1]: float(probs[0][1])}
20
+
21
+ # Gradio interface
22
+ iface = gr.Interface(
23
+ fn=classify_news,
24
+ inputs=gr.Textbox(lines=10, placeholder="Paste a news article here..."),
25
+ outputs=gr.Label(num_top_classes=2),
26
+ title="Fake News Detector",
27
+ description="Detect whether a news article is real or fake using a fine-tuned DistilBERT model."
28
+ )
29
+
30
+ # Launch the app
31
+ if __name__ == "__main__":
32
+ iface.launch()
best_model (4).pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c9e22fd8205fc26134360716442c61654b44bc4c063af46a124d3a2c368140be
3
+ size 267861862