GitHub Actions
commited on
Commit
·
124bd7f
1
Parent(s):
4199853
Sync updated app from GitHub
Browse files- app.py +45 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 6 |
+
|
| 7 |
+
# Load model and tokenizer
|
| 8 |
+
model_name = "hamzab/roberta-fake-news-classification"
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 11 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
+
model.to(device)
|
| 13 |
+
|
| 14 |
+
# Prediction function
|
| 15 |
+
def predict_fake(title, text):
|
| 16 |
+
input_str = f"<title>{title}<content>{text}<end>"
|
| 17 |
+
inputs = tokenizer.encode_plus(
|
| 18 |
+
input_str,
|
| 19 |
+
max_length=512,
|
| 20 |
+
padding="max_length",
|
| 21 |
+
truncation=True,
|
| 22 |
+
return_tensors="pt"
|
| 23 |
+
)
|
| 24 |
+
with torch.no_grad():
|
| 25 |
+
outputs = model(
|
| 26 |
+
inputs["input_ids"].to(device),
|
| 27 |
+
attention_mask=inputs["attention_mask"].to(device)
|
| 28 |
+
)
|
| 29 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=1)[0]
|
| 30 |
+
return {"Fake": float(probs[0]), "Real": float(probs[1])}
|
| 31 |
+
|
| 32 |
+
# Gradio interface
|
| 33 |
+
iface = gr.Interface(
|
| 34 |
+
fn=predict_fake,
|
| 35 |
+
inputs=[
|
| 36 |
+
gr.Textbox(label="Title"),
|
| 37 |
+
gr.Textbox(label="Content", lines=6)
|
| 38 |
+
],
|
| 39 |
+
outputs=gr.Label(num_top_classes=2),
|
| 40 |
+
title="Fake News Detector",
|
| 41 |
+
description="Enter a news headline and content to classify as Real or Fake using a RoBERTa model."
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
gradio
|