Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,92 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
""
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# 从环境变量中获取密码
|
| 6 |
+
ACCESS_PASSWORD = os.getenv("ACCESS_PASSWORD", "your_password_here") # 默认值为"your_password_here",仅测试用
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# 初始化 OpenAI 客户端
|
| 9 |
+
client = openai.Client(
|
| 10 |
+
base_url=os.getenv("OPENAI_BASE_URL"),
|
| 11 |
+
api_key=os.getenv("OPENAI_API_KEY")
|
| 12 |
+
)
|
| 13 |
|
| 14 |
+
# 可用模型列表
|
| 15 |
+
available_models = [
|
| 16 |
+
"gpt-3.5-turbo", "gpt-3.5-turbo-1106", "gpt-3.5-turbo-0125",
|
| 17 |
+
"gpt-3.5-turbo-16k", "gpt-3.5-turbo-16k-0613", "gpt-3.5-turbo-instruct",
|
| 18 |
+
"gpt-4", "gpt-4-0613", "gpt-4-1106-preview", "gpt-4-0125-preview",
|
| 19 |
+
"gpt-4-turbo-preview", "gpt-4-turbo", "gpt-4-turbo-2024-04-09",
|
| 20 |
+
"gpt-4o", "gpt-4o-2024-05-13", "gpt-4o-mini", "gpt-4o-mini-2024-07-18",
|
| 21 |
+
"text-embedding-ada-002", "text-embedding-3-small", "text-embedding-3-large",
|
| 22 |
+
"text-moderation-latest", "text-moderation-stable", "davinci-002", "babbage-002",
|
| 23 |
+
"dall-e-2", "dall-e-3", "whisper-1", "tts-1", "tts-1-1106", "tts-1-hd",
|
| 24 |
+
"tts-1-hd-1106", "o1-mini-2024-09-12", "o1-preview", "o1-mini",
|
| 25 |
+
"o1-preview-2024-09-12", "gpt-4o-2024-08-06", "chatgpt-4o-latest"
|
| 26 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
# 定义与API交互的函数
|
| 29 |
+
def generate_response(messages, model):
|
| 30 |
+
try:
|
| 31 |
+
response = client.chat.completions.create(
|
| 32 |
+
model=model,
|
| 33 |
+
messages=messages
|
| 34 |
+
)
|
| 35 |
+
return response.choices[0].message.content
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return str(e)
|
| 38 |
|
| 39 |
+
# 聊天处理函数
|
| 40 |
+
def chat_fn(history, model, user_input):
|
| 41 |
+
history.append({"role": "user", "content": user_input})
|
| 42 |
+
bot_reply = generate_response(history, model)
|
| 43 |
+
history.append({"role": "assistant", "content": bot_reply})
|
| 44 |
+
chat_history = [(msg["content"], None) if msg["role"] == "user" else (None, msg["content"]) for msg in history]
|
| 45 |
+
return chat_history, history
|
|
|
|
| 46 |
|
| 47 |
+
# 密码验证函数
|
| 48 |
+
def check_password(input_password):
|
| 49 |
+
if input_password == ACCESS_PASSWORD:
|
| 50 |
+
return "", gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
|
| 51 |
+
else:
|
| 52 |
+
return "Incorrect password. Try again.", gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)
|
| 53 |
|
| 54 |
+
# 使用 Gradio Blocks 创建聊天界面
|
| 55 |
+
with gr.Blocks() as demo:
|
| 56 |
+
gr.Markdown("# OpenAI Custom Chat API")
|
| 57 |
+
|
| 58 |
+
# 密码输入和验证区域
|
| 59 |
+
password_input = gr.Textbox(
|
| 60 |
+
placeholder="Enter password...",
|
| 61 |
+
type="password",
|
| 62 |
+
show_label=False
|
| 63 |
+
)
|
| 64 |
+
submit_btn = gr.Button("Submit")
|
| 65 |
+
login_msg = gr.Textbox(visible=False, show_label=False)
|
| 66 |
+
|
| 67 |
+
# 聊天界面部分
|
| 68 |
+
with gr.Column(visible=False) as chat_area:
|
| 69 |
+
model_selector = gr.Dropdown(
|
| 70 |
+
choices=available_models,
|
| 71 |
+
label="Select Model",
|
| 72 |
+
value="gpt-4o"
|
| 73 |
+
)
|
| 74 |
+
chatbot = gr.Chatbot(label="Chat with OpenAI")
|
| 75 |
+
state = gr.State([])
|
| 76 |
|
| 77 |
+
with gr.Row():
|
| 78 |
+
user_input = gr.Textbox(show_label=False, placeholder="Type your message here...", lines=1)
|
| 79 |
+
send_btn = gr.Button("Send")
|
| 80 |
+
|
| 81 |
+
send_btn.click(chat_fn, inputs=[state, model_selector, user_input], outputs=[chatbot, state], queue=False)
|
| 82 |
+
user_input.submit(chat_fn, inputs=[state, model_selector, user_input], outputs=[chatbot, state], queue=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
# 提交密码按钮触发密码验证
|
| 85 |
+
submit_btn.click(
|
| 86 |
+
check_password,
|
| 87 |
+
inputs=password_input,
|
| 88 |
+
outputs=[login_msg, chat_area, password_input, submit_btn]
|
| 89 |
+
)
|
| 90 |
|
| 91 |
+
# 运行 Gradio 应用并设置公开访问
|
| 92 |
+
demo.launch(share=True)
|