awacke1 commited on
Commit
732dfda
·
1 Parent(s): ada9267

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -82
app.py CHANGED
@@ -13,7 +13,8 @@ import os
13
  import csv
14
  from gradio import inputs, outputs
15
  import huggingface_hub
16
- from huggingface_hub import Repository, hf_hub_download, upload_file
 
17
  from datetime import datetime
18
  import fastapi
19
  from typing import List, Dict
@@ -58,6 +59,87 @@ Chatbot With persistent memory dataset allowing multiagent system AI to access a
58
  - [Language Models are Few-Shot Learners](https://paperswithcode.com/dataset/openwebtext) by Brown et al.
59
  """
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  #ChatGPT predict
62
  def predict(inputs, top_p, temperature, chat_counter, chatbot=[], history=[]): #repetition_penalty, top_k
63
 
@@ -141,6 +223,18 @@ def predict(inputs, top_p, temperature, chat_counter, chatbot=[], history=[]):
141
  history[-1] = partial_words
142
  chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ] # convert to tuples of list
143
  token_counter+=1
 
 
 
 
 
 
 
 
 
 
 
 
144
  yield chat, history, chat_counter # resembles {chatbot: chat, state: history}
145
 
146
  def take_last_tokens(inputs, note_history, history):
@@ -160,85 +254,6 @@ def add_note_to_history(note, note_history):# good example of non async since we
160
  def reset_textbox():
161
  return gr.update(value='')
162
 
163
- #Chatbot2 Save Results
164
- def SaveResult(text, outputfileName):
165
- basedir = os.path.dirname(__file__)
166
- savePath = outputfileName
167
- print("Saving: " + text + " to " + savePath)
168
- from os.path import exists
169
- file_exists = exists(savePath)
170
- if file_exists:
171
- with open(outputfileName, "a") as f: #append
172
- f.write(str(text.replace("\n"," ")))
173
- f.write('\n')
174
- else:
175
- with open(outputfileName, "w") as f: #write
176
- f.write(str("time, message, text\n")) # one time only to get column headers for CSV file
177
- f.write(str(text.replace("\n"," ")))
178
- f.write('\n')
179
- return
180
-
181
- #Chatbot2 Store Message
182
- def store_message(name: str, message: str, outputfileName: str):
183
- basedir = os.path.dirname(__file__)
184
- savePath = outputfileName
185
-
186
- # if file doesnt exist, create it with labels
187
- from os.path import exists
188
- file_exists = exists(savePath)
189
-
190
- if (file_exists==False):
191
- with open(savePath, "w") as f: #write
192
- f.write(str("time, message, text\n")) # one time only to get column headers for CSV file
193
- if name and message:
194
- writer = csv.DictWriter(f, fieldnames=["time", "message", "name"])
195
- writer.writerow(
196
- {"time": str(datetime.now()), "message": message.strip(), "name": name.strip() }
197
- )
198
- df = pd.read_csv(savePath)
199
- df = df.sort_values(df.columns[0],ascending=False)
200
- else:
201
- if name and message:
202
- with open(savePath, "a") as csvfile:
203
- writer = csv.DictWriter(csvfile, fieldnames=[ "time", "message", "name", ])
204
- writer.writerow(
205
- {"time": str(datetime.now()), "message": message.strip(), "name": name.strip() }
206
- )
207
- df = pd.read_csv(savePath)
208
- df = df.sort_values(df.columns[0],ascending=False)
209
- return df
210
-
211
- #Chatbot2 get base directory of saves
212
- def get_base(filename):
213
- basedir = os.path.dirname(__file__)
214
- print(basedir)
215
- loadPath = basedir + filename
216
- print(loadPath)
217
- return loadPath
218
-
219
- #Chatbot2 - History
220
- def chat(message, history):
221
- history = history or []
222
- if history:
223
- history_useful = ['</s> <s>'.join([str(a[0])+'</s> <s>'+str(a[1]) for a in history])]
224
- else:
225
- history_useful = []
226
- history_useful = add_note_to_history(message, history_useful)
227
- inputs = tokenizer(history_useful, return_tensors="pt")
228
- inputs, history_useful, history = take_last_tokens(inputs, history_useful, history)
229
- reply_ids = model.generate(**inputs)
230
- response = tokenizer.batch_decode(reply_ids, skip_special_tokens=True)[0]
231
- history_useful = add_note_to_history(response, history_useful)
232
- list_history = history_useful[0].split('</s> <s>')
233
- history.append((list_history[-2], list_history[-1]))
234
- df=pd.DataFrame()
235
- if UseMemory:
236
- outputfileName = 'ChatbotMemory3.csv' # Test first time file create
237
- df = store_message(message, response, outputfileName) # Save to dataset
238
- basedir = get_base(outputfileName)
239
- return history, df, basedir
240
-
241
-
242
  # 6. Use Gradio to pull it all together
243
  with gr.Blocks(css = """#col_container {width: 1000px; margin-left: auto; margin-right: auto;} #chatbot {height: 520px; overflow: auto;}""") as demo:
244
 
@@ -255,12 +270,12 @@ with gr.Blocks(css = """#col_container {width: 1000px; margin-left: auto; margin
255
  with gr.Row(): # inputs and buttons
256
  file = gr.File(label="File")
257
  s2 = gr.Markdown()
258
- b1.click(fn=chat, inputs=[t1, s1], outputs=[s1, df1, file])
259
 
260
 
261
  with gr.Column(elem_id = "col_container"):
262
  chatbot = gr.Chatbot(elem_id='chatbot')
263
- inputs = gr.Textbox(placeholder= "There is only one real true reward in life and this is existence or nonexistence. Everything else is a corollary.", label= "Type an input and press Enter") #t
264
  state = gr.State([])
265
  gpt = gr.Button()
266
 
 
13
  import csv
14
  from gradio import inputs, outputs
15
  import huggingface_hub
16
+ from huggingface_hub import Repository,
17
+ hub_download, upload_file
18
  from datetime import datetime
19
  import fastapi
20
  from typing import List, Dict
 
59
  - [Language Models are Few-Shot Learners](https://paperswithcode.com/dataset/openwebtext) by Brown et al.
60
  """
61
 
62
+
63
+ #Chatbot2 Save Results
64
+ def SaveResult(text, outputfileName):
65
+ basedir = os.path.dirname(__file__)
66
+ savePath = outputfileName
67
+ print("Saving: " + text + " to " + savePath)
68
+ from os.path import exists
69
+ file_exists = exists(savePath)
70
+ if file_exists:
71
+ with open(outputfileName, "a") as f: #append
72
+ f.write(str(text.replace("\n"," ")))
73
+ f.write('\n')
74
+ else:
75
+ with open(outputfileName, "w") as f: #write
76
+ f.write(str("time, message, text\n")) # one time only to get column headers for CSV file
77
+ f.write(str(text.replace("\n"," ")))
78
+ f.write('\n')
79
+ return
80
+
81
+ #Chatbot2 Store Message
82
+ def store_message(name: str, message: str, outputfileName: str):
83
+ basedir = os.path.dirname(__file__)
84
+ savePath = outputfileName
85
+
86
+ # if file doesnt exist, create it with labels
87
+ from os.path import exists
88
+ file_exists = exists(savePath)
89
+
90
+ if (file_exists==False):
91
+ with open(savePath, "w") as f: #write
92
+ f.write(str("time, message, text\n")) # one time only to get column headers for CSV file
93
+ if name and message:
94
+ writer = csv.DictWriter(f, fieldnames=["time", "message", "name"])
95
+ writer.writerow(
96
+ {"time": str(datetime.now()), "message": message.strip(), "name": name.strip() }
97
+ )
98
+ df = pd.read_csv(savePath)
99
+ df = df.sort_values(df.columns[0],ascending=False)
100
+ else:
101
+ if name and message:
102
+ with open(savePath, "a") as csvfile:
103
+ writer = csv.DictWriter(csvfile, fieldnames=[ "time", "message", "name", ])
104
+ writer.writerow(
105
+ {"time": str(datetime.now()), "message": message.strip(), "name": name.strip() }
106
+ )
107
+ df = pd.read_csv(savePath)
108
+ df = df.sort_values(df.columns[0],ascending=False)
109
+ return df
110
+
111
+ #Chatbot2 get base directory of saves
112
+ def get_base(filename):
113
+ basedir = os.path.dirname(__file__)
114
+ print(basedir)
115
+ loadPath = basedir + filename
116
+ print(loadPath)
117
+ return loadPath
118
+
119
+ #Chatbot2 - History
120
+ def chat(message, history):
121
+ history = history or []
122
+ if history:
123
+ history_useful = ['</s> <s>'.join([str(a[0])+'</s> <s>'+str(a[1]) for a in history])]
124
+ else:
125
+ history_useful = []
126
+ history_useful = add_note_to_history(message, history_useful)
127
+ inputs = tokenizer(history_useful, return_tensors="pt")
128
+ inputs, history_useful, history = take_last_tokens(inputs, history_useful, history)
129
+ reply_ids = model.generate(**inputs)
130
+ response = tokenizer.batch_decode(reply_ids, skip_special_tokens=True)[0]
131
+ history_useful = add_note_to_history(response, history_useful)
132
+ list_history = history_useful[0].split('</s> <s>')
133
+ history.append((list_history[-2], list_history[-1]))
134
+ df=pd.DataFrame()
135
+ if UseMemory:
136
+ outputfileName = 'ChatbotMemory3.csv' # Test first time file create
137
+ df = store_message(message, response, outputfileName) # Save to dataset
138
+ basedir = get_base(outputfileName)
139
+ return history, df, basedir
140
+
141
+
142
+
143
  #ChatGPT predict
144
  def predict(inputs, top_p, temperature, chat_counter, chatbot=[], history=[]): #repetition_penalty, top_k
145
 
 
223
  history[-1] = partial_words
224
  chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ] # convert to tuples of list
225
  token_counter+=1
226
+
227
+
228
+
229
+ df=pd.DataFrame()
230
+ if UseMemory:
231
+ outputfileName = 'ChatbotMemory3.csv' # Test first time file create
232
+ df = store_message(inputs, chat, outputfileName) # Save to dataset
233
+ basedir = get_base(outputfileName)
234
+ #return history, df, basedir
235
+
236
+
237
+
238
  yield chat, history, chat_counter # resembles {chatbot: chat, state: history}
239
 
240
  def take_last_tokens(inputs, note_history, history):
 
254
  def reset_textbox():
255
  return gr.update(value='')
256
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
257
  # 6. Use Gradio to pull it all together
258
  with gr.Blocks(css = """#col_container {width: 1000px; margin-left: auto; margin-right: auto;} #chatbot {height: 520px; overflow: auto;}""") as demo:
259
 
 
270
  with gr.Row(): # inputs and buttons
271
  file = gr.File(label="File")
272
  s2 = gr.Markdown()
273
+ #b1.click(fn=chat, inputs=[t1, s1], outputs=[s1, df1, file])
274
 
275
 
276
  with gr.Column(elem_id = "col_container"):
277
  chatbot = gr.Chatbot(elem_id='chatbot')
278
+ inputs = gr.Textbox(placeholder= "There is only one real true reward in life and this is existence versus nonexistence. Everything else is a corollary.", label= "Type an input and press Enter") #t
279
  state = gr.State([])
280
  gpt = gr.Button()
281