Omnibus commited on
Commit
f4c1a18
1 Parent(s): dd00039

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -157
app.py CHANGED
@@ -48,7 +48,7 @@ def parse_action(string: str):
48
 
49
  VERBOSE = True
50
  MAX_HISTORY = 100
51
- MAX_DATA = 1000
52
 
53
  def format_prompt(message, history):
54
  prompt = "<s>"
@@ -58,61 +58,6 @@ def format_prompt(message, history):
58
  prompt += f"[INST] {message} [/INST]"
59
  return prompt
60
 
61
- def call_search(purpose, task, history, action_input):
62
- return_list=[]
63
- print (action_input)
64
- #if action_input in query.tasks:
65
- print ("trying")
66
- try:
67
- if action_input != "" and action_input != None:
68
- action_input.strip('""')
69
- #model_list = api.list_models(filter=f"{action_input}",sort="last_modified",limit=1000,direction=-1)
70
- #model_list = api.list_models(filter=f"{action_input}",limit=1000)
71
- model_list = api.list_models(filter=f"{action_input}")
72
- this_obj = list(model_list)
73
- print(f'THIS_OBJ :: {this_obj[0]}')
74
- for i,eb in enumerate(this_obj):
75
- #return_list.append(this_obj[i].id)
76
- return_list.append({"id":this_obj[i].id,
77
- "author":this_obj[i].author,
78
- "created_at":this_obj[i].created_at,
79
- "last_modified":this_obj[i].last_modified,
80
- "private":this_obj[i].private,
81
- "gated":this_obj[i].gated,
82
- "disabled":this_obj[i].disabled,
83
- "downloads":this_obj[i].downloads,
84
- "likes":this_obj[i].likes,
85
- "library_name":this_obj[i].library_name,
86
- "tags":this_obj[i].tags,
87
- "pipeline_tag":this_obj[i].pipeline_tag,
88
- })
89
- #print (return_list)
90
- c=0
91
- rl = len(return_list)
92
- print(rl)
93
- for i in str(return_list):
94
- if i == " " or i==",":
95
- c +=1
96
-
97
- print (c)
98
- if rl > MAX_DATA:
99
- print("compressing...")
100
- return_list = compress_data(rl,purpose,task,return_list)
101
- history = "observation: the search results are:\n {}\n".format(return_list)
102
- return "MAIN", None, history, task
103
- else:
104
- history = "observation: I need to trigger a search using the following syntax:\naction: SEARCH action_input=URL\n"
105
- return "UPDATE-TASK", None, history, task
106
- except Exception as e:
107
- print (e)
108
- history = "observation: I need to trigger a search using the following syntax:\naction: SEARCH action_input=URL\n"
109
- return "UPDATE-TASK", None, history, task
110
-
111
- #else:
112
- # history = "observation: The search query I used did not return a valid response"
113
-
114
- return "MAIN", None, history, task
115
-
116
 
