Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
-
import
|
| 2 |
-
import os
|
| 3 |
-
import json
|
| 4 |
import httpx
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# ============================================================
|
| 7 |
-
#
|
| 8 |
# ============================================================
|
| 9 |
STEPFUN_API_KEY = os.environ.get("STEPFUN_API_KEY", "")
|
| 10 |
STEPFUN_BASE_URL = "https://api.stepfun.com/v1"
|
|
@@ -12,38 +12,36 @@ MODEL_NAME = "step-3.5-flash"
|
|
| 12 |
HF_CONFIG_URL = "https://huggingface.co/stepfun-ai/Step-3.5-Flash/raw/main/config.json"
|
| 13 |
STEPFUN_LOGO = "https://huggingface.co/stepfun-ai/Step-3.5-Flash/resolve/main/stepfun.svg"
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
|
|
|
|
| 18 |
def fetch_model_config():
|
| 19 |
-
global cached_config
|
| 20 |
try:
|
| 21 |
response = httpx.get(HF_CONFIG_URL, timeout=10.0)
|
| 22 |
if response.status_code == 200:
|
| 23 |
-
|
| 24 |
-
return cached_config
|
| 25 |
except Exception as e:
|
| 26 |
-
|
| 27 |
-
return
|
| 28 |
|
| 29 |
|
| 30 |
def format_messages(history, system_prompt: str, user_message: str):
|
| 31 |
-
"""将 chatbot history 转换为 API 消息格式"""
|
| 32 |
messages = []
|
| 33 |
if system_prompt.strip():
|
| 34 |
messages.append({"role": "system", "content": system_prompt})
|
| 35 |
-
for
|
| 36 |
-
|
| 37 |
-
messages.append({"role": "user", "content": user_msg})
|
| 38 |
-
if bot_msg:
|
| 39 |
-
messages.append({"role": "assistant", "content": bot_msg})
|
| 40 |
messages.append({"role": "user", "content": user_message})
|
| 41 |
return messages
|
| 42 |
|
| 43 |
|
| 44 |
-
def chat_stream(message: str, history, system_prompt: str, max_tokens: int, temperature: float, top_p: float):
|
| 45 |
"""流式聊天,返回 (reasoning, content) 生成器"""
|
| 46 |
-
fetch_model_config()
|
| 47 |
messages = format_messages(history, system_prompt, message)
|
| 48 |
|
| 49 |
reasoning = ""
|
|
@@ -90,92 +88,116 @@ def chat_stream(message: str, history, system_prompt: str, max_tokens: int, temp
|
|
| 90 |
yield reasoning, f"❌ 错误: {str(e)}"
|
| 91 |
|
| 92 |
|
| 93 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
examples = [
|
| 95 |
"请解释一下什么是机器学习?",
|
| 96 |
"帮我写一个 Python 快速排序算法",
|
| 97 |
"1000以内有多少个质数?",
|
| 98 |
"一个农夫需要把狼、羊和白菜都带过河,请问农夫该怎么办?",
|
| 99 |
]
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
# 左侧:思考过程 (1)
|
| 106 |
-
with gr.Column(scale=1, min_width=250):
|
| 107 |
-
gr.Markdown("### 💭 思考过程")
|
| 108 |
-
thinking_display = gr.Textbox(
|
| 109 |
-
value="等待输入...",
|
| 110 |
-
lines=20,
|
| 111 |
-
max_lines=20,
|
| 112 |
-
interactive=False,
|
| 113 |
-
show_label=False,
|
| 114 |
-
)
|
| 115 |
-
|
| 116 |
-
# 右侧:对话 (4)
|
| 117 |
-
with gr.Column(scale=4):
|
| 118 |
-
gr.Markdown("### 💬 对话")
|
| 119 |
-
chatbot = gr.Chatbot(
|
| 120 |
-
height=450,
|
| 121 |
-
show_label=False,
|
| 122 |
-
avatar_images=(None, STEPFUN_LOGO),
|
| 123 |
-
)
|
| 124 |
-
|
| 125 |
-
with gr.Row():
|
| 126 |
-
msg = gr.Textbox(
|
| 127 |
-
placeholder="输入消息...",
|
| 128 |
-
show_label=False,
|
| 129 |
-
scale=8,
|
| 130 |
-
container=False,
|
| 131 |
-
)
|
| 132 |
-
submit_btn = gr.Button("发送", variant="primary", scale=1)
|
| 133 |
-
clear_btn = gr.Button("🗑️", scale=0, min_width=50)
|
| 134 |
-
|
| 135 |
-
# 设置(折叠)
|
| 136 |
-
with gr.Accordion("⚙️ 设置", open=False):
|
| 137 |
-
with gr.Row():
|
| 138 |
-
system_prompt = gr.Textbox(label="系统提示词", value="你是一个有帮助的 AI 助手。", scale=2)
|
| 139 |
-
max_tokens = gr.Slider(256, 131072, value=4096, step=256, label="最大长度", scale=1)
|
| 140 |
-
temperature = gr.Slider(0.0, 1.5, value=0.7, step=0.1, label="Temperature", scale=1)
|
| 141 |
-
top_p = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p", scale=1)
|
| 142 |
-
|
| 143 |
-
gr.Examples(examples, inputs=msg, label="💡 试试这些")
|
| 144 |
-
|
| 145 |
-
# 事件处理
|
| 146 |
-
def respond(message, history, system_prompt, max_tokens, temperature, top_p):
|
| 147 |
-
if not message.strip():
|
| 148 |
-
yield history, "", ""
|
| 149 |
-
return
|
| 150 |
-
|
| 151 |
-
# 添加用户消息
|
| 152 |
-
history = history + [[message, None]]
|
| 153 |
-
yield history, "", "思考中..."
|
| 154 |
-
|
| 155 |
-
reasoning = ""
|
| 156 |
-
content = ""
|
| 157 |
-
|
| 158 |
-
for r, c in chat_stream(message, history[:-1], system_prompt, max_tokens, temperature, top_p):
|
| 159 |
-
reasoning = r if r else ""
|
| 160 |
-
content = c if c else "▌"
|
| 161 |
-
history[-1][1] = content
|
| 162 |
-
yield history, "", reasoning
|
| 163 |
-
|
| 164 |
-
history[-1][1] = content
|
| 165 |
-
yield history, "", reasoning
|
| 166 |
-
|
| 167 |
-
def on_clear():
|
| 168 |
-
return [], "", "等待输入..."
|
| 169 |
-
|
| 170 |
-
msg.submit(respond, [msg, chatbot, system_prompt, max_tokens, temperature, top_p], [chatbot, msg, thinking_display])
|
| 171 |
-
submit_btn.click(respond, [msg, chatbot, system_prompt, max_tokens, temperature, top_p], [chatbot, msg, thinking_display])
|
| 172 |
-
clear_btn.click(on_clear, outputs=[chatbot, msg, thinking_display])
|
| 173 |
-
demo.load(fetch_model_config)
|
| 174 |
-
|
| 175 |
-
return demo
|
| 176 |
|
| 177 |
|
| 178 |
if __name__ == "__main__":
|
| 179 |
-
|
| 180 |
-
demo.queue()
|
| 181 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
+
import streamlit as st
|
|
|
|
|
|
|
| 2 |
import httpx
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
# ============================================================
|
| 7 |
+
# 配置
|
| 8 |
# ============================================================
|
| 9 |
STEPFUN_API_KEY = os.environ.get("STEPFUN_API_KEY", "")
|
| 10 |
STEPFUN_BASE_URL = "https://api.stepfun.com/v1"
|
|
|
|
| 12 |
HF_CONFIG_URL = "https://huggingface.co/stepfun-ai/Step-3.5-Flash/raw/main/config.json"
|
| 13 |
STEPFUN_LOGO = "https://huggingface.co/stepfun-ai/Step-3.5-Flash/resolve/main/stepfun.svg"
|
| 14 |
|
| 15 |
+
st.set_page_config(
|
| 16 |
+
page_title="Step-3.5-Flash",
|
| 17 |
+
page_icon="🚀",
|
| 18 |
+
layout="wide",
|
| 19 |
+
)
|
| 20 |
|
| 21 |
|
| 22 |
+
@st.cache_data(ttl=3600)
|
| 23 |
def fetch_model_config():
|
|
|
|
| 24 |
try:
|
| 25 |
response = httpx.get(HF_CONFIG_URL, timeout=10.0)
|
| 26 |
if response.status_code == 200:
|
| 27 |
+
return response.json()
|
|
|
|
| 28 |
except Exception as e:
|
| 29 |
+
st.error(f"拉取 config.json 失败: {e}")
|
| 30 |
+
return None
|
| 31 |
|
| 32 |
|
| 33 |
def format_messages(history, system_prompt: str, user_message: str):
|
|
|
|
| 34 |
messages = []
|
| 35 |
if system_prompt.strip():
|
| 36 |
messages.append({"role": "system", "content": system_prompt})
|
| 37 |
+
for msg in history:
|
| 38 |
+
messages.append({"role": msg["role"], "content": msg["content"]})
|
|
|
|
|
|
|
|
|
|
| 39 |
messages.append({"role": "user", "content": user_message})
|
| 40 |
return messages
|
| 41 |
|
| 42 |
|
| 43 |
+
def chat_stream(message: str, history: list, system_prompt: str, max_tokens: int, temperature: float, top_p: float):
|
| 44 |
"""流式聊天,返回 (reasoning, content) 生成器"""
|
|
|
|
| 45 |
messages = format_messages(history, system_prompt, message)
|
| 46 |
|
| 47 |
reasoning = ""
|
|
|
|
| 88 |
yield reasoning, f"❌ 错误: {str(e)}"
|
| 89 |
|
| 90 |
|
| 91 |
+
def main():
|
| 92 |
+
st.title("🚀 Step-3.5-Flash")
|
| 93 |
+
st.caption("基于 [Step-3.5-Flash](https://huggingface.co/stepfun-ai/Step-3.5-Flash) 的智能对话助手")
|
| 94 |
+
|
| 95 |
+
# 初始化 session state
|
| 96 |
+
if "messages" not in st.session_state:
|
| 97 |
+
st.session_state.messages = []
|
| 98 |
+
if "thinking" not in st.session_state:
|
| 99 |
+
st.session_state.thinking = ""
|
| 100 |
+
|
| 101 |
+
# 侧边栏设置
|
| 102 |
+
with st.sidebar:
|
| 103 |
+
st.header("⚙️ 设置")
|
| 104 |
+
system_prompt = st.text_area("系统提示词", value="你是一个有帮助的 AI 助手。", height=80)
|
| 105 |
+
max_tokens = st.slider("最大长度", 256, 131072, 4096, step=256, help="最大 128k")
|
| 106 |
+
temperature = st.slider("Temperature", 0.0, 1.5, 0.7, step=0.1)
|
| 107 |
+
top_p = st.slider("Top-p", 0.1, 1.0, 0.9, step=0.05)
|
| 108 |
+
|
| 109 |
+
st.divider()
|
| 110 |
+
if st.button("🗑️ 清空对话", use_container_width=True):
|
| 111 |
+
st.session_state.messages = []
|
| 112 |
+
st.session_state.thinking = ""
|
| 113 |
+
st.rerun()
|
| 114 |
+
|
| 115 |
+
st.divider()
|
| 116 |
+
with st.expander("📋 模型配置"):
|
| 117 |
+
config = fetch_model_config()
|
| 118 |
+
if config:
|
| 119 |
+
st.json(config)
|
| 120 |
+
|
| 121 |
+
# 主界面:左右布局
|
| 122 |
+
col_thinking, col_chat = st.columns([1, 4])
|
| 123 |
+
|
| 124 |
+
# 左侧:思考过程
|
| 125 |
+
with col_thinking:
|
| 126 |
+
st.subheader("💭 思考过程")
|
| 127 |
+
thinking_container = st.container(height=500)
|
| 128 |
+
with thinking_container:
|
| 129 |
+
if st.session_state.thinking:
|
| 130 |
+
st.markdown(st.session_state.thinking)
|
| 131 |
+
else:
|
| 132 |
+
st.caption("等待输入...")
|
| 133 |
+
|
| 134 |
+
# 右侧:对话
|
| 135 |
+
with col_chat:
|
| 136 |
+
st.subheader("💬 对话")
|
| 137 |
+
|
| 138 |
+
# 显示历史消息
|
| 139 |
+
chat_container = st.container(height=450)
|
| 140 |
+
with chat_container:
|
| 141 |
+
for msg in st.session_state.messages:
|
| 142 |
+
with st.chat_message(msg["role"], avatar=STEPFUN_LOGO if msg["role"] == "assistant" else None):
|
| 143 |
+
st.markdown(msg["content"])
|
| 144 |
+
|
| 145 |
+
# 输入框
|
| 146 |
+
if prompt := st.chat_input("输入消息..."):
|
| 147 |
+
# 添加用户消息
|
| 148 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 149 |
+
|
| 150 |
+
# 显示用户消息
|
| 151 |
+
with chat_container:
|
| 152 |
+
with st.chat_message("user"):
|
| 153 |
+
st.markdown(prompt)
|
| 154 |
+
|
| 155 |
+
# 生成回复
|
| 156 |
+
with chat_container:
|
| 157 |
+
with st.chat_message("assistant", avatar=STEPFUN_LOGO):
|
| 158 |
+
response_placeholder = st.empty()
|
| 159 |
+
thinking_placeholder = col_thinking.empty()
|
| 160 |
+
|
| 161 |
+
full_response = ""
|
| 162 |
+
full_thinking = ""
|
| 163 |
+
|
| 164 |
+
for thinking, response in chat_stream(
|
| 165 |
+
prompt,
|
| 166 |
+
st.session_state.messages[:-1],
|
| 167 |
+
system_prompt,
|
| 168 |
+
max_tokens,
|
| 169 |
+
temperature,
|
| 170 |
+
top_p,
|
| 171 |
+
):
|
| 172 |
+
full_thinking = thinking
|
| 173 |
+
full_response = response if response else "▌"
|
| 174 |
+
response_placeholder.markdown(full_response)
|
| 175 |
+
|
| 176 |
+
# 更新思考过程
|
| 177 |
+
with thinking_placeholder.container(height=500):
|
| 178 |
+
if full_thinking:
|
| 179 |
+
st.markdown(full_thinking)
|
| 180 |
+
|
| 181 |
+
# 保存消息
|
| 182 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 183 |
+
st.session_state.thinking = full_thinking
|
| 184 |
+
st.rerun()
|
| 185 |
+
|
| 186 |
+
# 示例问题
|
| 187 |
+
st.divider()
|
| 188 |
+
st.subheader("💡 试试这些")
|
| 189 |
examples = [
|
| 190 |
"请解释一下什么是机器学习?",
|
| 191 |
"帮我写一个 Python 快速排序算法",
|
| 192 |
"1000以内有多少个质数?",
|
| 193 |
"一个农夫需要把狼、羊和白菜都带过河,请问农夫该怎么办?",
|
| 194 |
]
|
| 195 |
+
cols = st.columns(len(examples))
|
| 196 |
+
for i, example in enumerate(examples):
|
| 197 |
+
if cols[i].button(example, key=f"example_{i}", use_container_width=True):
|
| 198 |
+
st.session_state.messages.append({"role": "user", "content": example})
|
| 199 |
+
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
|
| 202 |
if __name__ == "__main__":
|
| 203 |
+
main()
|
|
|
|
|
|