Af2024ma commited on
Commit
639b28f
·
verified ·
1 Parent(s): e884cdd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py CHANGED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ import gradio as gr
3
+
4
+ # Attempt to load the model and run a test prediction
5
+ try:
6
+ sentiment_analysis = pipeline("sentiment-analysis")
7
+ test_output = sentiment_analysis("Testing the model with a simple sentence.")
8
+ print("Model test output:", test_output)
9
+ except Exception as e:
10
+ print(f"Failed to load or run model: {e}")
11
+
12
+ # Prediction function with error handling
13
+ def predict_sentiment(text):
14
+ try:
15
+ predictions = sentiment_analysis(text)
16
+ return f"Label: {predictions[0]['label']}, Score: {predictions[0]['score']:.4f}"
17
+ except Exception as e:
18
+ return f"Error processing input: {e}"
19
+
20
+
21
+ # Define example inputs
22
+ exams = [
23
+ "I absolutely love this product! It has changed my life.",
24
+ "This is the worst movie I have ever seen. Completely disappointing.",
25
+ "I'm not sure how I feel about this new update. It has some good points, but also many drawbacks.",
26
+ "The customer service was fantastic! Very helpful and polite.",
27
+ "Honestly, this was quite a mediocre experience. Nothing special."
28
+ ]
29
+
30
+ # Gradio interface setup
31
+ iface = gr.Interface(fn=predict_sentiment,
32
+ title="Sentiment Analysis",
33
+ description="Enter text to analyze sentiment. Powered by Hugging Face Transformers.",
34
+ inputs="text",
35
+ outputs="text",
36
+ examples=exams)
37
+
38
+ iface.launch()