Spaces:
Sleeping
Sleeping
File size: 688 Bytes
a0bfaa7 44f4e1f a0bfaa7 44f4e1f a0bfaa7 44f4e1f 2eeb4d4 44f4e1f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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() |