Spaces:
Running
Running
Upload 9 files
Browse files- src/model_tester.py +56 -35
src/model_tester.py
CHANGED
|
@@ -181,51 +181,72 @@ class ModelTester:
|
|
| 181 |
keyword: str,
|
| 182 |
api_key: str
|
| 183 |
) -> Optional[Dict[str, Any]]:
|
| 184 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
available_free = self.get_available_models(free_only=True)
|
| 186 |
-
|
| 187 |
|
|
|
|
| 188 |
candidates = []
|
| 189 |
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
candidates.extend([(m, "free_matched") for m in matched])
|
| 196 |
-
if not candidates:
|
| 197 |
-
candidates.extend([(m, "free_random") for m in available_free[:10]])
|
| 198 |
|
| 199 |
-
# 如果没
|
| 200 |
-
if not candidates and
|
| 201 |
-
candidates
|
|
|
|
| 202 |
|
| 203 |
-
# 如果
|
| 204 |
if not candidates:
|
|
|
|
| 205 |
try:
|
| 206 |
-
|
| 207 |
-
if
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
|
|
|
|
|
|
| 211 |
|
| 212 |
-
# 并发
|
| 213 |
-
if candidates:
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
|
|
|
| 229 |
return {
|
| 230 |
"success": False,
|
| 231 |
"model": candidates[0][0] if candidates else None,
|
|
|
|
| 181 |
keyword: str,
|
| 182 |
api_key: str
|
| 183 |
) -> Optional[Dict[str, Any]]:
|
| 184 |
+
# 第一步:从API获取最新的free模型列表
|
| 185 |
+
print(f"[try_best] Keyword: {keyword}, Refreshing model list...")
|
| 186 |
+
try:
|
| 187 |
+
self.refresh_model_list()
|
| 188 |
+
except Exception as e:
|
| 189 |
+
print(f"[try_best] refresh_model_list failed: {e}")
|
| 190 |
+
|
| 191 |
available_free = self.get_available_models(free_only=True)
|
| 192 |
+
print(f"[try_best] Found {len(available_free)} free models")
|
| 193 |
|
| 194 |
+
# 第二步:用关键词匹配模型
|
| 195 |
candidates = []
|
| 196 |
|
| 197 |
+
if keyword and available_free:
|
| 198 |
+
matched = [m for m in available_free if keyword.lower() in m.lower()]
|
| 199 |
+
print(f"[try_best] Keyword '{keyword}' matched: {matched}")
|
| 200 |
+
if matched:
|
| 201 |
+
candidates.extend([(m, "matched") for m in matched[:10]])
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
+
# 如果关键词没匹配或没提供关键词,随机取free模型
|
| 204 |
+
if not candidates and available_free:
|
| 205 |
+
candidates = [(m, "random") for m in available_free[:15]]
|
| 206 |
+
print(f"[try_best] Using random models: {candidates[:3]}")
|
| 207 |
|
| 208 |
+
# 如果列表为空,从API直接获取并测试
|
| 209 |
if not candidates:
|
| 210 |
+
print("[try_best] No candidates, fetching from API directly...")
|
| 211 |
try:
|
| 212 |
+
all_models = self.client.get_models()
|
| 213 |
+
all_free = [m.get("id", "") for m in all_models if ":free" in m.get("id", "")]
|
| 214 |
+
print(f"[try_best] API returned {len(all_free)} free models")
|
| 215 |
+
if all_free:
|
| 216 |
+
candidates = [(m, "api") for m in all_free[:20]]
|
| 217 |
+
except Exception as e:
|
| 218 |
+
print(f"[try_best] API fetch failed: {e}")
|
| 219 |
|
| 220 |
+
# 第三步:并发发送请求测试这些模型
|
| 221 |
+
if not candidates:
|
| 222 |
+
return {
|
| 223 |
+
"success": False,
|
| 224 |
+
"model": None,
|
| 225 |
+
"error": "No candidates available",
|
| 226 |
+
"method": "list_empty"
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
print(f"[try_best] Testing {len(candidates)} candidates...")
|
| 230 |
+
semaphore = asyncio.Semaphore(5)
|
| 231 |
+
|
| 232 |
+
async def try_one(model_id, match_type):
|
| 233 |
+
async with semaphore:
|
| 234 |
+
full_model = f"{model_id}:free" if ":free" not in model_id else model_id
|
| 235 |
+
print(f"[try_best] Testing model: {full_model}")
|
| 236 |
+
result = await self.try_model_direct(session, full_model, api_key)
|
| 237 |
+
print(f"[try_best] Result for {full_model}: {result.get('success') if result else 'None'}")
|
| 238 |
+
return result
|
| 239 |
+
|
| 240 |
+
tasks = [try_one(m, t) for m, t in candidates]
|
| 241 |
+
results = await asyncio.gather(*tasks, return_exceptions=True)
|
| 242 |
+
|
| 243 |
+
for i, result in enumerate(results):
|
| 244 |
+
if isinstance(result, dict) and result.get("success"):
|
| 245 |
+
result["method"] = f"list_{candidates[i][1]}"
|
| 246 |
+
print(f"[try_best] SUCCESS with {candidates[i][0]}")
|
| 247 |
+
return result
|
| 248 |
|
| 249 |
+
print(f"[try_best] All candidates failed")
|
| 250 |
return {
|
| 251 |
"success": False,
|
| 252 |
"model": candidates[0][0] if candidates else None,
|