DeepLearning101 commited on
Commit
ac5b826
·
verified ·
1 Parent(s): b851c1b

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +55 -24
src/streamlit_app.py CHANGED
@@ -5,7 +5,7 @@ import os
5
 
6
  st.set_page_config(page_title="AI 新知小助手", page_icon="📚", layout="wide")
7
 
8
- # 從環境變數中取得 API Key
9
  api_key = os.environ.get("GEMINI_API_KEY")
10
 
11
  if not api_key:
@@ -14,63 +14,94 @@ if not api_key:
14
 
15
  genai.configure(api_key=api_key)
16
 
17
- # 1. 定義你要鎖定的 GitHub 檔案清單
18
- # 請把 YOUR_ACCOUNT/YOUR_REPO 換成你真實的 GitHub 資訊
19
  BASE_URL = "https://raw.githubusercontent.com/Deep-Learning-101/deep-learning-101.github.io/main/"
20
- TARGET_FILES = {
21
- "大型語言模型 (LLM)": f"{BASE_URL}Large-Language-Model.md",
22
- "自然語言處理 (NLP)": f"{BASE_URL}Natural-Language-Processing.md",
23
- "語音處理 (Speech)": f"{BASE_URL}Speech-Processing.md",
24
- "電腦視覺 (CV)": f"{BASE_URL}Computer-Vision.md"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  }
26
 
27
- # 2. 批量抓取並格式化內容
28
  def fetch_all_knowledge():
29
  combined_knowledge = ""
30
- with st.spinner("正在從 GitHub 同步 4 大領域最新資訊..."):
31
- for category, url in TARGET_FILES.items():
32
  try:
33
- response = requests.get(url)
34
  response.raise_for_status()
35
- # 在每個檔案內容前加上明確的標題,幫助 AI 分類記憶
36
  combined_knowledge += f"\n\n## 【領域:{category}】\n"
37
  combined_knowledge += response.text
38
  except Exception as e:
39
  st.warning(f"無法同步 {category} 的資料:{e}")
40
  return combined_knowledge
41
 
42
- # 初始化 Session State
43
  if "knowledge" not in st.session_state:
44
  st.session_state.knowledge = fetch_all_knowledge()
45
 
46
  if "messages" not in st.session_state:
47
  st.session_state.messages = []
48
 
49
- # 側邊欄設計
50
  with st.sidebar:
 
 
 
 
 
 
 
 
 
 
51
  st.title("⚙️ 知識庫狀態")
52
- st.write("目前收錄以下領域:")
53
- for category in TARGET_FILES.keys():
54
- st.markdown(f"- {category}")
 
 
 
 
55
 
56
  st.markdown("---")
57
  if st.button("🔄 手動更新知識庫"):
58
  st.session_state.knowledge = fetch_all_knowledge()
59
- st.success("所有領域資料已重新抓取!")
60
 
61
- # 主介面
62
  st.title("📚 AI 演算法與論文社群助手")
63
  st.caption("知識庫涵蓋 LLM、NLP、Speech、CV。歡迎直接提問!")
64
 
65
  def get_gemini_response(user_input):
66
  system_instruction = f"""
67
  你是一位專業的 AI 技術分析專家。
68
- 以下是從 GitHub 同步的四個領域 (LLM, NLP, Speech, CV) 的最新技術簡介與重點
69
  ---
70
  {st.session_state.knowledge}
71
  ---
72
- 請嚴格基於上述提供的資訊來回答使用者的問題。
73
- 如果使用者詢問了上述資訊中沒有涵蓋的細節,請明確告知:「目前這份懶人包中尚未收錄關於問題的詳資訊」。
74
  """
