Hmjz100 commited on
Commit
fff5583
·
1 Parent(s): 5045fb5

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -18
app.py CHANGED
@@ -7,17 +7,17 @@ import requests
7
  API_URL = "https://ai.fakeopen.com/v1/chat/completions" # 用户需要提供自己的 OPENAI_API_KEY
8
 
9
  # 推断函数
10
- def predict(openai_gpt4_key, system_msg, inputs, top_p, temperature, max_tokens, presence_penalty, frequency_penalty, chat_counter, chatbot=[], history=[]):
11
 
12
  print(f"————————————————————")
13
  if inputs.strip() == '':
14
  inputs = "你好呀,简单介绍下你自己吧!"
15
- if openai_gpt4_key.strip() == '':
16
- openai_gpt4_key = "pk-this-is-a-real-free-pool-token-for-everyone"
17
 
18
  headers = {
19
  "Content-Type": "application/json",
20
- "Authorization": f"Bearer {openai_gpt4_key}" # 用户将提供自己的 OPENAI_API_KEY
21
  }
22
 
23
  print(f"用户消息: {inputs}")
@@ -61,7 +61,7 @@ def predict(openai_gpt4_key, system_msg, inputs, top_p, temperature, max_tokens,
61
  messages.append(temp)
62
  # 消息
63
  payload = {
64
- "model": "gpt-4",
65
  "messages": messages, # 类型为 [{"role": "user", "content": f"{inputs}"}],
66
  "temperature": temperature, # 温度
67
  "top_p": top_p, # Top-p
@@ -114,12 +114,12 @@ def set_visible_false():
114
  def set_visible_true():
115
  return gr.update(visible=True)
116
 
117
- title = """<h1 align="center">🔥 使用 Chat-Completions API 和 🚀 Gradio-Streaming 的 GPT4</h1>"""
118
  # 主题功能的显示消息
119
  theme_addon_msg = """<center>🌟 这个演示还向你介绍了 Gradio 主题。在 Gradio 网站上查看我们的 <a href="https://gradio.app/theming-guide/" target="_blank">主题指南🎨</a>来了解更多吧!你可以从头开始开发,用 <code>theme.push_to_hub()</code> 修改现有的 Gradio 主题,并简单地上传到 huggingface-hub 来与社区分享你的主题。</center>
120
  """
121
 
122
- # 使用信息添加有关 GPT4 系统消息的其他信息
123
  system_msg_info = """对话可以从系统消息开始,以轻松地指导助手的行为。
124
  系统消息有助于设置 AI 助手的行为。例如,可以用 '你是一个有帮助的助手。' 来指示助手。"""
125
 
@@ -130,24 +130,24 @@ theme = gr.themes.Soft(primary_hue="zinc", secondary_hue="purple", neutral_hue="
130
  with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;} #chatbot {height: 520px; overflow: auto;}""",
131
  theme=theme) as demo:
132
  gr.HTML(title)
133
- gr.HTML("""<h3 align="center">🔥 这个 Huggingface Gradio 演示为你提供了使用 GPT4 API 的访问权限,还支持系统消息。请注意,你需要提供自己的 OPENAI API 密钥以访问 GPT4 🙌</h1>""")
134
  gr.HTML(theme_addon_msg)
135
- gr.HTML('''<center><a href="https://huggingface.co/spaces/Hmjz100/ChatGPT4?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>复制这个 Space 并使用你的 OpenAI API 密钥安全运行</center>''')
136
 
137
  with gr.Column(elem_id = "col_container"):
138
- # 用户需要提供自己的 GPT4 API 密钥,不再由 Huggingface 提供
139
  with gr.Row():
140
- openai_gpt4_key = gr.Textbox(
141
- label="OpenAI GPT4 密钥",
142
  type="password",
143
  placeholder="pk-this-is-a-real-free-pool-token-for-everyone",
144
- info="您可以提供自己的 GPT4 密钥,或者使用自带的秘钥",
145
  )
146
  with gr.Accordion(label="系统消息:", open=False):
147
  system_msg = gr.Textbox(label="指示 AI 助手设置其行为", info=system_msg_info, value="", placeholder="在这里输入..")
148
  accordion_msg = gr.HTML(value="🚧 要修改系统消息,你必须刷新页面", visible=False)
149
 
150
- chatbot = gr.Chatbot(label='GPT4', elem_id="chatbot")
151
  inputs = gr.Textbox(placeholder="嗨!", label="输入文本并按 Enter 键")
152
  state = gr.State([])
153
  with gr.Row():
@@ -158,6 +158,18 @@ with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
158
 
159
  # 参数设置
160
  with gr.Accordion("高级参数", open=False):
 
 
 
 
 
 
 
 
 
 
 
 
161
  top_p = gr.Slider(
162
  minimum=-0, maximum=1.0, value=1.0, step=0.05,
163
  interactive=True,
@@ -171,10 +183,10 @@ with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
171
  info="使用何种采样温度,值在 0 到 2 之间。较高的数值如 0.8 会使输出更加随机,而较低的数值如 0.2 会使输出更加集中和确定。我们通常建议修改此参数或 Top-p,但不要同时修改两者。(默认: 1)",
172
  )
173
  max_tokens = gr.Slider(
174
- minimum=-0, maximum=8192, value=4096, step=1,
175
  interactive=True,
176
  label="最大 Token",
177
- info="助手生成一条信息可以包含的最大 token 数。最大 token 数也受到模型的总长度限制,上文的 token 数和生成的 token 数之和不能超过模型的 token 总数(默认: 4096)",
178
  )
179
  presence_penalty = gr.Slider(
180
  minimum=-2.0, maximum=2.0, value=0, step=0.1,
@@ -191,8 +203,8 @@ with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
191
  chat_counter = gr.Number(value=0, visible=False, precision=0)
192
 
193
  # 事件处理
194
- inputs.submit(predict, [openai_gpt4_key, system_msg, inputs, top_p, temperature, max_tokens, presence_penalty, frequency_penalty, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code],) # openai_api_key
195
- b1.click(predict, [openai_gpt4_key, system_msg, inputs, top_p, temperature, max_tokens, presence_penalty, frequency_penalty, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code],) # openai_api_key
196
 
197
  inputs.submit(set_visible_false, [], [system_msg])
198
  b1.click(set_visible_false, [], [system_msg])
 
7
  API_URL = "https://ai.fakeopen.com/v1/chat/completions" # 用户需要提供自己的 OPENAI_API_KEY
8
 
9
  # 推断函数
10
+ def predict(openai_gptapi_key, system_msg, inputs, top_p, temperature, max_tokens, presence_penalty, frequency_penalty, chat_counter, chatbot=[], history=[]):
11
 
12
  print(f"————————————————————")
13
  if inputs.strip() == '':
14
  inputs = "你好呀,简单介绍下你自己吧!"
15
+ if openai_gptapi_key.strip() == '':
16
+ openai_gptapi_key = "pk-this-is-a-real-free-pool-token-for-everyone"
17
 
18
  headers = {
19
  "Content-Type": "application/json",
20
+ "Authorization": f"Bearer {openai_gptapi_key}" # 用户将提供自己的 OPENAI_API_KEY
21
  }
22
 
23
  print(f"用户消息: {inputs}")
 
61
  messages.append(temp)
62
  # 消息
63
  payload = {
64
+ "model": model,
65
  "messages": messages, # 类型为 [{"role": "user", "content": f"{inputs}"}],
66
  "temperature": temperature, # 温度
67
  "top_p": top_p, # Top-p
 
114
  def set_visible_true():
115
  return gr.update(visible=True)
116
 
117
+ title = """<h1 align="center">🔥 使用 Chat-Completions API 和 🚀 Gradio-Streaming 的 ChatGPT</h1>"""
118
  # 主题功能的显示消息
119
  theme_addon_msg = """<center>🌟 这个演示还向你介绍了 Gradio 主题。在 Gradio 网站上查看我们的 <a href="https://gradio.app/theming-guide/" target="_blank">主题指南🎨</a>来了解更多吧!你可以从头开始开发,用 <code>theme.push_to_hub()</code> 修改现有的 Gradio 主题,并简单地上传到 huggingface-hub 来与社区分享你的主题。</center>
120
  """
121
 
122
+ # 使用信息添加有关 ChatGPT 系统消息的其他信息
123
  system_msg_info = """对话可以从系统消息开始,以轻松地指导助手的行为。
124
  系统消息有助于设置 AI 助手的行为。例如,可以用 '你是一个有帮助的助手。' 来指示助手。"""
125
 
 
130
  with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;} #chatbot {height: 520px; overflow: auto;}""",
131
  theme=theme) as demo:
132
  gr.HTML(title)
133
+ gr.HTML("""<h3 align="center">🔥 这个 Huggingface Gradio 演示为你提供了使用 ChatGPT API 的访问权限,还支持系统消息。请注意,你需要提供自己的 OPENAI API 密钥以访问 ChatGPT 🙌</h1>""")
134
  gr.HTML(theme_addon_msg)
135
+ gr.HTML('''<center><a href="https://huggingface.co/spaces/Hmjz100/ChatChatGPT?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>复制这个 Space 并使用你的 OpenAI API 密钥安全运行</center>''')
136
 
137
  with gr.Column(elem_id = "col_container"):
138
+ # 用户需要提供自己的 ChatGPT API 密钥,不再由 Huggingface 提供
139
  with gr.Row():
140
+ openai_gptapi_key = gr.Textbox(
141
+ label="OpenAI ChatGPT 密钥",
142
  type="password",
143
  placeholder="pk-this-is-a-real-free-pool-token-for-everyone",
144
+ info="您可以提供自己的 ChatGPT 密钥,或者使用自带的秘钥",
145
  )
146
  with gr.Accordion(label="系统消息:", open=False):
147
  system_msg = gr.Textbox(label="指示 AI 助手设置其行为", info=system_msg_info, value="", placeholder="在这里输入..")
148
  accordion_msg = gr.HTML(value="🚧 要修改系统消息,你必须刷新页面", visible=False)
149
 
150
+ chatbot = gr.Chatbot(label='ChatGPT', elem_id="chatbot")
151
  inputs = gr.Textbox(placeholder="嗨!", label="输入文本并按 Enter 键")
152
  state = gr.State([])
153
  with gr.Row():
 
158
 
159
  # 参数设置
160
  with gr.Accordion("高级参数", open=False):
161
+ model_max_tokens = {
162
+ "gpt-4": 8192,
163
+ "gpt-4-32k": 32768,
164
+ "gpt-3.5-turbo": 4096,
165
+ "gpt-3.5-turbo-16k": 16384,
166
+ }
167
+ model = gr.RadioButton(
168
+ choices=["gpt-4", "gpt-4-32k", "gpt-3.5-turbo", "gpt-3.5-turbo-16k"],
169
+ label="模型",
170
+ default="gpt-4",
171
+ group="model_group",
172
+ )
173
  top_p = gr.Slider(
174
  minimum=-0, maximum=1.0, value=1.0, step=0.05,
175
  interactive=True,
 
183
  info="使用何种采样温度,值在 0 到 2 之间。较高的数值如 0.8 会使输出更加随机,而较低的数值如 0.2 会使输出更加集中和确定。我们通常建议修改此参数或 Top-p,但不要同时修改两者。(默认: 1)",
184
  )
185
  max_tokens = gr.Slider(
186
+ minimum=-0, maximum=model_max_tokens[model], value=4000, step=1,
187
  interactive=True,
188
  label="最大 Token",
189
+ info="助手生成一条信息可以包含的最大 token 数。最大 token 数也受到模型的总长度限制,上文的 token 数和生成的 token 数之和不能超过模型的 token 总数(默认: 4000)",
190
  )
191
  presence_penalty = gr.Slider(
192
  minimum=-2.0, maximum=2.0, value=0, step=0.1,
 
203
  chat_counter = gr.Number(value=0, visible=False, precision=0)
204
 
205
  # 事件处理
206
+ inputs.submit(predict, [openai_gptapi_key, model, system_msg, inputs, top_p, temperature, max_tokens, presence_penalty, frequency_penalty, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code],) # openai_api_key
207
+ b1.click(predict, [openai_gptapi_key, model, system_msg, inputs, top_p, temperature, max_tokens, presence_penalty, frequency_penalty, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code],) # openai_api_key
208
 
209
  inputs.submit(set_visible_false, [], [system_msg])
210
  b1.click(set_visible_false, [], [system_msg])