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2 |
+
import requests
|
3 |
+
import bs4
|
4 |
+
import lxml
|
5 |
+
import os
|
6 |
+
from huggingface_hub import InferenceClient,HfApi
|
7 |
+
import random
|
8 |
+
import json
|
9 |
+
import datetime
|
10 |
+
import xmltodict
|
11 |
+
"""
|
12 |
+
from prompts import (
|
13 |
+
COMPRESS_HISTORY_PROMPT,
|
14 |
+
COMPRESS_DATA_PROMPT,
|
15 |
+
COMPRESS_DATA_PROMPT_SMALL,
|
16 |
+
PREFIX,
|
17 |
+
TASK_PROMPT,
|
18 |
+
)
|
19 |
+
api=HfApi()
|
20 |
+
client = InferenceClient(
|
21 |
+
"mistralai/Mixtral-8x7B-Instruct-v0.1"
|
22 |
+
)
|
23 |
+
|
24 |
+
def parse_action(string: str):
|
25 |
+
print("PARSING:")
|
26 |
+
print(string)
|
27 |
+
assert string.startswith("action:")
|
28 |
+
idx = string.find("action_input=")
|
29 |
+
print(idx)
|
30 |
+
if idx == -1:
|
31 |
+
print ("idx == -1")
|
32 |
+
print (string[8:])
|
33 |
+
return string[8:], None
|
34 |
+
|
35 |
+
print ("last return:")
|
36 |
+
print (string[8 : idx - 1])
|
37 |
+
print (string[idx + 13 :].strip("'").strip('"'))
|
38 |
+
return string[8 : idx - 1], string[idx + 13 :].strip("'").strip('"')
|
39 |
+
|
40 |
+
MAX_HISTORY = 100
|
41 |
+
MAX_DATA = 20000
|
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 run_gpt(
|
52 |
+
prompt_template,
|
53 |
+
stop_tokens,
|
54 |
+
max_tokens,
|
55 |
+
seed,
|
56 |
+
purpose,
|
57 |
+
**prompt_kwargs,
|
58 |
+
):
|
59 |
+
timestamp=datetime.datetime.now()
|
60 |
+
|
61 |
+
print(seed)
|
62 |
+
generate_kwargs = dict(
|
63 |
+
temperature=0.9,
|
64 |
+
max_new_tokens=max_tokens,
|
65 |
+
top_p=0.95,
|
66 |
+
repetition_penalty=1.0,
|
67 |
+
do_sample=True,
|
68 |
+
seed=seed,
|
69 |
+
)
|
70 |
+
|
71 |
+
content = PREFIX.format(
|
72 |
+
timestamp=timestamp,
|
73 |
+
purpose=purpose,
|
74 |
+
) + prompt_template.format(**prompt_kwargs)
|
75 |
+
if VERBOSE:
|
76 |
+
print(LOG_PROMPT.format(content))
|
77 |
+
|
78 |
+
|
79 |
+
#formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
|
80 |
+
#formatted_prompt = format_prompt(f'{content}', **prompt_kwargs['history'])
|
81 |
+
|
82 |
+
stream = client.text_generation(content, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
83 |
+
resp = ""
|
84 |
+
for response in stream:
|
85 |
+
resp += response.token.text
|
86 |
+
#yield resp
|
87 |
+
|
88 |
+
if VERBOSE:
|
89 |
+
print(LOG_RESPONSE.format(resp))
|
90 |
+
return resp
|
91 |
+
|
92 |
+
|
93 |
+
def compress_data(c,purpose, task, history, result):
|
94 |
+
seed=random.randint(1,1000000000)
|
95 |
+
print (c)
|
96 |
+
divr=int(c)/MAX_DATA
|
97 |
+
divi=int(divr)+1 if divr != int(divr) else int(divr)
|
98 |
+
chunk = int(int(c)/divr)
|
99 |
+
print(f'chunk:: {chunk}')
|
100 |
+
print(f'divr:: {divr}')
|
101 |
+
print (f'divi:: {divi}')
|
102 |
+
out = []
|
103 |
+
#out=""
|
104 |
+
s=0
|
105 |
+
e=chunk
|
106 |
+
print(f'e:: {e}')
|
107 |
+
new_history=""
|
108 |
+
task = f'Compile this data to fulfill the task: {task}, and complete the purpose: {purpose}\n'
|
109 |
+
for z in range(divi):
|
110 |
+
print(f's:e :: {s}:{e}')
|
111 |
+
|
112 |
+
hist = history[s:e]
|
113 |
+
|
114 |
+
resp = run_gpt(
|
115 |
+
COMPRESS_DATA_PROMPT,
|
116 |
+
stop_tokens=["observation:", "task:", "action:", "thought:"],
|
117 |
+
max_tokens=2048,
|
118 |
+
seed=seed,
|
119 |
+
purpose=purpose,
|
120 |
+
task=task,
|
121 |
+
knowledge=new_history,
|
122 |
+
history=hist,
|
123 |
+
)
|
124 |
+
new_history = resp
|
125 |
+
print (resp)
|
126 |
+
out+=resp
|
127 |
+
e=e+chunk
|
128 |
+
s=s+chunk
|
129 |
+
'''
|
130 |
+
resp = run_gpt(
|
131 |
+
COMPRESS_DATA_PROMPT,
|
132 |
+
stop_tokens=["observation:", "task:", "action:", "thought:"],
|
133 |
+
max_tokens=2048,
|
134 |
+
seed=seed,
|
135 |
+
purpose=purpose,
|
136 |
+
task=task,
|
137 |
+
knowledge=new_history,
|
138 |
+
history=result,
|
139 |
+
)
|
140 |
+
'''
|
141 |
+
print ("final" + resp)
|
142 |
+
history = "result: {}\n".format(resp)
|
143 |
+
return history
|
144 |
+
|
145 |
+
def find_all(purpose,task,history, rss_url, result):
|
146 |
+
return_list=[]
|
147 |
+
#if action_input in query.tasks:
|
148 |
+
print (f"trying URL:: {rss_url}")
|
149 |
+
lod=""
|
150 |
+
try:
|
151 |
+
if rss_url != "" and rss_url != None:
|
152 |
+
#rawp = []
|
153 |
+
out = []
|
154 |
+
r = requests.get(f'{rss_url}')
|
155 |
+
if ".json" in rss_url:
|
156 |
+
lod = json.loads(r.text)
|
157 |
+
if ".xml" in rss_url:
|
158 |
+
lod = xmltodict.parse(r.content)
|
159 |
+
if ".rss" in rss_url:
|
160 |
+
lod = xmltodict.parse(r.content)
|
161 |
+
else:
|
162 |
+
try:
|
163 |
+
lod = xmltodict.parse(r.content)
|
164 |
+
except Exception as e:
|
165 |
+
history+=f"observation: could not complete RSS Search due to this error:\n{e}"
|
166 |
+
return "MAIN", None, history, task, result
|
167 |
+
rawp = lod
|
168 |
+
print(f'RAWP::\n{rawp}')
|
169 |
+
cnt=0
|
170 |
+
cnt+=len(rawp)
|
171 |
+
out.append(rawp)
|
172 |
+
out = str(out)
|
173 |
+
rl = len(out)
|
174 |
+
print(f'rl:: {rl}')
|
175 |
+
c=0
|
176 |
+
for i in str(out):
|
177 |
+
c +=1
|
178 |
+
print (f'c:: {c}')
|
179 |
+
if c > MAX_HISTORY:
|
180 |
+
print("compressing...")
|
181 |
+
rawp = compress_data(c,purpose,task,out,result)
|
182 |
+
else:
|
183 |
+
rawp = out
|
184 |
+
result += rawp
|
185 |
+
|
186 |
+
print (rawp)
|
187 |
+
print (f'out:: {out}')
|
188 |
+
history = "observation: the search results are:\n {}\n".format(rawp)
|
189 |
+
task = "compile report and return action: COMPLETE"
|
190 |
+
return "MAIN", None, history, task, result
|
191 |
+
else:
|
192 |
+
history += "observation: An Error occured\nI need to trigger a search using the following syntax:\naction: READ-RSS action_input=URL\n"
|
193 |
+
return "UPDATE-TASK", None, history, task, result
|
194 |
+
except Exception as e:
|
195 |
+
print (e)
|
196 |
+
history += "observation: I need to trigger a search using the following syntax:\naction: READ-RSS action_input=URL\n"
|
197 |
+
return "UPDATE-TASK", None, history, task, result
|
198 |
+
return "MAIN", None, history, task, result
|
199 |
+
"""
|
200 |
+
def find_rss():
|
201 |
+
with open ('feeds.json','r') as j:
|
202 |
+
cont = json.loads(j.read())
|
203 |
+
print(cont)
|
204 |
+
for ea in cont:
|
205 |
+
print (ea)
|
206 |
+
'''
|
207 |
+
r = requests.get(f'{rss_url}')
|
208 |
+
if ".json" in rss_url:
|
209 |
+
lod = json.loads(r.text)
|
210 |
+
if ".xml" in rss_url:
|
211 |
+
lod = xmltodict.parse(r.content)
|
212 |
+
if ".rss" in rss_url:
|
213 |
+
lod = xmltodict.parse(r.content)
|
214 |
+
else:
|
215 |
+
try:
|
216 |
+
lod = xmltodict.parse(r.content)
|
217 |
+
except Exception as e:
|
218 |
+
history+=f"observation: could not complete RSS Search due to this error:\n{e}"
|
219 |
+
return "MAIN", None, history, task, result
|
220 |
+
rawp = lod
|
221 |
+
'''
|
222 |
+
|
223 |
+
with gr.Blocks() as app:
|
224 |
+
with gr.Row():
|
225 |
+
rss_search = gr.Textbox(label="search rss (xml,json)")
|
226 |
+
search_btn=gr.Button("find rss")
|
227 |
+
with gr.Row():
|
228 |
+
rss = gr.Textbox(label="rss")
|
229 |
+
btn = gr.Button("load rss")
|
230 |
+
r_btn=gr.Button("read")
|
231 |
+
with gr.Row():
|
232 |
+
out_json = gr.JSON()
|
233 |
+
fil = gr.Textbox()
|
234 |
+
r_btn.click(find_rss,None,None)
|
235 |
+
#r_btn.click(read_rss,None,[out_json,fil])
|
236 |
+
#search_btn.click(find_rss,rss_search,out_json)
|
237 |
+
#btn.click(get_rss,rss,out_json)
|
238 |
+
app.launch()
|