Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -155,7 +155,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)
|
@@ -194,17 +194,17 @@ def compress_data(c,purpose, task, history):
|
|
194 |
out+=resp
|
195 |
e=e+chunk
|
196 |
s=s+chunk
|
197 |
-
|
198 |
resp = run_gpt(
|
199 |
COMPRESS_DATA_PROMPT,
|
200 |
stop_tokens=["observation:", "task:", "action:", "thought:"],
|
201 |
-
max_tokens=
|
202 |
seed=seed,
|
203 |
purpose=purpose,
|
204 |
task=task,
|
205 |
knowledge=new_history,
|
206 |
-
history=
|
207 |
-
)
|
208 |
print ("final" + resp)
|
209 |
history = "result: {}\n".format(resp)
|
210 |
return history
|
@@ -226,7 +226,7 @@ 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:"],
|
@@ -256,10 +256,10 @@ def call_main(purpose, task, history, action_input):
|
|
256 |
#return "UPDATE-TASK", None, history, task
|
257 |
if "VERBOSE":
|
258 |
print(history)
|
259 |
-
return "MAIN", None, history, task
|
260 |
|
261 |
|
262 |
-
def call_set_task(purpose, task, history, action_input):
|
263 |
task = run_gpt(
|
264 |
TASK_PROMPT,
|
265 |
stop_tokens=[],
|
@@ -270,7 +270,7 @@ def call_set_task(purpose, task, history, action_input):
|
|
270 |
history=history,
|
271 |
).strip("\n")
|
272 |
history += "observation: task has been updated to: {}\n".format(task)
|
273 |
-
return "MAIN", None, history, task
|
274 |
|
275 |
|
276 |
|
@@ -281,7 +281,7 @@ def search_all(url):
|
|
281 |
|
282 |
|
283 |
|
284 |
-
def find_all(purpose,task,history, url):
|
285 |
return_list=[]
|
286 |
print (url)
|
287 |
#if action_input in query.tasks:
|
@@ -326,71 +326,31 @@ def find_all(purpose,task,history, url):
|
|
326 |
print (f'c:: {c}')
|
327 |
if c > MAX_HISTORY:
|
328 |
print("compressing...")
|
329 |
-
rawp = compress_data(c,purpose,task,out)
|
330 |
-
result = rawp
|
331 |
else:
|
332 |
rawp = out
|
|
|
|
|
333 |
print (rawp)
|
334 |
print (f'out:: {out}')
|
335 |
-
history
|
336 |
task = "compile report"
|
337 |
-
return "MAIN", None, history, task
|
338 |
else:
|
339 |
history += "observation: An Error occured\nI need to trigger a search using the following syntax:\naction: SCRAPE_WEBSITE action_input=URL\n"
|
340 |
-
return "MAIN", None, history, task
|
341 |
except Exception as e:
|
342 |
print (e)
|
343 |
history += "observation: I need to trigger a search using the following syntax:\naction: SCRAPE_WEBSITE action_input=URL\n"
|
344 |
-
return "MAIN", None, history, task
|
345 |
|
346 |
#else:
|
347 |
# history = "observation: The search query I used did not return a valid response"
|
348 |
|
349 |
-
return "MAIN", None, history, task
|
|
|
350 |
|
351 |
|
352 |
-
def find_it(url,q=None,num=None):
|
353 |
-
out = []
|
354 |
-
out_l = []
|
355 |
-
z=""
|
356 |
-
source = urllib.request.urlopen(url).read()
|
357 |
-
soup = bs4.BeautifulSoup(source,'lxml')
|
358 |
-
|
359 |
-
for p in soup.find_all(f'{q}'):
|
360 |
-
if num != "":
|
361 |
-
z=p.get(f'{num}')
|
362 |
-
|
363 |
-
try:
|
364 |
-
test = soup.select(f'{p.name}:first-child')
|
365 |
-
|
366 |
-
#print(p.findChildren())
|
367 |
-
except Exception as e:
|
368 |
-
print (e)
|
369 |
-
#out.append(p)
|
370 |
-
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}])
|
371 |
-
if p.string !=None:
|
372 |
-
out_l.append(p.string)
|
373 |
-
else:
|
374 |
-
out_l.append(z)
|
375 |
-
#out.append(p.parent.name)
|
376 |
-
print(dir(p))
|
377 |
-
print(p.parent.name)
|
378 |
-
for url in soup.find_all('a'):
|
379 |
-
print(url.get('href'))
|
380 |
-
|
381 |
-
#print(soup.get_text())
|
382 |
-
return out,out_l
|
383 |
-
|
384 |
-
def find_it2(url):
|
385 |
-
response = requests.get(url,a1=None,q2=None,q3=None)
|
386 |
-
try:
|
387 |
-
response.raise_for_status()
|
388 |
-
soup = BeautifulSoup(response.content, 'lxml')
|
389 |
-
out = 'URL Links:\n'.join([p.text for p in soup.find_all('a')])
|
390 |
-
return out
|
391 |
-
except Exception as e:
|
392 |
-
print (e)
|
393 |
-
return e
|
394 |
#################################
|
395 |
|
396 |
NAME_TO_FUNC = {
|
@@ -401,7 +361,7 @@ NAME_TO_FUNC = {
|
|
401 |
}
|
402 |
|
403 |
|
404 |
-
def run_action(purpose, task, history, action_name, action_input):
|
405 |
if action_name == "COMPLETE":
|
406 |
print("Complete - Exiting")
|
407 |
#exit(0)
|
@@ -417,14 +377,15 @@ def run_action(purpose, task, history, action_name, action_input):
|
|
417 |
assert action_name in NAME_TO_FUNC
|
418 |
|
419 |
print(f"RUN: {action_name} ACTION_INPUT: {action_input}")
|
420 |
-
return NAME_TO_FUNC[action_name](purpose, task, history, action_input)
|
421 |
else:
|
422 |
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"
|
423 |
|
424 |
-
return "MAIN", None, history, task
|
425 |
|
426 |
def run(purpose,history):
|
427 |
task=None
|
|
|
428 |
#history = ""
|
429 |
if not history:
|
430 |
history = ""
|
@@ -442,16 +403,17 @@ def run(purpose,history):
|
|
442 |
#print(history)
|
443 |
print("---")
|
444 |
|
445 |
-
action_name, action_input, history, task = run_action(
|
446 |
purpose,
|
447 |
task,
|
448 |
history,
|
449 |
action_name,
|
450 |
action_input,
|
|
|
451 |
)
|
452 |
-
yield
|
453 |
if action_name == "COMPLETE":
|
454 |
-
return
|
455 |
|
456 |
|
457 |
|
|
|
155 |
print(LOG_RESPONSE.format(resp))
|
156 |
return resp
|
157 |
|
158 |
+
def compress_data(c,purpose, task, history, result):
|
159 |
seed=random.randint(1,1000000000)
|
160 |
|
161 |
print (c)
|
|
|
194 |
out+=resp
|
195 |
e=e+chunk
|
196 |
s=s+chunk
|
197 |
+
|
198 |
resp = run_gpt(
|
199 |
COMPRESS_DATA_PROMPT,
|
200 |
stop_tokens=["observation:", "task:", "action:", "thought:"],
|
201 |
+
max_tokens=2048,
|
202 |
seed=seed,
|
203 |
purpose=purpose,
|
204 |
task=task,
|
205 |
knowledge=new_history,
|
206 |
+
history=result,
|
207 |
+
)
|
208 |
print ("final" + resp)
|
209 |
history = "result: {}\n".format(resp)
|
210 |
return history
|
|
|
226 |
return history
|
227 |
|
228 |
|
229 |
+
def call_main(purpose, task, history, action_input, result):
|
230 |
resp = run_gpt(
|
231 |
FINDER,
|
232 |
stop_tokens=["observation:", "task:"],
|
|
|
256 |
#return "UPDATE-TASK", None, history, task
|
257 |
if "VERBOSE":
|
258 |
print(history)
|
259 |
+
return "MAIN", None, history, task, result
|
260 |
|
261 |
|
262 |
+
def call_set_task(purpose, task, history, action_input, result):
|
263 |
task = run_gpt(
|
264 |
TASK_PROMPT,
|
265 |
stop_tokens=[],
|
|
|
270 |
history=history,
|
271 |
).strip("\n")
|
272 |
history += "observation: task has been updated to: {}\n".format(task)
|
273 |
+
return "MAIN", None, history, task, result
|
274 |
|
275 |
|
276 |
|
|
|
281 |
|
282 |
|
283 |
|
284 |
+
def find_all(purpose,task,history, url, result):
|
285 |
return_list=[]
|
286 |
print (url)
|
287 |
#if action_input in query.tasks:
|
|
|
326 |
print (f'c:: {c}')
|
327 |
if c > MAX_HISTORY:
|
328 |
print("compressing...")
|
329 |
+
rawp = compress_data(c,purpose,task,out,result)
|
|
|
330 |
else:
|
331 |
rawp = out
|
332 |
+
result += rawp
|
333 |
+
|
334 |
print (rawp)
|
335 |
print (f'out:: {out}')
|
336 |
+
history += "observation: the search results are:\n {}\n".format(rawp)
|
337 |
task = "compile report"
|
338 |
+
return "MAIN", None, history, task, result
|
339 |
else:
|
340 |
history += "observation: An Error occured\nI need to trigger a search using the following syntax:\naction: SCRAPE_WEBSITE action_input=URL\n"
|
341 |
+
return "MAIN", None, history, task, result
|
342 |
except Exception as e:
|
343 |
print (e)
|
344 |
history += "observation: I need to trigger a search using the following syntax:\naction: SCRAPE_WEBSITE action_input=URL\n"
|
345 |
+
return "MAIN", None, history, task, result
|
346 |
|
347 |
#else:
|
348 |
# history = "observation: The search query I used did not return a valid response"
|
349 |
|
350 |
+
return "MAIN", None, history, task, result
|
351 |
+
|
352 |
|
353 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
354 |
#################################
|
355 |
|
356 |
NAME_TO_FUNC = {
|
|
|
361 |
}
|
362 |
|
363 |
|
364 |
+
def run_action(purpose, task, history, action_name, action_input,result):
|
365 |
if action_name == "COMPLETE":
|
366 |
print("Complete - Exiting")
|
367 |
#exit(0)
|
|
|
377 |
assert action_name in NAME_TO_FUNC
|
378 |
|
379 |
print(f"RUN: {action_name} ACTION_INPUT: {action_input}")
|
380 |
+
return NAME_TO_FUNC[action_name](purpose, task, history, action_input, result)
|
381 |
else:
|
382 |
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"
|
383 |
|
384 |
+
return "MAIN", None, history, task, result
|
385 |
|
386 |
def run(purpose,history):
|
387 |
task=None
|
388 |
+
result=""
|
389 |
#history = ""
|
390 |
if not history:
|
391 |
history = ""
|
|
|
403 |
#print(history)
|
404 |
print("---")
|
405 |
|
406 |
+
action_name, action_input, history, task, result = run_action(
|
407 |
purpose,
|
408 |
task,
|
409 |
history,
|
410 |
action_name,
|
411 |
action_input,
|
412 |
+
result
|
413 |
)
|
414 |
+
yield result
|
415 |
if action_name == "COMPLETE":
|
416 |
+
return result
|
417 |
|
418 |
|
419 |
|