Spaces:
Runtime error
Runtime error
Delete config.py
Browse files
config.py
DELETED
|
@@ -1,104 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import sqlite3
|
| 3 |
-
import uuid
|
| 4 |
-
import datetime
|
| 5 |
-
|
| 6 |
-
# 1. 定位原项目在 Docker 容器内生成的 SQLite 数据库路径
|
| 7 |
-
# 原项目默认存放在 server/prisma/dev.db 或类似路径下,根据容器内实际路径做安全兼容
|
| 8 |
-
DB_PATHS = [
|
| 9 |
-
"/app/server/prisma/dev.db",
|
| 10 |
-
"server/prisma/dev.db",
|
| 11 |
-
"prisma/dev.db"
|
| 12 |
-
]
|
| 13 |
-
|
| 14 |
-
def get_active_db():
|
| 15 |
-
for path in DB_PATHS:
|
| 16 |
-
if os.path.exists(path):
|
| 17 |
-
return path
|
| 18 |
-
# 如果还没生成,尝试初始化一个路径
|
| 19 |
-
return "/app/server/prisma/dev.db"
|
| 20 |
-
|
| 21 |
-
def inject_secrets_to_sqlite():
|
| 22 |
-
db_path = get_active_db()
|
| 23 |
-
print(f"📦 正在连接 FreeLLMAPI 核心数据库: {db_path}")
|
| 24 |
-
|
| 25 |
-
# 对齐你配置的 HF Secrets 名字与项目数据库里的 Provider 标识
|
| 26 |
-
secrets_map = {
|
| 27 |
-
"GOOGLE_API_KEY": "google",
|
| 28 |
-
"GROQ_API_KEY": "groq",
|
| 29 |
-
"GITHUB_TOKEN": "github",
|
| 30 |
-
"OPENROUTER_API_KEY": "openrouter",
|
| 31 |
-
"MISTRAL_API_KEY": "mistral",
|
| 32 |
-
"TOGETHER_API_KEY": "together",
|
| 33 |
-
"NVIDIA_API_KEY": "nvidia",
|
| 34 |
-
"COHERE_API_KEY": "cohere",
|
| 35 |
-
"HF_TOKEN": "huggingface",
|
| 36 |
-
"CEREBRAS_API_KEY": "cerebras",
|
| 37 |
-
"SAMBANOVA_API_KEY": "sambanova",
|
| 38 |
-
"CLOUDFLARE_API_TOKEN": "cloudflare",
|
| 39 |
-
"ZHIPU_API_KEY": "zhipu"
|
| 40 |
-
}
|
| 41 |
-
|
| 42 |
-
try:
|
| 43 |
-
conn = sqlite3.connect(db_path)
|
| 44 |
-
cursor = conn.cursor()
|
| 45 |
-
|
| 46 |
-
# 2. 检查原项目内 Provider 或者是 Keys 表的结构
|
| 47 |
-
# 原项目的表名通常叫 Provider 或者 Credentials
|
| 48 |
-
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
|
| 49 |
-
tables = [t[0] for t in cursor.fetchall()]
|
| 50 |
-
print(f"📊 探测到系统当前表结构: {tables}")
|
| 51 |
-
|
| 52 |
-
# 寻找存储渠道的表(通常包含 'Provider' 或 'Credential' 关键字)
|
| 53 |
-
target_table = "Provider" if "Provider" in tables else None
|
| 54 |
-
if not target_table:
|
| 55 |
-
for t in tables:
|
| 56 |
-
if "key" in t.lower() or "provider" in t.lower():
|
| 57 |
-
target_table = t
|
| 58 |
-
break
|
| 59 |
-
|
| 60 |
-
if not target_table:
|
| 61 |
-
print("❌ 未能识别到原项目的密钥存储表,请先在前端随便手动添加任意一个Key以初始化数据库。")
|
| 62 |
-
return
|
| 63 |
-
|
| 64 |
-
# 3. 遍历并强行注入 Secrets
|
| 65 |
-
success_count = 0
|
| 66 |
-
now = datetime.datetime.utcnow().isoformat()
|
| 67 |
-
|
| 68 |
-
for secret_name, provider_id in secrets_map.items():
|
| 69 |
-
actual_key = os.getenv(secret_name)
|
| 70 |
-
if not actual_key:
|
| 71 |
-
continue
|
| 72 |
-
|
| 73 |
-
# 检查数据库中是否已存在该渠道,防止重复插入
|
| 74 |
-
cursor.execute(f"SELECT id FROM {target_table} WHERE name = ? OR provider = ?", (provider_id, provider_id))
|
| 75 |
-
exists = cursor.fetchone()
|
| 76 |
-
|
| 77 |
-
if not exists:
|
| 78 |
-
# 动态生成符合 Prisma 规范的记录
|
| 79 |
-
# 💡 注意:原版采用了不加密或自加密,这里直接存入(如果原版带加密,脚本运行后前端会显示加密格式)
|
| 80 |
-
record_id = str(uuid.uuid4())
|
| 81 |
-
try:
|
| 82 |
-
# 根据标准的 Prisma 字段结构进行弹性插入
|
| 83 |
-
cursor.execute(f"""
|
| 84 |
-
INSERT INTO {target_table} (id, name, provider, apiKey, status, createdAt, updatedAt)
|
| 85 |
-
VALUES (?, ?, ?, ?, ?, ?, ?)
|
| 86 |
-
""", (record_id, provider_id, provider_id, actual_key, "active", now, now))
|
| 87 |
-
success_count += 1
|
| 88 |
-
except sqlite3.OperationalError:
|
| 89 |
-
# 如果字段不完全对齐,尝试最简化插入
|
| 90 |
-
cursor.execute(f"""
|
| 91 |
-
INSERT INTO {target_table} (id, name, apiKey)
|
| 92 |
-
VALUES (?, ?, ?)
|
| 93 |
-
""", (record_id, provider_id, actual_key))
|
| 94 |
-
success_count += 1
|
| 95 |
-
|
| 96 |
-
conn.commit()
|
| 97 |
-
conn.close()
|
| 98 |
-
print(f"🏁 注入成功!已将 {success_count} 个云端 Secrets 强制同步到本地 SQLite。")
|
| 99 |
-
|
| 100 |
-
except Exception as e:
|
| 101 |
-
print(f"⚠️ 自动注入过程中发生意外,正在等待系统原生初始化: {str(e)}")
|
| 102 |
-
|
| 103 |
-
if __name__ == "__main__":
|
| 104 |
-
inject_secrets_to_sqlite()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|