serovia's picture
Update app.py
18c8f6c verified
Raw History Blame Contribute Delete
1.82 kB
import spaces
from pathlib import Path
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
# The directory containing the fine-tuned BERT model
MODEL_DIR = Path(__file__).resolve().parent / "experiments_full"
# Load the model once when the application starts
tokenizer = AutoTokenizer.from_pretrained(str(MODEL_DIR))
model = AutoModelForSequenceClassification.from_pretrained(
str(MODEL_DIR)
).to("cuda")
model.eval()
@spaces.GPU(duration=30)
def predict(text):
if not text or not text.strip():
raise gr.Error("Please enter an English tweet.")
inputs = tokenizer(
text,
return_tensors="pt",
truncation=True,
padding="max_length",
max_length=128,
)
inputs = {k: v.to("cuda") for k, v in inputs.items()}
with torch.inference_mode():
outputs = model(**inputs)
probabilities = torch.softmax(outputs.logits, dim=-1)[0]
return {
"Not Disaster (0)": float(probabilities[0]),
"Disaster (1)": float(probabilities[1]),
}
demo = gr.Interface(
fn=predict,
inputs=gr.Textbox(
lines=4,
label="Tweet",
placeholder="Enter an English tweet...",
),
outputs=gr.Label(
num_top_classes=2,
label="Prediction",
),
title="BERT Disaster Tweet Classifier",
description=(
"Classify whether an English tweet refers to a real disaster. "
"Fine-tuned BERT-base-uncased model."
),
examples=[
["Forest fire near La Ronge Sask. Canada"],
["I love spending time with my friends!"],
["A powerful earthquake has damaged several buildings."],
],
)
if __name__ == "__main__":
demo.launch()