Spaces:
Runtime error
Runtime error
Download app.py from UliC/adhd-brain-dump: direct link, hf CLI and curl.
- Browser
- Download file 3.54 kB
-
https://huggingface.co/spaces/UliC/adhd-brain-dump/resolve/main/app.py
- Command line
-
hf download hf://spaces/UliC/adhd-brain-dump/app.py
-
curl -L -o app.py https://huggingface.co/spaces/UliC/adhd-brain-dump/resolve/main/app.py
3.54 kB
| import os | |
| import json | |
| import streamlit as st | |
| import pandas as pd | |
| import google.generativeai as genai | |
| import gspread # 核心变动:直接调用底层引擎,不再受制于 st-gsheets-connection | |
| # --- 1. 页面配置 --- | |
| st.set_page_config(page_title="ADHD 智能行动台 V3.0", page_icon="🧠", layout="centered") | |
| SPREADSHEET_ID = "1YOygrFUtysFFRgixFLVg0vz0e07vKkCcvFiu7Bpa-Cc" | |
| # --- 2. 内存级凭证注入 (Gspread 原生模式) --- | |
| if "GCP_SA_JSON" not in os.environ: | |
| st.error("🚨 缺少 GCP_SA_JSON Secret。") | |
| st.stop() | |
| try: | |
| sa_dict = json.loads(os.environ["GCP_SA_JSON"]) | |
| # 直接使用原生库进行内存认证,完美避开所有系统限制 | |
| gc = gspread.service_account_from_dict(sa_dict) | |
| sheet = gc.open_by_key(SPREADSHEET_ID).worksheet("Sheet1") | |
| except Exception as e: | |
| st.error(f"🚨 原生连接失败: {e}") | |
| st.stop() | |
| # --- 3. AI 密钥读取 --- | |
| try: | |
| GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY") | |
| genai.configure(api_key=GOOGLE_API_KEY) | |
| model = genai.GenerativeModel('gemini-2.0-flash') | |
| except Exception as e: | |
| st.warning(f"⚠️ AI 配置异常: {e}") | |
| def load_data(): | |
| try: | |
| records = sheet.get_all_records() | |
| if records: | |
| return pd.DataFrame(records) | |
| return pd.DataFrame(columns=["type", "content"]) | |
| except Exception as e: | |
| return pd.DataFrame(columns=["type", "content"]) | |
| # --- 4. 交互界面 --- | |
| st.title("🎯 ADHD 智能行动台 V3.0") | |
| st.caption("Gspread 原生直连 | 极速增量写入模式") | |
| def brain_dump_ui(): | |
| with st.form("quick_dump", clear_on_submit=True): | |
| user_input = st.text_area("抛出你的想法:", height=100) | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| save_only = st.form_submit_button("直接同步 ☁️") | |
| with col2: | |
| save_with_ai = st.form_submit_button("AI 拆解 ✨") | |
| if (save_only or save_with_ai) and user_input: | |
| final_content = user_input | |
| if save_with_ai: | |
| with st.spinner("重构逻辑中..."): | |
| try: | |
| ai_suggestion = model.generate_content( | |
| f"你是一个为说明书思维设计的拆解工具。拆解任务:{user_input}。直接给出3-5个具备体感的原子步骤。" | |
| ).text | |
| final_content = f"【原想法】: {user_input}\n\n【指南】:\n{ai_suggestion}" | |
| except Exception as e: | |
| final_content += f"\n(AI失败: {e})" | |
| try: | |
| # 核心突破:直接调用 append_row。只需零点几秒,告别全表覆盖! | |
| sheet.append_row(["task_dump", final_content]) | |
| st.toast("✅ 数据已极速写入云端", icon="💾") | |
| if save_with_ai: | |
| st.success("拆解完成!") | |
| except Exception as e: | |
| st.error(f"❌ 写入失败: {e}") | |
| brain_dump_ui() | |
| st.divider() | |
| data = load_data() | |
| if not data.empty: | |
| with st.expander("历史记录 (点开查看)"): | |
| # 反转顺序,最新的在最上面 | |
| for index, row in data[::-1].iterrows(): | |
| st.write(row.get("content", "")) | |
| st.markdown("---") | |
| else: | |
| st.info("仓库是空的,去倾倒你的第一个任务吧!\n⚠️ 重要提示:请去你的 Google 表格,把 A1 单元格写上 type,B1 单元格写上 content,以防读取报错。") |