25 lines
858 B
Python
25 lines
858 B
Python
import gradio as gr
|
|
from transformers import pipeline, AutoTokenizer, AutoConfig, AutoModelForSequenceClassification
|
|
|
|
modelName="distilbert-base-uncased-finetuned-sst-2-english"
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained(modelName)
|
|
model = AutoModelForSequenceClassification.from_pretrained(modelName)
|
|
sentimentPipeline = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
|
|
|
|
def sentiment_analysis(text):
|
|
results = sentimentPipeline(text)
|
|
|
|
return f"Sentiment: {results[0].get('label')}, Score: {results[0].get('score'):.2f}"
|
|
|
|
demo = gr.Interface(fn=sentiment_analysis,
|
|
inputs='text',
|
|
outputs='text',
|
|
title = "文本情感分析"
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
demo.queue(concurrency_count=3)
|
|
demo.launch(server_name = "0.0.0.0", server_port = 7028)
|