HuggingFaceDemo / app.py
Af2024ma's picture
Update app.py
639b28f verified
from transformers import pipeline
import gradio as gr
# Attempt to load the model and run a test prediction
try:
sentiment_analysis = pipeline("sentiment-analysis")
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}")
# Prediction function with error handling
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}"
# Define example inputs
exams = [
"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."
]
# Gradio interface setup
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=exams)
iface.launch()