import gradio as gr import torch from transformers import pipeline model = pipeline(task="sentiment-analysis", model="tkurtulus/Turkish-AI-rlines-SentimentAnalysis") def sentiment_analysis(text): res = model(text)[0] res_label = {} if res["label"] == "positive": res_label["positive"] = res["score"] res_label["negative"] = 1 - res["score"] res_label["neutral"] = 1 - res["score"] if res["label"] == "negative": res_label["negative"] = res["score"] res_label["positive"] = 1 - res["score"] res_label["neutral"] = 1 - res["score"] if res["label"] == "neutral": res_label["neutral"] = res["score"] res_label["positive"] = 1 - res["score"] res_label["negative"] = 1 - res["score"] return res_label custom_css = """ #component-0 { max-width: 600px; margin: 0 auto; } h1,h2 { text-align: center; } a { color: #77b3ee !important; text-decoration: none !important; } a:hover { text-decoration: underline !important; } """ browser_tab_title = "Sentiment Analysis" intro_markdown = """## Sentiment Analysis Using the [distilbert-base-uncased-finetuned-sst-2-english](https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english) model, trained on movie reviews.""" with gr.Blocks(title=browser_tab_title, css=custom_css) as demo: with gr.Row(): with gr.Column(): title = gr.Markdown(intro_markdown) text_input = gr.Textbox(placeholder="Enter a positive or negative sentence here...", label="Text") label_output = gr.Label(label="Sentiment outcome") button_run = gr.Button("Compute sentiment") button_run.click(sentiment_analysis, inputs=text_input, outputs=label_output) gr.Examples(["That's great!", "The movie was bad.", "How are you"], text_input) if __name__ == "__main__": demo.launch()