Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,31 +21,42 @@ st.set_page_config(
|
|
| 21 |
# 自定义样式
|
| 22 |
st.markdown("""
|
| 23 |
<style>
|
| 24 |
-
/*
|
| 25 |
-
.
|
| 26 |
background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
|
| 27 |
border: 1px solid #e2e8f0;
|
| 28 |
border-radius: 12px;
|
| 29 |
padding: 16px;
|
| 30 |
-
height: calc(100vh -
|
| 31 |
overflow-y: auto;
|
| 32 |
font-size: 14px;
|
| 33 |
line-height: 1.7;
|
| 34 |
-
color: #475569;
|
| 35 |
white-space: pre-wrap;
|
| 36 |
word-break: break-word;
|
| 37 |
}
|
| 38 |
-
.
|
| 39 |
width: 6px;
|
| 40 |
}
|
| 41 |
-
.
|
| 42 |
background: #cbd5e1;
|
| 43 |
border-radius: 3px;
|
| 44 |
}
|
| 45 |
-
.
|
| 46 |
background: #94a3b8;
|
| 47 |
}
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
font-size: 14px;
|
| 50 |
font-weight: 600;
|
| 51 |
color: #64748b;
|
|
@@ -54,10 +65,8 @@ st.markdown("""
|
|
| 54 |
align-items: center;
|
| 55 |
gap: 6px;
|
| 56 |
}
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
}
|
| 60 |
-
.no-thinking {
|
| 61 |
color: #94a3b8;
|
| 62 |
font-style: italic;
|
| 63 |
text-align: center;
|
|
@@ -68,9 +77,10 @@ st.markdown("""
|
|
| 68 |
#MainMenu {visibility: hidden;}
|
| 69 |
footer {visibility: hidden;}
|
| 70 |
|
| 71 |
-
/* 调整
|
| 72 |
.block-container {
|
| 73 |
-
padding-top:
|
|
|
|
| 74 |
}
|
| 75 |
</style>
|
| 76 |
""", unsafe_allow_html=True)
|
|
@@ -149,33 +159,40 @@ def chat_stream(message: str, history: list, system_prompt: str, max_tokens: int
|
|
| 149 |
|
| 150 |
|
| 151 |
def render_thinking_panel(thinking_text: str):
|
| 152 |
-
"""渲染
|
| 153 |
if thinking_text:
|
| 154 |
escaped_text = thinking_text.replace("&", "&").replace("<", "<").replace(">", ">")
|
| 155 |
st.markdown(f"""
|
| 156 |
-
<div class="
|
| 157 |
-
<div class="thinking-panel">{escaped_text}</div>
|
| 158 |
""", unsafe_allow_html=True)
|
| 159 |
else:
|
| 160 |
st.markdown("""
|
| 161 |
-
<div class="
|
| 162 |
-
<div class="thinking-panel">
|
| 163 |
-
<div class="no-
|
| 164 |
</div>
|
| 165 |
""", unsafe_allow_html=True)
|
| 166 |
|
| 167 |
|
| 168 |
-
def
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
-
# 初始化 session state
|
| 172 |
-
if "messages" not in st.session_state:
|
| 173 |
-
st.session_state.messages = []
|
| 174 |
-
if "pending_prompt" not in st.session_state:
|
| 175 |
-
st.session_state.pending_prompt = None
|
| 176 |
-
if "current_thinking" not in st.session_state:
|
| 177 |
-
st.session_state.current_thinking = ""
|
| 178 |
|
|
|
|
| 179 |
# 侧边栏设置
|
| 180 |
with st.sidebar:
|
| 181 |
st.header("⚙️ 设置")
|
|
@@ -188,6 +205,7 @@ def main():
|
|
| 188 |
if st.button("🗑️ 清空对话", use_container_width=True):
|
| 189 |
st.session_state.messages = []
|
| 190 |
st.session_state.current_thinking = ""
|
|
|
|
| 191 |
st.rerun()
|
| 192 |
|
| 193 |
st.divider()
|
|
@@ -196,106 +214,115 @@ def main():
|
|
| 196 |
if config:
|
| 197 |
st.json(config)
|
| 198 |
|
| 199 |
-
#
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
-
#
|
| 203 |
-
|
| 204 |
-
# 获取最新的思考内容
|
| 205 |
-
latest_thinking = st.session_state.current_thinking
|
| 206 |
-
if not latest_thinking and st.session_state.messages:
|
| 207 |
-
# 从最后一条 assistant 消息获取思考内容
|
| 208 |
-
for msg in reversed(st.session_state.messages):
|
| 209 |
-
if msg["role"] == "assistant" and msg.get("thinking"):
|
| 210 |
-
latest_thinking = msg["thinking"]
|
| 211 |
-
break
|
| 212 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
thinking_placeholder = st.empty()
|
| 214 |
with thinking_placeholder:
|
| 215 |
-
render_thinking_panel(
|
| 216 |
|
| 217 |
-
# 右侧:对话区域
|
| 218 |
with right_col:
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
with thinking_placeholder:
|
| 271 |
-
render_thinking_panel(full_thinking)
|
| 272 |
-
|
| 273 |
-
# 更新回答
|
| 274 |
-
response_placeholder.markdown(full_response)
|
| 275 |
-
|
| 276 |
-
# 保存消息
|
| 277 |
-
st.session_state.messages.append({
|
| 278 |
-
"role": "assistant",
|
| 279 |
-
"content": full_response,
|
| 280 |
-
"thinking": full_thinking,
|
| 281 |
-
})
|
| 282 |
-
st.session_state.current_thinking = ""
|
| 283 |
-
st.rerun()
|
| 284 |
-
|
| 285 |
-
# 示例问题(底部)
|
| 286 |
-
if not st.session_state.messages:
|
| 287 |
-
st.divider()
|
| 288 |
-
st.caption("💡 试试这些问题:")
|
| 289 |
-
examples = [
|
| 290 |
-
"请解释一下什么是机器学习?",
|
| 291 |
-
"帮我写一个 Python 快速排序算法",
|
| 292 |
-
"1000以内有多少个质数?",
|
| 293 |
-
]
|
| 294 |
-
cols = st.columns(len(examples))
|
| 295 |
-
for i, example in enumerate(examples):
|
| 296 |
-
if cols[i].button(example, key=f"ex_{i}", use_container_width=True):
|
| 297 |
-
st.session_state.pending_prompt = example
|
| 298 |
-
st.rerun()
|
| 299 |
|
| 300 |
|
| 301 |
if __name__ == "__main__":
|
|
|
|
| 21 |
# 自定义样式
|
| 22 |
st.markdown("""
|
| 23 |
<style>
|
| 24 |
+
/* 面板通用样式 */
|
| 25 |
+
.panel {
|
| 26 |
background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
|
| 27 |
border: 1px solid #e2e8f0;
|
| 28 |
border-radius: 12px;
|
| 29 |
padding: 16px;
|
| 30 |
+
height: calc(100vh - 280px);
|
| 31 |
overflow-y: auto;
|
| 32 |
font-size: 14px;
|
| 33 |
line-height: 1.7;
|
|
|
|
| 34 |
white-space: pre-wrap;
|
| 35 |
word-break: break-word;
|
| 36 |
}
|
| 37 |
+
.panel::-webkit-scrollbar {
|
| 38 |
width: 6px;
|
| 39 |
}
|
| 40 |
+
.panel::-webkit-scrollbar-thumb {
|
| 41 |
background: #cbd5e1;
|
| 42 |
border-radius: 3px;
|
| 43 |
}
|
| 44 |
+
.panel::-webkit-scrollbar-thumb:hover {
|
| 45 |
background: #94a3b8;
|
| 46 |
}
|
| 47 |
+
|
| 48 |
+
/* 思考面板 */
|
| 49 |
+
.thinking-panel {
|
| 50 |
+
color: #64748b;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
/* 回答面板 */
|
| 54 |
+
.answer-panel {
|
| 55 |
+
color: #1e293b;
|
| 56 |
+
background: linear-gradient(135deg, #ffffff 0%, #f8fafc 100%);
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
.panel-title {
|
| 60 |
font-size: 14px;
|
| 61 |
font-weight: 600;
|
| 62 |
color: #64748b;
|
|
|
|
| 65 |
align-items: center;
|
| 66 |
gap: 6px;
|
| 67 |
}
|
| 68 |
+
|
| 69 |
+
.no-content {
|
|
|
|
|
|
|
| 70 |
color: #94a3b8;
|
| 71 |
font-style: italic;
|
| 72 |
text-align: center;
|
|
|
|
| 77 |
#MainMenu {visibility: hidden;}
|
| 78 |
footer {visibility: hidden;}
|
| 79 |
|
| 80 |
+
/* 调整布局 */
|
| 81 |
.block-container {
|
| 82 |
+
padding-top: 1.5rem;
|
| 83 |
+
padding-bottom: 1rem;
|
| 84 |
}
|
| 85 |
</style>
|
| 86 |
""", unsafe_allow_html=True)
|
|
|
|
| 159 |
|
| 160 |
|
| 161 |
def render_thinking_panel(thinking_text: str):
|
| 162 |
+
"""渲染思考面板"""
|
| 163 |
if thinking_text:
|
| 164 |
escaped_text = thinking_text.replace("&", "&").replace("<", "<").replace(">", ">")
|
| 165 |
st.markdown(f"""
|
| 166 |
+
<div class="panel-title">💭 思考过程</div>
|
| 167 |
+
<div class="panel thinking-panel">{escaped_text}</div>
|
| 168 |
""", unsafe_allow_html=True)
|
| 169 |
else:
|
| 170 |
st.markdown("""
|
| 171 |
+
<div class="panel-title">💭 思考过程</div>
|
| 172 |
+
<div class="panel thinking-panel">
|
| 173 |
+
<div class="no-content">等待模型思考...</div>
|
| 174 |
</div>
|
| 175 |
""", unsafe_allow_html=True)
|
| 176 |
|
| 177 |
|
| 178 |
+
def render_answer_panel(answer_text: str):
|
| 179 |
+
"""渲染回答面板"""
|
| 180 |
+
if answer_text:
|
| 181 |
+
escaped_text = answer_text.replace("&", "&").replace("<", "<").replace(">", ">")
|
| 182 |
+
st.markdown(f"""
|
| 183 |
+
<div class="panel-title">💬 回答</div>
|
| 184 |
+
<div class="panel answer-panel">{escaped_text}</div>
|
| 185 |
+
""", unsafe_allow_html=True)
|
| 186 |
+
else:
|
| 187 |
+
st.markdown("""
|
| 188 |
+
<div class="panel-title">💬 回答</div>
|
| 189 |
+
<div class="panel answer-panel">
|
| 190 |
+
<div class="no-content">等待模型回答...</div>
|
| 191 |
+
</div>
|
| 192 |
+
""", unsafe_allow_html=True)
|
| 193 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
+
def main():
|
| 196 |
# 侧边栏设置
|
| 197 |
with st.sidebar:
|
| 198 |
st.header("⚙️ 设置")
|
|
|
|
| 205 |
if st.button("🗑️ 清空对话", use_container_width=True):
|
| 206 |
st.session_state.messages = []
|
| 207 |
st.session_state.current_thinking = ""
|
| 208 |
+
st.session_state.current_answer = ""
|
| 209 |
st.rerun()
|
| 210 |
|
| 211 |
st.divider()
|
|
|
|
| 214 |
if config:
|
| 215 |
st.json(config)
|
| 216 |
|
| 217 |
+
# 初始化 session state
|
| 218 |
+
if "messages" not in st.session_state:
|
| 219 |
+
st.session_state.messages = []
|
| 220 |
+
if "pending_prompt" not in st.session_state:
|
| 221 |
+
st.session_state.pending_prompt = None
|
| 222 |
+
if "current_thinking" not in st.session_state:
|
| 223 |
+
st.session_state.current_thinking = ""
|
| 224 |
+
if "current_answer" not in st.session_state:
|
| 225 |
+
st.session_state.current_answer = ""
|
| 226 |
|
| 227 |
+
# 标题
|
| 228 |
+
st.title("🚀 Step-3.5-Flash")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
+
# 输入区域(标题下方)
|
| 231 |
+
input_col1, input_col2 = st.columns([6, 1])
|
| 232 |
+
with input_col1:
|
| 233 |
+
user_input = st.text_input("输入消息", placeholder="请输入您的问题...", label_visibility="collapsed")
|
| 234 |
+
with input_col2:
|
| 235 |
+
send_clicked = st.button("发送", use_container_width=True, type="primary")
|
| 236 |
+
|
| 237 |
+
# 示例问题
|
| 238 |
+
if not st.session_state.messages:
|
| 239 |
+
st.caption("💡 试试这些问题:")
|
| 240 |
+
examples = [
|
| 241 |
+
"请解释一下什么是机器学习?",
|
| 242 |
+
"帮我写一个 Python 快速排序算法",
|
| 243 |
+
"1000以内有多少个质数?",
|
| 244 |
+
]
|
| 245 |
+
cols = st.columns(len(examples))
|
| 246 |
+
for i, example in enumerate(examples):
|
| 247 |
+
if cols[i].button(example, key=f"ex_{i}", use_container_width=True):
|
| 248 |
+
st.session_state.pending_prompt = example
|
| 249 |
+
st.rerun()
|
| 250 |
+
|
| 251 |
+
st.divider()
|
| 252 |
+
|
| 253 |
+
# 左右等高布局
|
| 254 |
+
left_col, right_col = st.columns(2)
|
| 255 |
+
|
| 256 |
+
# 获取当前显示内容
|
| 257 |
+
display_thinking = st.session_state.current_thinking
|
| 258 |
+
display_answer = st.session_state.current_answer
|
| 259 |
+
|
| 260 |
+
# 如果没有正在生成的内容,显示最后一条消息
|
| 261 |
+
if not display_thinking and not display_answer and st.session_state.messages:
|
| 262 |
+
for msg in reversed(st.session_state.messages):
|
| 263 |
+
if msg["role"] == "assistant":
|
| 264 |
+
display_thinking = msg.get("thinking", "")
|
| 265 |
+
display_answer = msg.get("content", "")
|
| 266 |
+
break
|
| 267 |
+
|
| 268 |
+
# 创建占位符
|
| 269 |
+
with left_col:
|
| 270 |
thinking_placeholder = st.empty()
|
| 271 |
with thinking_placeholder:
|
| 272 |
+
render_thinking_panel(display_thinking)
|
| 273 |
|
|
|
|
| 274 |
with right_col:
|
| 275 |
+
answer_placeholder = st.empty()
|
| 276 |
+
with answer_placeholder:
|
| 277 |
+
render_answer_panel(display_answer)
|
| 278 |
+
|
| 279 |
+
# 处理输入
|
| 280 |
+
prompt = None
|
| 281 |
+
if st.session_state.pending_prompt:
|
| 282 |
+
prompt = st.session_state.pending_prompt
|
| 283 |
+
st.session_state.pending_prompt = None
|
| 284 |
+
elif send_clicked and user_input:
|
| 285 |
+
prompt = user_input
|
| 286 |
+
elif user_input and st.session_state.get("last_input") != user_input:
|
| 287 |
+
# 检测回车提交(通过检测输入变化)
|
| 288 |
+
pass
|
| 289 |
+
|
| 290 |
+
if prompt:
|
| 291 |
+
# 添加用户消息
|
| 292 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 293 |
+
|
| 294 |
+
full_response = ""
|
| 295 |
+
full_thinking = ""
|
| 296 |
+
|
| 297 |
+
for thinking, response in chat_stream(
|
| 298 |
+
prompt,
|
| 299 |
+
st.session_state.messages[:-1],
|
| 300 |
+
system_prompt,
|
| 301 |
+
max_tokens,
|
| 302 |
+
temperature,
|
| 303 |
+
top_p,
|
| 304 |
+
):
|
| 305 |
+
full_thinking = thinking
|
| 306 |
+
full_response = response if response else "▌"
|
| 307 |
+
|
| 308 |
+
# 更新显示
|
| 309 |
+
st.session_state.current_thinking = full_thinking
|
| 310 |
+
st.session_state.current_answer = full_response
|
| 311 |
+
|
| 312 |
+
with thinking_placeholder:
|
| 313 |
+
render_thinking_panel(full_thinking)
|
| 314 |
+
with answer_placeholder:
|
| 315 |
+
render_answer_panel(full_response)
|
| 316 |
+
|
| 317 |
+
# 保存消息
|
| 318 |
+
st.session_state.messages.append({
|
| 319 |
+
"role": "assistant",
|
| 320 |
+
"content": full_response,
|
| 321 |
+
"thinking": full_thinking,
|
| 322 |
+
})
|
| 323 |
+
st.session_state.current_thinking = ""
|
| 324 |
+
st.session_state.current_answer = ""
|
| 325 |
+
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
|
| 327 |
|
| 328 |
if __name__ == "__main__":
|