Spaces:
Sleeping
Sleeping
Colin Arrowsmith
commited on
Commit
·
d48b278
1
Parent(s):
12f0f06
Log question and answer for each downvote to CSV
Browse files
app.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import time
|
|
|
|
|
|
|
| 3 |
import openai
|
| 4 |
import gradio as gr
|
| 5 |
|
|
@@ -32,7 +34,7 @@ chat = ChatOpenAI(model_name="gpt-4",temperature=0)
|
|
| 32 |
# Make sure we don't exceed estimation of token limit:
|
| 33 |
TOKEN_LIMIT = 4096 # GPT-3.5 Turbo token limit
|
| 34 |
BUFFER = 100 # Extra tokens to consider for incoming messages
|
| 35 |
-
LOG_PATH = "/data/
|
| 36 |
|
| 37 |
def estimate_tokens(texts):
|
| 38 |
return sum([len(t.split()) for t in texts])
|
|
@@ -90,14 +92,39 @@ def bot(chatbot_history):
|
|
| 90 |
time.sleep(0.05)
|
| 91 |
yield chatbot_history
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
if data.liked:
|
| 96 |
print("You upvoted this response: " + data.value)
|
| 97 |
else:
|
| 98 |
print("You downvoted this response: " + data.value)
|
| 99 |
-
|
| 100 |
-
|
|
|
|
| 101 |
|
| 102 |
# The Gradio App interface
|
| 103 |
with gr.Blocks() as demo:
|
|
@@ -112,7 +139,7 @@ with gr.Blocks() as demo:
|
|
| 112 |
bot, chatbot, chatbot,
|
| 113 |
)
|
| 114 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 115 |
-
chatbot.like(vote,
|
| 116 |
|
| 117 |
# Enable queuing
|
| 118 |
demo.queue()
|
|
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
+
import csv
|
| 4 |
+
from datetime import datetime
|
| 5 |
import openai
|
| 6 |
import gradio as gr
|
| 7 |
|
|
|
|
| 34 |
# Make sure we don't exceed estimation of token limit:
|
| 35 |
TOKEN_LIMIT = 4096 # GPT-3.5 Turbo token limit
|
| 36 |
BUFFER = 100 # Extra tokens to consider for incoming messages
|
| 37 |
+
LOG_PATH = "/data/downvoted_responses.csv" # File in which to log downvoted responses
|
| 38 |
|
| 39 |
def estimate_tokens(texts):
|
| 40 |
return sum([len(t.split()) for t in texts])
|
|
|
|
| 92 |
time.sleep(0.05)
|
| 93 |
yield chatbot_history
|
| 94 |
|
| 95 |
+
def log_to_csv(question, answer, log_path=LOG_PATH):
|
| 96 |
+
"""Append a line to a CSV. Create a new file if needed."""
|
| 97 |
+
now = datetime.today().strftime("%Y%m%d_%H:%M:%S")
|
| 98 |
+
if not os.path.isfile(log_path):
|
| 99 |
+
# Add the column names to the CSV
|
| 100 |
+
with open(log_path, "a") as csv_file:
|
| 101 |
+
writer = csv.writer(csv_file)
|
| 102 |
+
writer.writerow(["datetime", "user_question", "bot_response"])
|
| 103 |
+
|
| 104 |
+
# Write the disliked message to the CSV
|
| 105 |
+
with open(log_path, "a") as csv_file:
|
| 106 |
+
writer = csv.writer(csv_file)
|
| 107 |
+
writer.writerow([now, question, answer])
|
| 108 |
+
|
| 109 |
+
def get_voted_qa_pair(history, voted_answer):
|
| 110 |
+
"""Return the question-answer pair from the chat history, given a
|
| 111 |
+
particular bot answer. Note: This is required because the 'vote'
|
| 112 |
+
event handler only has access to the answer that was liked/disliked.
|
| 113 |
+
"""
|
| 114 |
+
for question, answer in history:
|
| 115 |
+
if answer == voted_answer:
|
| 116 |
+
return question, answer
|
| 117 |
+
|
| 118 |
+
def vote(data: gr.LikeData, history, log_path=LOG_PATH):
|
| 119 |
+
"""This is a function to do something with the voted information"""
|
| 120 |
+
print(history)
|
| 121 |
if data.liked:
|
| 122 |
print("You upvoted this response: " + data.value)
|
| 123 |
else:
|
| 124 |
print("You downvoted this response: " + data.value)
|
| 125 |
+
# Find Q/A pair that was disliked
|
| 126 |
+
question, answer = get_voted_qa_pair(history, data.value)
|
| 127 |
+
log_to_csv(question, answer, log_path)
|
| 128 |
|
| 129 |
# The Gradio App interface
|
| 130 |
with gr.Blocks() as demo:
|
|
|
|
| 139 |
bot, chatbot, chatbot,
|
| 140 |
)
|
| 141 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 142 |
+
chatbot.like(vote, chatbot, None)
|
| 143 |
|
| 144 |
# Enable queuing
|
| 145 |
demo.queue()
|