Spaces:
Sleeping
Sleeping
File size: 3,627 Bytes
91317aa 389c2a7 30f0a69 389c2a7 91317aa 74cf769 389c2a7 543baf6 389c2a7 543baf6 91317aa 543baf6 91317aa 389c2a7 c26deb8 389c2a7 1590514 389c2a7 c26deb8 389c2a7 b25b5d0 b8e5788 b25b5d0 04b65f5 b25b5d0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
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() |