File size: 1,426 Bytes
639b28f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()