Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,39 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# Load the sentiment analysis pipeline
|
| 5 |
sentiment_pipeline = pipeline("sentiment-analysis")
|
| 6 |
|
|
|
|
| 7 |
def sentiment_analysis(message, history):
|
| 8 |
-
# Perform sentiment analysis on the user's message
|
| 9 |
result = sentiment_pipeline(message)[0]
|
| 10 |
label = result["label"]
|
| 11 |
score = result["score"]
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Load the sentiment analysis pipeline
|
| 5 |
sentiment_pipeline = pipeline("sentiment-analysis")
|
| 6 |
|
| 7 |
+
# Function to analyze sentiment
|
| 8 |
def sentiment_analysis(message, history):
|
|
|
|
| 9 |
result = sentiment_pipeline(message)[0]
|
| 10 |
label = result["label"]
|
| 11 |
score = result["score"]
|
| 12 |
+
response = f"Sentiment: {label}, Confidence: {score:.2f}"
|
| 13 |
+
return response
|
| 14 |
|
| 15 |
+
# Function to record feedback
|
| 16 |
+
feedback_store = [] # A list to store feedback
|
| 17 |
+
|
| 18 |
+
def record_feedback(response, feedback):
|
| 19 |
+
feedback_store.append({"response": response, "feedback": feedback})
|
| 20 |
+
return f"Thank you for your feedback! ({len(feedback_store)} recorded)"
|
| 21 |
+
|
| 22 |
+
# Gradio Interface
|
| 23 |
+
with gr.Blocks() as demo:
|
| 24 |
+
chat = gr.ChatInterface(fn=sentiment_analysis, type="messages")
|
| 25 |
+
|
| 26 |
+
# Additional components for feedback
|
| 27 |
+
with gr.Row():
|
| 28 |
+
feedback_input = gr.Textbox(placeholder="Enter your feedback here", label="Feedback")
|
| 29 |
+
record_button = gr.Button("Submit Feedback")
|
| 30 |
+
|
| 31 |
+
# Feedback submission functionality
|
| 32 |
+
feedback_status = gr.Textbox(interactive=False, label="Feedback Status")
|
| 33 |
+
record_button.click(
|
| 34 |
+
fn=record_feedback,
|
| 35 |
+
inputs=[chat.output_component, feedback_input],
|
| 36 |
+
outputs=feedback_status,
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
demo.launch()
|