Update webapp.py

This commit is contained in:
Joseph Cheung 2023-02-20 21:16:40 +08:00
parent 2ba967f1c0
commit 3a423887c9

View file

@ -746,107 +746,110 @@ def search():
number_of_results = 0 number_of_results = 0
# OPENAI GPT # OPENAI GPT
url_pair = {} try:
prompt = "" url_pair = {}
for res in results: prompt = ""
if 'url' not in res: continue for res in results:
if 'content' not in res: continue if 'url' not in res: continue
if 'title' not in res: continue if 'content' not in res: continue
if res['content'] == '': continue if 'title' not in res: continue
new_url = 'https://url'+str(len(url_pair)+1) if res['content'] == '': continue
url_pair[new_url] = res['url'] new_url = 'https://url'+str(len(url_pair)+1)
res['title'] = res['title'].replace("التغريدات مع الردود بواسطة","") url_pair[new_url] = res['url']
res['content'] = res['content'].replace("Translate Tweet. ","") res['title'] = res['title'].replace("التغريدات مع الردود بواسطة","")
res['content'] = res['content'].replace("Learn more ","") res['content'] = res['content'].replace("Translate Tweet. ","")
res['content'] = res['content'].replace("Translate Tweet.","") res['content'] = res['content'].replace("Learn more ","")
res['content'] = res['content'].replace("Learn more.","") res['content'] = res['content'].replace("Translate Tweet.","")
tmp_prompt = res['title'] +'\n'+ res['content'] + '\n' + new_url +'\n' res['content'] = res['content'].replace("Learn more.","")
if len(prompt)+len(tmp_prompt)<3000: tmp_prompt = res['title'] +'\n'+ res['content'] + '\n' + new_url +'\n'
prompt += tmp_prompt +'\n' if len(prompt)+len(tmp_prompt)<3000:
if prompt != "": prompt += tmp_prompt +'\n'
gpt = "" if prompt != "":
gpt_url = "https://api.openai.com/v1/engines/text-davinci-003/completions" gpt = ""
gpt_headers = { gpt_url = "https://api.openai.com/v1/engines/text-davinci-003/completions"
"Authorization": "Bearer "+os.environ['GPTKEY'], gpt_headers = {
"Content-Type": "application/json", "Authorization": "Bearer "+os.environ['GPTKEY'],
"OpenAI-Organization": os.environ['GPTORG'] "Content-Type": "application/json",
} "OpenAI-Organization": os.environ['GPTORG']
if original_search_query != search_query.query:
gpt_data = {
"prompt": prompt+"\n以上是问题 " + original_search_query + " 的搜索结果用简体中文分条总结简报在文中用markdown脚注指对应内容来源链接",
"max_tokens": 1000,
"temperature": 0.7,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0,
"best_of": 1,
"echo": False,
"logprobs": 0,
"stream": False
} }
else:
gpt_data = {
"prompt": prompt+"\n以上是关键词 " + search_query.query + " 的搜索结果用简体中文分条总结简报在文中用markdown脚注指对应内容来源链接",
"max_tokens": 1000,
"temperature": 0.7,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0,
"best_of": 1,
"echo": False,
"logprobs": 0,
"stream": False
}
gpt_response = requests.post(gpt_url, headers=gpt_headers, data=json.dumps(gpt_data))
gpt_json = gpt_response.json()
if 'choices' in gpt_json:
gpt = gpt_json['choices'][0]['text']
gpt = gpt.replace("简报:","").replace("简报:","")
for urls in url_pair.keys():
gpt = gpt.replace(urls,url_pair[urls])
rgpt = gpt
if gpt and gpt!="":
if original_search_query != search_query.query: if original_search_query != search_query.query:
gpt = "Search 为您搜索:" + search_query.query + "\n\n" + gpt gpt_data = {
for i in range(1,16): "prompt": prompt+"\n以上是问题 " + original_search_query + " 的搜索结果用简体中文分条总结简报在文中用markdown脚注指对应内容来源链接",
gpt = gpt.replace("["+str(i)+"] http","[^"+str(i)+"]: http").replace("["+str(i)+"]http","[^"+str(i)+"]: http").replace("["+str(i)+"]","[^"+str(i)+"]") "max_tokens": 1000,
rgpt = gpt "temperature": 0.7,
gpt = markdown.markdown( gpt , extensions=['footnotes']) "top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0,
"best_of": 1,
"echo": False,
"logprobs": 0,
"stream": False
}
else:
gpt_data = {
"prompt": prompt+"\n以上是关键词 " + search_query.query + " 的搜索结果用简体中文分条总结简报在文中用markdown脚注指对应内容来源链接",
"max_tokens": 1000,
"temperature": 0.7,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0,
"best_of": 1,
"echo": False,
"logprobs": 0,
"stream": False
}
gpt_response = requests.post(gpt_url, headers=gpt_headers, data=json.dumps(gpt_data))
gpt_json = gpt_response.json()
if 'choices' in gpt_json:
gpt = gpt_json['choices'][0]['text']
gpt = gpt.replace("简报:","").replace("简报:","")
for urls in url_pair.keys(): for urls in url_pair.keys():
gpt = gpt.replace("#fn:"+urls.replace("https://url",""),url_pair[urls]) gpt = gpt.replace(urls,url_pair[urls])
gpt = gpt.replace("#fn:url"+urls.replace("https://url",""),url_pair[urls]) rgpt = gpt
gpt = re.sub(r'<div class="footnote">(.*?)</div>', '', gpt, flags=re.DOTALL)
gpt = gpt + '''<style> if gpt and gpt!="":
a.footnote-ref{ if original_search_query != search_query.query:
position: relative; gpt = "Search 为您搜索:" + search_query.query + "\n\n" + gpt
display: inline-flex; for i in range(1,16):
align-items: center; gpt = gpt.replace("["+str(i)+"] http","[^"+str(i)+"]: http").replace("["+str(i)+"]http","[^"+str(i)+"]: http").replace("["+str(i)+"]","[^"+str(i)+"]")
justify-content: center; rgpt = gpt
font-size: 10px; gpt = markdown.markdown( gpt , extensions=['footnotes'])
font-weight: 600;
vertical-align: top; for urls in url_pair.keys():
top: 5px; gpt = gpt.replace("#fn:"+urls.replace("https://url",""),url_pair[urls])
margin: 2px 2px 2px; gpt = gpt.replace("#fn:url"+urls.replace("https://url",""),url_pair[urls])
min-width: 14px; gpt = re.sub(r'<div class="footnote">(.*?)</div>', '', gpt, flags=re.DOTALL)
height: 14px; gpt = gpt + '''<style>
border-radius: 3px; a.footnote-ref{
color: rgb(18, 59, 182); position: relative;
background: rgb(209, 219, 250); display: inline-flex;
outline: transparent solid 1px; align-items: center;
} justify-content: center;
</style> font-size: 10px;
''' font-weight: 600;
for i in range(1, 16): vertical-align: top;
rgpt = rgpt.replace(f"[{i}]", "") top: 5px;
rgpt = rgpt.replace(f"[^{i}]", "") margin: 2px 2px 2px;
gptbox = { min-width: 14px;
'infobox': 'GPT3', height: 14px;
'id': 'gpt'+str(len(prompt)), border-radius: 3px;
'content': gpt, color: rgb(18, 59, 182);
} background: rgb(209, 219, 250);
result_container.infoboxes.append(gptbox) outline: transparent solid 1px;
}
</style>
'''
for i in range(1, 16):
rgpt = rgpt.replace(f"[{i}]", "")
rgpt = rgpt.replace(f"[^{i}]", "")
gptbox = {
'infobox': 'GPT3',
'id': 'gpt'+str(len(prompt)),
'content': gpt,
}
result_container.infoboxes.append(gptbox)
except Exception as ee:
logger.exception(ee, exc_info=True)
# checkin for a external bang # checkin for a external bang