Spaces:
Sleeping
Sleeping
Update scripts/sync_bailian_models.py
Browse files- scripts/sync_bailian_models.py +23 -24
scripts/sync_bailian_models.py
CHANGED
|
@@ -2,33 +2,32 @@
|
|
| 2 |
import os
|
| 3 |
import sys
|
| 4 |
import sqlite3
|
| 5 |
-
import
|
|
|
|
|
|
|
| 6 |
from datetime import datetime
|
| 7 |
|
| 8 |
-
# 设置数据库路径和百炼API端点
|
| 9 |
DB_PATH = "/app/server/data/freeapi.db"
|
| 10 |
BAILIAN_URL = "https://dashscope.aliyuncs.com/compatible-mode/v1/models"
|
| 11 |
|
| 12 |
def main():
|
| 13 |
-
# 获取百炼的API Key
|
| 14 |
api_key = os.getenv("BAILIAN_API_KEY")
|
| 15 |
if not api_key:
|
| 16 |
-
print("
|
| 17 |
-
return
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
|
|
|
| 21 |
try:
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
print("⚠️ 警告: 未能从API获取到任何模型。")
|
| 29 |
-
return 1
|
| 30 |
except Exception as e:
|
| 31 |
-
print(f"❌
|
| 32 |
return 1
|
| 33 |
|
| 34 |
# 连接数据库
|
|
@@ -36,25 +35,25 @@ def main():
|
|
| 36 |
conn = sqlite3.connect(DB_PATH)
|
| 37 |
cursor = conn.cursor()
|
| 38 |
except Exception as e:
|
| 39 |
-
print(f"❌
|
| 40 |
return 1
|
| 41 |
|
| 42 |
-
# 同步模型列表
|
| 43 |
added_count = 0
|
| 44 |
for model_id in models_list:
|
| 45 |
-
# 检查模型是否已存在
|
| 46 |
cursor.execute("SELECT 1 FROM models WHERE id = ?", (model_id,))
|
| 47 |
if not cursor.fetchone():
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
added_count += 1
|
| 51 |
-
print(f"➕
|
| 52 |
else:
|
| 53 |
-
print(f"⏭️
|
| 54 |
|
| 55 |
conn.commit()
|
| 56 |
conn.close()
|
| 57 |
-
print(f"✅
|
| 58 |
return 0
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import os
|
| 3 |
import sys
|
| 4 |
import sqlite3
|
| 5 |
+
import json
|
| 6 |
+
import urllib.request
|
| 7 |
+
import urllib.error
|
| 8 |
from datetime import datetime
|
| 9 |
|
|
|
|
| 10 |
DB_PATH = "/app/server/data/freeapi.db"
|
| 11 |
BAILIAN_URL = "https://dashscope.aliyuncs.com/compatible-mode/v1/models"
|
| 12 |
|
| 13 |
def main():
|
|
|
|
| 14 |
api_key = os.getenv("BAILIAN_API_KEY")
|
| 15 |
if not api_key:
|
| 16 |
+
print("⚠️ BAILIAN_API_KEY not set, skipping model sync")
|
| 17 |
+
return 0
|
| 18 |
|
| 19 |
+
# 请求百炼 API
|
| 20 |
+
req = urllib.request.Request(BAILIAN_URL)
|
| 21 |
+
req.add_header("Authorization", f"Bearer {api_key}")
|
| 22 |
try:
|
| 23 |
+
with urllib.request.urlopen(req, timeout=30) as response:
|
| 24 |
+
data = json.loads(response.read().decode('utf-8'))
|
| 25 |
+
models_list = [item["id"] for item in data.get("data", [])]
|
| 26 |
+
if not models_list:
|
| 27 |
+
print("⚠️ No models found from API")
|
| 28 |
+
return 1
|
|
|
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
+
print(f"❌ Failed to fetch models: {e}")
|
| 31 |
return 1
|
| 32 |
|
| 33 |
# 连接数据库
|
|
|
|
| 35 |
conn = sqlite3.connect(DB_PATH)
|
| 36 |
cursor = conn.cursor()
|
| 37 |
except Exception as e:
|
| 38 |
+
print(f"❌ Cannot connect to database: {e}")
|
| 39 |
return 1
|
| 40 |
|
|
|
|
| 41 |
added_count = 0
|
| 42 |
for model_id in models_list:
|
|
|
|
| 43 |
cursor.execute("SELECT 1 FROM models WHERE id = ?", (model_id,))
|
| 44 |
if not cursor.fetchone():
|
| 45 |
+
# 根据你的数据库实际结构调整 provider_id(可能需要查询 provider 表)
|
| 46 |
+
# 这里假设百炼渠道的 provider_id 为 1,请根据实际情况修改
|
| 47 |
+
cursor.execute("INSERT INTO models (id, name, provider_id, enabled, created_at) VALUES (?, ?, 1, 1, ?)",
|
| 48 |
+
(model_id, model_id, datetime.utcnow().isoformat()))
|
| 49 |
added_count += 1
|
| 50 |
+
print(f"➕ Added model: {model_id}")
|
| 51 |
else:
|
| 52 |
+
print(f"⏭️ Already exists: {model_id}")
|
| 53 |
|
| 54 |
conn.commit()
|
| 55 |
conn.close()
|
| 56 |
+
print(f"✅ Sync completed. New models added: {added_count}")
|
| 57 |
return 0
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|