import gradio as gr
from transformers import pipeline


unmasker = pipeline('fill-mask', model='bert-base-uncased')

def sentiment_analysis(text):
    results = unmasker(text)
    total_results = ""

    for result in results:
        total_results += f"Token: {result.get('token_str')}, Score: {result.get('score')}"
        total_results += '\r\n'

    return total_results
    
demo = gr.Interface(fn=sentiment_analysis, 
                    inputs='text',
                    outputs='text',
                    examples=[['Paris is the [MASK] of France.'], ['The goal of life is [MASK].']],
                    title = "文本情感分析"
                    )


if __name__ == "__main__":
    demo.queue(concurrency_count=3)
    demo.launch(server_name = "0.0.0.0", server_port = 7028)
