Spaces:
Sleeping
Sleeping
Update scripts/sync_bailian_models.py
Browse files- scripts/sync_bailian_models.py +47 -31
scripts/sync_bailian_models.py
CHANGED
|
@@ -9,22 +9,44 @@ from datetime import datetime
|
|
| 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,
|
| 13 |
-
""
|
| 14 |
-
cursor = conn.execute(f"PRAGMA table_info({table_name})")
|
| 15 |
return [row[1] for row in cursor.fetchall()]
|
| 16 |
|
| 17 |
-
def
|
| 18 |
-
"""获取或创建百炼 provider"""
|
| 19 |
cursor = conn.cursor()
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
row = cursor.fetchone()
|
| 23 |
if row:
|
| 24 |
return row[0]
|
| 25 |
-
#
|
| 26 |
-
cursor.execute("
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
conn.commit()
|
| 29 |
return cursor.lastrowid
|
| 30 |
|
|
@@ -48,37 +70,31 @@ def main():
|
|
| 48 |
print(f"❌ Failed to fetch models: {e}")
|
| 49 |
return 1
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
conn.row_factory = sqlite3.Row
|
| 55 |
-
except Exception as e:
|
| 56 |
-
print(f"❌ Cannot connect to database: {e}")
|
| 57 |
-
return 1
|
| 58 |
|
| 59 |
-
# 获取
|
| 60 |
-
|
| 61 |
-
print(f"
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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':
|
|
@@ -90,7 +106,7 @@ def main():
|
|
| 90 |
elif col == 'created_at':
|
| 91 |
values.append(datetime.utcnow().isoformat())
|
| 92 |
elif col == 'name':
|
| 93 |
-
values.append(model_id)
|
| 94 |
sql = f"INSERT INTO models ({','.join(insert_cols)}) VALUES ({placeholders})"
|
| 95 |
cursor.execute(sql, values)
|
| 96 |
added += 1
|
|
|
|
| 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):
|
| 13 |
+
cursor = conn.execute(f"PRAGMA table_info({table})")
|
|
|
|
| 14 |
return [row[1] for row in cursor.fetchall()]
|
| 15 |
|
| 16 |
+
def ensure_provider_table(conn):
|
|
|
|
| 17 |
cursor = conn.cursor()
|
| 18 |
+
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name IN ('providers', 'channels')")
|
| 19 |
+
tables = [row[0] for row in cursor.fetchall()]
|
| 20 |
+
if 'providers' in tables:
|
| 21 |
+
return 'providers'
|
| 22 |
+
elif 'channels' in tables:
|
| 23 |
+
return 'channels'
|
| 24 |
+
else:
|
| 25 |
+
# 创建 providers 表
|
| 26 |
+
cursor.execute("""
|
| 27 |
+
CREATE TABLE providers (
|
| 28 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 29 |
+
name TEXT,
|
| 30 |
+
base_url TEXT,
|
| 31 |
+
api_key TEXT,
|
| 32 |
+
created_at TEXT
|
| 33 |
+
)
|
| 34 |
+
""")
|
| 35 |
+
conn.commit()
|
| 36 |
+
return 'providers'
|
| 37 |
+
|
| 38 |
+
def get_or_create_bailian_provider(conn, provider_table):
|
| 39 |
+
cursor = conn.cursor()
|
| 40 |
+
# 尝试查找已有百炼记录
|
| 41 |
+
cursor.execute(f"SELECT id FROM {provider_table} WHERE base_url LIKE '%dashscope.aliyuncs.com%' LIMIT 1")
|
| 42 |
row = cursor.fetchone()
|
| 43 |
if row:
|
| 44 |
return row[0]
|
| 45 |
+
# 插入新记录
|
| 46 |
+
cursor.execute(f"""
|
| 47 |
+
INSERT INTO {provider_table} (name, base_url, api_key, created_at)
|
| 48 |
+
VALUES (?, ?, ?, ?)
|
| 49 |
+
""", ("Bailian", BAILIAN_URL, "", datetime.utcnow().isoformat()))
|
| 50 |
conn.commit()
|
| 51 |
return cursor.lastrowid
|
| 52 |
|
|
|
|
| 70 |
print(f"❌ Failed to fetch models: {e}")
|
| 71 |
return 1
|
| 72 |
|
| 73 |
+
conn = sqlite3.connect(DB_PATH)
|
| 74 |
+
provider_table = ensure_provider_table(conn)
|
| 75 |
+
provider_id = get_or_create_bailian_provider(conn, provider_table)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
# 获取 models 表的列
|
| 78 |
+
model_columns = get_table_columns(conn, "models")
|
| 79 |
+
print(f"models table columns: {model_columns}")
|
| 80 |
|
| 81 |
+
# 准备插入语句(根据实际列名动态构建)
|
| 82 |
+
insert_cols = []
|
| 83 |
+
for col in ['id', 'provider_id', 'enabled', 'created_at', 'name']:
|
| 84 |
+
if col in model_columns:
|
| 85 |
+
insert_cols.append(col)
|
| 86 |
+
if not insert_cols:
|
| 87 |
+
print("❌ models table missing expected columns")
|
| 88 |
+
return 1
|
| 89 |
|
|
|
|
|
|
|
|
|
|
| 90 |
placeholders = ','.join(['?' for _ in insert_cols])
|
|
|
|
| 91 |
cursor = conn.cursor()
|
| 92 |
added = 0
|
| 93 |
for model_id in models:
|
|
|
|
| 94 |
cursor.execute("SELECT 1 FROM models WHERE id = ?", (model_id,))
|
| 95 |
if cursor.fetchone():
|
| 96 |
print(f"⏭️ Already exists: {model_id}")
|
| 97 |
continue
|
|
|
|
|
|
|
| 98 |
values = []
|
| 99 |
for col in insert_cols:
|
| 100 |
if col == 'id':
|
|
|
|
| 106 |
elif col == 'created_at':
|
| 107 |
values.append(datetime.utcnow().isoformat())
|
| 108 |
elif col == 'name':
|
| 109 |
+
values.append(model_id)
|
| 110 |
sql = f"INSERT INTO models ({','.join(insert_cols)}) VALUES ({placeholders})"
|
| 111 |
cursor.execute(sql, values)
|
| 112 |
added += 1
|