From 4f27eb6c42124de32a8c7a604d00776bd130143f Mon Sep 17 00:00:00 2001 From: zhenghu <1831829219@qq.com> Date: Tue, 14 Apr 2026 17:57:57 +0800 Subject: [PATCH] =?UTF-8?q?refactor(ui):=20=E4=BD=BF=E7=94=A8=20Streamlit?= =?UTF-8?q?=20=E5=8E=9F=E7=94=9F=E7=BB=84=E4=BB=B6=E7=AE=80=E5=8C=96?= =?UTF-8?q?=E7=95=8C=E9=9D=A2=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除大量自定义 CSS 样式,仅保留字体设置 - 将自定义 HTML 结构替换为 st.title/st.caption/st.container/st.info 等原生组件 - 简化快捷问题、输入框、回答展示、错误提示和页脚的实现 - 减少维护复杂度,提升代码可读性 --- app.py | 242 ++++++--------------------------------------------------- 1 file changed, 22 insertions(+), 220 deletions(-) diff --git a/app.py b/app.py index 2d86cc2..669c97d 100644 --- a/app.py +++ b/app.py @@ -17,179 +17,11 @@ st.set_page_config( initial_sidebar_state="collapsed", ) -# ─── Custom CSS ────────────────────────────────────────────────────────────── +# ─── Minimal CSS ───────────────────────────────────────────────────────────── st.markdown(""" """, unsafe_allow_html=True) @@ -205,31 +37,22 @@ QUICK_QUESTIONS = [ # ─── Header ────────────────────────────────────────────────────────────────── -st.markdown(""" -
-

🌾 农技问答

-

有农业问题,随时来问

-
-""", unsafe_allow_html=True) +st.title("🌾 农技问答") +st.caption("有农业问题,随时来问") +st.markdown("
", unsafe_allow_html=True) # ─── Quick Questions ───────────────────────────────────────────────────────── -chips_html = '
' -for q in QUICK_QUESTIONS: - chips_html += f'{q}' -chips_html += '
' -st.markdown(chips_html, unsafe_allow_html=True) +with st.container(border=False): + cols = st.columns(len(QUICK_QUESTIONS)) + for i, q in enumerate(QUICK_QUESTIONS): + with cols[i]: + if st.button(q, key=f"chip_{q}", use_container_width=True): + st.session_state.user_input = q + st.rerun() -# Handle chip clicks via native buttons (invisible, placed neatly) -cols = st.columns(len(QUICK_QUESTIONS)) -for i, q in enumerate(QUICK_QUESTIONS): - with cols[i]: - st.markdown("
" + " " + "
", unsafe_allow_html=True) - if st.button(q, key=f"chip_{q}", use_container_width=True): - st.session_state.user_input = q - st.rerun() +st.markdown("
", unsafe_allow_html=True) # ─── Input ─────────────────────────────────────────────────────────────────── -st.markdown('
', unsafe_allow_html=True) user_input = st.text_area( "请输入您的问题", value=st.session_state.get("user_input", ""), @@ -237,10 +60,7 @@ user_input = st.text_area( placeholder="例如:水稻稻瘟病怎么防治?", label_visibility="collapsed", ) -col1, col2 = st.columns([1, 6]) -with col1: - submitted = st.button("发送", use_container_width=True) -st.markdown('
', unsafe_allow_html=True) +submitted = st.button("发送", type="primary") # ─── Settings Panel ────────────────────────────────────────────────────────── with st.expander("⚙️ 模型设置"): @@ -292,25 +112,16 @@ if submitted and user_input.strip(): if reasoning_piece: full_reasoning += reasoning_piece if enable_thinking: - thinking_placeholder.markdown( - f'
' - f'正在思考...
' - f'
{full_reasoning}
' - f'
', - unsafe_allow_html=True, + thinking_placeholder.info( + "**正在思考...**\n\n" + full_reasoning ) content_piece = delta.get("content", "") if content_piece: full_content += content_piece - answer_placeholder.markdown( - f'
' - f'

🌱 回答

' - f'
' - f'{full_content}
' - f'
', - unsafe_allow_html=True, - ) + with answer_placeholder.container(border=True): + st.markdown("### 🌱 回答") + st.markdown(full_content) if full_reasoning and enable_thinking: thinking_placeholder.empty() @@ -318,19 +129,10 @@ if submitted and user_input.strip(): st.markdown(full_reasoning) except httpx.HTTPStatusError as e: - st.markdown( - f'
请求失败 (HTTP {e.response.status_code}): {e.response.text}
', - unsafe_allow_html=True, - ) + st.error(f"请求失败 (HTTP {e.response.status_code}): {e.response.text}") except Exception as e: - st.markdown( - f'
请求异常: {e}
', - unsafe_allow_html=True, - ) + st.error(f"请求异常: {e}") # ─── Footer ─────────────────────────────────────────────────────────────────── -st.markdown(""" - -""", unsafe_allow_html=True) +st.divider() +st.caption("农业技术知识问答 · 仅供参考")