Spaces:
Sleeping
Sleeping
Adding feedback
Browse files
app.py
CHANGED
@@ -5,6 +5,9 @@ from openai import OpenAI
|
|
5 |
from langfuse import Langfuse
|
6 |
from langfuse.decorators import observe
|
7 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
8 |
|
9 |
# Load environment variables from .env file if it exists
|
10 |
load_dotenv()
|
@@ -27,6 +30,29 @@ except Exception as e:
|
|
27 |
print(f"Warning: Langfuse client initialization failed: {e}")
|
28 |
langfuse = None
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
def create_chat_interface():
|
31 |
# Initialize session state
|
32 |
session_id = str(uuid.uuid4())
|
@@ -60,13 +86,11 @@ def create_chat_interface():
|
|
60 |
# Add model info to the span
|
61 |
if langfuse and trace:
|
62 |
try:
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
metadata={"model": "gpt-3.5-turbo", "tokens": response.usage.total_tokens}
|
67 |
-
)
|
68 |
except Exception as e:
|
69 |
-
print(f"Warning: Failed to update
|
70 |
|
71 |
return response.choices[0].message.content
|
72 |
|
@@ -147,7 +171,15 @@ Write like the stakes are real. Because they are.
|
|
147 |
|
148 |
# Update chat history with new messages
|
149 |
chat_history.append({"role": "user", "content": message})
|
150 |
-
chat_history.append({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
|
152 |
return chat_history, ""
|
153 |
|
@@ -164,8 +196,12 @@ Write like the stakes are real. Because they are.
|
|
164 |
|
165 |
chatbot = gr.Chatbot(
|
166 |
height=600,
|
167 |
-
type="messages" # Use the new messages format
|
|
|
|
|
|
|
168 |
)
|
|
|
169 |
with gr.Row():
|
170 |
msg = gr.Textbox(
|
171 |
show_label=False,
|
@@ -174,8 +210,76 @@ Write like the stakes are real. Because they are.
|
|
174 |
)
|
175 |
submit = gr.Button("Send")
|
176 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
submit.click(respond, [msg, chatbot], [chatbot, msg])
|
178 |
msg.submit(respond, [msg, chatbot], [chatbot, msg])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
|
180 |
return demo
|
181 |
|
|
|
5 |
from langfuse import Langfuse
|
6 |
from langfuse.decorators import observe
|
7 |
from dotenv import load_dotenv
|
8 |
+
import csv
|
9 |
+
from datetime import datetime
|
10 |
+
import json
|
11 |
|
12 |
# Load environment variables from .env file if it exists
|
13 |
load_dotenv()
|
|
|
30 |
print(f"Warning: Langfuse client initialization failed: {e}")
|
31 |
langfuse = None
|
32 |
|
33 |
+
# Feedback file setup
|
34 |
+
FEEDBACK_FILE = "feedback.csv"
|
35 |
+
FEEDBACK_HEADERS = ["timestamp", "session_id", "message", "response", "rating", "comment"]
|
36 |
+
|
37 |
+
def save_feedback(session_id, message, response, rating, comment):
|
38 |
+
"""Save feedback to CSV file."""
|
39 |
+
try:
|
40 |
+
file_exists = os.path.isfile(FEEDBACK_FILE)
|
41 |
+
with open(FEEDBACK_FILE, 'a', newline='') as f:
|
42 |
+
writer = csv.DictWriter(f, fieldnames=FEEDBACK_HEADERS)
|
43 |
+
if not file_exists:
|
44 |
+
writer.writeheader()
|
45 |
+
writer.writerow({
|
46 |
+
"timestamp": datetime.now().isoformat(),
|
47 |
+
"session_id": session_id,
|
48 |
+
"message": message,
|
49 |
+
"response": response,
|
50 |
+
"rating": rating,
|
51 |
+
"comment": comment
|
52 |
+
})
|
53 |
+
except Exception as e:
|
54 |
+
print(f"Error saving feedback: {e}")
|
55 |
+
|
56 |
def create_chat_interface():
|
57 |
# Initialize session state
|
58 |
session_id = str(uuid.uuid4())
|
|
|
86 |
# Add model info to the span
|
87 |
if langfuse and trace:
|
88 |
try:
|
89 |
+
trace.update(
|
90 |
+
metadata={"model": "gpt-3.5-turbo", "tokens": response.usage.total_tokens}
|
91 |
+
)
|
|
|
|
|
92 |
except Exception as e:
|
93 |
+
print(f"Warning: Failed to update trace metadata: {e}")
|
94 |
|
95 |
return response.choices[0].message.content
|
96 |
|
|
|
171 |
|
172 |
# Update chat history with new messages
|
173 |
chat_history.append({"role": "user", "content": message})
|
174 |
+
chat_history.append({
|
175 |
+
"role": "assistant",
|
176 |
+
"content": assistant_message,
|
177 |
+
"feedback": {
|
178 |
+
"rating": gr.Radio(choices=["π", "π"], label="Rate this response", show_label=False),
|
179 |
+
"comment": gr.Textbox(label="Comment (optional)", placeholder="Share your thoughts...", lines=1),
|
180 |
+
"submit": gr.Button("Submit Feedback")
|
181 |
+
}
|
182 |
+
})
|
183 |
|
184 |
return chat_history, ""
|
185 |
|
|
|
196 |
|
197 |
chatbot = gr.Chatbot(
|
198 |
height=600,
|
199 |
+
type="messages", # Use the new messages format
|
200 |
+
show_label=False,
|
201 |
+
elem_id="chatbot",
|
202 |
+
show_copy_button=True
|
203 |
)
|
204 |
+
|
205 |
with gr.Row():
|
206 |
msg = gr.Textbox(
|
207 |
show_label=False,
|
|
|
210 |
)
|
211 |
submit = gr.Button("Send")
|
212 |
|
213 |
+
# Feedback components
|
214 |
+
with gr.Row(visible=False) as feedback_row:
|
215 |
+
with gr.Column():
|
216 |
+
rating = gr.Radio(
|
217 |
+
choices=["π", "π"],
|
218 |
+
label="Rate this response ",
|
219 |
+
show_label=True
|
220 |
+
)
|
221 |
+
comment = gr.Textbox(
|
222 |
+
label="Additional comments (optional)",
|
223 |
+
placeholder="Share your thoughts...",
|
224 |
+
lines=2
|
225 |
+
)
|
226 |
+
feedback_btn = gr.Button("Submit Feedback")
|
227 |
+
feedback_status = gr.Textbox(label="Status", interactive=False)
|
228 |
+
|
229 |
+
# Store the last message and response for feedback
|
230 |
+
last_message = gr.State("")
|
231 |
+
last_response = gr.State("")
|
232 |
+
|
233 |
+
def show_feedback(evt: gr.SelectData):
|
234 |
+
"""Show feedback UI when a message is selected."""
|
235 |
+
# Get the selected message from chat history
|
236 |
+
selected_message = chatbot.value[evt.index]
|
237 |
+
if selected_message["role"] == "assistant":
|
238 |
+
# Get the user message that prompted this response
|
239 |
+
user_message = chatbot.value[evt.index - 1]["content"]
|
240 |
+
assistant_message = selected_message["content"]
|
241 |
+
# Truncate the response for the label if it's too long
|
242 |
+
truncated_response = assistant_message[:100] + "..." if len(assistant_message) > 100 else assistant_message
|
243 |
+
return {
|
244 |
+
feedback_row: gr.update(visible=True),
|
245 |
+
last_message: user_message,
|
246 |
+
last_response: assistant_message,
|
247 |
+
rating: gr.update(label=f"Rate this response: {truncated_response}")
|
248 |
+
}
|
249 |
+
return {
|
250 |
+
feedback_row: gr.update(visible=False),
|
251 |
+
last_message: "",
|
252 |
+
last_response: "",
|
253 |
+
rating: gr.update(label="Rate this response")
|
254 |
+
}
|
255 |
+
|
256 |
+
def handle_feedback(rating, comment, message, response):
|
257 |
+
"""Handle feedback submission."""
|
258 |
+
save_feedback(
|
259 |
+
session_id,
|
260 |
+
message,
|
261 |
+
response,
|
262 |
+
rating,
|
263 |
+
comment
|
264 |
+
)
|
265 |
+
return "Thank you for your feedback!"
|
266 |
+
|
267 |
submit.click(respond, [msg, chatbot], [chatbot, msg])
|
268 |
msg.submit(respond, [msg, chatbot], [chatbot, msg])
|
269 |
+
|
270 |
+
# Show feedback UI when a message is selected
|
271 |
+
chatbot.select(
|
272 |
+
show_feedback,
|
273 |
+
None,
|
274 |
+
[feedback_row, last_message, last_response, rating]
|
275 |
+
)
|
276 |
+
|
277 |
+
# Handle feedback submission
|
278 |
+
feedback_btn.click(
|
279 |
+
handle_feedback,
|
280 |
+
[rating, comment, last_message, last_response],
|
281 |
+
[feedback_status]
|
282 |
+
)
|
283 |
|
284 |
return demo
|
285 |
|