Spaces:
Sleeping
Sleeping
Colin Arrowsmith
commited on
Commit
·
fcd27aa
1
Parent(s):
d48b278
Copy logged QA pairs from persistent storage to repo after each downvote
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
import csv
|
|
|
|
| 4 |
from datetime import datetime
|
| 5 |
import openai
|
| 6 |
import gradio as gr
|
|
@@ -34,7 +35,8 @@ chat = ChatOpenAI(model_name="gpt-4",temperature=0)
|
|
| 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 |
-
|
|
|
|
| 38 |
|
| 39 |
def estimate_tokens(texts):
|
| 40 |
return sum([len(t.split()) for t in texts])
|
|
@@ -92,20 +94,23 @@ def bot(chatbot_history):
|
|
| 92 |
time.sleep(0.05)
|
| 93 |
yield chatbot_history
|
| 94 |
|
| 95 |
-
def log_to_csv(question, answer
|
| 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(
|
| 99 |
# Add the column names to the CSV
|
| 100 |
-
with open(
|
| 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(
|
| 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'
|
|
@@ -115,7 +120,7 @@ def get_voted_qa_pair(history, voted_answer):
|
|
| 115 |
if answer == voted_answer:
|
| 116 |
return question, answer
|
| 117 |
|
| 118 |
-
def vote(data: gr.LikeData, history
|
| 119 |
"""This is a function to do something with the voted information"""
|
| 120 |
print(history)
|
| 121 |
if data.liked:
|
|
@@ -124,7 +129,7 @@ def vote(data: gr.LikeData, history, log_path=LOG_PATH):
|
|
| 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
|
| 128 |
|
| 129 |
# The Gradio App interface
|
| 130 |
with gr.Blocks() as demo:
|
|
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
import csv
|
| 4 |
+
import shutil
|
| 5 |
from datetime import datetime
|
| 6 |
import openai
|
| 7 |
import gradio as gr
|
|
|
|
| 35 |
# Make sure we don't exceed estimation of token limit:
|
| 36 |
TOKEN_LIMIT = 4096 # GPT-3.5 Turbo token limit
|
| 37 |
BUFFER = 100 # Extra tokens to consider for incoming messages
|
| 38 |
+
PERSISTENT_LOG_PATH = "/data/downvoted_responses.csv" # File in which to log downvoted responses
|
| 39 |
+
LOCAL_LOG_PATH = "./data/downvoted_responses.csv"
|
| 40 |
|
| 41 |
def estimate_tokens(texts):
|
| 42 |
return sum([len(t.split()) for t in texts])
|
|
|
|
| 94 |
time.sleep(0.05)
|
| 95 |
yield chatbot_history
|
| 96 |
|
| 97 |
+
def log_to_csv(question, answer):
|
| 98 |
"""Append a line to a CSV. Create a new file if needed."""
|
| 99 |
now = datetime.today().strftime("%Y%m%d_%H:%M:%S")
|
| 100 |
+
if not os.path.isfile(PERSISTENT_LOG_PATH):
|
| 101 |
# Add the column names to the CSV
|
| 102 |
+
with open(PERSISTENT_LOG_PATH, "w+") as csv_file:
|
| 103 |
writer = csv.writer(csv_file)
|
| 104 |
writer.writerow(["datetime", "user_question", "bot_response"])
|
| 105 |
|
| 106 |
# Write the disliked message to the CSV
|
| 107 |
+
with open(PERSISTENT_LOG_PATH, "a") as csv_file:
|
| 108 |
writer = csv.writer(csv_file)
|
| 109 |
writer.writerow([now, question, answer])
|
| 110 |
|
| 111 |
+
# Copy file from persistent storage to local repo
|
| 112 |
+
shutil.copyfile(PERSISTENT_LOG_PATH, LOCAL_LOG_PATH)
|
| 113 |
+
|
| 114 |
def get_voted_qa_pair(history, voted_answer):
|
| 115 |
"""Return the question-answer pair from the chat history, given a
|
| 116 |
particular bot answer. Note: This is required because the 'vote'
|
|
|
|
| 120 |
if answer == voted_answer:
|
| 121 |
return question, answer
|
| 122 |
|
| 123 |
+
def vote(data: gr.LikeData, history):
|
| 124 |
"""This is a function to do something with the voted information"""
|
| 125 |
print(history)
|
| 126 |
if data.liked:
|
|
|
|
| 129 |
print("You downvoted this response: " + data.value)
|
| 130 |
# Find Q/A pair that was disliked
|
| 131 |
question, answer = get_voted_qa_pair(history, data.value)
|
| 132 |
+
log_to_csv(question, answer)
|
| 133 |
|
| 134 |
# The Gradio App interface
|
| 135 |
with gr.Blocks() as demo:
|