import gradio as gr
from transformers import pipeline


classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)

def sentiment_analysis(text):
    results = classifier(text)
    total_result = ""

    for result in results[0]:
        total_result += f"Sentiment: {result.get('label')}, Score: {result.get('score'):.2f}" 
        total_result += '\r\n'

    return total_result
    
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)
