|
from transformers import pipeline |
|
import gradio as gr |
|
|
|
|
|
try: |
|
|
|
model_name = "distilbert-base-uncased-finetuned-sst-2-english" |
|
sentiment_analysis = pipeline("sentiment-analysis", model=model_name) |
|
test_output = sentiment_analysis("Testing the model with a simple sentence.") |
|
print("Model test output:", test_output) |
|
except Exception as e: |
|
print(f"Failed to load or run model: {e}") |
|
|
|
|
|
def predict_sentiment(text): |
|
try: |
|
predictions = sentiment_analysis(text) |
|
return f"Label: {predictions[0]['label']}, Score: {predictions[0]['score']:.4f}" |
|
except Exception as e: |
|
return f"Error processing input: {e}" |
|
|
|
|
|
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." |
|
] |
|
|
|
|
|
iface = gr.Interface(fn=predict_sentiment, |
|
title="Sentiment Analysis", |
|
description="Enter text to analyze sentiment. Powered by Hugging Face Transformers.", |
|
inputs="text", |
|
outputs="text", |
|
examples=examples) |
|
|
|
iface.launch() |
|
|