Update src/streamlit_app.py
Browse files- src/streamlit_app.py +50 -48
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 |
"""
|
|
@@ -23,17 +23,18 @@ st.markdown(
|
|
| 23 |
width: auto;
|
| 24 |
padding: 5px 15px;
|
| 25 |
border-radius: 20px;
|
|
|
|
| 26 |
}
|
| 27 |
|
| 28 |
-
/*
|
| 29 |
.block-container {
|
| 30 |
padding-top: 1.5rem !important;
|
| 31 |
-
padding-bottom:
|
| 32 |
padding-left: 1rem !important;
|
| 33 |
padding-right: 1rem !important;
|
| 34 |
}
|
| 35 |
|
| 36 |
-
/*
|
| 37 |
header {
|
| 38 |
visibility: hidden;
|
| 39 |
}
|
|
@@ -51,37 +52,39 @@ if not api_key:
|
|
| 51 |
genai.configure(api_key=api_key)
|
| 52 |
|
| 53 |
# 1. 基礎設定與連結
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
|
| 58 |
KNOWLEDGE_MAP = {
|
| 59 |
-
"
|
| 60 |
-
"raw_url": f"{
|
| 61 |
-
"page_url": "
|
| 62 |
-
"repo_url": "https://github.com/
|
| 63 |
},
|
| 64 |
-
"
|
| 65 |
-
"raw_url": f"{
|
| 66 |
-
"page_url": "
|
| 67 |
-
"repo_url": "https://github.com/
|
| 68 |
},
|
| 69 |
-
"
|
| 70 |
-
"raw_url": f"{
|
| 71 |
-
"page_url": "
|
| 72 |
-
"repo_url": "https://github.com/
|
| 73 |
},
|
| 74 |
-
"
|
| 75 |
-
"raw_url": f"{
|
| 76 |
-
"page_url": "
|
| 77 |
-
"repo_url": "https://github.com/
|
| 78 |
}
|
| 79 |
}
|
| 80 |
|
| 81 |
# 2. 批量抓取內容
|
| 82 |
def fetch_all_knowledge():
|
| 83 |
combined_knowledge = ""
|
| 84 |
-
with st.spinner("正在同步
|
| 85 |
for category, info in KNOWLEDGE_MAP.items():
|
| 86 |
try:
|
| 87 |
response = requests.get(info["raw_url"])
|
|
@@ -89,7 +92,7 @@ def fetch_all_knowledge():
|
|
| 89 |
combined_knowledge += f"\n\n## 【領域:{category}】\n"
|
| 90 |
combined_knowledge += response.text
|
| 91 |
except Exception as e:
|
| 92 |
-
st.warning(f"無法同步 {category} 的資料:{e}")
|
| 93 |
return combined_knowledge
|
| 94 |
|
| 95 |
# 初始化 Session State
|
|
@@ -99,90 +102,89 @@ if "knowledge" not in st.session_state:
|
|
| 99 |
if "messages" not in st.session_state:
|
| 100 |
st.session_state.messages = []
|
| 101 |
|
| 102 |
-
# 用於處理範例按鈕點擊的狀態
|
| 103 |
if "example_prompt" not in st.session_state:
|
| 104 |
st.session_state.example_prompt = None
|
| 105 |
|
| 106 |
# 3. 側邊欄設計
|
| 107 |
with st.sidebar:
|
| 108 |
-
|
|
|
|
| 109 |
st.title("⚙️ 知識庫狀態")
|
| 110 |
for category, info in KNOWLEDGE_MAP.items():
|
| 111 |
with st.expander(category):
|
| 112 |
st.markdown(f"🔗 [瀏覽網頁]({info['page_url']})")
|
| 113 |
st.markdown(f"📂 [GitHub 原始碼]({info['repo_url']})")
|
| 114 |
st.markdown("---")
|
| 115 |
-
if st.button("🔄 手動更新
|
| 116 |
st.session_state.knowledge = fetch_all_knowledge()
|
| 117 |
st.success("資料已重新抓取!")
|
| 118 |
|
| 119 |
# 4. 主介面與範例問句
|
| 120 |
-
st.title("
|
| 121 |
-
st.caption("
|
| 122 |
|
| 123 |
-
# 顯示範例問句按鈕
|
| 124 |
example_cols = st.columns(3)
|
| 125 |
examples = [
|
| 126 |
-
"
|
| 127 |
-
"
|
| 128 |
-
"
|
| 129 |
]
|
| 130 |
|
| 131 |
for col, ex in zip(example_cols, examples):
|
| 132 |
if col.button(ex):
|
| 133 |
st.session_state.example_prompt = ex
|
| 134 |
|
| 135 |
-
# 5. 模型回覆邏輯(
|
| 136 |
def get_gemini_response(user_input):
|
| 137 |
system_instruction = f"""
|
| 138 |
-
你是
|
| 139 |
-
|
|
|
|
|
|
|
| 140 |
---
|
| 141 |
{st.session_state.knowledge}
|
| 142 |
---
|
| 143 |
-
請嚴格基於上述提供的資訊來回答問題。
|
|
|
|
| 144 |
"""
|
| 145 |
try:
|
|
|
|
| 146 |
model = genai.GenerativeModel(
|
| 147 |
-
model_name="gemini-flash-
|
| 148 |
system_instruction=system_instruction
|
| 149 |
)
|
| 150 |
chat = model.start_chat(history=[])
|
| 151 |
response = chat.send_message(user_input)
|
| 152 |
return response.text
|
| 153 |
except Exception as e:
|
| 154 |
-
# 捕捉 API 額度滿了 (429) 或其他錯誤
|
| 155 |
error_msg = str(e)
|
| 156 |
if "429" in error_msg or "quota" in error_msg.lower():
|
| 157 |
-
return "⚠️ **系統提示:
|
| 158 |
else:
|
| 159 |
return f"❌ **發生預期外錯誤**\n\n訊息:{error_msg}"
|
| 160 |
|
| 161 |
# 6. 對話邏輯
|
| 162 |
-
|
| 163 |
-
prompt = st.chat_input("想瞭解哪方面的技術?")
|
| 164 |
|
| 165 |
if st.session_state.example_prompt:
|
| 166 |
prompt = st.session_state.example_prompt
|
| 167 |
-
st.session_state.example_prompt = None
|
| 168 |
|
| 169 |
if prompt:
|
| 170 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 171 |
|
| 172 |
-
# 顯示所有歷史訊息
|
| 173 |
for message in st.session_state.messages:
|
| 174 |
with st.chat_message(message["role"]):
|
| 175 |
st.markdown(message["content"])
|
| 176 |
|
| 177 |
-
# 產生新回覆
|
| 178 |
with st.chat_message("assistant"):
|
| 179 |
response_text = get_gemini_response(prompt)
|
| 180 |
st.markdown(response_text)
|
| 181 |
|
| 182 |
st.session_state.messages.append({"role": "assistant", "content": response_text})
|
| 183 |
-
st.rerun()
|
| 184 |
else:
|
| 185 |
-
# 僅在沒有新輸入時顯示歷史訊息
|
| 186 |
for message in st.session_state.messages:
|
| 187 |
with st.chat_message(message["role"]):
|
| 188 |
st.markdown(message["content"])
|
|
|
|
| 4 |
import os
|
| 5 |
|
| 6 |
# 0. 頁面配置與 CSS 注入(隱藏側邊欄捲軸)
|
| 7 |
+
st.set_page_config(page_title="CIE CIE TAIPEI 線上客服", page_icon="🍸", layout="wide")
|
| 8 |
|
| 9 |
st.markdown(
|
| 10 |
"""
|
|
|
|
| 23 |
width: auto;
|
| 24 |
padding: 5px 15px;
|
| 25 |
border-radius: 20px;
|
| 26 |
+
border: 1px solid #D4AF37; /* 加入一點微奢華的金色邊框 */
|
| 27 |
}
|
| 28 |
|
| 29 |
+
/* 3. 極度壓縮頂部空白與兩側邊距,專為小視窗優化 */
|
| 30 |
.block-container {
|
| 31 |
padding-top: 1.5rem !important;
|
| 32 |
+
padding-bottom: 5rem !important; /* 底部留白避免對話被輸入框蓋住 */
|
| 33 |
padding-left: 1rem !important;
|
| 34 |
padding-right: 1rem !important;
|
| 35 |
}
|
| 36 |
|
| 37 |
+
/* 4. 隱藏 Streamlit 預設的頂部裝飾與漢堡選單 */
|
| 38 |
header {
|
| 39 |
visibility: hidden;
|
| 40 |
}
|
|
|
|
| 52 |
genai.configure(api_key=api_key)
|
| 53 |
|
| 54 |
# 1. 基礎設定與連結
|
| 55 |
+
# ⚠️ 注意:這裡預設你把 md 檔放在 github 的 ai-knowledge 資料夾下。如果是放在根目錄,請把 ai-knowledge/ 刪除。
|
| 56 |
+
MD_BASE_URL = "https://raw.githubusercontent.com/ciecietaipei/ciecietaipei.github.io/main/ai-knowledge/"
|
| 57 |
+
# 從你們的 assets 資料夾抓取 Logo
|
| 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 |
+
"🍸 原創調酒 (Cocktails)": {
|
| 63 |
+
"raw_url": f"{MD_BASE_URL}cocktails.md",
|
| 64 |
+
"page_url": f"{HOME_URL}/cocktails.html",
|
| 65 |
+
"repo_url": "https://github.com/ciecietaipei/ciecietaipei.github.io/blob/main/ai-knowledge/cocktails.md"
|
| 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("正在同步 CIE CIE 最新菜單與資訊..."):
|
| 88 |
for category, info in KNOWLEDGE_MAP.items():
|
| 89 |
try:
|
| 90 |
response = requests.get(info["raw_url"])
|
|
|
|
| 92 |
combined_knowledge += f"\n\n## 【領域:{category}】\n"
|
| 93 |
combined_knowledge += response.text
|
| 94 |
except Exception as e:
|
| 95 |
+
st.warning(f"無法同步 {category} 的資料:請確認 GitHub 路徑是否正確。錯誤訊息:{e}")
|
| 96 |
return combined_knowledge
|
| 97 |
|
| 98 |
# 初始化 Session State
|
|
|
|
| 102 |
if "messages" not in st.session_state:
|
| 103 |
st.session_state.messages = []
|
| 104 |
|
|
|
|
| 105 |
if "example_prompt" not in st.session_state:
|
| 106 |
st.session_state.example_prompt = None
|
| 107 |
|
| 108 |
# 3. 側邊欄設計
|
| 109 |
with st.sidebar:
|
| 110 |
+
# 放入 CIE CIE 的圓形 Logo
|
| 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("🍸 CIE CIE 線上微醺客服")
|
| 124 |
+
st.caption("關於酒單、餐點、環境或訂位規範,歡迎直接詢問小助手!")
|
| 125 |
|
| 126 |
+
# 顯示專屬範例問句按鈕
|
| 127 |
example_cols = st.columns(3)
|
| 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 |
+
你現在是位於台北市大安區信義安和的微醺酒吧『CIE CIE TAIPEI』的專業線上客服小助手。
|
| 142 |
+
你的說話風格熱情、有質感且帶有一點輕鬆微醺的氛圍。
|
| 143 |
+
|
| 144 |
+
以下是從官方網站最新同步的資訊(包含原創調酒、經典酒水、精緻餐點、環境與規範):
|
| 145 |
---
|
| 146 |
{st.session_state.knowledge}
|
| 147 |
---
|
| 148 |
+
請嚴格基於上述提供的資訊來回答客人的問題。
|
| 149 |
+
如果客人問了菜單上沒有的品項,或超出了上述資訊範圍,請禮貌地告知:「目前小助手手邊沒有這項資訊,建議您直接私訊我們的 IG 或是現場詢問調酒師喔!」
|
| 150 |
"""
|
| 151 |
try:
|
| 152 |
+
# 建議使用 1.5-flash,反應速度最快且對文本理解極佳
|
| 153 |
model = genai.GenerativeModel(
|
| 154 |
+
model_name="gemini-1.5-flash-latest",
|
| 155 |
system_instruction=system_instruction
|
| 156 |
)
|
| 157 |
chat = model.start_chat(history=[])
|
| 158 |
response = chat.send_message(user_input)
|
| 159 |
return response.text
|
| 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
|
| 172 |
+
st.session_state.example_prompt = None
|
| 173 |
|
| 174 |
if prompt:
|
| 175 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 176 |
|
|
|
|
| 177 |
for message in st.session_state.messages:
|
| 178 |
with st.chat_message(message["role"]):
|
| 179 |
st.markdown(message["content"])
|
| 180 |
|
|
|
|
| 181 |
with st.chat_message("assistant"):
|
| 182 |
response_text = get_gemini_response(prompt)
|
| 183 |
st.markdown(response_text)
|
| 184 |
|
| 185 |
st.session_state.messages.append({"role": "assistant", "content": response_text})
|
| 186 |
+
st.rerun()
|
| 187 |
else:
|
|
|
|
| 188 |
for message in st.session_state.messages:
|
| 189 |
with st.chat_message(message["role"]):
|
| 190 |
st.markdown(message["content"])
|