Spaces:
Runtime error
Runtime error
File size: 936 Bytes
2b86ee4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 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() |