Spaces:
Sleeping
Sleeping
Commit ·
e4f2a3f
1
Parent(s): e8d9db4
updated the error
Browse files
app.py
CHANGED
|
@@ -1,32 +1,32 @@
|
|
| 1 |
from transformers import pipeline
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
|
| 7 |
-
#
|
| 8 |
def analyze_sentiment(text):
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
#
|
| 19 |
iface = gr.Interface(
|
| 20 |
fn=analyze_sentiment,
|
| 21 |
-
inputs=gr.Textbox(lines=
|
| 22 |
outputs=[
|
| 23 |
-
gr.
|
| 24 |
-
gr.
|
| 25 |
],
|
| 26 |
-
title="
|
| 27 |
-
description="Enter
|
| 28 |
)
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
iface.launch()
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
+
# Load sentiment analysis model
|
| 5 |
+
classifier = pipeline("sentiment-analysis")
|
| 6 |
|
| 7 |
+
# Define the function Gradio will call
|
| 8 |
def analyze_sentiment(text):
|
| 9 |
+
if not text:
|
| 10 |
+
return "No input", 0
|
| 11 |
+
try:
|
| 12 |
+
result = classifier(text)[0] # should return a dict
|
| 13 |
+
label = result.get('label', 'Unknown')
|
| 14 |
+
score = round(result.get('score', 0), 3)
|
| 15 |
+
return label, score
|
| 16 |
+
except Exception as e:
|
| 17 |
+
return f"Error: {str(e)}", 0
|
| 18 |
|
| 19 |
+
# Define the Gradio Interface
|
| 20 |
iface = gr.Interface(
|
| 21 |
fn=analyze_sentiment,
|
| 22 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
|
| 23 |
outputs=[
|
| 24 |
+
gr.Textbox(label="Sentiment"),
|
| 25 |
+
gr.Number(label="Confidence Score")
|
| 26 |
],
|
| 27 |
+
title="Sentiment Analyzer",
|
| 28 |
+
description="Enter a sentence to get its sentiment (POSITIVE/NEGATIVE) and confidence score."
|
| 29 |
)
|
| 30 |
|
| 31 |
+
# Run the app
|
| 32 |
+
iface.launch()
|
|
|