| import gradio as gr | |
| import torch | |
| from transformers import pipeline | |
| model = pipeline(task="sentiment-analysis", model="tkurtulus/TurkishAirlines-SentimentAnalysisModel") | |
| 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 = "Turkish Airlines Customer Reviews Sentiment Analysis" | |
| intro_markdown = """## Sentiment Analysis | |
| Using the [TurkishAirlines-SentimentAnalysisModel](https://huggingface.co/tkurtulus/TurkishAirlines-SentimentAnalysisModel), trained on Twitter customer reviews with 3 sentiment levels (positive, negative, neutral).""" | |
| 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(["Such a great experience!", "My luggage was lost.", "Food service was outstanding"], text_input) | |
| if __name__ == "__main__": | |
| demo.launch() |