Spaces:
Sleeping
Sleeping
from transformers import AutoTokenizer, AutoModelForSequenceClassification | |
import torch | |
import gradio as gr | |
model_name = "ljz512187207/Social_Media_Fake_News_Detection" | |
tokenizer = AutoTokenizer.from_pretrained(model_name) | |
model = AutoModelForSequenceClassification.from_pretrained(model_name) | |
device = 'cuda' if torch.cuda.is_available() else 'cpu' | |
model.to(device) | |
def predict_fake(title, text): | |
print("Title:", title) | |
print("Text:", text) | |
input_str = "<title>" + title + "<content>" + text + "<end>" | |
input_ids = tokenizer.encode_plus(input_str, max_length=512, padding="max_length", truncation=True, return_tensors="pt") | |
with torch.no_grad(): | |
output = model(input_ids["input_ids"].to(device), attention_mask=input_ids["attention_mask"].to(device)) | |
print("Output:", output) | |
probabilities = torch.nn.Softmax(dim=-1)(output.logits)[0] | |
print("Probabilities:", probabilities) | |
return { | |
"Fake": probabilities[0].item(), | |
"Real": probabilities[1].item() | |
} | |
iface = gr.Interface( | |
fn=predict_fake, | |
inputs=[ | |
gr.Textbox(lines=1, label="Title"), | |
gr.Textbox(lines=6, label="Text") | |
], | |
outputs="label" | |
) | |
iface.launch(share=True) | |