Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -63,22 +63,22 @@ def get_full_context(input):
|
|
| 63 |
index = doc.metadata['index']
|
| 64 |
context += str(data.iloc[[index]])
|
| 65 |
|
| 66 |
-
return
|
| 67 |
|
| 68 |
|
| 69 |
is_first_run = True # Flag to check if it's the first run
|
| 70 |
|
| 71 |
# Here is the langchain
|
| 72 |
def predict(history, input):
|
| 73 |
-
|
| 74 |
global is_first_run # Use the global flag
|
| 75 |
if is_first_run:
|
| 76 |
context = get_full_context(input)
|
| 77 |
-
print(
|
| 78 |
is_first_run = False # Set the flag to False after the first run
|
| 79 |
else:
|
| 80 |
context = ""
|
| 81 |
-
|
| 82 |
history_langchain_format = []
|
| 83 |
history_langchain_format.append(SystemMessage(content=f"{ORIG_SYSTEM_MESSAGE_PROMPT}, here is the user information: {user_info_simulated}"))
|
| 84 |
for human, ai in history:
|
|
@@ -121,64 +121,35 @@ def bot(chatbot_history):
|
|
| 121 |
time.sleep(0.05)
|
| 122 |
yield chatbot_history
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
now = datetime.today().strftime("%Y%m%d_%H:%M:%S")
|
| 127 |
-
if not os.path.isfile(PERSISTENT_LOG_PATH):
|
| 128 |
-
# Add the column names to the CSV
|
| 129 |
-
with open(PERSISTENT_LOG_PATH, "w+") as csv_file:
|
| 130 |
-
writer = csv.writer(csv_file)
|
| 131 |
-
writer.writerow(["datetime", "user_question", "bot_response"])
|
| 132 |
-
|
| 133 |
-
# Write the disliked message to the CSV
|
| 134 |
-
with open(PERSISTENT_LOG_PATH, "a") as csv_file:
|
| 135 |
-
writer = csv.writer(csv_file)
|
| 136 |
-
writer.writerow([now, question, answer])
|
| 137 |
-
|
| 138 |
-
# Copy file from persistent storage to local repo
|
| 139 |
-
shutil.copyfile(PERSISTENT_LOG_PATH, LOCAL_LOG_PATH)
|
| 140 |
-
|
| 141 |
-
def get_voted_qa_pair(history, voted_answer):
|
| 142 |
-
"""Return the question-answer pair from the chat history, given a
|
| 143 |
-
particular bot answer. Note: This is required because the 'vote'
|
| 144 |
-
event handler only has access to the answer that was liked/disliked.
|
| 145 |
-
"""
|
| 146 |
-
for question, answer in history:
|
| 147 |
-
if answer == voted_answer:
|
| 148 |
-
return question, answer
|
| 149 |
-
|
| 150 |
-
def vote(data: gr.LikeData, history):
|
| 151 |
-
"""This is a function to do something with the voted information"""
|
| 152 |
-
print(history)
|
| 153 |
if data.liked:
|
| 154 |
print("You upvoted this response: " + data.value)
|
| 155 |
else:
|
| 156 |
print("You downvoted this response: " + data.value)
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
def reset_flag():
|
| 162 |
global is_first_run
|
| 163 |
is_first_run = True
|
| 164 |
|
| 165 |
# The Gradio App interface
|
| 166 |
with gr.Blocks() as demo:
|
| 167 |
-
gr.Markdown("""<h1><center>
|
| 168 |
-
gr.Markdown("""<p><center>
|
| 169 |
-
chatbot = gr.Chatbot(
|
| 170 |
-
textbox = gr.Textbox(
|
| 171 |
clear_button = gr.ClearButton(components=[chatbot])
|
| 172 |
clear_button.click(reset_flag, None, None)
|
| 173 |
-
|
| 174 |
# Chain user and bot functions with `.then()`
|
| 175 |
textbox.submit(user, [textbox, chatbot], [textbox, chatbot], queue=False).then(
|
| 176 |
bot, chatbot, chatbot,
|
| 177 |
)
|
| 178 |
-
chatbot.like(vote,
|
| 179 |
|
| 180 |
# Enable queuing
|
| 181 |
demo.queue()
|
| 182 |
-
demo.launch(debug=True
|
| 183 |
-
|
| 184 |
|
|
|
|
| 63 |
index = doc.metadata['index']
|
| 64 |
context += str(data.iloc[[index]])
|
| 65 |
|
| 66 |
+
return context
|
| 67 |
|
| 68 |
|
| 69 |
is_first_run = True # Flag to check if it's the first run
|
| 70 |
|
| 71 |
# Here is the langchain
|
| 72 |
def predict(history, input):
|
| 73 |
+
|
| 74 |
global is_first_run # Use the global flag
|
| 75 |
if is_first_run:
|
| 76 |
context = get_full_context(input)
|
| 77 |
+
print(context) # For debugging
|
| 78 |
is_first_run = False # Set the flag to False after the first run
|
| 79 |
else:
|
| 80 |
context = ""
|
| 81 |
+
|
| 82 |
history_langchain_format = []
|
| 83 |
history_langchain_format.append(SystemMessage(content=f"{ORIG_SYSTEM_MESSAGE_PROMPT}, here is the user information: {user_info_simulated}"))
|
| 84 |
for human, ai in history:
|
|
|
|
| 121 |
time.sleep(0.05)
|
| 122 |
yield chatbot_history
|
| 123 |
|
| 124 |
+
# This is a function to do something with the voted information (TODO: Save this info somewhere?)
|
| 125 |
+
def vote(data: gr.LikeData):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
if data.liked:
|
| 127 |
print("You upvoted this response: " + data.value)
|
| 128 |
else:
|
| 129 |
print("You downvoted this response: " + data.value)
|
| 130 |
+
with open("output.txt", "a") as text_file:
|
| 131 |
+
print(f"Disliked content: {data.value}", file=text_file)
|
| 132 |
+
|
|
|
|
| 133 |
def reset_flag():
|
| 134 |
global is_first_run
|
| 135 |
is_first_run = True
|
| 136 |
|
| 137 |
# The Gradio App interface
|
| 138 |
with gr.Blocks() as demo:
|
| 139 |
+
gr.Markdown("""<h1><center>TROUBLESHOOTING Bot by CIONIC</center></h1>""")
|
| 140 |
+
gr.Markdown("""<p><center>To start a new case, press clear.</center></p>""")
|
| 141 |
+
chatbot = gr.Chatbot("CIONIC")
|
| 142 |
+
textbox = gr.Textbox("Please describe your issue in 1-3 sentences")
|
| 143 |
clear_button = gr.ClearButton(components=[chatbot])
|
| 144 |
clear_button.click(reset_flag, None, None)
|
| 145 |
+
|
| 146 |
# Chain user and bot functions with `.then()`
|
| 147 |
textbox.submit(user, [textbox, chatbot], [textbox, chatbot], queue=False).then(
|
| 148 |
bot, chatbot, chatbot,
|
| 149 |
)
|
| 150 |
+
# chatbot.like(vote, None, None)
|
| 151 |
|
| 152 |
# Enable queuing
|
| 153 |
demo.queue()
|
| 154 |
+
demo.launch(debug=True)
|
|
|
|
| 155 |
|