Spaces:
Sleeping
Sleeping
Download app.py from serovia/bert-disaster-classifier: direct link, hf CLI and curl.
- Browser
- Download file 1.82 kB
-
https://huggingface.co/spaces/serovia/bert-disaster-classifier/resolve/main/app.py
- Command line
-
hf download hf://spaces/serovia/bert-disaster-classifier/app.py
-
curl -L -o app.py https://huggingface.co/spaces/serovia/bert-disaster-classifier/resolve/main/app.py
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() | |
| 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() | |