-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeedback_bot.py
More file actions
182 lines (148 loc) · 7.78 KB
/
feedback_bot.py
File metadata and controls
182 lines (148 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import streamlit as st
import pandas as pd
import shared_utils as utils
from datetime import datetime
def run():
st.title("Share Your Feedback")
if "fb_page" not in st.session_state:
st.session_state.fb_page = "CHAT"
if "fb_messages" not in st.session_state:
st.session_state.fb_messages = [
{"role": "assistant", "content": "Hello. We would appreciate your feedback. Please share your experience, suggestions, or any concerns you may have."}
]
if "fb_record" not in st.session_state:
st.session_state.fb_record = {field: None for field in utils.FEEDBACK_FIELDS}
with st.sidebar:
st.markdown("### Your Feedback")
filled = len([f for f in utils.FEEDBACK_FIELDS
if f != "Feedback_Timestamp" and st.session_state.fb_record.get(f)])
total = len([f for f in utils.FEEDBACK_FIELDS if f != "Feedback_Timestamp"])
if filled > 0:
st.progress(filled / total)
st.caption(f"{filled}/{total} sections completed")
st.markdown("---")
if st.button("Undo Last Message"):
if len(st.session_state.fb_messages) > 1:
st.session_state.fb_messages.pop()
if st.session_state.fb_messages and st.session_state.fb_messages[-1]['role'] == 'user':
st.session_state.fb_messages.pop()
st.rerun()
if st.session_state.fb_page == "CHAT":
for msg in st.session_state.fb_messages:
with st.chat_message(msg["role"]):
st.markdown(msg["content"])
if prompt := st.chat_input("Type your feedback here..."):
st.session_state.fb_messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
# SMART EXTRACTION
remaining = [f for f in utils.FEEDBACK_FIELDS
if f != "Feedback_Timestamp" and st.session_state.fb_record.get(f) is None]
# For feedback, we use simpler extraction since there are only 2 main fields
extracted = {}
# Identify topic keywords
if "Feedback_Topic" in remaining:
topics = {
"service": ["service", "support", "help", "assistance"],
"product": ["product", "quality", "feature", "functionality"],
"website": ["website", "app", "interface", "navigation", "ui"],
"billing": ["billing", "payment", "charge", "invoice", "price"],
"suggestion": ["suggest", "recommend", "improve", "enhancement", "idea"],
"complaint": ["complaint", "issue", "problem", "concern", "dissatisfied"]
}
text_lower = prompt.lower()
for topic, keywords in topics.items():
if any(kw in text_lower for kw in keywords):
extracted["Feedback_Topic"] = topic.title()
break
# If no keyword match but text is substantial, use first few words
if "Feedback_Topic" not in extracted and len(prompt.split()) > 3:
first_words = " ".join(prompt.split()[:4])
extracted["Feedback_Topic"] = first_words
# If message is detailed, capture as the main feedback
if "Feedback_Cause_Help" in remaining and len(prompt) > 20:
extracted["Feedback_Cause_Help"] = prompt
if extracted:
for field, value in extracted.items():
st.session_state.fb_record[field] = value
st.toast(f"Noted: {field}")
remaining = [f for f in utils.FEEDBACK_FIELDS
if f != "Feedback_Timestamp" and st.session_state.fb_record.get(f) is None]
if remaining:
with st.spinner("Thinking..."):
ai_reply = utils.generate_ai_response(
st.session_state.fb_messages,
st.session_state.fb_record,
remaining,
"FEEDBACK"
)
st.session_state.fb_messages.append({"role": "assistant", "content": ai_reply})
with st.chat_message("assistant"):
st.write_stream(utils.stream_text(ai_reply))
else:
st.session_state.fb_page = "REVIEW"
st.rerun()
else:
# Small talk or unclear input
with st.spinner("..."):
ai_reply = utils.generate_small_talk_response(
st.session_state.fb_messages,
["your feedback"]
)
st.session_state.fb_messages.append({"role": "assistant", "content": ai_reply})
with st.chat_message("assistant"):
st.write_stream(utils.stream_text(ai_reply))
# --- REVIEW PAGE ---
elif st.session_state.fb_page == "REVIEW":
st.subheader("Review Your Feedback")
st.markdown("Take a moment to review what you've shared. You can edit anything below!")
display_data = {k: v for k, v in st.session_state.fb_record.items()
if v is not None and k != "Feedback_Timestamp"}
if not display_data:
st.warning("Let's add some feedback first!")
if st.button("Back to Chat"):
st.session_state.fb_page = "CHAT"
st.rerun()
return
df = pd.DataFrame(list(display_data.items()), columns=["Field", "Value"])
edited_df = st.data_editor(
df,
use_container_width=True,
hide_index=True,
num_rows="fixed",
column_config={
"Field": st.column_config.TextColumn(disabled=True, width="medium"),
"Value": st.column_config.TextColumn("Your Feedback", width="large")
}
)
st.markdown("---")
col1, col2, col3 = st.columns([2, 2, 1])
with col1:
if st.button("Submit Feedback", type="primary", use_container_width=True):
for index, row in edited_df.iterrows():
st.session_state.fb_record[row["Field"]] = row["Value"]
st.session_state.fb_record["Feedback_Timestamp"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with st.spinner("Sending your feedback..."):
success = utils.save_to_sheet(st.session_state.fb_record, "FEEDBACK")
if success:
st.session_state.fb_page = "SUCCESS"
st.rerun()
else:
st.error("Submission failed. Please try again.")
with col2:
if st.button("Add More Details", use_container_width=True):
st.session_state.fb_page = "CHAT"
st.rerun()
with col3:
if st.button("Reset"):
st.session_state.clear()
st.rerun()
# --- SUCCESS PAGE ---
elif st.session_state.fb_page == "SUCCESS":
st.balloons()
st.success("### Thank You for Your Feedback")
st.markdown("We truly appreciate you taking the time to share your thoughts with us.")
st.info("**Your voice matters!** Our team reviews all feedback to continuously improve our services.")
if st.button("Share More Feedback", type="primary"):
st.session_state.clear()
st.rerun()