75
  model = genai.GenerativeModel(
76
  model_name="gemini-flash-latest",
@@ -87,7 +118,7 @@ for message in st.session_state.messages:
87
  st.markdown(message["content"])
88
 
89
  # 接收輸入
90
- if prompt := st.chat_input("例如:請幫我比較一下 CV 跟 NLP 目前常用的技術?"):
91
  st.session_state.messages.append({"role": "user", "content": prompt})
92
  with st.chat_message("user"):
93
  st.markdown(prompt)
 
5
 
6
  st.set_page_config(page_title="AI 新知小助手", page_icon="📚", layout="wide")
7
 
8
+ # 從環境變數中取得 API Key
9
  api_key = os.environ.get("GEMINI_API_KEY")
10
 
11
  if not api_key:
 
14
 
15
  genai.configure(api_key=api_key)
16
 
17
+ # 1. 基礎設與連結
 
18
  BASE_URL = "https://raw.githubusercontent.com/Deep-Learning-101/deep-learning-101.github.io/main/"
19
+ LOGO_URL = f"{BASE_URL}DeepLearning101-LOGO.png"
20
+ HOME_URL = "https://deep-learning-101.github.io"
21
+
22
+ # 定義知識庫檔案與對應的社群連結
23
+ KNOWLEDGE_MAP = {
24
+ "大型語言模型 (LLM)": {
25
+ "raw_url": f"{BASE_URL}Large-Language-Model.md",
26
+ "page_url": "https://deep-learning-101.github.io/Large-Language-Model",
27
+ "repo_url": "https://github.com/Deep-Learning-101/Natural-Language-Processing-Paper/blob/main/Large-Language-Model.md"
28
+ },
29
+ "自然語言處理 (NLP)": {
30
+ "raw_url": f"{BASE_URL}Natural-Language-Processing.md",
31
+ "page_url": "https://deep-learning-101.github.io/Natural-Language-Processing",
32
+ "repo_url": "https://github.com/Deep-Learning-101/Natural-Language-Processing-Paper"
33
+ },
34
+ "語音處理 (Speech)": {
35
+ "raw_url": f"{BASE_URL}Speech-Processing.md",
36
+ "page_url": "https://deep-learning-101.github.io/Speech-Processing",
37
+ "repo_url": "https://github.com/Deep-Learning-101/Speech-Processing-Paper"
38
+ },
39
+ "電腦視覺 (CV)": {
40
+ "raw_url": f"{BASE_URL}Computer-Vision.md",
41
+ "page_url": "https://deep-learning-101.github.io/Computer-Vision",
42
+ "repo_url": "https://github.com/Deep-Learning-101/Computer-Vision-Paper"
43
+ }
44
  }
45
 
46
+ # 2. 批量抓取內容
47
  def fetch_all_knowledge():
48
  combined_knowledge = ""
49
+ with st.spinner("正在同步 GitHub 最新資訊..."):
50
+ for category, info in KNOWLEDGE_MAP.items():
51
  try:
52
+ response = requests.get(info["raw_url"])
53
  response.raise_for_status()
 
54
  combined_knowledge += f"\n\n## 【領域:{category}】\n"
55
  combined_knowledge += response.text
56
  except Exception as e:
57
  st.warning(f"無法同步 {category} 的資料:{e}")
58
  return combined_knowledge
59
 
 
60
  if "knowledge" not in st.session_state:
61
  st.session_state.knowledge = fetch_all_knowledge()
62
 
63
  if "messages" not in st.session_state:
64
  st.session_state.messages = []
65
 
66
+ # 3. 側邊欄設計
67
  with st.sidebar:
68
+ # 放置可點擊的 Logo
69
+ st.markdown(
70
+ f"""
71
+ <a href="{HOME_URL}" target="_blank">
72
+ <img src="{LOGO_URL}" width="100%">
73
+ </a>
74
+ """,
75
+ unsafe_allow_html=True
76
+ )
77
+
78
  st.title("⚙️ 知識庫狀態")
79
+ st.write("目前收錄領域與連結:")
80
+
81
+ # 迴圈產生各領域連結
82
+ for category, info in KNOWLEDGE_MAP.items():
83
+ with st.expander(category):
84
+ st.markdown(f"🔗 [瀏覽網頁]({info['page_url']})")
85
+ st.markdown(f"📂 [GitHub 原始碼]({info['repo_url']})")
86
 
87
  st.markdown("---")
88
  if st.button("🔄 手動更新知識庫"):
89
  st.session_state.knowledge = fetch_all_knowledge()
90
+ st.success("資料已重新抓取!")
91
 
92
+ # 4. 主介面
93
  st.title("📚 AI 演算法與論文社群助手")
94
  st.caption("知識庫涵蓋 LLM、NLP、Speech、CV。歡迎直接提問!")
95
 
96
  def get_gemini_response(user_input):
97
  system_instruction = f"""
98
  你是一位專業的 AI 技術分析專家。
99
+ 以下是從 GitHub 同步的技術資訊
100
  ---
101
  {st.session_state.knowledge}
102
  ---
103
+ 請嚴格基於上述提供的資訊來回答問題。
104
+ 如果資訊中未收錄,請告知:「目前懶人包中尚未收���此細」。
105
  """
106
  model = genai.GenerativeModel(
107
  model_name="gemini-flash-latest",
 
118
  st.markdown(message["content"])
119
 
120
  # 接收輸入
121
+ if prompt := st.chat_input("想瞭解哪方面的技術?"):
122
  st.session_state.messages.append({"role": "user", "content": prompt})
123
  with st.chat_message("user"):
124
  st.markdown(prompt)