Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -36,9 +36,9 @@ def analyze_sentiment(user_input: str):
|
|
| 36 |
result = sentiment_pipeline(user_input)[0]
|
| 37 |
sentiment = result['label']
|
| 38 |
confidence = result['score']
|
| 39 |
-
return sentiment, confidence
|
| 40 |
except Exception as e:
|
| 41 |
-
return f"Error analyzing sentiment: {e}", None
|
| 42 |
|
| 43 |
# Function to save data
|
| 44 |
def save_feedback(user_input: str, sentiment: str, confidence: float, user_feedback: str) -> str:
|
|
@@ -58,35 +58,48 @@ def save_feedback(user_input: str, sentiment: str, confidence: float, user_feedb
|
|
| 58 |
f.write("\n")
|
| 59 |
# Push the changes to Hugging Face Hub
|
| 60 |
scheduler.push_to_hub()
|
| 61 |
-
return "
|
| 62 |
except Exception as e:
|
| 63 |
-
return f"Error saving
|
| 64 |
|
| 65 |
# Gradio Interface
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
return save_status
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
with gr.Row():
|
| 84 |
-
user_feedback = gr.Textbox(label="Your Feedback on the Model's Response", placeholder="Type your feedback here...")
|
| 85 |
-
submit_button = gr.Button("Analyze")
|
| 86 |
-
feedback_submit_button = gr.Button("Submit Feedback")
|
| 87 |
|
| 88 |
-
# Connect
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
demo.launch()
|
|
|
|
| 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:
|
|
|
|
| 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()
|