Spaces:
Sleeping
Sleeping
Update scripts/sync_bailian_models.py
Browse files- scripts/sync_bailian_models.py +62 -17
scripts/sync_bailian_models.py
CHANGED
|
@@ -4,27 +4,45 @@ 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 |
-
#
|
| 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 |
-
|
| 26 |
-
if not
|
| 27 |
-
print("⚠️ No models found
|
| 28 |
return 1
|
| 29 |
except Exception as e:
|
| 30 |
print(f"❌ Failed to fetch models: {e}")
|
|
@@ -33,27 +51,54 @@ def main():
|
|
| 33 |
# 连接数据库
|
| 34 |
try:
|
| 35 |
conn = sqlite3.connect(DB_PATH)
|
| 36 |
-
|
| 37 |
except Exception as e:
|
| 38 |
print(f"❌ Cannot connect to database: {e}")
|
| 39 |
return 1
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
cursor.execute("SELECT 1 FROM models WHERE id = ?", (model_id,))
|
| 44 |
-
if
|
| 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: {
|
| 57 |
return 0
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
|
|
|
| 4 |
import sqlite3
|
| 5 |
import json
|
| 6 |
import urllib.request
|
|
|
|
| 7 |
from datetime import datetime
|
| 8 |
|
| 9 |
DB_PATH = "/app/server/data/freeapi.db"
|
| 10 |
BAILIAN_URL = "https://dashscope.aliyuncs.com/compatible-mode/v1/models"
|
| 11 |
|
| 12 |
+
def get_table_columns(conn, table_name):
|
| 13 |
+
"""返回表的列名列表"""
|
| 14 |
+
cursor = conn.execute(f"PRAGMA table_info({table_name})")
|
| 15 |
+
return [row[1] for row in cursor.fetchall()]
|
| 16 |
+
|
| 17 |
+
def get_or_create_bailian_provider(conn):
|
| 18 |
+
"""获取或创建百炼 provider"""
|
| 19 |
+
cursor = conn.cursor()
|
| 20 |
+
# 尝试查找已有的百炼 provider(根据 base_url 包含 dashscope)
|
| 21 |
+
cursor.execute("SELECT id FROM providers WHERE base_url LIKE '%dashscope.aliyuncs.com%' LIMIT 1")
|
| 22 |
+
row = cursor.fetchone()
|
| 23 |
+
if row:
|
| 24 |
+
return row[0]
|
| 25 |
+
# 如果没有,创建一个新的
|
| 26 |
+
cursor.execute("INSERT INTO providers (name, base_url, api_key, created_at) VALUES (?, ?, ?, ?)",
|
| 27 |
+
("Bailian", BAILIAN_URL, "", datetime.utcnow().isoformat()))
|
| 28 |
+
conn.commit()
|
| 29 |
+
return cursor.lastrowid
|
| 30 |
+
|
| 31 |
def main():
|
| 32 |
api_key = os.getenv("BAILIAN_API_KEY")
|
| 33 |
if not api_key:
|
| 34 |
print("⚠️ BAILIAN_API_KEY not set, skipping model sync")
|
| 35 |
return 0
|
| 36 |
|
| 37 |
+
# 获取模型列表
|
| 38 |
req = urllib.request.Request(BAILIAN_URL)
|
| 39 |
req.add_header("Authorization", f"Bearer {api_key}")
|
| 40 |
try:
|
| 41 |
with urllib.request.urlopen(req, timeout=30) as response:
|
| 42 |
data = json.loads(response.read().decode('utf-8'))
|
| 43 |
+
models = [item["id"] for item in data.get("data", [])]
|
| 44 |
+
if not models:
|
| 45 |
+
print("⚠️ No models found")
|
| 46 |
return 1
|
| 47 |
except Exception as e:
|
| 48 |
print(f"❌ Failed to fetch models: {e}")
|
|
|
|
| 51 |
# 连接数据库
|
| 52 |
try:
|
| 53 |
conn = sqlite3.connect(DB_PATH)
|
| 54 |
+
conn.row_factory = sqlite3.Row
|
| 55 |
except Exception as e:
|
| 56 |
print(f"❌ Cannot connect to database: {e}")
|
| 57 |
return 1
|
| 58 |
|
| 59 |
+
# 获取 provider_id
|
| 60 |
+
provider_id = get_or_create_bailian_provider(conn)
|
| 61 |
+
print(f"Using provider_id: {provider_id}")
|
| 62 |
+
|
| 63 |
+
# 获取 models 表列名
|
| 64 |
+
columns = get_table_columns(conn, "models")
|
| 65 |
+
print(f"models table columns: {columns}")
|
| 66 |
+
|
| 67 |
+
# 确定插入时使用的列
|
| 68 |
+
insert_cols = [col for col in ['id', 'provider_id', 'enabled', 'created_at', 'name'] if col in columns]
|
| 69 |
+
# 如果 'name' 列存在,则 name 使用 model_id;否则忽略
|
| 70 |
+
placeholders = ','.join(['?' for _ in insert_cols])
|
| 71 |
+
|
| 72 |
+
cursor = conn.cursor()
|
| 73 |
+
added = 0
|
| 74 |
+
for model_id in models:
|
| 75 |
+
# 检查是否已存在
|
| 76 |
cursor.execute("SELECT 1 FROM models WHERE id = ?", (model_id,))
|
| 77 |
+
if cursor.fetchone():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
print(f"⏭️ Already exists: {model_id}")
|
| 79 |
+
continue
|
| 80 |
+
|
| 81 |
+
# 准备插入数据
|
| 82 |
+
values = []
|
| 83 |
+
for col in insert_cols:
|
| 84 |
+
if col == 'id':
|
| 85 |
+
values.append(model_id)
|
| 86 |
+
elif col == 'provider_id':
|
| 87 |
+
values.append(provider_id)
|
| 88 |
+
elif col == 'enabled':
|
| 89 |
+
values.append(1)
|
| 90 |
+
elif col == 'created_at':
|
| 91 |
+
values.append(datetime.utcnow().isoformat())
|
| 92 |
+
elif col == 'name':
|
| 93 |
+
values.append(model_id) # name 与 id 相同
|
| 94 |
+
sql = f"INSERT INTO models ({','.join(insert_cols)}) VALUES ({placeholders})"
|
| 95 |
+
cursor.execute(sql, values)
|
| 96 |
+
added += 1
|
| 97 |
+
print(f"➕ Added model: {model_id}")
|
| 98 |
|
| 99 |
conn.commit()
|
| 100 |
conn.close()
|
| 101 |
+
print(f"✅ Sync completed. New models added: {added}")
|
| 102 |
return 0
|
| 103 |
|
| 104 |
if __name__ == "__main__":
|