Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
@@ -690,7 +690,95 @@ def mindAsk(
|
|
690 |
past_key_values,
|
691 |
)
|
692 |
|
693 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
694 |
|
695 |
|
696 |
with gr.Blocks(title="🐰Bofan Ai🐰", theme=gr.themes.Soft(text_size="sm")) as demo:
|
@@ -758,6 +846,7 @@ with gr.Blocks(title="🐰Bofan Ai🐰", theme=gr.themes.Soft(text_size="sm")) a
|
|
758 |
with gr.Row():
|
759 |
fitnessAskBtn = gr.Button("🥼健康咨询", variant="primary")
|
760 |
mindAskBtn = gr.Button("😶🌫️心理咨询", variant="primary")
|
|
|
761 |
|
762 |
with gr.Column(scale=1):
|
763 |
gr.HTML("""<h3 align="center">🍀您好,除健康咨询和心理咨询外,其它功能使用前,请先清空历史,并输入问题。🍀</h3>""")
|
@@ -1090,6 +1179,23 @@ with gr.Blocks(title="🐰Bofan Ai🐰", theme=gr.themes.Soft(text_size="sm")) a
|
|
1090 |
outputs=[chatbot, history, past_key_values],
|
1091 |
)
|
1092 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1093 |
deleteBtn.click(delete_last_turn, [chatbot, history], [chatbot, history])
|
1094 |
|
1095 |
with gr.Accordion("Example inputs", open=False):
|
|
|
690 |
past_key_values,
|
691 |
)
|
692 |
|
693 |
+
from bs4 import BeautifulSoup
|
694 |
+
import requests
|
695 |
+
|
696 |
+
def scrape_text(url, proxies) -> str:
|
697 |
+
"""从网页抓取文本,限制为前500个字符
|
698 |
+
|
699 |
+
参数:
|
700 |
+
url (str): 要抓取文本的网址
|
701 |
+
|
702 |
+
返回:
|
703 |
+
str: 抓取到的文本,最多为前500个字符
|
704 |
+
"""
|
705 |
+
headers = {
|
706 |
+
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36',
|
707 |
+
'Content-Type': 'text/plain',
|
708 |
+
}
|
709 |
+
try:
|
710 |
+
response = requests.get(url, headers=headers, proxies=proxies, timeout=8)
|
711 |
+
if response.encoding == "ISO-8859-1":
|
712 |
+
response.encoding = response.apparent_encoding
|
713 |
+
except:
|
714 |
+
return "无法连接到该网页"
|
715 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
716 |
+
for script in soup(["script", "style"]):
|
717 |
+
script.extract()
|
718 |
+
text = soup.get_text()
|
719 |
+
# 截取文本,限制最多500个字符
|
720 |
+
text = text[:500]
|
721 |
+
return text
|
722 |
+
|
723 |
+
# 定义函数:联网搜索并更新聊天界面
|
724 |
+
def GGSearch(
|
725 |
+
user_input, chatbot, max_length, top_p, temperature, history, past_key_values
|
726 |
+
):
|
727 |
+
global GGSearchins # 确保 GGSearchins 在函数内部可用
|
728 |
+
|
729 |
+
# 使用用户输入进行联网搜索
|
730 |
+
url = f"https://www.google.com/search?q={user_input}"
|
731 |
+
headers = {
|
732 |
+
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36'
|
733 |
+
}
|
734 |
+
|
735 |
+
try:
|
736 |
+
response = requests.get(url, headers=headers)
|
737 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
738 |
+
search_results = []
|
739 |
+
for g in soup.find_all('div', class_='g'):
|
740 |
+
anchors = g.find_all('a')
|
741 |
+
if anchors:
|
742 |
+
link = anchors[0]['href']
|
743 |
+
if link.startswith('/url?q='):
|
744 |
+
link = link[7:]
|
745 |
+
if not link.startswith('http'):
|
746 |
+
continue
|
747 |
+
search_results.append(link)
|
748 |
+
except:
|
749 |
+
search_results = []
|
750 |
+
|
751 |
+
# 限制搜索结果数量为3
|
752 |
+
search_results = search_results[:5]
|
753 |
+
|
754 |
+
# 从搜索结果抓取文本并存储到 GGSearchins
|
755 |
+
scraped_texts = []
|
756 |
+
for link in search_results:
|
757 |
+
scraped_text = scrape_text(link, proxies=None) # 假设抓取不需要代理
|
758 |
+
scraped_texts.append(scraped_text)
|
759 |
+
|
760 |
+
# 将抓取到的文本拼接并存储到 GGSearchins
|
761 |
+
GGSearchins = "\n".join(scraped_text for scraped_text in scraped_texts)
|
762 |
+
|
763 |
+
# 更新聊天界面和历史记录
|
764 |
+
chatbot.append(("联网搜索结果:", GGSearchins))
|
765 |
+
history.append(("联网搜索结果:", GGSearchins))
|
766 |
+
|
767 |
+
user_input = txtSumins+GGSearchins
|
768 |
+
|
769 |
+
# 继续正常的 GPT 对话流程
|
770 |
+
yield from predict(
|
771 |
+
RETRY_FLAG,
|
772 |
+
user_input,
|
773 |
+
chatbot,
|
774 |
+
max_length,
|
775 |
+
top_p,
|
776 |
+
temperature,
|
777 |
+
history=[],
|
778 |
+
past_key_values=None,
|
779 |
+
)
|
780 |
+
|
781 |
+
|
782 |
|
783 |
|
784 |
with gr.Blocks(title="🐰Bofan Ai🐰", theme=gr.themes.Soft(text_size="sm")) as demo:
|
|
|
846 |
with gr.Row():
|
847 |
fitnessAskBtn = gr.Button("🥼健康咨询", variant="primary")
|
848 |
mindAskBtn = gr.Button("😶🌫️心理咨询", variant="primary")
|
849 |
+
GGSearchBtn = gr.Button("🐞联网搜索", variant="primary")
|
850 |
|
851 |
with gr.Column(scale=1):
|
852 |
gr.HTML("""<h3 align="center">🍀您好,除健康咨询和心理咨询外,其它功能使用前,请先清空历史,并输入问题。🍀</h3>""")
|
|
|
1179 |
outputs=[chatbot, history, past_key_values],
|
1180 |
)
|
1181 |
|
1182 |
+
GGSearchBtn.click(
|
1183 |
+
reset_state, outputs=[chatbot, history, past_key_values], show_progress="full"
|
1184 |
+
)
|
1185 |
+
GGSearchBtn.click(
|
1186 |
+
GGSearch,
|
1187 |
+
inputs=[
|
1188 |
+
user_input,
|
1189 |
+
chatbot,
|
1190 |
+
max_length,
|
1191 |
+
top_p,
|
1192 |
+
temperature,
|
1193 |
+
history,
|
1194 |
+
past_key_values,
|
1195 |
+
],
|
1196 |
+
# outputs = [chatbot, history, last_user_message, user_message]
|
1197 |
+
outputs=[chatbot, history, past_key_values],
|
1198 |
+
)
|
1199 |
deleteBtn.click(delete_last_turn, [chatbot, history], [chatbot, history])
|
1200 |
|
1201 |
with gr.Accordion("Example inputs", open=False):
|