35 lines
1.3 KiB
Python
35 lines
1.3 KiB
Python
|
import gradio as gr
|
||
|
from transformers import pipeline
|
||
|
|
||
|
|
||
|
sentimentPipeline = pipeline("zero-shot-classification", model="cross-encoder/nli-distilroberta-base")
|
||
|
|
||
|
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=[['Apple just announced the newest iPhone X', 'technology, sports, politics'], ['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)
|