Update src/streamlit_app.py
Browse files- 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 |
-
#
|
| 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. 定
|
| 18 |
-
# 請把 YOUR_ACCOUNT/YOUR_REPO 換成你真實的 GitHub 資訊
|
| 19 |
BASE_URL = "https://raw.githubusercontent.com/Deep-Learning-101/deep-learning-101.github.io/main/"
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
}
|
| 26 |
|
| 27 |
-
# 2. 批量抓取
|
| 28 |
def fetch_all_knowledge():
|
| 29 |
combined_knowledge = ""
|
| 30 |
-
with st.spinner("正在
|
| 31 |
-
for category,
|
| 32 |
try:
|
| 33 |
-
response = requests.get(
|
| 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 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
以下是
|
| 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("
|
| 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)
|