feat: 初始化 AgricultureQA 农技智能问答应用

- 搭建基于 Qwen3.5 的 Streamlit 农业技术知识问答应用
  - 支持思维链推理展示与模型参数调节
  - 新增项目核心文件:app.py、config.py、main.py
  - 新增工程化配置:pyproject.toml、justfile、Dockerfile、.gitignore
  - 完善 README 文档,包含快速开始、部署说明及项目结构
This commit is contained in:
zhenghu
2026-04-13 16:49:12 +08:00
parent 972c568a66
commit 256fe12605
9 changed files with 1142 additions and 2 deletions

265
app.py Normal file
View File

@@ -0,0 +1,265 @@
"""
农技智能问答
基于 Qwen3.5 大模型的农业技术知识问答应用
"""
import httpx
import streamlit as st
from config import CHAT_API_URL, CHAT_MODEL, HEADERS
# ─── Page Config ────────────────────────────────────────────────────────────
st.set_page_config(
page_title="农技智能问答",
page_icon="🌾",
layout="wide",
initial_sidebar_state="expanded",
)
# ─── Custom CSS ──────────────────────────────────────────────────────────────
st.markdown("""
<style>
:root {
--bg-dark: #0a1628;
--bg-card: #0f2040;
--accent-green: #4ade80;
--accent-gold: #f59e0b;
--accent-blue: #38bdf8;
--text-primary: #e2e8f0;
--text-muted: #64748b;
--border: rgba(74, 222, 128, 0.2);
}
html, body, [class*="css"] {
font-family: "PingFang SC", "Microsoft YaHei", "Noto Serif SC", serif;
background-color: var(--bg-dark);
color: var(--text-primary);
}
.stApp {
background: linear-gradient(135deg, #0a1628 0%, #0d1f3c 50%, #091520 100%);
}
/* Sidebar */
[data-testid="stSidebar"] {
background: linear-gradient(180deg, #0f2040 0%, #0a1628 100%);
border-right: 1px solid var(--border);
}
[data-testid="stSidebar"] .stSlider label,
[data-testid="stSidebar"] .stCheckbox label {
color: var(--accent-green) !important;
font-size: 0.82rem;
font-family: "JetBrains Mono", "Fira Code", "Source Code Pro", monospace;
letter-spacing: 0.05em;
}
/* Section headers */
.section-header {
font-size: 0.75rem;
font-family: "JetBrains Mono", "Fira Code", "Source Code Pro", monospace;
letter-spacing: 0.15em;
color: var(--accent-gold);
text-transform: uppercase;
border-bottom: 1px solid rgba(245,158,11,0.3);
padding-bottom: 6px;
margin-bottom: 16px;
margin-top: 24px;
}
/* Hero title */
.hero-title {
font-size: 2rem;
font-weight: 700;
background: linear-gradient(135deg, var(--accent-green), var(--accent-blue));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
line-height: 1.2;
}
.hero-sub {
font-family: "JetBrains Mono", "Fira Code", "Source Code Pro", monospace;
font-size: 0.8rem;
color: var(--text-muted);
letter-spacing: 0.1em;
margin-top: 4px;
}
/* Answer card */
.answer-card {
background: linear-gradient(135deg, #0f2040, #132b55);
border: 1px solid var(--border);
border-radius: 12px;
padding: 24px;
margin: 16px 0;
line-height: 1.8;
}
.answer-card h3 {
color: var(--accent-green);
font-family: "JetBrains Mono", "Fira Code", "Source Code Pro", monospace;
font-size: 0.85rem;
letter-spacing: 0.08em;
margin-bottom: 12px;
}
/* Thinking card */
.thinking-card {
background: rgba(56, 189, 248, 0.05);
border: 1px solid rgba(56, 189, 248, 0.2);
border-radius: 10px;
padding: 18px;
margin: 12px 0;
}
/* Token usage */
.token-usage {
font-family: "JetBrains Mono", "Fira Code", "Source Code Pro", monospace;
font-size: 0.78rem;
color: var(--text-muted);
background: rgba(255,255,255,0.03);
border-radius: 6px;
padding: 8px 14px;
margin-top: 12px;
display: inline-block;
}
/* Quick question buttons */
[data-testid="stSidebar"] button[kind="secondary"] {
background: rgba(74,222,128,0.08);
border: 1px solid rgba(74,222,128,0.2);
color: var(--text-primary);
font-size: 0.82rem;
text-align: left;
white-space: normal;
line-height: 1.4;
transition: all 0.2s ease;
}
[data-testid="stSidebar"] button[kind="secondary"]:hover {
background: rgba(74,222,128,0.15);
border-color: rgba(74,222,128,0.4);
}
/* Override streamlit slider colors */
.stSlider [data-baseweb="slider"] [data-testid="stTickBarMin"],
.stSlider [data-baseweb="slider"] [data-testid="stTickBarMax"] {
color: var(--text-muted);
}
/* Alert boxes */
.alert-error {
background: rgba(239,68,68,0.1);
border-left: 3px solid #ef4444;
border-radius: 0 8px 8px 0;
padding: 12px 16px;
margin: 8px 0;
font-size: 0.9rem;
}
</style>
""", unsafe_allow_html=True)
# ─── Quick Questions ─────────────────────────────────────────────────────────
QUICK_QUESTIONS = [
"水稻稻瘟病怎么防治?什么时候打药效果最好?",
"小麦叶片上出现铁锈色的粉状物是什么病?怎么处理?",
"如何判断玉米是否需要灌溉?有哪些判断方法?",
"如何种榴莲?怎样成熟快?",
]
# ─── Sidebar ─────────────────────────────────────────────────────────────────
with st.sidebar:
st.markdown('<div class="hero-title">🌾 农技问答</div>', unsafe_allow_html=True)
st.markdown('<div class="hero-sub">AGRICULTURE QA SYSTEM v1.0</div>', unsafe_allow_html=True)
st.markdown("---")
st.markdown('<div class="section-header">⚙️ 模型参数</div>', unsafe_allow_html=True)
temperature = st.slider("Temperature", 0.0, 1.0, 0.7, 0.1)
top_p = st.slider("Top P", 0.0, 1.0, 0.8, 0.1)
enable_thinking = st.checkbox("开启思维链", value=True)
st.markdown('<div class="section-header">💡 快捷提问</div>', unsafe_allow_html=True)
for q in QUICK_QUESTIONS:
if st.button(q, key=q):
st.session_state.user_input = q
# ─── Main Layout ─────────────────────────────────────────────────────────────
st.markdown("""
<div style="display:flex; align-items:baseline; gap:16px; margin-bottom:4px;">
<div class="hero-title">农技智能问答</div>
</div>
<div class="hero-sub">Powered by Qwen3.5 &nbsp;|&nbsp; 支持思维链推理的农业技术知识问答</div>
""", unsafe_allow_html=True)
st.markdown("<br>", unsafe_allow_html=True)
# 输入区域
user_input = st.text_area(
"请输入您的农业问题:",
value=st.session_state.get("user_input", ""),
height=120,
placeholder="例如:水稻稻瘟病怎么防治?",
)
if st.button("提交", type="primary") and user_input.strip():
with st.spinner("模型正在思考中..."):
try:
payload = {
"model": CHAT_MODEL,
"messages": [{"role": "user", "content": user_input.strip()}],
"temperature": temperature,
"top_p": top_p,
"presence_penalty": 1.5,
"chat_template_kwargs": {"enable_thinking": enable_thinking},
}
resp = httpx.post(
CHAT_API_URL, headers=HEADERS, json=payload, timeout=120
)
resp.raise_for_status()
data = resp.json()
choice = data["choices"][0]["message"]
reasoning = choice.get("reasoning_content", "")
content = choice["content"]
usage = data["usage"]
# 显示回答
st.markdown(f"""
<div class="answer-card">
<h3>💬 模型回答</h3>
<div style="color: var(--text-primary); font-size: 0.95rem;">{content}</div>
</div>
""", unsafe_allow_html=True)
# 显示思考过程(可折叠)
if reasoning and enable_thinking:
with st.expander("🧠 查看思考过程"):
st.markdown(reasoning)
# Token 用量
st.markdown(f"""
<div class="token-usage">
Token 用量 — 输入: {usage['prompt_tokens']} | \
输出: {usage['completion_tokens']} | \
合计: {usage['total_tokens']}
</div>
""", unsafe_allow_html=True)
except httpx.HTTPStatusError as e:
st.markdown(
f'<div class="alert-error">请求失败 (HTTP {e.response.status_code}): {e.response.text}</div>',
unsafe_allow_html=True,
)
except Exception as e:
st.markdown(
f'<div class="alert-error">请求异常: {e}</div>',
unsafe_allow_html=True,
)
# ─── Footer ───────────────────────────────────────────────────────────────────
st.markdown("<br>", unsafe_allow_html=True)
st.markdown("""
<div style="text-align:center; font-family:'JetBrains Mono',monospace; font-size:0.72rem;
color:#334155; padding:16px; border-top:1px solid rgba(74,222,128,0.1);">
Qwen3.5 + Thinking Chain &nbsp;|&nbsp; 农技智能问答系统
</div>
""", unsafe_allow_html=True)