import gradio as gr from transformers import pipeline sentimentPipeline = pipeline("zero-shot-classification", model="typeform/distilbert-base-uncased-mnli") def sentiment_analysis(text, labels): candidate_labels = labels.split(',') results = sentimentPipeline(text, candidate_labels) total_results = "" index = 0 for candidate_label in candidate_labels: total_results += f"Sentiment: {results.get('labels')[index]}, Score: {results.get('scores')[index]}" total_results += '\r\n' index += 1 return total_results demo = gr.Interface(fn=sentiment_analysis, inputs=[ gr.components.Textbox(label="Text"), gr.components.Textbox(label="Label") ], outputs='text', examples=[['I have a problem with my iphone that needs to be resolved asap!!', 'urgent, not urgent, phone, tablet, computer'], ['Last week I upgraded my iOS version and ever since then my phone has been overheating whenever I use your app.', 'mobile, website, billing, account access']], title = "文本情感分析" ) if __name__ == "__main__": demo.queue(concurrency_count=3) demo.launch(server_name = "0.0.0.0", server_port = 7028)