Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -41,7 +41,7 @@ def analyze_sentiment(user_input: str):
|
|
| 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) -> str:
|
| 45 |
try:
|
| 46 |
with scheduler.lock:
|
| 47 |
with JSON_DATASET_PATH.open("a") as f:
|
|
@@ -50,13 +50,14 @@ def save_feedback(user_input: str, sentiment: str, confidence: float) -> str:
|
|
| 50 |
"review": user_input,
|
| 51 |
"sentiment": sentiment,
|
| 52 |
"confidence": confidence,
|
|
|
|
| 53 |
"datetime": datetime.now().isoformat(),
|
| 54 |
},
|
| 55 |
f,
|
| 56 |
)
|
| 57 |
f.write("\n")
|
| 58 |
# Push the changes to Hugging Face Hub
|
| 59 |
-
scheduler.push_to_hub()
|
| 60 |
return "Data saved successfully"
|
| 61 |
except Exception as e:
|
| 62 |
return f"Error saving data: {e}"
|
|
@@ -65,18 +66,27 @@ def save_feedback(user_input: str, sentiment: str, confidence: float) -> str:
|
|
| 65 |
def handle_feedback(user_input):
|
| 66 |
sentiment, confidence = analyze_sentiment(user_input)
|
| 67 |
if confidence is None: # Check for sentiment analysis errors
|
| 68 |
-
return sentiment
|
| 69 |
result_message = f"Sentiment: {sentiment}, Confidence: {confidence:.2f}"
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
with gr.Blocks() as demo:
|
| 76 |
with gr.Row():
|
| 77 |
user_input = gr.Textbox(label="Enter Your Review", placeholder="Type your review here...")
|
| 78 |
output = gr.Textbox(label="Analysis Result", interactive=False)
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
demo.launch()
|
|
|
|
| 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:
|
| 45 |
try:
|
| 46 |
with scheduler.lock:
|
| 47 |
with JSON_DATASET_PATH.open("a") as f:
|
|
|
|
| 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 "Data saved successfully"
|
| 62 |
except Exception as e:
|
| 63 |
return f"Error saving data: {e}"
|
|
|
|
| 66 |
def handle_feedback(user_input):
|
| 67 |
sentiment, confidence = analyze_sentiment(user_input)
|
| 68 |
if confidence is None: # Check for sentiment analysis errors
|
| 69 |
+
return sentiment, "", ""
|
| 70 |
result_message = f"Sentiment: {sentiment}, Confidence: {confidence:.2f}"
|
| 71 |
+
return result_message, sentiment, confidence
|
| 72 |
+
|
| 73 |
+
def submit_feedback(user_input, sentiment, confidence, user_feedback):
|
| 74 |
+
if not user_feedback:
|
| 75 |
+
return "Please provide feedback before submitting."
|
| 76 |
+
save_status = save_feedback(user_input, sentiment, confidence, user_feedback)
|
| 77 |
+
return save_status
|
| 78 |
|
| 79 |
with gr.Blocks() as demo:
|
| 80 |
with gr.Row():
|
| 81 |
user_input = gr.Textbox(label="Enter Your Review", placeholder="Type your review here...")
|
| 82 |
output = gr.Textbox(label="Analysis Result", interactive=False)
|
| 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 functions to buttons
|
| 89 |
+
submit_button.click(fn=handle_feedback, inputs=user_input, outputs=[output, user_feedback, output])
|
| 90 |
+
feedback_submit_button.click(fn=submit_feedback, inputs=[user_input, user_feedback, output, user_feedback], outputs=output)
|
| 91 |
|
| 92 |
+
demo.launch()
|