Update src/streamlit_app.py
Browse files- src/streamlit_app.py +39 -46
src/streamlit_app.py
CHANGED
|
@@ -4,7 +4,7 @@ import requests
|
|
| 4 |
import os
|
| 5 |
|
| 6 |
# 0. 頁面配置與 CSS 注入(隱藏側邊欄捲軸)
|
| 7 |
-
st.set_page_config(page_title="
|
| 8 |
|
| 9 |
st.markdown(
|
| 10 |
"""
|
|
@@ -18,12 +18,19 @@ st.markdown(
|
|
| 18 |
scrollbar-width: none;
|
| 19 |
}
|
| 20 |
|
| 21 |
-
/* 2. 調整範例按鈕樣式 */
|
| 22 |
.stButton button {
|
| 23 |
width: auto;
|
| 24 |
padding: 5px 15px;
|
| 25 |
border-radius: 20px;
|
| 26 |
-
border: 1px solid #
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
}
|
| 28 |
|
| 29 |
/* 3. 極度壓縮頂部空白與兩側邊距,專為小視窗優化 */
|
|
@@ -52,39 +59,23 @@ if not api_key:
|
|
| 52 |
genai.configure(api_key=api_key)
|
| 53 |
|
| 54 |
# 1. 基礎設定與連結
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
LOGO_URL = "https://raw.githubusercontent.com/ciecietaipei/ciecietaipei.github.io/main/assets/ciecie_logo_circle.png"
|
| 59 |
-
HOME_URL = "https://ciecietaipei.github.io"
|
| 60 |
|
|
|
|
| 61 |
KNOWLEDGE_MAP = {
|
| 62 |
-
"
|
| 63 |
-
"raw_url":
|
| 64 |
-
"page_url":
|
| 65 |
-
"repo_url": "https://github.com/
|
| 66 |
-
},
|
| 67 |
-
"🍷 經典酒水 (Drinks)": {
|
| 68 |
-
"raw_url": f"{MD_BASE_URL}drinks.md",
|
| 69 |
-
"page_url": f"{HOME_URL}/drinks.html",
|
| 70 |
-
"repo_url": "https://github.com/ciecietaipei/ciecietaipei.github.io/blob/main/ai-knowledge/drinks.md"
|
| 71 |
-
},
|
| 72 |
-
"🍽️ 精緻餐點 (Foods)": {
|
| 73 |
-
"raw_url": f"{MD_BASE_URL}foods.md",
|
| 74 |
-
"page_url": f"{HOME_URL}/foods.html",
|
| 75 |
-
"repo_url": "https://github.com/ciecietaipei/ciecietaipei.github.io/blob/main/ai-knowledge/foods.md"
|
| 76 |
-
},
|
| 77 |
-
"🛋️ 空間環境 (Environment)": {
|
| 78 |
-
"raw_url": f"{MD_BASE_URL}environment.md",
|
| 79 |
-
"page_url": f"{HOME_URL}/environment.html",
|
| 80 |
-
"repo_url": "https://github.com/ciecietaipei/ciecietaipei.github.io/blob/main/ai-knowledge/environment.md"
|
| 81 |
}
|
| 82 |
}
|
| 83 |
|
| 84 |
# 2. 批量抓取內容
|
| 85 |
def fetch_all_knowledge():
|
| 86 |
combined_knowledge = ""
|
| 87 |
-
with st.spinner("正在同步
|
| 88 |
for category, info in KNOWLEDGE_MAP.items():
|
| 89 |
try:
|
| 90 |
response = requests.get(info["raw_url"])
|
|
@@ -107,49 +98,51 @@ if "example_prompt" not in st.session_state:
|
|
| 107 |
|
| 108 |
# 3. 側邊欄設計
|
| 109 |
with st.sidebar:
|
| 110 |
-
|
| 111 |
-
st.markdown(f'<a href="{HOME_URL}" target="_blank"><img src="{LOGO_URL}" width="100%" style="border-radius:50%; margin-bottom:20px;"></a>', unsafe_allow_html=True)
|
| 112 |
st.title("⚙️ 知識庫狀態")
|
| 113 |
for category, info in KNOWLEDGE_MAP.items():
|
| 114 |
with st.expander(category):
|
| 115 |
st.markdown(f"🔗 [瀏覽網頁]({info['page_url']})")
|
| 116 |
st.markdown(f"📂 [GitHub 原始碼]({info['repo_url']})")
|
| 117 |
st.markdown("---")
|
| 118 |
-
if st.button("🔄 手動更新
|
| 119 |
st.session_state.knowledge = fetch_all_knowledge()
|
| 120 |
st.success("資料已重新抓取!")
|
| 121 |
|
| 122 |
# 4. 主介面與範例問句
|
| 123 |
-
st.title("
|
| 124 |
-
st.caption("
|
| 125 |
|
| 126 |
# 顯示專屬範例問句按鈕
|
| 127 |
-
example_cols = st.columns(
|
| 128 |
examples = [
|
| 129 |
-
"
|
| 130 |
-
"
|
| 131 |
-
"
|
|
|
|
|
|
|
|
|
|
| 132 |
]
|
| 133 |
|
| 134 |
for col, ex in zip(example_cols, examples):
|
| 135 |
if col.button(ex):
|
| 136 |
st.session_state.example_prompt = ex
|
| 137 |
|
| 138 |
-
# 5. 模型回覆邏輯(設定
|
| 139 |
def get_gemini_response(user_input):
|
| 140 |
system_instruction = f"""
|
| 141 |
-
你現在是
|
| 142 |
-
你的說話風格
|
| 143 |
|
| 144 |
-
以下是從
|
| 145 |
---
|
| 146 |
{st.session_state.knowledge}
|
| 147 |
---
|
| 148 |
-
請嚴格基於上述提供的資訊來回答
|
| 149 |
-
如果
|
| 150 |
"""
|
| 151 |
try:
|
| 152 |
-
# 建議使用 1.5-flash,
|
| 153 |
model = genai.GenerativeModel(
|
| 154 |
model_name="gemini-flash-lite-latest",
|
| 155 |
system_instruction=system_instruction
|
|
@@ -160,12 +153,12 @@ def get_gemini_response(user_input):
|
|
| 160 |
except Exception as e:
|
| 161 |
error_msg = str(e)
|
| 162 |
if "429" in error_msg or "quota" in error_msg.lower():
|
| 163 |
-
return "⚠️ **系統提示:**\n\n目前
|
| 164 |
else:
|
| 165 |
return f"❌ **發生預期外錯誤**\n\n訊息:{error_msg}"
|
| 166 |
|
| 167 |
# 6. 對話邏輯
|
| 168 |
-
prompt = st.chat_input("
|
| 169 |
|
| 170 |
if st.session_state.example_prompt:
|
| 171 |
prompt = st.session_state.example_prompt
|
|
|
|
| 4 |
import os
|
| 5 |
|
| 6 |
# 0. 頁面配置與 CSS 注入(隱藏側邊欄捲軸)
|
| 7 |
+
st.set_page_config(page_title="S.H.I.E.L.D. 戰情小助手", page_icon="🛡️", layout="wide")
|
| 8 |
|
| 9 |
st.markdown(
|
| 10 |
"""
|
|
|
|
| 18 |
scrollbar-width: none;
|
| 19 |
}
|
| 20 |
|
| 21 |
+
/* 2. 調整範例按鈕樣式 (改為科技藍風格) */
|
| 22 |
.stButton button {
|
| 23 |
width: auto;
|
| 24 |
padding: 5px 15px;
|
| 25 |
border-radius: 20px;
|
| 26 |
+
border: 1px solid #58a6ff;
|
| 27 |
+
color: #58a6ff;
|
| 28 |
+
background-color: transparent;
|
| 29 |
+
transition: all 0.3s ease;
|
| 30 |
+
}
|
| 31 |
+
.stButton button:hover {
|
| 32 |
+
background-color: #58a6ff;
|
| 33 |
+
color: #0d1117;
|
| 34 |
}
|
| 35 |
|
| 36 |
/* 3. 極度壓縮頂部空白與兩側邊距,專為小視窗優化 */
|
|
|
|
| 59 |
genai.configure(api_key=api_key)
|
| 60 |
|
| 61 |
# 1. 基礎設定與連結
|
| 62 |
+
# 抓取 Deep Learning 101 的 Logo (你也可以換成 SHIELD 專屬 Logo)
|
| 63 |
+
LOGO_URL = "https://raw.githubusercontent.com/Deep-Learning-101/deep-learning-101.github.io/main/DeepLearning101-LOGO.png"
|
| 64 |
+
HOME_URL = "https://deep-learning-101.github.io/SHIELD/"
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
# 將知識庫指向 S.H.I.E.L.D. 的 README.md
|
| 67 |
KNOWLEDGE_MAP = {
|
| 68 |
+
"🛡️ S.H.I.E.L.D. 系統白皮書": {
|
| 69 |
+
"raw_url": "https://raw.githubusercontent.com/Deep-Learning-101/SHIELD/main/README.md",
|
| 70 |
+
"page_url": "https://deep-learning-101.github.io/SHIELD/",
|
| 71 |
+
"repo_url": "https://github.com/Deep-Learning-101/SHIELD"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
}
|
| 73 |
}
|
| 74 |
|
| 75 |
# 2. 批量抓取內容
|
| 76 |
def fetch_all_knowledge():
|
| 77 |
combined_knowledge = ""
|
| 78 |
+
with st.spinner("正在同步 S.H.I.E.L.D. 戰情資料庫..."):
|
| 79 |
for category, info in KNOWLEDGE_MAP.items():
|
| 80 |
try:
|
| 81 |
response = requests.get(info["raw_url"])
|
|
|
|
| 98 |
|
| 99 |
# 3. 側邊欄設計
|
| 100 |
with st.sidebar:
|
| 101 |
+
st.markdown(f'<a href="{HOME_URL}" target="_blank"><img src="{LOGO_URL}" width="100%" style="margin-bottom:20px;"></a>', unsafe_allow_html=True)
|
|
|
|
| 102 |
st.title("⚙️ 知識庫狀態")
|
| 103 |
for category, info in KNOWLEDGE_MAP.items():
|
| 104 |
with st.expander(category):
|
| 105 |
st.markdown(f"🔗 [瀏覽網頁]({info['page_url']})")
|
| 106 |
st.markdown(f"📂 [GitHub 原始碼]({info['repo_url']})")
|
| 107 |
st.markdown("---")
|
| 108 |
+
if st.button("🔄 手動更新情資庫"):
|
| 109 |
st.session_state.knowledge = fetch_all_knowledge()
|
| 110 |
st.success("資料已重新抓取!")
|
| 111 |
|
| 112 |
# 4. 主介面與範例問句
|
| 113 |
+
st.title("🤖 S.H.I.E.L.D. 戰情小助手")
|
| 114 |
+
st.caption("我是 Deep Learning 101 的主權 AI 戰情官,專注於解答 S.H.I.E.L.D. 的架構與防禦機制。")
|
| 115 |
|
| 116 |
# 顯示專屬範例問句按鈕
|
| 117 |
+
example_cols = st.columns(6)
|
| 118 |
examples = [
|
| 119 |
+
"🧠 傳統 AI 常有幻覺,雙腦架構如何落實 AI 治理?",
|
| 120 |
+
"🏭 企業機密文檔,如何無損轉化為 AI 微調燃料?",
|
| 121 |
+
"⚔️ 揭秘「紅藍隊自主對抗」與動態語意防火牆",
|
| 122 |
+
"🕸️ 知識圖譜發威:如何秒級推演受災爆炸半徑?",
|
| 123 |
+
"🚀 捨棄傳統向量庫?解析 S.H.I.E.L.D. 雙引擎檢索",
|
| 124 |
+
"🤖 從給建議到「自動修補」:Agent 如何執行 ChatOps?"
|
| 125 |
]
|
| 126 |
|
| 127 |
for col, ex in zip(example_cols, examples):
|
| 128 |
if col.button(ex):
|
| 129 |
st.session_state.example_prompt = ex
|
| 130 |
|
| 131 |
+
# 5. 模型回覆邏輯(設定資安 AI 架構師人設)
|
| 132 |
def get_gemini_response(user_input):
|
| 133 |
system_instruction = f"""
|
| 134 |
+
你現在是 Deep Learning 101 開發的『S.H.I.E.L.D. 戰情小助手』。
|
| 135 |
+
你的說話風格專業、精確,帶有資安專家與 AI 架構師的科技感,致力於推廣企業主權 AI 防禦理念。
|
| 136 |
|
| 137 |
+
以下是從 GitHub 同步的 S.H.I.E.L.D. 系統白皮書與技術架構資訊:
|
| 138 |
---
|
| 139 |
{st.session_state.knowledge}
|
| 140 |
---
|
| 141 |
+
請嚴格基於上述提供的資訊來回答使用者的問題。
|
| 142 |
+
如果使用者問了超出 S.H.I.E.L.D. 白皮書範圍的問題,請禮貌地告知:「目前的戰情資料庫尚未收錄此資訊,建議您查閱 Deep Learning 101 的其他專案或直接聯繫維護團隊。」
|
| 143 |
"""
|
| 144 |
try:
|
| 145 |
+
# 建議使用 1.5-flash,對於技術長文的理解力較強
|
| 146 |
model = genai.GenerativeModel(
|
| 147 |
model_name="gemini-flash-lite-latest",
|
| 148 |
system_instruction=system_instruction
|
|
|
|
| 153 |
except Exception as e:
|
| 154 |
error_msg = str(e)
|
| 155 |
if "429" in error_msg or "quota" in error_msg.lower():
|
| 156 |
+
return "⚠️ **系統提示:**\n\n目前 API 請求已達上限,戰情中心通訊稍有延遲!請稍等幾分鐘後再試。"
|
| 157 |
else:
|
| 158 |
return f"❌ **發生預期外錯誤**\n\n訊息:{error_msg}"
|
| 159 |
|
| 160 |
# 6. 對話邏輯
|
| 161 |
+
prompt = st.chat_input("請輸入您對 S.H.I.E.L.D. 系統的疑問...")
|
| 162 |
|
| 163 |
if st.session_state.example_prompt:
|
| 164 |
prompt = st.session_state.example_prompt
|