import gradio as gr from transformers import pipeline # 加载 Hugging Face 的预训练文本分类模型 classifier = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english") # 定义预测函数 def predict(text): result = classifier(text) return f"Sentiment: {result[0]['label']} (Confidence: {result[0]['score']:.4f})" # 创建 Gradio 界面 iface = gr.Interface( fn=predict, inputs=gr.Textbox(lines=2, placeholder="Enter text to classify..."), outputs="text", title="Text Sentiment Classifier", description="Enter a sentence to predict its sentiment using a pre-trained model." ) # 启动 Gradio 应用 iface.launch()