Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +59 -81
src/streamlit_app.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import google.generativeai as genai
|
| 3 |
import os
|
|
@@ -7,30 +9,38 @@ from datetime import datetime
|
|
| 7 |
# 設定 Streamlit 頁面
|
| 8 |
st.set_page_config(page_title="Gemini Chat Logger", page_icon="🤖")
|
| 9 |
|
| 10 |
-
#
|
| 11 |
def get_api_key():
|
| 12 |
-
# 嘗試
|
| 13 |
-
api_key = os.
|
| 14 |
-
if api_key:
|
| 15 |
-
return api_key
|
| 16 |
-
|
| 17 |
-
# 嘗試方法 2:手動輸入
|
| 18 |
-
api_key = st.text_input("請輸入您的 Google API 金鑰", type="password")
|
| 19 |
-
if api_key:
|
| 20 |
-
return api_key
|
| 21 |
-
|
| 22 |
-
# 如果所有方法都失敗
|
| 23 |
-
st.error("未找到 API 金鑰。請設置環境變數或手動輸入。")
|
| 24 |
-
st.stop()
|
| 25 |
|
| 26 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
api_key = get_api_key()
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
st.stop()
|
| 35 |
|
| 36 |
# CSV 檔案路徑
|
|
@@ -48,94 +58,62 @@ def ensure_csv_exists():
|
|
| 48 |
except Exception as e:
|
| 49 |
st.error(f"創建 CSV 檔案時發生錯誤: {e}")
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
def log_conversation(user_input,
|
| 53 |
try:
|
| 54 |
with open(CSV_FILE, mode="a", newline="", encoding="utf-8") as f:
|
| 55 |
writer = csv.writer(f)
|
| 56 |
-
writer.writerow([datetime.now().isoformat(), user_input,
|
| 57 |
except Exception as e:
|
| 58 |
st.error(f"記錄對話時發生錯誤: {e}")
|
| 59 |
|
| 60 |
# Streamlit 主應用程式
|
| 61 |
def main():
|
| 62 |
st.title("🤖 Gemini Chat Logger")
|
| 63 |
-
|
| 64 |
-
# 初始化 Gemini 模型
|
| 65 |
-
try:
|
| 66 |
-
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 67 |
-
except Exception as e:
|
| 68 |
-
st.error(f"初始化模型失敗: {e}")
|
| 69 |
-
return
|
| 70 |
-
|
| 71 |
-
# 確保 CSV 檔案存在
|
| 72 |
-
ensure_csv_exists()
|
| 73 |
|
| 74 |
-
#
|
| 75 |
if "messages" not in st.session_state:
|
| 76 |
st.session_state.messages = []
|
| 77 |
|
| 78 |
-
# 顯示歷史
|
| 79 |
for message in st.session_state.messages:
|
| 80 |
with st.chat_message(message["role"]):
|
| 81 |
st.markdown(message["content"])
|
| 82 |
|
| 83 |
# 使用者輸入
|
| 84 |
-
if prompt := st.chat_input("
|
| 85 |
-
# 將使用者訊息
|
| 86 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 87 |
-
|
| 88 |
# 在聊天介面顯示使用者訊息
|
| 89 |
with st.chat_message("user"):
|
| 90 |
st.markdown(prompt)
|
| 91 |
|
| 92 |
-
#
|
| 93 |
try:
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
gemini_response = response.text.strip()
|
| 97 |
-
st.markdown(gemini_response)
|
| 98 |
|
| 99 |
-
#
|
| 100 |
-
|
|
|
|
| 101 |
|
| 102 |
-
# 將
|
| 103 |
-
st.session_state.messages.append({"role": "assistant", "content":
|
| 104 |
|
| 105 |
-
|
| 106 |
-
st.
|
| 107 |
-
|
| 108 |
-
# 運行主應用程式
|
| 109 |
-
if __name__ == "__main__":
|
| 110 |
-
main()
|
| 111 |
-
# Use an official Python runtime as a parent image
|
| 112 |
-
FROM python:3.9-slim
|
| 113 |
-
|
| 114 |
-
# Set the working directory in the container
|
| 115 |
-
WORKDIR /app
|
| 116 |
-
|
| 117 |
-
# Install system dependencies
|
| 118 |
-
RUN apt-get update && apt-get install -y \
|
| 119 |
-
build-essential \
|
| 120 |
-
curl \
|
| 121 |
-
software-properties-common \
|
| 122 |
-
git \
|
| 123 |
-
&& rm -rf /var/lib/apt/lists/*
|
| 124 |
-
|
| 125 |
-
# Upgrade pip
|
| 126 |
-
RUN pip install --upgrade pip
|
| 127 |
-
|
| 128 |
-
# Copy the current directory contents into the container at /app
|
| 129 |
-
COPY . /app
|
| 130 |
|
| 131 |
-
#
|
| 132 |
-
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
| 136 |
|
| 137 |
-
#
|
| 138 |
-
|
| 139 |
|
| 140 |
-
#
|
| 141 |
-
|
|
|
|
|
|
| 1 |
+
# Streamlit Gemini Chat Logger
|
| 2 |
+
|
| 3 |
import streamlit as st
|
| 4 |
import google.generativeai as genai
|
| 5 |
import os
|
|
|
|
| 9 |
# 設定 Streamlit 頁面
|
| 10 |
st.set_page_config(page_title="Gemini Chat Logger", page_icon="🤖")
|
| 11 |
|
| 12 |
+
# API 金鑰管理
|
| 13 |
def get_api_key():
|
| 14 |
+
# 嘗試從環境變數獲取 API 金鑰
|
| 15 |
+
api_key = os.environ.get("GOOGLE_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# 如果環境變數中沒有,嘗試從 Streamlit secrets 獲取
|
| 18 |
+
if not api_key:
|
| 19 |
+
try:
|
| 20 |
+
api_key = st.secrets["GOOGLE_API_KEY"]
|
| 21 |
+
except KeyError:
|
| 22 |
+
# 如果 secrets 中也沒有,提示使用者
|
| 23 |
+
st.warning("""
|
| 24 |
+
### 🔑 API 金鑰未找到
|
| 25 |
+
請設定 Google API 金鑰:
|
| 26 |
+
1. 環境變數:設定 `GOOGLE_API_KEY`
|
| 27 |
+
2. Streamlit secrets:在 `.streamlit/secrets.toml` 中添加 `GOOGLE_API_KEY`
|
| 28 |
+
3. 在下方輸入 API 金鑰
|
| 29 |
+
""")
|
| 30 |
+
|
| 31 |
+
# 提供手動輸入 API 金鑰的選項
|
| 32 |
+
api_key = st.text_input("輸入您的 Google API 金鑰", type="password")
|
| 33 |
+
|
| 34 |
+
return api_key
|
| 35 |
+
|
| 36 |
+
# 配置 API 金鑰
|
| 37 |
api_key = get_api_key()
|
| 38 |
+
if api_key:
|
| 39 |
+
try:
|
| 40 |
+
genai.configure(api_key=api_key)
|
| 41 |
+
except Exception as e:
|
| 42 |
+
st.error(f"配置 API 金鑰時發生錯誤: {e}")
|
| 43 |
+
else:
|
| 44 |
st.stop()
|
| 45 |
|
| 46 |
# CSV 檔案路徑
|
|
|
|
| 58 |
except Exception as e:
|
| 59 |
st.error(f"創建 CSV 檔案時發生錯誤: {e}")
|
| 60 |
|
| 61 |
+
# 記錄對話到 CSV
|
| 62 |
+
def log_conversation(user_input, model_response):
|
| 63 |
try:
|
| 64 |
with open(CSV_FILE, mode="a", newline="", encoding="utf-8") as f:
|
| 65 |
writer = csv.writer(f)
|
| 66 |
+
writer.writerow([datetime.now().isoformat(), user_input, model_response])
|
| 67 |
except Exception as e:
|
| 68 |
st.error(f"記錄對話時發生錯誤: {e}")
|
| 69 |
|
| 70 |
# Streamlit 主應用程式
|
| 71 |
def main():
|
| 72 |
st.title("🤖 Gemini Chat Logger")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
# 初始化會話狀態
|
| 75 |
if "messages" not in st.session_state:
|
| 76 |
st.session_state.messages = []
|
| 77 |
|
| 78 |
+
# 顯示歷史訊息
|
| 79 |
for message in st.session_state.messages:
|
| 80 |
with st.chat_message(message["role"]):
|
| 81 |
st.markdown(message["content"])
|
| 82 |
|
| 83 |
# 使用者輸入
|
| 84 |
+
if prompt := st.chat_input("輸入您的訊息"):
|
| 85 |
+
# 將使用者訊息新增到會話狀態
|
| 86 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 87 |
+
|
| 88 |
# 在聊天介面顯示使用者訊息
|
| 89 |
with st.chat_message("user"):
|
| 90 |
st.markdown(prompt)
|
| 91 |
|
| 92 |
+
# 使用 Gemini 模型生成回應
|
| 93 |
try:
|
| 94 |
+
# 初始化模型
|
| 95 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
# 生成回應
|
| 98 |
+
response = model.generate_content(prompt)
|
| 99 |
+
model_response = response.text.strip()
|
| 100 |
|
| 101 |
+
# 將模型回應新增到會話狀態
|
| 102 |
+
st.session_state.messages.append({"role": "assistant", "content": model_response})
|
| 103 |
|
| 104 |
+
# 在聊天介面顯示模型回應
|
| 105 |
+
with st.chat_message("assistant"):
|
| 106 |
+
st.markdown(model_response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
+
# 記錄對話到 CSV
|
| 109 |
+
log_conversation(prompt, model_response)
|
| 110 |
|
| 111 |
+
except Exception as e:
|
| 112 |
+
st.error(f"生成回應時發生錯誤: {e}")
|
| 113 |
|
| 114 |
+
# 確保 CSV 檔案存在
|
| 115 |
+
ensure_csv_exists()
|
| 116 |
|
| 117 |
+
# 執行主應用程式
|
| 118 |
+
if __name__ == "__main__":
|
| 119 |
+
main()
|