DeepLearning101 commited on
Commit
424a5d0
·
verified ·
1 Parent(s): f6bc934

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +28 -12
src/streamlit_app.py CHANGED
@@ -11,17 +11,17 @@ st.markdown(
11
  [data-testid="stSidebar"] section::-webkit-scrollbar { display: none; }
12
  [data-testid="stSidebar"] section { -ms-overflow-style: none; scrollbar-width: none; }
13
 
14
- /* 🚀 2. 全域字體縮小,適應手機螢幕 */
15
  html, body, [class*="st-"] {
16
  font-size: 14px !important;
17
  }
18
 
19
- /* 🚀 3. 調整範例按鈕樣式:變矮、字體變小、支援多行自動折行 */
20
  .stButton button {
21
  width: 100%;
22
  padding: 6px 10px; /* 縮小上下內距 */
23
  border-radius: 12px;
24
- border: 1px solid #4CAF50; /* 改為溫暖的綠色系 */
25
  color: #4CAF50;
26
  background-color: transparent;
27
  font-size: 13px !important;
@@ -69,7 +69,7 @@ genai.configure(api_key=api_key)
69
  # 1. 基礎設定與連結
70
  HOME_URL = "https://kidaid.org.tw/"
71
 
72
- # 設定本地端的 MD 檔案對應字典 (確保這些案跟 app.py 放在同個資料夾下)
73
  KNOWLEDGE_MAP = {
74
  "🧠 認知發展": "Cognition_Content.md",
75
  "🗣️ 語言發展": "Language.md",
@@ -80,20 +80,26 @@ KNOWLEDGE_MAP = {
80
  "🧩 ASD/早療對策": "CountermeasuresADHD.md"
81
  }
82
 
83
- # 2. 批量讀取本地端檔案內容
84
  def fetch_all_knowledge():
85
  combined_knowledge = ""
 
 
 
86
  with st.spinner("正在載入育兒發展與療育資訊..."):
87
  for category, filename in KNOWLEDGE_MAP.items():
 
 
 
88
  try:
89
- # 直接讀取本地資料夾中的 Markdown 檔案
90
- with open(filename, "r", encoding="utf-8") as f:
91
  combined_knowledge += f"\n\n## 【領域:{category}】\n"
92
  combined_knowledge += f.read()
93
  except FileNotFoundError:
94
- st.warning(f"無法載入 {category}:找不到檔案 `{filename}`,請確認檔案已上傳至 Space。")
95
  except Exception as e:
96
  st.error(f"讀取 {filename} 時發生錯誤:{e}")
 
97
  return combined_knowledge
98
 
99
  # 初始化 Session State
@@ -109,9 +115,9 @@ if "example_prompt" not in st.session_state:
109
  # 3. 側邊欄設計
110
  with st.sidebar:
111
  st.title("⚙️ 知識庫狀態")
112
- st.caption("以下資料已成功載入 AI 大腦:")
113
  for category, filename in KNOWLEDGE_MAP.items():
114
- st.markdown(f" **{category}** (`{filename}`)")
115
 
116
  st.markdown("---")
117
  st.markdown(f"🔗 [前往 育兒成 官方網站]({HOME_URL})")
@@ -119,15 +125,25 @@ with st.sidebar:
119
  if st.button("🔄 重新載入知識庫"):
120
  st.session_state.knowledge = fetch_all_knowledge()
121
  st.success("資料已重新載入!")
 
 
 
 
 
 
 
 
 
 
 
122
 
123
  # 4. 主介面與範例問句
124
  st.title("🧸 育兒成 - 線上衛教小助手")
125
  st.caption("關於 0-5 歲兒童的認知、語言、動作、情緒、飲食及早療對策,歡迎直接詢問!(資料來源:育兒成 | 全方位兒童發展整合照護平臺)")
126
 
127
  # 顯示專屬範例問句按鈕
128
- example_cols = st.columns(6)
129
  examples = [
130
- "👶 0-1歲的寶寶一直不發出聲音,家長可以怎麼引導?",
131
  "🥦 小孩挑食不吃青菜,有什麼應對的小撇步嗎?",
132
  "🏃 2-3歲的孩子粗大動作應該發展到什麼程度?",
133
  "🖐️ 怎麼訓練 4-5 歲幼兒的握筆姿勢?",
 
11
  [data-testid="stSidebar"] section::-webkit-scrollbar { display: none; }
12
  [data-testid="stSidebar"] section { -ms-overflow-style: none; scrollbar-width: none; }
13
 
14
+ /* 2. 全域字體縮小,適應手機螢幕 */
15
  html, body, [class*="st-"] {
16
  font-size: 14px !important;
17
  }
18
 
19
+ /* 3. 調整範例按鈕樣式:變矮、字體變小、支援多行自動折行 */
20
  .stButton button {
21
  width: 100%;
22
  padding: 6px 10px; /* 縮小上下內距 */
23
  border-radius: 12px;
24
+ border: 1px solid #4CAF50; /* 溫暖的綠色系 */
25
  color: #4CAF50;
26
  background-color: transparent;
27
  font-size: 13px !important;
 
69
  # 1. 基礎設定與連結
70
  HOME_URL = "https://kidaid.org.tw/"
71
 
72
+ # 設定本地端的 MD 檔案對應字典 (大小寫必須與上傳的名完全)
73
  KNOWLEDGE_MAP = {
74
  "🧠 認知發展": "Cognition_Content.md",
75
  "🗣️ 語言發展": "Language.md",
 
80
  "🧩 ASD/早療對策": "CountermeasuresADHD.md"
81
  }
82
 
83
+ # 2. 批量讀取本地端檔案內容 (使用絕對路徑)
84
  def fetch_all_knowledge():
85
  combined_knowledge = ""
86
+ # 自動獲取 app.py 所在的資料夾絕對路徑,確保雲端環境找得到檔案
87
+ base_dir = os.path.dirname(os.path.abspath(__file__))
88
+
89
  with st.spinner("正在載入育兒發展與療育資訊..."):
90
  for category, filename in KNOWLEDGE_MAP.items():
91
+ # 將資料夾路徑與檔案名稱結合
92
+ file_path = os.path.join(base_dir, filename)
93
+
94
  try:
95
+ with open(file_path, "r", encoding="utf-8") as f:
 
96
  combined_knowledge += f"\n\n## 【領域:{category}】\n"
97
  combined_knowledge += f.read()
98
  except FileNotFoundError:
99
+ st.warning(f"無法載入 {category}:系統在路徑 `{file_path}` 找不到檔案,請確認檔名大小寫與上傳位置。")
100
  except Exception as e:
101
  st.error(f"讀取 {filename} 時發生錯誤:{e}")
102
+
103
  return combined_knowledge
104
 
105
  # 初始化 Session State
 
115
  # 3. 側邊欄設計
116
  with st.sidebar:
117
  st.title("⚙️ 知識庫狀態")
118
+ st.caption("以下資料嘗試從系統載入:")
119
  for category, filename in KNOWLEDGE_MAP.items():
120
+ st.markdown(f"📄 **{category}** (`{filename}`)")
121
 
122
  st.markdown("---")
123
  st.markdown(f"🔗 [前往 育兒成 官方網站]({HOME_URL})")
 
125
  if st.button("🔄 重新載入知識庫"):
126
  st.session_state.knowledge = fetch_all_knowledge()
127
  st.success("資料已重新載入!")
128
+
129
+ st.markdown("---")
130
+ # 開發者偵錯工具:按下去可以看到系統當下資料夾裡到底有哪些檔案
131
+ with st.expander("🛠️ 開發者偵錯:檢查檔案列表"):
132
+ base_dir = os.path.dirname(os.path.abspath(__file__))
133
+ st.write(f"目前工作目錄:`{base_dir}`")
134
+ st.write("目前資料夾內的所有檔案與資料夾:")
135
+ try:
136
+ st.write(os.listdir(base_dir))
137
+ except Exception as e:
138
+ st.write(f"無法讀取目錄內容:{e}")
139
 
140
  # 4. 主介面與範例問句
141
  st.title("🧸 育兒成 - 線上衛教小助手")
142
  st.caption("關於 0-5 歲兒童的認知、語言、動作、情緒、飲食及早療對策,歡迎直接詢問!(資料來源:育兒成 | 全方位兒童發展整合照護平臺)")
143
 
144
  # 顯示專屬範例問句按鈕
145
+ example_cols = st.columns(5)
146
  examples = [
 
147
  "🥦 小孩挑食不吃青菜,有什麼應對的小撇步嗎?",
148
  "🏃 2-3歲的孩子粗大動作應該發展到什麼程度?",
149
  "🖐️ 怎麼訓練 4-5 歲幼兒的握筆姿勢?",