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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
|
24 |
def find_it(url,q=None,num=None):
|
@@ -63,8 +422,24 @@ def find_it2(url):
|
|
63 |
except Exception as e:
|
64 |
print (e)
|
65 |
return e
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
|
|
68 |
with gr.Blocks() as app:
|
69 |
with gr.Row():
|
70 |
with gr.Column(scale=1):
|
@@ -86,3 +461,4 @@ with gr.Blocks() as app:
|
|
86 |
|
87 |
app.launch()
|
88 |
|
|
|
|
3 |
import requests
|
4 |
import bs4
|
5 |
import lxml
|
6 |
+
import os
|
7 |
+
#import subprocess
|
8 |
+
from huggingface_hub import InferenceClient,HfApi
|
9 |
+
import random
|
10 |
+
import json
|
11 |
+
#from query import tasks
|
12 |
+
import query
|
13 |
+
from prompts import (
|
14 |
+
FINDER,
|
15 |
+
COMPRESS_HISTORY_PROMPT,
|
16 |
+
COMPRESS_DATA_PROMPT,
|
17 |
+
LOG_PROMPT,
|
18 |
+
LOG_RESPONSE,
|
19 |
+
PREFIX,
|
20 |
+
TASK_PROMPT,
|
21 |
+
)
|
22 |
+
api=HfApi()
|
23 |
+
|
24 |
+
|
25 |
+
|
26 |
+
client = InferenceClient(
|
27 |
+
"mistralai/Mixtral-8x7B-Instruct-v0.1"
|
28 |
+
)
|
29 |
+
|
30 |
+
def parse_action(string: str):
|
31 |
+
assert string.startswith("action:")
|
32 |
+
idx = string.find("action_input=")
|
33 |
+
if idx == -1:
|
34 |
+
return string[8:], None
|
35 |
+
return string[8 : idx - 1], string[idx + 13 :].strip("'").strip('"')
|
36 |
+
|
37 |
+
|
38 |
+
|
39 |
+
VERBOSE = False
|
40 |
+
MAX_HISTORY = 100
|
41 |
+
MAX_DATA = 100
|
42 |
+
|
43 |
+
def format_prompt(message, history):
|
44 |
+
prompt = "<s>"
|
45 |
+
for user_prompt, bot_response in history:
|
46 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
47 |
+
prompt += f" {bot_response}</s> "
|
48 |
+
prompt += f"[INST] {message} [/INST]"
|
49 |
+
return prompt
|
50 |
+
|
51 |
+
def call_search(purpose, task, history, action_input):
|
52 |
+
return_list=[]
|
53 |
+
print (action_input)
|
54 |
+
#if action_input in query.tasks:
|
55 |
+
print ("trying")
|
56 |
+
try:
|
57 |
+
if action_input != "" and action_input != None:
|
58 |
+
action_input.strip('""')
|
59 |
+
#model_list = api.list_models(filter=f"{action_input}",sort="last_modified",limit=1000,direction=-1)
|
60 |
+
#model_list = api.list_models(filter=f"{action_input}",limit=1000)
|
61 |
+
model_list = api.list_models(filter=f"{action_input}")
|
62 |
+
this_obj = list(model_list)
|
63 |
+
print(f'THIS_OBJ :: {this_obj[0]}')
|
64 |
+
for i,eb in enumerate(this_obj):
|
65 |
+
#return_list.append(this_obj[i].id)
|
66 |
+
return_list.append({"id":this_obj[i].id,
|
67 |
+
"author":this_obj[i].author,
|
68 |
+
"created_at":this_obj[i].created_at,
|
69 |
+
"last_modified":this_obj[i].last_modified,
|
70 |
+
"private":this_obj[i].private,
|
71 |
+
"gated":this_obj[i].gated,
|
72 |
+
"disabled":this_obj[i].disabled,
|
73 |
+
"downloads":this_obj[i].downloads,
|
74 |
+
"likes":this_obj[i].likes,
|
75 |
+
"library_name":this_obj[i].library_name,
|
76 |
+
"tags":this_obj[i].tags,
|
77 |
+
"pipeline_tag":this_obj[i].pipeline_tag,
|
78 |
+
})
|
79 |
+
#print (return_list)
|
80 |
+
c=0
|
81 |
+
rl = len(return_list)
|
82 |
+
print(rl)
|
83 |
+
for i in str(return_list):
|
84 |
+
if i == " " or i==",":
|
85 |
+
c +=1
|
86 |
+
|
87 |
+
print (c)
|
88 |
+
if rl > MAX_DATA:
|
89 |
+
print("compressing...")
|
90 |
+
return_list = compress_data(rl,purpose,task,return_list)
|
91 |
+
history = "observation: the search results are:\n {}\n".format(return_list)
|
92 |
+
return "COMPLETE", None, history, task
|
93 |
+
else:
|
94 |
+
history = "observation: I need to trigger a search using the following syntax:\naction: SEARCH action_input=SEARCH_QUERY\n"
|
95 |
+
return "UPDATE-TASK", None, history, task
|
96 |
+
except Exception as e:
|
97 |
+
print (e)
|
98 |
+
history = "observation: I need to trigger a search using the following syntax:\naction: SEARCH action_input=SEARCH_QUERY\n"
|
99 |
+
return "UPDATE-TASK", None, history, task
|
100 |
+
|
101 |
+
#else:
|
102 |
+
# history = "observation: The search query I used did not return a valid response"
|
103 |
+
|
104 |
+
return "MAIN", None, history, task
|
105 |
|
106 |
+
|
107 |
+
def run_gpt(
|
108 |
+
prompt_template,
|
109 |
+
stop_tokens,
|
110 |
+
max_tokens,
|
111 |
+
seed,
|
112 |
+
purpose,
|
113 |
+
**prompt_kwargs,
|
114 |
+
):
|
115 |
+
print(seed)
|
116 |
+
generate_kwargs = dict(
|
117 |
+
temperature=0.9,
|
118 |
+
max_new_tokens=max_tokens,
|
119 |
+
top_p=0.95,
|
120 |
+
repetition_penalty=1.0,
|
121 |
+
do_sample=True,
|
122 |
+
seed=seed,
|
123 |
+
)
|
124 |
+
|
125 |
+
content = PREFIX.format(
|
126 |
+
purpose=purpose,
|
127 |
+
) + prompt_template.format(**prompt_kwargs)
|
128 |
+
if VERBOSE:
|
129 |
+
print(LOG_PROMPT.format(content))
|
130 |
+
|
131 |
+
|
132 |
+
#formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
|
133 |
+
#formatted_prompt = format_prompt(f'{content}', history)
|
134 |
+
|
135 |
+
stream = client.text_generation(content, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
136 |
+
resp = ""
|
137 |
+
for response in stream:
|
138 |
+
resp += response.token.text
|
139 |
+
#yield resp
|
140 |
+
|
141 |
+
if VERBOSE:
|
142 |
+
print(LOG_RESPONSE.format(resp))
|
143 |
+
return resp
|
144 |
+
|
145 |
+
def compress_data(c,purpose, task, history):
|
146 |
+
seed=random.randint(1,1000000000)
|
147 |
+
|
148 |
+
print (c)
|
149 |
+
#tot=len(purpose)
|
150 |
+
#print(tot)
|
151 |
+
divr=int(c)/MAX_DATA
|
152 |
+
divi=int(divr)+1 if divr != int(divr) else int(divr)
|
153 |
+
chunk = int(int(c)/divr)
|
154 |
+
print(f'chunk:: {chunk}')
|
155 |
+
print(f'divr:: {divr}')
|
156 |
+
print (f'divi:: {divi}')
|
157 |
+
out = []
|
158 |
+
#out=""
|
159 |
+
s=0
|
160 |
+
e=chunk
|
161 |
+
print(f'e:: {e}')
|
162 |
+
new_history=""
|
163 |
+
task = f'Compile this data to fulfill the task: {task}, and complete the purpose: {purpose}\n'
|
164 |
+
for z in range(divi):
|
165 |
+
print(f's:e :: {s}:{e}')
|
166 |
+
|
167 |
+
hist = history[s:e]
|
168 |
+
|
169 |
+
resp = run_gpt(
|
170 |
+
COMPRESS_DATA_PROMPT_SMALL,
|
171 |
+
stop_tokens=["observation:", "task:", "action:", "thought:"],
|
172 |
+
max_tokens=2048,
|
173 |
+
seed=seed,
|
174 |
+
purpose=purpose,
|
175 |
+
task=task,
|
176 |
+
knowledge=new_history,
|
177 |
+
history=hist,
|
178 |
+
)
|
179 |
+
new_history = resp
|
180 |
+
print (resp)
|
181 |
+
out+=resp
|
182 |
+
e=e+chunk
|
183 |
+
s=s+chunk
|
184 |
+
'''
|
185 |
+
resp = run_gpt(
|
186 |
+
COMPRESS_DATA_PROMPT,
|
187 |
+
stop_tokens=["observation:", "task:", "action:", "thought:"],
|
188 |
+
max_tokens=1024,
|
189 |
+
seed=seed,
|
190 |
+
purpose=purpose,
|
191 |
+
task=task,
|
192 |
+
knowledge=new_history,
|
193 |
+
history="All data has been recieved.",
|
194 |
+
)'''
|
195 |
+
print ("final" + resp)
|
196 |
+
history = "observation: {}\n".format(resp)
|
197 |
+
return history
|
198 |
+
|
199 |
+
|
200 |
+
|
201 |
+
|
202 |
+
def compress_history(purpose, task, history):
|
203 |
+
resp = run_gpt(
|
204 |
+
COMPRESS_HISTORY_PROMPT,
|
205 |
+
stop_tokens=["observation:", "task:", "action:", "thought:"],
|
206 |
+
max_tokens=512,
|
207 |
+
seed=random.randint(1,1000000000),
|
208 |
+
purpose=purpose,
|
209 |
+
task=task,
|
210 |
+
history=history,
|
211 |
+
)
|
212 |
+
history = "observation: {}\n".format(resp)
|
213 |
+
return history
|
214 |
+
|
215 |
+
|
216 |
+
def call_main(purpose, task, history, action_input):
|
217 |
+
resp = run_gpt(
|
218 |
+
MODEL_FINDER,
|
219 |
+
stop_tokens=["observation:", "task:"],
|
220 |
+
max_tokens=2048,
|
221 |
+
seed=random.randint(1,1000000000),
|
222 |
+
purpose=purpose,
|
223 |
+
TASKS=f'{query.tasks}',
|
224 |
+
task=task,
|
225 |
+
history=history,
|
226 |
+
)
|
227 |
+
lines = resp.strip().strip("\n").split("\n")
|
228 |
+
for line in lines:
|
229 |
+
if line == "":
|
230 |
+
continue
|
231 |
+
if line.startswith("thought: "):
|
232 |
+
history += "{}\n".format(line)
|
233 |
+
elif line.startswith("action: COMPLETE"):
|
234 |
+
return "COMPLETE", None, history, task
|
235 |
+
elif line.startswith("action: "):
|
236 |
+
action_name, action_input = parse_action(line)
|
237 |
+
history += "{}\n".format(line)
|
238 |
+
return action_name, action_input, history, task
|
239 |
+
else:
|
240 |
+
|
241 |
+
#history += "observation: {}\n".format(line)
|
242 |
+
#assert False, "unknown action: {}".format(line)
|
243 |
+
return "UPDATE-TASK", None, history, task
|
244 |
+
|
245 |
+
return "MAIN", None, history, task
|
246 |
+
|
247 |
+
|
248 |
+
def call_set_task(purpose, task, history, action_input):
|
249 |
+
task = run_gpt(
|
250 |
+
TASK_PROMPT,
|
251 |
+
stop_tokens=[],
|
252 |
+
max_tokens=1024,
|
253 |
+
seed=random.randint(1,1000000000),
|
254 |
+
purpose=purpose,
|
255 |
+
task=task,
|
256 |
+
history=history,
|
257 |
+
).strip("\n")
|
258 |
+
history += "observation: task has been updated to: {}\n".format(task)
|
259 |
+
return "MAIN", None, history, task
|
260 |
+
|
261 |
+
|
262 |
+
NAME_TO_FUNC = {
|
263 |
+
"MAIN": call_main,
|
264 |
+
"UPDATE-TASK": call_set_task,
|
265 |
+
"SEARCH_ENGINE": find_all,
|
266 |
+
"WEBSITE_SCRAPE": find_all,
|
267 |
+
|
268 |
+
|
269 |
+
}
|
270 |
+
|
271 |
+
|
272 |
+
def run_action(purpose, task, history, action_name, action_input):
|
273 |
+
if action_name == "COMPLETE":
|
274 |
+
print("Complete - Exiting")
|
275 |
+
#exit(0)
|
276 |
+
return "COMPLETE", None, history, task
|
277 |
+
|
278 |
+
# compress the history when it is long
|
279 |
+
if len(history.split("\n")) > MAX_HISTORY:
|
280 |
+
if VERBOSE:
|
281 |
+
print("COMPRESSING HISTORY")
|
282 |
+
history = compress_history(purpose, task, history)
|
283 |
+
if action_name in NAME_TO_FUNC:
|
284 |
+
|
285 |
+
assert action_name in NAME_TO_FUNC
|
286 |
+
|
287 |
+
print("RUN: ", action_name, action_input)
|
288 |
+
return NAME_TO_FUNC[action_name](purpose, task, history, action_input)
|
289 |
+
else:
|
290 |
+
history += "observation: The TOOL I tried to use returned an error, I need to select a tool from: (UPDATE-TASK, SEARCH, COMPLETE)\n"
|
291 |
+
|
292 |
+
return "MAIN", None, history, task
|
293 |
+
|
294 |
+
def run(purpose,history):
|
295 |
+
task=None
|
296 |
+
history = ""
|
297 |
+
#if not history:
|
298 |
+
# history = []
|
299 |
+
action_name = "SEARCH" if task is None else "MAIN"
|
300 |
+
action_input = None
|
301 |
+
while True:
|
302 |
+
print("")
|
303 |
+
print("")
|
304 |
+
print("---")
|
305 |
+
#print("purpose:", purpose)
|
306 |
+
print("task:", task)
|
307 |
+
print("---")
|
308 |
+
#print(history)
|
309 |
+
print("---")
|
310 |
+
|
311 |
+
action_name, action_input, history, task = run_action(
|
312 |
+
purpose,
|
313 |
+
task,
|
314 |
+
history,
|
315 |
+
action_name,
|
316 |
+
action_input,
|
317 |
+
)
|
318 |
+
yield history
|
319 |
+
if action_name == "COMPLETE":
|
320 |
+
return history
|
321 |
+
|
322 |
+
|
323 |
+
|
324 |
+
|
325 |
+
|
326 |
+
###########################################################
|
327 |
+
def search_all(url):
|
328 |
+
source=""
|
329 |
+
return source
|
330 |
+
|
331 |
+
|
332 |
+
|
333 |
+
def find_all(purpose,task,history, url):
|
334 |
+
return_list=[]
|
335 |
+
print (action_input)
|
336 |
+
#if action_input in query.tasks:
|
337 |
+
print ("trying")
|
338 |
+
try:
|
339 |
+
if action_input != "" and action_input != None:
|
340 |
+
rawp = []
|
341 |
+
source = urllib.request.urlopen(url).read()
|
342 |
+
soup = bs4.BeautifulSoup(source,'lxml')
|
343 |
+
# title of the page
|
344 |
+
print(soup.title)
|
345 |
+
# get attributes:
|
346 |
+
print(soup.title.name)
|
347 |
+
# get values:
|
348 |
+
print(soup.title.string)
|
349 |
+
# beginning navigation:
|
350 |
+
print(soup.title.parent.name)
|
351 |
+
#rawp.append([tag.name for tag in soup.find_all()] )
|
352 |
+
print([tag.name for tag in soup.find_all()])
|
353 |
+
rawp=soup.text
|
354 |
+
c=0
|
355 |
+
rl = len(rawp)
|
356 |
+
print(rl)
|
357 |
+
for i in str(rawp):
|
358 |
+
if i == " " or i==",":
|
359 |
+
c +=1
|
360 |
+
|
361 |
+
print (c)
|
362 |
+
if c > MAX_DATA:
|
363 |
+
print("compressing...")
|
364 |
+
rawp = compress_data(c,purpose,task,rawp)
|
365 |
+
print (rawp)
|
366 |
+
history = "observation: the search results are:\n {}\n".format(rawp)
|
367 |
+
task = "complete?"
|
368 |
+
return "UPDATE-TASK", None, history, task
|
369 |
+
else:
|
370 |
+
history = "observation: I need to trigger a search using the following syntax:\naction: SEARCH action_input=SEARCH_QUERY\n"
|
371 |
+
return "UPDATE-TASK", None, history, task
|
372 |
+
except Exception as e:
|
373 |
+
print (e)
|
374 |
+
history = "observation: I need to trigger a search using the following syntax:\naction: SEARCH action_input=SEARCH_QUERY\n"
|
375 |
+
return "UPDATE-TASK", None, history, task
|
376 |
+
|
377 |
+
#else:
|
378 |
+
# history = "observation: The search query I used did not return a valid response"
|
379 |
+
|
380 |
+
return "MAIN", None, history, task
|
381 |
|
382 |
|
383 |
def find_it(url,q=None,num=None):
|
|
|
422 |
except Exception as e:
|
423 |
print (e)
|
424 |
return e
|
425 |
+
#################################
|
426 |
+
|
427 |
+
examples =[
|
428 |
+
"find the most popular model that I can use to generate an image by providing a text prompt",
|
429 |
+
"return the top 10 models that I can use to identify objects in images",
|
430 |
+
"which models have the most likes from each category?"
|
431 |
+
]
|
432 |
+
|
433 |
|
434 |
+
gr.ChatInterface(
|
435 |
+
fn=run,
|
436 |
+
chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
|
437 |
+
title="Mixtral 46.7B Powered <br> Search",
|
438 |
+
examples=examples,
|
439 |
+
concurrency_limit=20,
|
440 |
+
).launch(show_api=False)
|
441 |
|
442 |
+
'''
|
443 |
with gr.Blocks() as app:
|
444 |
with gr.Row():
|
445 |
with gr.Column(scale=1):
|
|
|
461 |
|
462 |
app.launch()
|
463 |
|
464 |
+
'''
|