chuan commited on
Commit
16ceaf4
·
1 Parent(s): 97507d2

feat: improve config check UI and sync logic

Browse files
Files changed (2) hide show
  1. app.py +14 -0
  2. 服务/数据采集/hf_sync.py +29 -40
app.py CHANGED
@@ -110,11 +110,25 @@ def 获取监控数据():
110
  except Exception as e:
111
  return pd.DataFrame([{"错误": f"获取数据失败: {str(e)}"}])
112
 
 
 
 
 
 
 
 
 
113
  # ==========================================
114
  # 3. 构建 Gradio 界面
115
  # ==========================================
116
  with gr.Blocks(title="Quant_Unified 监控中心", theme=gr.themes.Soft()) as demo:
117
  gr.Markdown("# 🚀 Quant_Unified 量化系统监控中心")
 
 
 
 
 
 
118
  gr.Markdown("实时展示部署在云端的采集服务状态。数据通过 Supabase 同步。")
119
 
120
  with gr.Row():
 
110
  except Exception as e:
111
  return pd.DataFrame([{"错误": f"获取数据失败: {str(e)}"}])
112
 
113
+ def 检查配置状态():
114
+ status = {
115
+ "SUPABASE_URL": "✅ 已配置" if os.getenv("SUPABASE_URL") else "❌ 未配置",
116
+ "SUPABASE_KEY": "✅ 已配置" if (os.getenv("SUPABASE_SERVICE_ROLE_KEY") or os.getenv("SUPABASE_ANON_KEY")) else "❌ 未配置",
117
+ "HF_TOKEN": "✅ 已配置" if os.getenv("HF_TOKEN") else "❌ 未配置 (需手动在 Settings -> Secrets 配置)",
118
+ }
119
+ return status
120
+
121
  # ==========================================
122
  # 3. 构建 Gradio 界面
123
  # ==========================================
124
  with gr.Blocks(title="Quant_Unified 监控中心", theme=gr.themes.Soft()) as demo:
125
  gr.Markdown("# 🚀 Quant_Unified 量化系统监控中心")
126
+
127
+ with gr.Accordion("🛠️ 系统环境检查", open=False):
128
+ conf = 检查配置状态()
129
+ for k, v in conf.items():
130
+ gr.Markdown(f"**{k}**: {v}")
131
+
132
  gr.Markdown("实时展示部署在云端的采集服务状态。数据通过 Supabase 同步。")
133
 
134
  with gr.Row():
服务/数据采集/hf_sync.py CHANGED
@@ -22,54 +22,43 @@ LOCAL_DATA_DIR = Path(__file__).resolve().parents[2] / "data" / "行情数据_
22
  # ---------------------------------------------------------
23
 
24
  def sync_to_hf():
25
- """将本地整理好的数据同步到 Hugging Face Dataset"""
26
  token = os.getenv("HF_TOKEN")
27
  if not token:
28
- logger.error("❌ 未发现 HF_TOKEN 环境变量,请在 Space Settings 中添加 Secret: HF_TOKEN (需要 Write 权限)")
29
  return False
30
-
31
- api = HfApi(token=token)
32
-
33
- # 1. 确保仓库存在
34
  try:
35
- create_repo(repo_id=DATASET_REPO, repo_type="dataset", exist_ok=True)
36
- logger.info(f"✅ 数据集仓库已就绪: {DATASET_REPO}")
37
- except Exception as e:
38
- logger.error(f" 创建/访问仓库失败: {e}")
39
- return False
 
 
 
 
40
 
41
- # 2. 扫描本地整理好的数据文件
42
- if not LOCAL_DATA_DIR.exists():
43
- logger.warning(f"⚠️ 本地整理目录不存在: {LOCAL_DATA_DIR}")
44
- return False
45
 
46
- files_to_upload = list(LOCAL_DATA_DIR.rglob("*.parquet"))
47
- if not files_to_upload:
48
- logger.info("ℹ️ 没有发现需要上传的 .parquet 文件")
49
- return True
50
-
51
- logger.info(f"🚀 准备同步 {len(files_to_upload)} 个文件到云端...")
52
-
53
- # 3. 批量上传
54
- try:
55
- # 我们按日期分目录上传,保持目录结构
56
- # 这里的 path_in_repo 会保持 LOCAL_DATA_DIR 之后的相对路径
57
- for file_path in files_to_upload:
58
- relative_path = file_path.relative_to(LOCAL_DATA_DIR)
59
- path_in_repo = str(relative_path)
60
-
61
- logger.info(f"正在上传: {path_in_repo}")
62
- api.upload_file(
63
- path_or_fileobj=str(file_path),
64
- path_in_repo=path_in_repo,
65
- repo_id=DATASET_REPO,
66
- repo_type="dataset",
67
- )
68
-
69
- logger.info("✨ 所有文件同步完成!")
70
  return True
71
  except Exception as e:
72
- logger.error(f"❌ 同步过程中出错: {e}")
73
  return False
74
 
75
  if __name__ == "__main__":
 
22
  # ---------------------------------------------------------
23
 
24
  def sync_to_hf():
25
+ """执行同步逻辑"""
26
  token = os.getenv("HF_TOKEN")
27
  if not token:
28
+ logger.error("❌ 未找到 HF_TOKEN,无法同步到 Dataset。请在 Space Secrets 中配置。")
29
  return False
30
+
31
+ # 获取或创建数据集
32
+ repo_id = DATASET_REPO
 
33
  try:
34
+ api = HfApi(token=token)
35
+ # 尝试创建仓库,如果已存在会报错,我们忽略它
36
+ try:
37
+ api.create_repo(repo_id=repo_id, repo_type="dataset", exist_ok=True)
38
+ logger.info(f"✅ 数据集仓库已就绪: {repo_id}")
39
+ except Exception as e:
40
+ if "already exists" not in str(e):
41
+ logger.error(f"❌ 创建/检查数据集仓库失败: {e}")
42
+ return False
43
 
44
+ # 检查本地目录
45
+ if not LOCAL_DATA_DIR.exists():
46
+ logger.warning(f"⚠️ 本地目录不存在,跳过同步: {LOCAL_DATA_DIR}")
47
+ return True
48
 
49
+ # 上传整个目录
50
+ logger.info(f"� 正在同步数据到 Hugging Face Dataset: {repo_id}...")
51
+ api.upload_folder(
52
+ folder_path=str(LOCAL_DATA_DIR),
53
+ repo_id=repo_id,
54
+ repo_type="dataset",
55
+ commit_message=f"Auto-sync: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
56
+ ignore_patterns=["*.tmp", "*.log"]
57
+ )
58
+ logger.info("✅ 同步完成!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  return True
60
  except Exception as e:
61
+ logger.error(f"❌ 同步过程中发生错误: {e}")
62
  return False
63
 
64
  if __name__ == "__main__":