Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,105 +1,18 @@
|
|
| 1 |
-
import json
|
| 2 |
-
from datetime import datetime
|
| 3 |
-
from pathlib import Path
|
| 4 |
-
from uuid import uuid4
|
| 5 |
-
import os
|
| 6 |
-
|
| 7 |
import gradio as gr
|
| 8 |
-
from huggingface_hub import CommitScheduler
|
| 9 |
from transformers import pipeline
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
# Initialize Sentiment Analysis Pipeline
|
| 28 |
-
try:
|
| 29 |
-
sentiment_pipeline = pipeline("sentiment-analysis")
|
| 30 |
-
except Exception as e:
|
| 31 |
-
raise RuntimeError(f"Failed to load sentiment-analysis pipeline: {e}")
|
| 32 |
-
|
| 33 |
-
# Function to analyze sentiment
|
| 34 |
-
def analyze_sentiment(user_input: str):
|
| 35 |
-
try:
|
| 36 |
-
result = sentiment_pipeline(user_input)[0]
|
| 37 |
-
sentiment = result['label']
|
| 38 |
-
confidence = result['score']
|
| 39 |
-
return f"Sentiment: {sentiment}, Confidence: {confidence:.2f}", sentiment, confidence
|
| 40 |
-
except Exception as e:
|
| 41 |
-
return f"Error analyzing sentiment: {e}", None, None
|
| 42 |
-
|
| 43 |
-
# Function to save data
|
| 44 |
-
def save_feedback(user_input: str, sentiment: str, confidence: float, user_feedback: str) -> str:
|
| 45 |
-
try:
|
| 46 |
-
with scheduler.lock:
|
| 47 |
-
with JSON_DATASET_PATH.open("a") as f:
|
| 48 |
-
json.dump(
|
| 49 |
-
{
|
| 50 |
-
"review": user_input,
|
| 51 |
-
"sentiment": sentiment,
|
| 52 |
-
"confidence": confidence,
|
| 53 |
-
"user_feedback": user_feedback,
|
| 54 |
-
"datetime": datetime.now().isoformat(),
|
| 55 |
-
},
|
| 56 |
-
f,
|
| 57 |
-
)
|
| 58 |
-
f.write("\n")
|
| 59 |
-
# Push the changes to Hugging Face Hub
|
| 60 |
-
scheduler.push_to_hub()
|
| 61 |
-
return "Feedback saved successfully!"
|
| 62 |
-
except Exception as e:
|
| 63 |
-
return f"Error saving feedback: {e}"
|
| 64 |
-
|
| 65 |
-
# Gradio Interface
|
| 66 |
-
with gr.Blocks() as demo:
|
| 67 |
-
with gr.Column():
|
| 68 |
-
gr.Markdown("### Sentiment Analysis with User Feedback")
|
| 69 |
-
|
| 70 |
-
with gr.Row():
|
| 71 |
-
user_input = gr.Textbox(label="Enter Your Review", placeholder="Type your review here...")
|
| 72 |
-
analyze_button = gr.Button("Analyze")
|
| 73 |
-
|
| 74 |
-
analysis_output = gr.Textbox(label="Analysis Result", interactive=False)
|
| 75 |
-
|
| 76 |
-
feedback_section = gr.Column(visible=False) # Initially hidden
|
| 77 |
-
with feedback_section:
|
| 78 |
-
feedback_input = gr.Textbox(label="Your Feedback on the Model's Response", placeholder="Type your feedback here...")
|
| 79 |
-
feedback_button = gr.Button("Submit Feedback")
|
| 80 |
-
|
| 81 |
-
feedback_status = gr.Textbox(label="Feedback Status", interactive=False)
|
| 82 |
-
|
| 83 |
-
# Button Logic
|
| 84 |
-
def analyze_and_show_feedback(user_input):
|
| 85 |
-
result_message, sentiment, confidence = analyze_sentiment(user_input)
|
| 86 |
-
return result_message, gr.update(visible=True), user_input, sentiment, confidence
|
| 87 |
-
|
| 88 |
-
def submit_feedback(user_input, sentiment, confidence, user_feedback):
|
| 89 |
-
if not user_feedback:
|
| 90 |
-
return "Please provide feedback before submitting."
|
| 91 |
-
return save_feedback(user_input, sentiment, confidence, user_feedback)
|
| 92 |
-
|
| 93 |
-
# Connect button actions
|
| 94 |
-
analyze_button.click(
|
| 95 |
-
fn=analyze_and_show_feedback,
|
| 96 |
-
inputs=[user_input],
|
| 97 |
-
outputs=[analysis_output, feedback_section, user_input, feedback_section, feedback_section]
|
| 98 |
-
)
|
| 99 |
-
feedback_button.click(
|
| 100 |
-
fn=submit_feedback,
|
| 101 |
-
inputs=[user_input, analysis_output, feedback_status, user_input],
|
| 102 |
-
outputs=feedback_status
|
| 103 |
-
)
|
| 104 |
-
|
| 105 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Load the sentiment analysis pipeline from Hugging Face
|
| 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 |
+
return f"Sentiment: {label}, Confidence: {score:.2f}"
|
| 13 |
+
|
| 14 |
+
# Create a Gradio ChatInterface with the sentiment analysis function
|
| 15 |
+
gr.ChatInterface(
|
| 16 |
+
fn=sentiment_analysis,
|
| 17 |
+
type="messages"
|
| 18 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|