返回提交历史
Modified
g4f/Provider/needs_auth/LMArena.py
+40
-37
Modified
scripts/start-browser.sh
+1
-0
XFEstudio/gpt4free
Add socat command to start-browser.sh for TCP forwarding
d51ae1c2
代码差异
2 个文件
+41
-37
@@ -119,6 +119,7 @@ class LMArena(AsyncGeneratorProvider, ProviderModelMixin, AuthFileMixin):
119
119
"getEmailProvider": "607c2dd3d84af5a00b322b577498d1b2a739c5dfe0",
120
120
"deleteAccount": "40a57e8c369eaf8a82483fae2f8106489ce041dffd",
121
121
}
122
_grecaptcha = []
122
123
123
124
@classmethod
124
125
def load_models(cls, models_data: str):
@@ -199,7 +200,6 @@ class LMArena(AsyncGeneratorProvider, ProviderModelMixin, AuthFileMixin):
199
200
@classmethod
200
201
async def get_args_from_nodriver(cls, proxy, clear_cookies=False):
201
202
cache_file = cls.get_cache_file()
202
grecaptcha = []
203
203
204
204
async def callback(page: nodriver.Tab):
205
205
try:
@@ -245,11 +245,12 @@ class LMArena(AsyncGeneratorProvider, ProviderModelMixin, AuthFileMixin):
245
245
await asyncio.sleep(1)
246
246
while not await page.evaluate('window.grecaptcha && window.grecaptcha.enterprise'):
247
247
await asyncio.sleep(1)
248
captcha = await page.evaluate(
249
"""window.grecaptcha.enterprise.execute('6LeTGMcsAAAAALuIlkVwIxaAuZA8VledA6d3Nnb0', { action: 'chat_submit' } );""",
250
await_promise=True)
251
grecaptcha.append(captcha)
252
debug.log("Obtained grecaptcha token.")
248
for _ in range(1):
249
captcha = await page.evaluate(
250
"""window.grecaptcha.enterprise.execute('6LeTGMcsAAAAALuIlkVwIxaAuZA8VledA6d3Nnb0', { action: 'chat_submit' } );""",
251
await_promise=True)
252
cls._grecaptcha.append(captcha)
253
debug.log("Obtained grecaptcha token.")
253
254
html = await page.get_content()
254
255
await cls.__load_actions(html)
255
256
@@ -259,37 +260,39 @@ class LMArena(AsyncGeneratorProvider, ProviderModelMixin, AuthFileMixin):
259
260
with cache_file.open("w") as f:
260
261
json.dump(args, f)
261
262
262
return args, next(iter(grecaptcha))
263
return args
263
264
264
265
@classmethod
265
266
async def get_grecaptcha(cls, args, proxy):
266
267
cache_file = cls.get_cache_file()
267
grecaptcha = []
268
if cls._grecaptcha:
269
return args
268
270
269
271
async def callback(page: nodriver.Tab):
270
272
while not await page.evaluate('window.grecaptcha && window.grecaptcha.enterprise'):
271
273
await asyncio.sleep(1)
272
captcha = await page.evaluate(
273
"""new Promise((resolve) => {
274
window.grecaptcha.enterprise.ready(async () => {
275
try {
276
const token = await window.grecaptcha.enterprise.execute(
277
'6LeTGMcsAAAAALuIlkVwIxaAuZA8VledA6d3Nnb0',
278
{ action: 'chat_submit' }
279
);
280
resolve(token);
281
} catch (e) {
282
console.error("[LMArena API] reCAPTCHA execute failed:", e);
283
resolve(null);
284
}
285
});
286
});""",
287
await_promise=True
288
)
289
if isinstance(captcha, str):
290
grecaptcha.append(captcha)
291
else:
292
raise Exception(captcha)
274
for _ in range(1):
275
captcha = await page.evaluate(
276
"""new Promise((resolve) => {
277
window.grecaptcha.enterprise.ready(async () => {
278
try {
279
const token = await window.grecaptcha.enterprise.execute(
280
'6LeTGMcsAAAAALuIlkVwIxaAuZA8VledA6d3Nnb0',
281
{ action: 'chat_submit' }
282
);
283
resolve(token);
284
} catch (e) {
285
console.error("[LMArena API] reCAPTCHA execute failed:", e);
286
resolve(null);
287
}
288
});
289
});""",
290
await_promise=True
291
)
292
if isinstance(captcha, str):
293
cls._grecaptcha.append(captcha)
294
else:
295
raise Exception(captcha)
293
296
html = await page.get_content()
294
297
await cls.__load_actions(html)
295
298
@@ -300,7 +303,7 @@ class LMArena(AsyncGeneratorProvider, ProviderModelMixin, AuthFileMixin):
300
303
with cache_file.open("w") as f:
301
304
json.dump(args, f)
302
305
303
return args, next(iter(grecaptcha))
306
return args
304
307
305
308
@classmethod
306
309
async def __load_actions(cls, html):
@@ -375,7 +378,6 @@ class LMArena(AsyncGeneratorProvider, ProviderModelMixin, AuthFileMixin):
375
378
for v, k in start_id:
376
379
if len(v) == 42:
377
380
cls._next_actions[k] = v
378
debug.log(f"{k}: {v}")
379
381
else:
380
382
debug.error(f"wrong {k} value: {v}")
381
383
@@ -496,13 +498,12 @@ class LMArena(AsyncGeneratorProvider, ProviderModelMixin, AuthFileMixin):
496
498
prompt = get_last_user_message(messages)
497
499
cache_file = cls.get_cache_file()
498
500
args = cls.read_args(kwargs.get("lmarena_args", {}))
499
grecaptcha = kwargs.pop("grecaptcha", "")
500
501
_need_clear_cookies = False
501
502
for _ in range(2):
502
503
if args:
503
504
pass
504
505
elif has_nodriver:
505
args, grecaptcha = await cls.get_args_from_nodriver(proxy, _need_clear_cookies)
506
args = await cls.get_args_from_nodriver(proxy, _need_clear_cookies)
506
507
else:
507
508
raise MissingRequirementsError("No auth file found and nodriver is not available.")
508
509
@@ -533,7 +534,7 @@ class LMArena(AsyncGeneratorProvider, ProviderModelMixin, AuthFileMixin):
533
534
if modelAId and modelBId:
534
535
mode = "side-by-side"
535
536
elif modelAId:
536
mode = "direct"
537
mode = "direct-battle"
537
538
else:
538
539
mode = "battle"
539
540
if conversation and getattr(conversation, "evaluationSessionId", None):
@@ -546,9 +547,9 @@ class LMArena(AsyncGeneratorProvider, ProviderModelMixin, AuthFileMixin):
546
547
userMessageId = str(uuid7())
547
548
modelAMessageId = str(uuid7())
548
549
modelBMessageId = str(uuid7())
549
if not grecaptcha and has_nodriver:
550
if not cls._grecaptcha and has_nodriver:
550
551
debug.log("No grecaptcha token found, obtaining new one...")
551
args, grecaptcha = await cls.get_grecaptcha(args, proxy)
552
args = await cls.get_grecaptcha(args, proxy)
552
553
files = await cls.prepare_images(args, media)
553
554
data = {
554
555
"id": evaluationSessionId,
@@ -561,7 +562,7 @@ class LMArena(AsyncGeneratorProvider, ProviderModelMixin, AuthFileMixin):
561
562
"metadata": {}
562
563
},
563
564
"modality": "image" if is_image_model else "chat",
564
"recaptchaV3Token": grecaptcha
565
"recaptchaV3Token": cls._grecaptcha.pop() if cls._grecaptcha else "",
565
566
}
566
567
if modelAId:
567
568
data["modelAId"] = modelAId
@@ -630,6 +631,8 @@ class LMArena(AsyncGeneratorProvider, ProviderModelMixin, AuthFileMixin):
630
631
debug.log(f"{cls.__name__}: Cloudflare error")
631
632
continue
632
633
except RateLimitError as error:
634
if "prompt failed" in str(error):
635
raise
633
636
args = None
634
637
_need_clear_cookies = True
635
638
debug.error(error)
@@ -131,6 +131,7 @@ if [[ "$BROWSER_BIN" == *.exe ]]; then
131
131
rm -f ~/.g4f/cookies/.browser_is_open
132
132
"$BROWSER_BIN" "${BROWSER_FLAGS[@]}"
133
133
else
134
socat tcp-listen:9223,reuseaddr,fork tcp:localhost:57011 &
134
135
# Linux: loop and relaunch if the browser exits
135
136
while true; do
136
137
rm -f ~/.g4f/cookies/.browser_is_open