File size: 779 Bytes
833a847
 
ebcbab1
fbaf352
 
 
 
ebcbab1
833a847
 
 
 
 
 
 
ebcbab1
833a847
ebcbab1
833a847
 
 
 
 
ebcbab1
 
833a847
ebcbab1
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
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch

# 加载微调的模型和tokenizer
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

def classify_text(text):
    inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
    with torch.no_grad():
        outputs = model(**inputs)
    logits = outputs.logits
    predicted_class = torch.argmax(logits, dim=1).item()
    return f"Predicted class: {predicted_class}"

import gradio as gr

interface = gr.Interface(
    fn=classify_text,
    inputs="text",
    outputs="text",
    title="BERT Text Classifier"
)

interface.launch()