Spaces:
Sleeping
Sleeping
from transformers import pipeline | |
import gradio as gr | |
# Load models only once to improve performance | |
model1 = pipeline(model="siebert/sentiment-roberta-large-english") | |
model2 = pipeline(model="finiteautomata/bertweet-base-sentiment-analysis") | |
def predict_sentiment(text, model_choice): | |
try: | |
if model_choice == "Model 1 (RoBERTa-large)": | |
predictions = model1(text) | |
elif model_choice == "Model 2 (BERTweet)": | |
predictions = model2(text) | |
else: | |
return "Model choice is not recognized." | |
if predictions: | |
return f"Label: {predictions[0]['label']}, Score: {predictions[0]['score']:.4f}" | |
else: | |
return "No predictions returned by the model." | |
except Exception as e: | |
# This catches any error and returns it directly | |
return f"Error processing input: {e}" | |
def documentation(): | |
return """ | |
## Sentiment Analysis Documentation | |
Welcome to the Sentiment Analysis Demo! This interactive application leverages advanced machine learning models to analyze and predict the sentiment of the text you provide. | |
### About the Technology | |
Sentiment analysis is a branch of artificial intelligence that involves the computational identification and categorization of sentiment expressed in written language. The underlying models are trained on large datasets with various annotations to learn how to predict sentiment accurately. | |
This demo utilizes two different models from the Hugging Face Transformers library: | |
- **Model 1**: RoBERTa-large for sentiment analysis fine-tuned for diverse English text sources to enhance generalization across different types of texts (reviews, tweets, etc.). | |
- **Model 2**: BERTweet for sentiment analysis specifically fine-tuned for English Tweets. | |
### Data Privacy | |
We value your privacy. All input texts are processed in real-time and are not stored or used for any purpose other than sentiment analysis during your session. | |
Enjoy exploring the nuances of sentiment in text with our cutting-edge AI models! 🙂 | |
""" | |
with gr.Blocks(title="Sentiment Analysis", theme=gr.themes.Soft()) as demo: | |
with gr.Tabs(): | |
with gr.TabItem("Demo"): | |
gr.Markdown("### Sentiment Analysis Demo\nEnter your text and select a model to get the sentiment analysis.") | |
with gr.Row(): | |
with gr.Column(scale=2): | |
text_input = gr.Textbox(label="Input Text", placeholder="Type here or select an example...") | |
model_choice = gr.Radio(["Model 1 (RoBERTa-large)", "Model 2 (BERTweet)"], label="Model Choice", value="Model 1 (RoBERTa-large)") | |
submit_button = gr.Button("Analyze") | |
with gr.Column(): | |
output = gr.Label() | |
examples = gr.Examples(examples=[ | |
"I absolutely love this product! It has changed my life.", | |
"This is the worst movie I have ever seen. Completely disappointing.", | |
"I'm not sure how I feel about this new update. It has some good points, but also many drawbacks.", | |
"The customer service was fantastic! Very helpful and polite.", | |
"Honestly, this was quite a mediocre experience. Nothing special." | |
], inputs=text_input) | |
submit_button.click( | |
predict_sentiment, | |
inputs=[text_input, model_choice], | |
outputs=output | |
) | |
with gr.TabItem("Documentation"): | |
doc_text = gr.Markdown(documentation()) | |
demo.launch() |