Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,77 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 55 |
+
# User input section
|
| 56 |
+
user_input = st.text_input("Enter your message:")
|
| 57 |
+
if st.button("Get Response"):
|
| 58 |
+
# Generate AI response (replace with your model's logic)
|
| 59 |
+
ai_response = f"Response to: {user_input}"
|
| 60 |
+
st.session_state.prompts.append(user_input)
|
| 61 |
+
st.session_state.responses.append(ai_response)
|
| 62 |
+
st.write("AI Response:", ai_response)
|
| 63 |
+
|
| 64 |
+
# Feedback section
|
| 65 |
+
if len(st.session_state.prompts) > 0 and len(st.session_state.responses) > 0:
|
| 66 |
+
st.write("Provide feedback for the last interaction:")
|
| 67 |
+
feedback_comment = st.text_area("Your Feedback:")
|
| 68 |
+
feedback_value = st.radio("Feedback Type:", options=["Positive", "Negative"], index=0)
|
| 69 |
+
if st.button("Submit Feedback"):
|
| 70 |
+
feedback_type = 1 if feedback_value == "Positive" else 0
|
| 71 |
+
save_feedback(
|
| 72 |
+
st.session_state.prompts[-1],
|
| 73 |
+
st.session_state.responses[-1],
|
| 74 |
+
feedback_comment,
|
| 75 |
+
feedback_type,
|
| 76 |
+
)
|
| 77 |
+
st.success("Feedback saved successfully!")
|