forked from LemonQu-GIT/ChatGLM-6B-Engineering
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamlit_utils.py
278 lines (258 loc) · 9.97 KB
/
streamlit_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import requests, re, json, io, base64, os
from urllib.parse import quote
from bs4 import BeautifulSoup
from PIL import Image, PngImagePlugin
def ext_zhihu(url):
if "/answer" in url:
rep = url.replace('https://www.zhihu.com/question/','')
rep_l = rep[rep.rfind("/answer"):][7:]
rep = 'https://www.zhihu.com/question/' + rep.replace("/answer"+rep_l,"")
return rep
else:
return url
def redirect_url(url):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36",
}
r = requests.get(url, headers=headers, allow_redirects=False) # 不允许重定向
if r.status_code == 302:
real_url = r.headers.get('Location')
else:
real_url = re.findall("URL='(.*?)'", r.text)[0]
return real_url
def search_web(keyword):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.44",
}
url = quote("https://www.sogou.com/web?query="+str(keyword),safe='/:?=.')
r = requests.get(url, headers=headers)
html = r.text
soup = BeautifulSoup(html, 'html.parser')
item_list = soup.find_all(class_='struct201102')
relist = []
for items in item_list:
item_prelist = items.find(class_ = "vr-title")
item_title = re.sub(r'(<[^>]+>|\s)','',str(item_prelist))
href_s = item_prelist.find(class_ = "", href=True)
href = href_s["href"]
if href[0] == "/":
href_f = redirect_url("https://www.sogou.com"+href)
else:
href_f = href
relist.append([item_title, href_f])
return relist
def search_zhihu_que(url):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.44",
}
r = requests.get(url, headers=headers)
html = r.text
soup = BeautifulSoup(html, 'html.parser')
item_list = soup.find_all(class_='List-item')
relist = []
for items in item_list:
item_prelist = items.find(class_ = "RichText ztext CopyrightRichText-richText css-1g0fqss")
item_title = re.sub(r'(<[^>]+>|\s)','',str(item_prelist))
relist.append(item_title)
return relist
def search_zhihu_zhuanlan(url):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.44",
}
r = requests.get(url, headers=headers)
html = r.text
soup = BeautifulSoup(html, 'html.parser')
item_list = soup.find(class_='RichText ztext Post-RichText css-1g0fqss')
item_title = re.sub(r'(<[^>]+>|\s)','',str(item_list))
return item_title
def search_baike(url):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.44",
}
if "baike.sogou.com" in url:
r = requests.get(url, headers=headers)
html_s = r.text
soup_s = BeautifulSoup(html_s, 'html.parser')
item_list_s = soup_s.find(class_='lemma_name')
item_title = re.sub(r'(<[^>]+>|\s)','',str(item_list_s))
item_title = item_title.replace('编辑词条','')
r = requests.get("https://baike.baidu.com/api/openapi/BaikeLemmaCardApi?scope=103&format=json&appid=379020&bk_key="+item_title+"&bk_length=600", headers=headers)
resp = r.text.encode('utf-8').decode('gbk')
resp_json = json.loads(resp)
answer = resp_json.get('abstract')
if answer == None:
answer_f = soup_s.find(class_='abstract')
item_title = re.sub(r'(<[^>]+>|\s)','',str(answer_f))
return item_title
else:
return resp_json.get('abstract')
else:
r = requests.get("https://baike.baidu.com/api/openapi/BaikeLemmaCardApi?scope=103&format=json&appid=379020&bk_key="+url+"&bk_length=600", headers=headers)
resp = r.text.encode('utf-8').decode('gbk')
resp_json = json.loads(resp)
answer = resp_json.get('abstract')
return answer
def search_wx(url):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.44",
}
r = requests.get(url, headers=headers)
html = r.text
soup = BeautifulSoup(html, 'html.parser')
item_list = soup.find(class_='rich_media_wrp')
item_title = re.sub(r'(<[^>]+>|\s)','',str(item_list))
return item_title
def search_news_sohu(url):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.44",
}
r = requests.get(url, headers=headers)
html = r.text
soup = BeautifulSoup(html, 'html.parser')
item_list = soup.find(class_='article')
item_title = re.sub(r'(<[^>]+>|\s)','',str(item_list))
return item_title
def search_news_163(url):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.44",
}
r = requests.get(url, headers=headers)
html = r.text
soup = BeautifulSoup(html, 'html.parser')
item_list = soup.find(class_='post_body')
item_title = re.sub(r'(<[^>]+>|\s)','',str(item_list))
return item_title
def search_bilibili(url):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.44",
}
r = requests.get(url, headers=headers)
html = r.text
soup = BeautifulSoup(html, 'html.parser')
item_list = soup.find(class_='article-content')
item_title = re.sub(r'(<[^>]+>|\s)','',str(item_list))
return item_title
def search_csdn(url):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.44",
}
r = requests.get(url, headers=headers)
html = r.text
soup = BeautifulSoup(html, 'html.parser')
item_list = soup.find(class_='article_content clearfix')
item_title = re.sub(r'(<[^>]+>|\s)','',str(item_list))
return item_title
def search_github(keyword,opt):
token = os.getenv('git_token')
if not token == None:
headers={"Authorization":"token "+ str(token)}
else:
headers={}
new_key = ""
for ch in keyword:
if not u'\u4e00' <= ch <= u'\u9fff':
new_key += ch
url = quote("https://api.github.com/search/repositories?q="+str(new_key)+"&sort=stars&order=desc",safe='/:?=.&')
r = requests.get(url, headers=headers)
resp_json = json.loads(r.text)
items = resp_json.get('items')
relist = []
items = items[0:int(opt)]
for repos in items:
item_json = json.dumps(repos)
item_json = json.loads(str(item_json))
addr = item_json.get('full_name')
url = quote("https://api.github.com/repos/"+str(addr)+"/readme",safe='/:?=.&')
r = requests.get(url, headers=headers)
repo_json = json.loads(r.text)
readme = repo_json.get('download_url')
r = requests.get(str(readme).replace("https://raw.githubusercontent.com/","https://raw.fastgit.org/"), headers=headers)
relist.append(repr(r.text))
return relist
def search_main(item, feature):
web_list = search_web(item)
return_list = []
print(feature)
#print(web_list)
if '百科' in feature:
ans = str(search_baike(item))
if not ans == "":
return_list.append(ans)
if 'GitHub' in feature:
ans = str(search_github(item,1))
if not ans == "":
return_list.append(ans)
for items in web_list:
if "zhihu.com/question/" in items[1] and '知乎回复' in feature:
return_list.append(str(search_zhihu_que(ext_zhihu(items[1]))))
if "baike.sogou.com" in items[1] and '百科' in feature:
return_list.append(str(search_baike(items[1])))
if "mp.weixin.qq.com" in items[1] and '微信公众号' in feature:
return_list.append(str(search_wx(items[1])))
if "zhuanlan.zhihu.com" in items[1] and '知乎专栏' in feature:
return_list.append(str(search_zhihu_zhuanlan(items[1])))
if "163.com/dy/article/" in items[1] and '新闻' in feature:
return_list.append(str(search_news_163(items[1])))
if "sohu.com/a/" in items[1] and '新闻' in feature:
return_list.append(str(search_news_sohu(items[1])))
if "bilibili.com/read/" in items[1] and 'B站专栏' in feature:
return_list.append(str(search_bilibili(items[1])))
if "blog.csdn.net" in items[1] and 'CSDN' in feature:
return_list.append(str(search_csdn(items[1])))
return return_list
def test_if_zhcn(string):
for ch in string:
if u'\u4e00' <= ch <= u'\u9fff':
return True
return False
def translate(word):
if test_if_zhcn(word):
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null'
key = {
'type': "AUTO",
'i': word,
"doctype": "json",
"version": "2.1",
"keyfrom": "fanyi.web",
"ue": "UTF-8",
"action": "FY_BY_CLICKBUTTON",
"typoResult": "true"
}
response = requests.post(url, data=key)
if response.status_code == 200:
list_trans = response.text
result = json.loads(list_trans)
return result['translateResult'][0][0]['tgt']
else:
return word
def chatglm_json(prompt, history, max_length, top_p, temperature):
url = "http://127.0.0.1:8000"
payload = {
"prompt": prompt,
"history": history,
"max_length": max_length,
"top_p": top_p,
"temperature": temperature
}
response = requests.post(url, json=payload)
json_resp_raw = response.json()
json_resp_raw_list = json.dumps(json_resp_raw)
return json_resp_raw_list
def stable_diffusion(Pprompt,Nprompt):
url = "http://127.0.0.1:7861"
payload = {
"prompt": Pprompt,
"steps": 5,
"negative_prompt": Nprompt
}
response = requests.post(url=f'{url}/sdapi/v1/txt2img', json=payload)
r = response.json()
for i in r['images']:
image = Image.open(io.BytesIO(base64.b64decode(i.split(",",1)[0])))
png_payload = {
"image": "data:image/png;base64," + i
}
response2 = requests.post(url=f'{url}/sdapi/v1/png-info', json=png_payload)
pnginfo = PngImagePlugin.PngInfo()
pnginfo.add_text("parameters", response2.json().get("info"))
image.save('stable_diffusion.png', pnginfo=pnginfo)