Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,92 @@
|
|
|
|
|
| 1 |
import json
|
| 2 |
-
|
| 3 |
-
from pathlib import Path
|
| 4 |
-
from uuid import uuid4
|
| 5 |
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
repo_type="dataset",
|
| 19 |
-
folder_path=JSON_DATASET_DIR,
|
| 20 |
-
path_in_repo="data",
|
| 21 |
-
)
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
st.
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
st.
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
import json
|
| 3 |
+
import os
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Initialize results file
|
| 6 |
+
filename = "results.json"
|
| 7 |
|
| 8 |
+
if not os.path.exists(filename):
|
| 9 |
+
with open(filename, "w") as file:
|
| 10 |
+
json.dump([], file)
|
| 11 |
|
| 12 |
+
# Function to load existing data
|
| 13 |
+
def load_existing_data():
|
| 14 |
+
with open(filename, "r") as file:
|
| 15 |
+
try:
|
| 16 |
+
return json.load(file)
|
| 17 |
+
except json.JSONDecodeError:
|
| 18 |
+
return []
|
| 19 |
|
| 20 |
+
# Function to save feedback
|
| 21 |
+
def save_feedback(prompt, response, feedback_comment, feedback_value):
|
| 22 |
+
existing_data = load_existing_data()
|
| 23 |
+
|
| 24 |
+
# Check for existing feedback for the same prompt-response pair
|
| 25 |
+
found = False
|
| 26 |
+
for data in existing_data:
|
| 27 |
+
if data.get("prompt") == prompt and data.get("response") == response:
|
| 28 |
+
data["feedback"] = feedback_comment
|
| 29 |
+
data["positiveORnegative"] = "Positive" if feedback_value == 1 else "Negative"
|
| 30 |
+
found = True
|
| 31 |
+
break
|
| 32 |
+
|
| 33 |
+
if not found:
|
| 34 |
+
# Add new feedback entry
|
| 35 |
+
new_entry = {
|
| 36 |
+
"prompt": prompt,
|
| 37 |
+
"response": response,
|
| 38 |
+
"feedback": feedback_comment,
|
| 39 |
+
"positiveORnegative": "Positive" if feedback_value == 1 else "Negative"
|
| 40 |
+
}
|
| 41 |
+
existing_data.append(new_entry)
|
| 42 |
+
|
| 43 |
+
# Save updated data
|
| 44 |
+
with open(filename, "w") as file:
|
| 45 |
+
json.dump(existing_data, file, indent=4)
|
| 46 |
|
| 47 |
+
# Streamlit UI
|
| 48 |
+
st.title("Feedback-enabled Chatbot")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
if "prompts" not in st.session_state:
|
| 51 |
+
st.session_state.prompts = []
|
| 52 |
+
if "responses" not in st.session_state:
|
| 53 |
+
st.session_state.responses = []
|
| 54 |
+
if "show_feedback" not in st.session_state:
|
| 55 |
+
st.session_state.show_feedback = False
|
| 56 |
|
| 57 |
+
# User input section
|
| 58 |
+
user_input = st.text_input("Enter your message:")
|
| 59 |
+
if st.button("Get Response"):
|
| 60 |
+
# Generate AI response (replace with your model's logic)
|
| 61 |
+
ai_response = f"Response to: {user_input}"
|
| 62 |
+
st.session_state.prompts.append(user_input)
|
| 63 |
+
st.session_state.responses.append(ai_response)
|
| 64 |
+
st.session_state.show_feedback = True
|
| 65 |
+
st.write("AI Response:", ai_response)
|
| 66 |
|
| 67 |
+
# Feedback section
|
| 68 |
+
if st.session_state.show_feedback and len(st.session_state.prompts) > 0 and len(st.session_state.responses) > 0:
|
| 69 |
+
st.write("Provide feedback for the last interaction:")
|
| 70 |
+
|
| 71 |
+
col1, col2 = st.columns(2)
|
| 72 |
+
with col1:
|
| 73 |
+
if st.button("👍 Thumbs Up"):
|
| 74 |
+
st.session_state.feedback_value = 1
|
| 75 |
+
st.session_state.show_feedback_box = True
|
| 76 |
+
with col2:
|
| 77 |
+
if st.button("👎 Thumbs Down"):
|
| 78 |
+
st.session_state.feedback_value = 0
|
| 79 |
+
st.session_state.show_feedback_box = True
|
| 80 |
+
|
| 81 |
+
if "show_feedback_box" in st.session_state and st.session_state.show_feedback_box:
|
| 82 |
+
feedback_comment = st.text_area("Your Feedback:")
|
| 83 |
+
if st.button("Submit Feedback"):
|
| 84 |
+
save_feedback(
|
| 85 |
+
st.session_state.prompts[-1],
|
| 86 |
+
st.session_state.responses[-1],
|
| 87 |
+
feedback_comment,
|
| 88 |
+
st.session_state.feedback_value,
|
| 89 |
+
)
|
| 90 |
+
st.success("Feedback saved successfully!")
|
| 91 |
+
st.session_state.show_feedback_box = False
|
| 92 |
+
st.session_state.show_feedback = False
|