Spaces:
Runtime error
Runtime error
Download app.py from Markrivera/text-classification: direct link, hf CLI and curl.
- Browser
- Download file 936 Bytes
-
https://huggingface.co/spaces/Markrivera/text-classification/resolve/main/app.py
- Command line
-
hf download hf://spaces/Markrivera/text-classification/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Markrivera/text-classification/resolve/main/app.py
936 Bytes
| import gradio as gr | |
| import torch | |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification | |
| model_dir = "experiments" # 改成你的训练输出目录 | |
| tokenizer = AutoTokenizer.from_pretrained(model_dir) | |
| model = AutoModelForSequenceClassification.from_pretrained(model_dir) | |
| def inference(input_text): | |
| inputs = tokenizer( | |
| input_text, | |
| max_length=128, | |
| truncation=True, | |
| padding=True, | |
| return_tensors="pt" | |
| ) | |
| with torch.no_grad(): | |
| logits = model(**inputs).logits | |
| pred_id = logits.argmax(dim=-1).item() | |
| if hasattr(model.config, "id2label"): | |
| return model.config.id2label.get(pred_id, str(pred_id)) | |
| return str(pred_id) | |
| demo = gr.Interface( | |
| fn=inference, | |
| inputs=gr.Textbox(lines=5, label="Input Text"), | |
| outputs=gr.Textbox(label="Predicted Label"), | |
| title="Text Classification Demo" | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |