runsdata commited on
Commit
38352d8
·
1 Parent(s): e8e4249

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -45
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 str(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(f"Context is as follows: {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,64 +121,35 @@ def bot(chatbot_history):
121
  time.sleep(0.05)
122
  yield chatbot_history
123
 
124
- def log_to_csv(question, answer):
125
- """Append a line to a CSV. Create a new file if needed."""
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
- # Find Q/A pair that was disliked
158
- question, answer = get_voted_qa_pair(history, data.value)
159
- log_to_csv(question, answer)
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>Troubleshooting Bot by CIONIC</center></h1>""")
168
- gr.Markdown("""<p><center>Create a new case by clicking the "Clear" button.</center></p>""")
169
- chatbot = gr.Chatbot(label="DylanAI")
170
- textbox = gr.Textbox(label="Type your question here")
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, chatbot, None)
179
 
180
  # Enable queuing
181
  demo.queue()
182
- demo.launch(debug=True, share=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