Spaces:
Runtime error
Runtime error
allinaigc
commited on
Commit
•
7af3c54
0
Parent(s):
Duplicate from allinaigc/internet_GPT_1105
Browse files- .gitattributes +34 -0
- README.md +13 -0
- app.py +128 -0
- requirements.txt +5 -0
.gitattributes
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: GPT Internet Temp
|
3 |
+
emoji: 😻
|
4 |
+
colorFrom: green
|
5 |
+
colorTo: yellow
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.28.3
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
duplicated_from: allinaigc/internet_GPT_1105
|
11 |
+
---
|
12 |
+
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
'''
|
2 |
+
1. 增加的可联网的模式,可以增强对话的能力。
|
3 |
+
1. 联网与不联网模式统一在一个函数里面,通过choice来选择。
|
4 |
+
'''
|
5 |
+
|
6 |
+
import openai
|
7 |
+
import gradio as gr
|
8 |
+
# from gradio import Radio
|
9 |
+
import os
|
10 |
+
import openai
|
11 |
+
import requests
|
12 |
+
from rich import print
|
13 |
+
import gc
|
14 |
+
|
15 |
+
## 这里是huggingface的secret里面存放当前可以用的api key。
|
16 |
+
openai.api_key = os.environ['my_api_key']
|
17 |
+
os.environ["OPENAI_API_KEY"] = os.environ['my_api_key']
|
18 |
+
|
19 |
+
### Bing Search
|
20 |
+
bing_search_api_key = os.environ['bing_api_key']
|
21 |
+
bing_search_endpoint = 'https://api.bing.microsoft.com/v7.0/search'
|
22 |
+
|
23 |
+
def clear_conversation():
|
24 |
+
return gr.update(value=None, visible=True), None, "",
|
25 |
+
|
26 |
+
def search(query):
|
27 |
+
# Construct a request
|
28 |
+
# mkt = 'en-EN'
|
29 |
+
mkt = 'zh-CN'
|
30 |
+
params = {'q': query, 'mkt': mkt}
|
31 |
+
headers = {'Ocp-Apim-Subscription-Key': bing_search_api_key}
|
32 |
+
|
33 |
+
# Call the API
|
34 |
+
try:
|
35 |
+
response = requests.get(bing_search_endpoint, headers=headers, params=params)
|
36 |
+
response.raise_for_status()
|
37 |
+
json = response.json()
|
38 |
+
return json["webPages"]["value"]
|
39 |
+
# print("\nJSON Response:\n")
|
40 |
+
# pprint(response.json())
|
41 |
+
|
42 |
+
except Exception as e:
|
43 |
+
raise e
|
44 |
+
|
45 |
+
messages = [
|
46 |
+
# {"role": "system", "content": "You are a helpful and kind AI Assistant."},
|
47 |
+
{"role": "system", "content": "你是一个专业和友好的AI助手。"},]
|
48 |
+
|
49 |
+
|
50 |
+
def chatbot(input, choice):
|
51 |
+
global messages #! 通过制定messages可以在非增强模式中,记忆对话。
|
52 |
+
history = []
|
53 |
+
try:
|
54 |
+
if input and choice!='联网增强模式':
|
55 |
+
print('start the NO internet version of ChatGPT')
|
56 |
+
messages.append({"role": "user", "content": input})
|
57 |
+
for resp in openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages, stream=True, max_tokens=2048,temperature=0.9):
|
58 |
+
answer = str(resp['choices'][0]['delta'].get('content'))
|
59 |
+
if answer != "None":
|
60 |
+
history.append(answer)
|
61 |
+
result = "".join(history).strip() #* working!
|
62 |
+
yield result
|
63 |
+
|
64 |
+
elif input and choice=='联网增强模式':
|
65 |
+
print('start the internet version of ChatGPT')
|
66 |
+
|
67 |
+
#NOTE: 重置messages,等于遗忘了之前的所有记录。
|
68 |
+
messages = [
|
69 |
+
# {"role": "system", "content": "You are a helpful and kind AI Assistant."},
|
70 |
+
{"role": "system", "content": "你是一个专业和友好的AI助手。"},
|
71 |
+
]
|
72 |
+
|
73 |
+
internet_search_result = search(input)
|
74 |
+
search_prompt = [f"Source:\nTitle: {result['name']}\nURL: {result['url']}\nContent: {result['snippet']}" for result in internet_search_result]
|
75 |
+
print('content:\n', search_prompt[0])
|
76 |
+
# prompt = "Use these sources to answer the question:\n\n" + "\n\n".join(search_prompt) + "\n\nQuestion: " + input + "\n\nAnswer:"
|
77 |
+
prompt = "Use these sources to answer the question:\n\n" + "\n\n".join(search_prompt[0:3]) + "\n\nQuestion: " + input + "\n\nAnswer:" ## 限制了只有3个搜索结果。
|
78 |
+
messages.append({"role": "user", "content": prompt})
|
79 |
+
|
80 |
+
## no streaming version.
|
81 |
+
# messages.append({"role": "user", "content": input})
|
82 |
+
# print(input)
|
83 |
+
# chat = openai.ChatCompletion.create(
|
84 |
+
# model="gpt-3.5-turbo", messages=messages
|
85 |
+
# )
|
86 |
+
# reply = chat.choices[0].message.content
|
87 |
+
# messages.append({"role": "assistant", "content": reply})
|
88 |
+
|
89 |
+
## streaming version. typewriter effect, word by word output.
|
90 |
+
for resp in openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages, stream=True, max_tokens=2048, temperature=0.9):
|
91 |
+
|
92 |
+
# sys.stdout.write(str(resp['choices'][0]['delta'].get('content'))) ## 这个方式仅在terminal中是working的。
|
93 |
+
# sys.stdout.flush()
|
94 |
+
|
95 |
+
#* 以下内容在Gradio中是working的。
|
96 |
+
answer = str(resp['choices'][0]['delta'].get('content'))
|
97 |
+
if answer != "None":
|
98 |
+
history.append(answer)
|
99 |
+
result = "".join(history).strip() #* working!
|
100 |
+
yield result
|
101 |
+
|
102 |
+
except Exception as e:
|
103 |
+
print(e)
|
104 |
+
|
105 |
+
# return None
|
106 |
+
|
107 |
+
css = "textarea {-webkit-text-fill-color:black; -webkit-opacity: 1;}"
|
108 |
+
|
109 |
+
## 如果要优化显示效果,参见: https://gradio.app/theming-guide/
|
110 |
+
try:
|
111 |
+
inputs = [gr.inputs.Textbox(lines=5, label="请输入你的问题/任务"), gr.Radio(['默认ChatGPT模式', '联网增强模式'], label="ChatGPT运行模式")] #! working. 如果有多个input,那么需要把这些input放在一个list里面,然后传给inputs。注意这里如果更改了选项的文字,需要到chatbot中更改对应的文字。
|
112 |
+
outputs = gr.outputs.Textbox(label="ChatGPT的回答").style(show_copy_button=True)
|
113 |
+
interface = gr.Interface(
|
114 |
+
# fn=chatbot,
|
115 |
+
fn=chatbot,
|
116 |
+
inputs=inputs,
|
117 |
+
outputs=outputs,
|
118 |
+
title="极速版ChatGPT",
|
119 |
+
description="_联网增强模式:可联网的ChatGPT, 使用Internet公共搜索引擎作为增强知识库。选择这个模式,信息检索范围将大幅增加,但回答质量将变得些许不稳定,且与一般搜索引擎如百度类似,回答记忆只限于一轮。_",
|
120 |
+
theme=gr.themes.Soft(),
|
121 |
+
css=css,
|
122 |
+
)
|
123 |
+
interface.queue(concurrency_count=100)
|
124 |
+
interface.launch(height=500)
|
125 |
+
|
126 |
+
except Exception as e:
|
127 |
+
print(e)
|
128 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
openai
|
3 |
+
requests
|
4 |
+
streamlit
|
5 |
+
streamlit_chat
|