test / app.py
ningrumdaud's picture
Update app.py
b25b5d0 verified
raw
history blame
2.62 kB
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-base)":
predictions = model2(text)
return f"Label: {predictions[0]['label']}, Score: {predictions[0]['score']:.4f}"
except Exception as e:
return f"Error processing input: {e}"
def documentation():
return """
## Sentiment Analysis Documentation
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.
Choose a model from the dropdown and enter text to see the sentiment prediction.
"""
with gr.Blocks(title="Sentiment Analysis", theme=gr.themes.Soft()) as demo:
with gr.Tabs():
with gr.TabItem("Demo"):
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()
doc_text.update(documentation())
demo.launch()