Spaces:
Sleeping
Sleeping
wanglei2762
commited on
Commit
·
4d41c6a
1
Parent(s):
10911bb
Init StarrySpace,Add image(s) using LFS
Browse files- app.py +170 -49
- images/bot_avatar.png +3 -0
- images/user_avatar.png +3 -0
app.py
CHANGED
|
@@ -1,64 +1,185 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
"""
|
| 5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
for
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
|
|
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
+
import time
|
| 5 |
|
| 6 |
+
# --- 配置 ---
|
| 7 |
+
ARK_API_KEY = os.environ.get("ARK_API_KEY")
|
| 8 |
+
if not ARK_API_KEY:
|
| 9 |
+
raise ValueError("ARK_API_KEY not found. Please set it in Space Secrets.")
|
| 10 |
+
|
| 11 |
+
client = openai.OpenAI(api_key=ARK_API_KEY, base_url="https://ark.cn-beijing.volces.com/api/v3")
|
| 12 |
+
|
| 13 |
+
# --- 可选模型列表 ---
|
| 14 |
+
# 键是用户看到的显示名称,值是实际的API模型ID
|
| 15 |
+
AVAILABLE_MODELS = {
|
| 16 |
+
"DeepSeek R1 (250120)": "deepseek-r1-250120",
|
| 17 |
+
"DeepSeek v3 (250324)": "deepseek-v3-250324" # 示例,你需要有这个模型的权限
|
| 18 |
+
# 你可以添加更多你账户下可用的模型
|
| 19 |
+
# "模型名称A": "模型ID_A",
|
| 20 |
+
# "模型名称B": "模型ID_B",
|
| 21 |
+
}
|
| 22 |
+
DEFAULT_MODEL_DISPLAY_NAME = list(AVAILABLE_MODELS.keys())[0] # 默认选择第一个模型
|
| 23 |
+
|
| 24 |
+
# --- 自定义 CSS ---
|
| 25 |
+
custom_css = """
|
| 26 |
+
body {
|
| 27 |
+
background-color: #f0f2f5;
|
| 28 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 29 |
+
}
|
| 30 |
+
.gradio-container {
|
| 31 |
+
border-radius: 15px !important;
|
| 32 |
+
box-shadow: 0 4px 12px rgba(0,0,0,0.1) !important;
|
| 33 |
+
background-color: #ffffff;
|
| 34 |
+
}
|
| 35 |
+
.message-bubble-user {
|
| 36 |
+
background-color: #007bff !important;
|
| 37 |
+
color: white !important;
|
| 38 |
+
border-radius: 15px 15px 0 15px !important;
|
| 39 |
+
align-self: flex-end;
|
| 40 |
+
max-width: 70%;
|
| 41 |
+
}
|
| 42 |
+
.message-bubble-bot {
|
| 43 |
+
background-color: #e9ecef !important;
|
| 44 |
+
color: #333 !important;
|
| 45 |
+
border-radius: 15px 15px 15px 0 !important;
|
| 46 |
+
align-self: flex-start;
|
| 47 |
+
max-width: 70%;
|
| 48 |
+
}
|
| 49 |
+
.message-bubble-user .message-text, .message-bubble-bot .message-text {
|
| 50 |
+
font-size: 1rem !important;
|
| 51 |
+
padding: 10px 15px !important;
|
| 52 |
+
}
|
| 53 |
+
.avatar-container img {
|
| 54 |
+
border-radius: 50% !important;
|
| 55 |
+
width: 40px !important;
|
| 56 |
+
height: 40px !important;
|
| 57 |
+
object-fit: cover;
|
| 58 |
+
margin: 5px !important;
|
| 59 |
+
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
| 60 |
+
}
|
| 61 |
+
.textbox textarea {
|
| 62 |
+
border-radius: 20px !important;
|
| 63 |
+
border: 1px solid #ced4da !important;
|
| 64 |
+
padding: 10px 15px !important;
|
| 65 |
+
font-size: 1rem !important;
|
| 66 |
+
box-shadow: none !important;
|
| 67 |
+
}
|
| 68 |
+
.submit-button {
|
| 69 |
+
background-color: #007bff !important;
|
| 70 |
+
color: white !important;
|
| 71 |
+
border-radius: 20px !important;
|
| 72 |
+
font-weight: bold !important;
|
| 73 |
+
padding: 10px 20px !important;
|
| 74 |
+
transition: background-color 0.2s ease-in-out;
|
| 75 |
+
}
|
| 76 |
+
.submit-button:hover {
|
| 77 |
+
background-color: #0056b3 !important;
|
| 78 |
+
}
|
| 79 |
+
.gr-prose h1, .gr-prose h2, .gr-prose h3 {
|
| 80 |
+
color: #343a40;
|
| 81 |
+
text-align: center;
|
| 82 |
+
}
|
| 83 |
+
.gr-prose p {
|
| 84 |
+
color: #495057;
|
| 85 |
+
text-align: center;
|
| 86 |
+
font-size: 1.1em;
|
| 87 |
+
}
|
| 88 |
+
/* 为下拉菜单添加一些边距 */
|
| 89 |
+
.gradio-dropdown {
|
| 90 |
+
margin-bottom: 15px !important;
|
| 91 |
+
}
|
| 92 |
"""
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
# --- 聊天逻辑 (修改以接收模型ID) ---
|
| 95 |
+
def call_openai_with_history(message: str, history: list, selected_model_display_name: str):
|
| 96 |
+
"""
|
| 97 |
+
调用 OpenAI API,并处理聊天历史。
|
| 98 |
+
message: 当前用户输入
|
| 99 |
+
history: Gradio 的聊天历史
|
| 100 |
+
selected_model_display_name: 用户从下拉菜单选择的模型显示名称
|
| 101 |
+
"""
|
| 102 |
+
actual_model_id = AVAILABLE_MODELS.get(selected_model_display_name, AVAILABLE_MODELS[DEFAULT_MODEL_DISPLAY_NAME])
|
| 103 |
+
print(f"使用模型: {actual_model_id}") # 调试信息
|
| 104 |
|
| 105 |
+
messages_for_api = [{"role": "system", "content": "你是一个乐于助人、风趣幽默的AI助手。"}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
for user_msg, bot_msg in history:
|
| 108 |
+
# Gradio 的 history 格式是 [[user_msg1, bot_msg1], ...]
|
| 109 |
+
# 当 chatbot type='messages' 时,history 格式是 [{'role':'user', 'content':...}, {'role':'assistant', 'content':...}]
|
| 110 |
+
# 这里需要根据 chatbot 的 type 来调整 history 的处理方式
|
| 111 |
+
# 由于 chatbot_component.type = 'messages', history 已经是 OpenAI 期望的格式了
|
| 112 |
+
# 但是 ChatInterface 传递给 fn 的 history 仍然是 [[user, bot], ...] 格式
|
| 113 |
+
# 所以我们仍然需要转换
|
| 114 |
+
if isinstance(user_msg, str): # 确保是 [[user, bot], ...] 格式
|
| 115 |
+
messages_for_api.append({"role": "user", "content": user_msg})
|
| 116 |
+
if bot_msg and isinstance(bot_msg, str):
|
| 117 |
+
messages_for_api.append({"role": "assistant", "content": bot_msg})
|
| 118 |
|
| 119 |
+
messages_for_api.append({"role": "user", "content": message})
|
| 120 |
|
| 121 |
+
try:
|
| 122 |
+
response = client.chat.completions.create(
|
| 123 |
+
model=actual_model_id, # 使用选择的模型ID
|
| 124 |
+
messages=messages_for_api,
|
| 125 |
+
temperature=0.7,
|
| 126 |
+
max_tokens=1024, # 可以适当增加 max_tokens
|
| 127 |
+
)
|
| 128 |
+
bot_response = response.choices[0].message.content
|
| 129 |
+
return bot_response
|
| 130 |
|
| 131 |
+
except openai.APIError as e:
|
| 132 |
+
error_message = f"OpenAI API Error: {e}"
|
| 133 |
+
print(error_message)
|
| 134 |
+
if hasattr(e, 'message'):
|
| 135 |
+
return f"抱歉,调用AI服务时出错: {e.message}"
|
| 136 |
+
return f"抱歉,调用AI服务时出错: {str(e)}"
|
| 137 |
+
except Exception as e:
|
| 138 |
+
error_message = f"An unexpected error occurred: {e}"
|
| 139 |
+
print(error_message)
|
| 140 |
+
return "抱歉,发生了一个未知错误。"
|
| 141 |
|
|
|
|
|
|
|
| 142 |
|
| 143 |
+
# --- Gradio 界面 ---
|
| 144 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
|
| 145 |
+
gr.Markdown("# 🤖 Starry的智能AI聊天伙伴")
|
| 146 |
+
gr.Markdown("与基于大模型的AI助手进行友好对话吧!")
|
| 147 |
|
| 148 |
+
# 添加模型选择下拉菜单
|
| 149 |
+
model_selector = gr.Dropdown(
|
| 150 |
+
choices=list(AVAILABLE_MODELS.keys()), # 用户看到的选项是显示名称
|
| 151 |
+
value=DEFAULT_MODEL_DISPLAY_NAME, # 默认值
|
| 152 |
+
label="选择AI模型",
|
| 153 |
+
info="更改模型后,新的对话将使用所选模型。",
|
| 154 |
+
elem_classes="gradio-dropdown" # 应用自定义CSS类 (可选)
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
chatbot_component = gr.Chatbot(
|
| 158 |
+
label="聊天窗口",
|
| 159 |
+
type='messages', # 确保使用 'messages' 类型,以便更好地处理头像和角色
|
| 160 |
+
avatar_images=("images/user_avatar.png", "images/bot_avatar.png"),
|
| 161 |
+
height=500,
|
| 162 |
+
show_label=False # 隐藏 Chatbot 自己的标签,因为上面已经有标题了
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
chat_interface = gr.ChatInterface(
|
| 166 |
+
fn=call_openai_with_history,
|
| 167 |
+
chatbot=chatbot_component,
|
| 168 |
+
additional_inputs=[model_selector], # 将下拉菜单作为额外输入传递给 fn
|
| 169 |
+
examples=[
|
| 170 |
+
["你好呀!"],
|
| 171 |
+
["给我讲个关于太空的笑话。"],
|
| 172 |
+
["用Python写一个快速排序算法。"],
|
| 173 |
+
],
|
| 174 |
+
submit_btn="➤ 发送",
|
| 175 |
+
)
|
| 176 |
|
| 177 |
+
# 如果你的 Gradio 版本较低,可能没有 avatar_images 直接在 Chatbot 构造函数里
|
| 178 |
+
# 如果头像图片不显示,请确保 `images` 文件夹和里面的 `user_avatar.png` `bot_avatar.png`
|
| 179 |
+
# 文件与你的 app.py 在同一级别,或者你提供了正确的相对/绝对路径。
|
| 180 |
+
# 对于 Hugging Face Spaces,通常将 `images` 文件夹放在仓库根目录。
|
| 181 |
|
| 182 |
+
# --- 启动应用 ---
|
| 183 |
if __name__ == "__main__":
|
| 184 |
+
# demo.queue() # 如果 API 调用时间较长,可以启用队列
|
| 185 |
+
demo.launch()
|
images/bot_avatar.png
ADDED
|
|
Git LFS Details
|
images/user_avatar.png
ADDED
|
|
Git LFS Details
|