117
  def run_gpt(
118
  prompt_template,
@@ -143,7 +88,7 @@ def run_gpt(
143
 
144
 
145
  #formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
146
- #formatted_prompt = format_prompt(f'{content}', history)
147
 
148
  stream = client.text_generation(content, **generate_kwargs, stream=True, details=True, return_full_text=False)
149
  resp = ""
@@ -155,7 +100,7 @@ def run_gpt(
155
  print(LOG_RESPONSE.format(resp))
156
  return resp
157
 
158
- def compress_data(c,purpose, task, history):
159
  seed=random.randint(1,1000000000)
160
 
161
  print (c)
@@ -180,15 +125,15 @@ def compress_data(c,purpose, task, history):
180
  hist = history[s:e]
181
 
182
  resp = run_gpt(
183
- COMPRESS_DATA_PROMPT_SMALL,
184
  stop_tokens=["observation:", "task:", "action:", "thought:"],
185
- max_tokens=2048,
186
  seed=seed,
187
  purpose=purpose,
188
  task=task,
189
  knowledge=new_history,
190
  history=hist,
191
- )
192
  new_history = resp
193
  print (resp)
194
  out+=resp
@@ -198,15 +143,16 @@ def compress_data(c,purpose, task, history):
198
  resp = run_gpt(
199
  COMPRESS_DATA_PROMPT,
200
  stop_tokens=["observation:", "task:", "action:", "thought:"],
201
- max_tokens=1024,
202
  seed=seed,
203
  purpose=purpose,
204
  task=task,
205
  knowledge=new_history,
206
- history="All data has been recieved.",
207
- )'''
 
208
  print ("final" + resp)
209
- history = "observation: {}\n".format(resp)
210
  return history
211
 
212
 
@@ -226,41 +172,40 @@ def compress_history(purpose, task, history):
226
  return history
227
 
228
 
229
- def call_main(purpose, task, history, action_input):
230
  resp = run_gpt(
231
  FINDER,
232
- stop_tokens=["observation:", "task:", "action:"],
233
- max_tokens=512,
234
  seed=random.randint(1,1000000000),
235
  purpose=purpose,
236
  task=task,
237
  history=history,
238
  )
239
  lines = resp.strip().strip("\n").split("\n")
 
240
  for line in lines:
241
  if line == "":
242
  continue
243
  if line.startswith("thought: "):
244
  history += "{}\n".format(line)
245
- if line.startswith("action: COMPLETE"):
246
- print("COMPLETE called")
247
- return "COMPLETE", None, history, task
248
- if line.startswith("action:"):
249
  action_name, action_input = parse_action(line)
250
  print(f'ACTION::{action_name} -- INPUT :: {action_input}')
251
- history += "{}\n".format(line)
252
- return action_name, action_input, history, task
253
  else:
254
-
255
- history += "{}\n".format(line)
256
  #assert False, "unknown action: {}".format(line)
257
  #return "UPDATE-TASK", None, history, task
258
  if "VERBOSE":
259
  print(history)
260
- return "MAIN", None, history, task
261
 
262
 
263
- def call_set_task(purpose, task, history, action_input):
264
  task = run_gpt(
265
  TASK_PROMPT,
266
  stop_tokens=[],
@@ -271,7 +216,7 @@ def call_set_task(purpose, task, history, action_input):
271
  history=history,
272
  ).strip("\n")
273
  history += "observation: task has been updated to: {}\n".format(task)
274
- return "MAIN", None, history, task
275
 
276
 
277
 
@@ -282,7 +227,7 @@ def search_all(url):
282
 
283
 
284
 
285
- def find_all(purpose,task,history, url):
286
  return_list=[]
287
  print (url)
288
  #if action_input in query.tasks:
@@ -299,89 +244,59 @@ def find_all(purpose,task,history, url):
299
  # get attributes:
300
  print(soup.title.name)
301
  # get values:
302
- print(soup.title.string)
303
  # beginning navigation:
304
- print(soup.title.parent.name)
305
  #rawp.append([tag.name for tag in soup.find_all()] )
306
- print([tag.name for tag in soup.find_all()])
307
  rawp=(f'RAW TEXT RETURNED: {soup.text}')
 
 
308
  out.append(rawp)
 
309
  q=("a","p","span","content","article")
310
- for p in soup.find_all(q):
311
- out.append([{q:p.string,"parent":p.parent.name,"previous":[b for b in p.previous],"first-child":[b.name for b in p.children],"content":p}])
 
 
 
312
  c=0
313
  out = str(out)
314
  rl = len(out)
 
315
  print(f'rl:: {rl}')
316
  #for ea in out:
317
  for i in str(out):
318
- if i == " " or i=="," or i=="\n":
319
  c +=1
320
  print (f'c:: {c}')
321
- if rl > MAX_DATA:
322
  print("compressing...")
323
- rawp = compress_data(c,purpose,task,out)
 
 
 
 
324
  print (rawp)
325
  print (f'out:: {out}')
326
- history += "observation: the search results are:\n {}\n".format(out)
327
- task = "complete?"
328
- return "MAIN", None, history, task
329
  else:
330
- history += "observation: I need to trigger a search using the following syntax:\naction: SCRAPE_WEBSITE action_input=URL\n"
331
- return "MAIN", None, history, task
332
  except Exception as e:
333
  print (e)
334
  history += "observation: I need to trigger a search using the following syntax:\naction: SCRAPE_WEBSITE action_input=URL\n"
335
- return "MAIN", None, history, task
336
 
337
  #else:
338
  # history = "observation: The search query I used did not return a valid response"
339
 
340
- return "MAIN", None, history, task
 
341
 
342
 
343
- def find_it(url,q=None,num=None):
344
- out = []
345
- out_l = []
346
- z=""
347
- source = urllib.request.urlopen(url).read()
348
- soup = bs4.BeautifulSoup(source,'lxml')
349
-
350
- for p in soup.find_all(f'{q}'):
351
- if num != "":
352
- z=p.get(f'{num}')
353
-
354
- try:
355
- test = soup.select(f'{p.name}:first-child')
356
-
357
- #print(p.findChildren())
358
- except Exception as e:
359
- print (e)
360
- #out.append(p)
361
- out.append([{q:p.string,"additional":z,"parent":p.parent.name,"previous":[b for b in p.previous],"first-child":[b.name for b in p.children],"content":p}])
362
- if p.string !=None:
363
- out_l.append(p.string)
364
- else:
365
- out_l.append(z)
366
- #out.append(p.parent.name)
367
- print(dir(p))
368
- print(p.parent.name)
369
- for url in soup.find_all('a'):
370
- print(url.get('href'))
371
-
372
- #print(soup.get_text())
373
- return out,out_l
374
-
375
- def find_it2(url):
376
- response = requests.get(url,a1=None,q2=None,q3=None)
377
- try:
378
- response.raise_for_status()
379
- soup = BeautifulSoup(response.content, 'lxml')
380
- out = 'URL Links:\n'.join([p.text for p in soup.find_all('a')])
381
- return out
382
- except Exception as e:
383
- print (e)
384
- return e
385
  #################################
386
 
387
  NAME_TO_FUNC = {
@@ -392,11 +307,11 @@ NAME_TO_FUNC = {
392
  }
393
 
394
 
395
- def run_action(purpose, task, history, action_name, action_input):
396
- if action_name == "COMPLETE":
397
  print("Complete - Exiting")
398
  #exit(0)
399
- return "COMPLETE", None, history, task
400
 
401
  # compress the history when it is long
402
  if len(history.split("\n")) > MAX_HISTORY:
@@ -408,18 +323,21 @@ def run_action(purpose, task, history, action_name, action_input):
408
  assert action_name in NAME_TO_FUNC
409
 
410
  print(f"RUN: {action_name} ACTION_INPUT: {action_input}")
411
- return NAME_TO_FUNC[action_name](purpose, task, history, action_input)
412
  else:
413
- history += "observation: The TOOL I tried to use returned an error, I need to select a tool from: (UPDATE-TASK, SEARCH_ENGINE, WEBSITE_SCRAPE, COMPLETE)\n"
414
 
415
- return "MAIN", None, history, task
416
 
417
  def run(purpose,history):
418
  task=None
419
- history = ""
420
- #if not history:
421
- # history = []
422
- action_name = "SEARCH_ENGINE" if task is None else "MAIN"
 
 
 
423
  action_input = None
424
  while True:
425
  print("")
@@ -431,36 +349,38 @@ def run(purpose,history):
431
  #print(history)
432
  print("---")
433
 
434
- action_name, action_input, history, task = run_action(
435
  purpose,
436
  task,
437
  history,
438
  action_name,
439
  action_input,
 
440
  )
441
- yield history
442
  if action_name == "COMPLETE":
443
- return history
 
444
 
445
 
446
 
447
 
448
  examples =[
 
449
  "what are todays breaking news stories?",
450
- "find the most popular model that I can use to generate an image by providing a text prompt",
451
- "return the top 10 models that I can use to identify objects in images",
452
- "which models have the most likes from each category?"
453
  ]
454
 
455
 
456
- app = gr.ChatInterface(
457
  fn=run,
458
  chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
459
  title="Mixtral 46.7B Powered <br> Search",
460
  examples=examples,
461
  concurrency_limit=20,
462
- )
463
- app.launch()
464
  '''
465
  with gr.Blocks() as app:
466
  with gr.Row():
@@ -483,4 +403,4 @@ with gr.Blocks() as app:
483
 
484
  app.launch()
485
 
486
- '''
 
48
 
49
  VERBOSE = True
50
  MAX_HISTORY = 100
51
+ MAX_DATA = 20000
52
 
53
  def format_prompt(message, history):
54
  prompt = "<s>"
 
58
  prompt += f"[INST] {message} [/INST]"
59
  return prompt
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  def run_gpt(
63
  prompt_template,
 
88
 
89
 
90
  #formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
91
+ #formatted_prompt = format_prompt(f'{content}', **prompt_kwargs['history'])
92
 
93
  stream = client.text_generation(content, **generate_kwargs, stream=True, details=True, return_full_text=False)
94
  resp = ""
 
100
  print(LOG_RESPONSE.format(resp))
101
  return resp
102
 
103
+ def compress_data(c,purpose, task, history, result):
104
  seed=random.randint(1,1000000000)
105
 
106
  print (c)
 
125
  hist = history[s:e]
126
 
127
  resp = run_gpt(
128
+ COMPRESS_DATA_PROMPT,
129
  stop_tokens=["observation:", "task:", "action:", "thought:"],
130
+ max_tokens=1024,
131
  seed=seed,
132
  purpose=purpose,
133
  task=task,
134
  knowledge=new_history,
135
  history=hist,
136
+ ).strip('\n')
137
  new_history = resp
138
  print (resp)
139
  out+=resp
 
143
  resp = run_gpt(
144
  COMPRESS_DATA_PROMPT,
145
  stop_tokens=["observation:", "task:", "action:", "thought:"],
146
+ max_tokens=2048,
147
  seed=seed,
148
  purpose=purpose,
149
  task=task,
150
  knowledge=new_history,
151
+ history=result,
152
+ )
153
+ '''
154
  print ("final" + resp)
155
+ history = "result: {}\n".format(resp)
156
  return history
157
 
158
 
 
172
  return history
173
 
174
 
175
+ def call_main(purpose, task, history, action_input, result):
176
  resp = run_gpt(
177
  FINDER,
178
+ stop_tokens=["observation:", "task:"],
179
+ max_tokens=2096,
180
  seed=random.randint(1,1000000000),
181
  purpose=purpose,
182
  task=task,
183
  history=history,
184
  )
185
  lines = resp.strip().strip("\n").split("\n")
186
+ #history=""
187
  for line in lines:
188
  if line == "":
189
  continue
190
  if line.startswith("thought: "):
191
  history += "{}\n".format(line)
192
+
193
+ if line.startswith("action: "):
 
 
194
  action_name, action_input = parse_action(line)
195
  print(f'ACTION::{action_name} -- INPUT :: {action_input}')
196
+ #history += "{}\n".format(line)
197
+ return action_name, action_input, history, task, result
198
  else:
199
+ pass
200
+ #history += "{}\n".format(line)
201
  #assert False, "unknown action: {}".format(line)
202
  #return "UPDATE-TASK", None, history, task
203
  if "VERBOSE":
204
  print(history)
205
+ return "MAIN", None, history, task, result
206
 
207
 
208
+ def call_set_task(purpose, task, history, action_input, result):
209
  task = run_gpt(
210
  TASK_PROMPT,
211
  stop_tokens=[],
 
216
  history=history,
217
  ).strip("\n")
218
  history += "observation: task has been updated to: {}\n".format(task)
219
+ return "MAIN", None, history, task, result
220
 
221
 
222
 
 
227
 
228
 
229
 
230
+ def find_all(purpose,task,history, url, result):
231
  return_list=[]
232
  print (url)
233
  #if action_input in query.tasks:
 
244
  # get attributes:
245
  print(soup.title.name)
246
  # get values:
247
+ #print(soup.title.string)
248
  # beginning navigation:
249
+ #print(soup.title.parent.name)
250
  #rawp.append([tag.name for tag in soup.find_all()] )
251
+ #print([tag.name for tag in soup.find_all()])
252
  rawp=(f'RAW TEXT RETURNED: {soup.text}')
253
+ cnt=0
254
+ cnt+=len(rawp)
255
  out.append(rawp)
256
+ out.append("HTML fragments: ")
257
  q=("a","p","span","content","article")
258
+ for p in soup.find_all("a"):
259
+ #cnt+=len(p.string)
260
+ #cnt+=len(p)
261
+
262
+ out.append([{"LINK TITLE":p.get('title'),"URL":p.get('href'),"STRING":p.string}])
263
  c=0
264
  out = str(out)
265
  rl = len(out)
266
+ #print (cnt)
267
  print(f'rl:: {rl}')
268
  #for ea in out:
269
  for i in str(out):
270
+ if i == " " or i=="," or i=="\n" or i=="/" or i=="." or i=="<":
271
  c +=1
272
  print (f'c:: {c}')
273
+ if c > MAX_HISTORY:
274
  print("compressing...")
275
+ rawp = compress_data(c,purpose,task,out,result)
276
+ else:
277
+ rawp = out
278
+ result += rawp
279
+
280
  print (rawp)
281
  print (f'out:: {out}')
282
+ history += "observation: the search results are:\n {}\n".format(rawp)
283
+ task = "compile report or complete?"
284
+ return "MAIN", None, history, task, result
285
  else:
286
+ history += "observation: An Error occured\nI need to trigger a search using the following syntax:\naction: SCRAPE_WEBSITE action_input=URL\n"
287
+ return "MAIN", None, history, task, result
288
  except Exception as e:
289
  print (e)
290
  history += "observation: I need to trigger a search using the following syntax:\naction: SCRAPE_WEBSITE action_input=URL\n"
291
+ return "MAIN", None, history, task, result
292
 
293
  #else:
294
  # history = "observation: The search query I used did not return a valid response"
295
 
296
+ return "MAIN", None, history, task, result
297
+
298
 
299
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
300
  #################################
301
 
302
  NAME_TO_FUNC = {
 
307
  }
308
 
309
 
310
+ def run_action(purpose, task, history, action_name, action_input,result):
311
+ if "COMPLETE" in action_name:
312
  print("Complete - Exiting")
313
  #exit(0)
314
+ return "COMPLETE", None, history, task, result
315
 
316
  # compress the history when it is long
317
  if len(history.split("\n")) > MAX_HISTORY:
 
323
  assert action_name in NAME_TO_FUNC
324
 
325
  print(f"RUN: {action_name} ACTION_INPUT: {action_input}")
326
+ return NAME_TO_FUNC[action_name](purpose, task, history, action_input, result)
327
  else:
328
+ history += "observation: The TOOL I tried to use returned an error, I need to select a tool from: (UPDATE-TASK, SEARCH_ENGINE, SCRAPE_WEBSITE, COMPLETE)\n"
329
 
330
+ return "MAIN", None, history, task, result
331
 
332
  def run(purpose,history):
333
  task=None
334
+ result=""
335
+ #history = ""
336
+ if not history:
337
+ history = ""
338
+ else:
339
+ history=str(history)
340
+ action_name = "MAIN"
341
  action_input = None
342
  while True:
343
  print("")
 
349
  #print(history)
350
  print("---")
351
 
352
+ action_name, action_input, history, task, result = run_action(
353
  purpose,
354
  task,
355
  history,
356
  action_name,
357
  action_input,
358
+ result
359
  )
360
+ yield result
361
  if action_name == "COMPLETE":
362
+ break
363
+ return result
364
 
365
 
366
 
367
 
368
  examples =[
369
+ "What is the current weather in Florida?",
370
  "what are todays breaking news stories?",
371
+ "Find the best deals on flippers for scuba diving",
372
+ "Teach me to fly a helicopter"
 
373
  ]
374
 
375
 
376
+ gr.ChatInterface(
377
  fn=run,
378
  chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
379
  title="Mixtral 46.7B Powered <br> Search",
380
  examples=examples,
381
  concurrency_limit=20,
382
+ css="height:600px;"
383
+ ).launch()
384
  '''
385
  with gr.Blocks() as app:
386
  with gr.Row():
 
403
 
404
  app.launch()
405
 
406
+ '''