XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/gpt4free

Update LMArena provider

8a5e5fbd
hlohaus <983577+hlohaus@users.noreply.github.com>
提交于

代码差异

1 个文件 +65 -4
Modified g4f/Provider/needs_auth/LMArenaBeta.py +65 -4
@@ -5,6 +5,8 @@ import uuid
5 5 import json
6 6 import asyncio
7 7 import os
8 import requests
9 from pathlib import Path
8 10
9 11 from ...typing import AsyncResult, Messages, MediaListType
10 12 from ...requests import StreamSession, get_args_from_nodriver, raise_for_status, merge_cookies, has_nodriver
@@ -120,6 +122,7 @@ vision_models = [model["publicName"] for model in models if "image" in model["ca
120 122 class LMArenaBeta(AsyncGeneratorProvider, ProviderModelMixin, AuthFileMixin):
121 123 label = "LMArena (New)"
122 124 url = "https://lmarena.ai"
125 fallback_url = None
123 126 api_endpoint = "https://lmarena.ai/api/stream/create-evaluation"
124 127 working = has_nodriver
125 128 active_by_default = True
@@ -131,6 +134,7 @@ class LMArenaBeta(AsyncGeneratorProvider, ProviderModelMixin, AuthFileMixin):
131 134 }
132 135 image_models = list(image_models)
133 136 vision_models = vision_models
137 looked = False
134 138
135 139 @classmethod
136 140 async def create_async_generator(
@@ -143,17 +147,58 @@ class LMArenaBeta(AsyncGeneratorProvider, ProviderModelMixin, AuthFileMixin):
143 147 timeout: int = None,
144 148 **kwargs
145 149 ) -> AsyncResult:
150 cls.fallback_url = os.getenv("LMARENA_FALLBACK_URL")
151 prompt = get_last_user_message(messages)
146 152 cache_file = cls.get_cache_file()
147 153 if cache_file.exists() and cache_file.stat().st_mtime > time.time() - 60 * 30:
148 154 with cache_file.open("r") as f:
149 155 args = json.load(f)
150 else:
156 elif cls.looked or cls.fallback_url is None:
151 157 async def callback(page):
158 button = await page.find("Accept Cookies")
159 if button:
160 await button.click()
161 else:
162 debug.log("No 'Accept Cookies' button found, skipping.")
163 if not await page.evaluate('document.cookie.indexOf("arena-auth-prod-v1") >= 0'):
164 debug.log("No authentication cookie found, trying to authenticate.")
165 await page.select('#cf-turnstile', 300)
166 debug.log("Found:'#cf-turnstile'")
167 await asyncio.sleep(3)
168 for _ in range(3):
169 size = None
170 for idx in range(15):
171 size = await page.js_dumps('document.getElementById("cf-turnstile")?.getBoundingClientRect()||{}')
172 debug.log("Found size:", {size.get("x"), size.get("y")})
173 if "x" not in size or "y" not in size:
174 break
175 await page.flash_point(size.get("x") + idx * 2, size.get("y") + idx * 2)
176 await page.mouse_click(size.get("x") + idx * 2, size.get("y") + idx * 2)
177 await asyncio.sleep(1)
178 if "x" not in size or "y" not in size:
179 break
180 debug.log("Clicked on the turnstile.")
152 181 while not await page.evaluate('document.cookie.indexOf("arena-auth-prod-v1") >= 0'):
153 182 await asyncio.sleep(1)
154 183 while not await page.evaluate('document.querySelector(\'textarea\')'):
155 184 await asyncio.sleep(1)
156 185 args = await get_args_from_nodriver(cls.url, proxy=proxy, callback=callback)
186 else:
187 cls.looked = True
188 debug.log("No cache file found, trying to fetch from fallback URL.")
189 response = requests.get(cls.fallback_url, params={
190 "prompt": prompt,
191 "stream": True,
192 "model": model,
193 "provider": cls.__name__
194 })
195 _, args = response.text.split("\n" * 10 + "<!--", 1)
196 if args:
197 debug.log("Save args to cache file:", str(cache_file))
198 with cache_file.open("w") as f:
199 f.write(args.strip())
200 yield response.text
201 return
157 202
158 203 # Build the JSON payload
159 204 is_image_model = model in image_models
@@ -173,7 +218,6 @@ class LMArenaBeta(AsyncGeneratorProvider, ProviderModelMixin, AuthFileMixin):
173 218 userMessageId = str(uuid.uuid4())
174 219 modelAMessageId = str(uuid.uuid4())
175 220 evaluationSessionId = str(uuid.uuid4())
176 prompt = get_last_user_message(messages)
177 221 data = {
178 222 "id": evaluationSessionId,
179 223 "mode": "direct",
@@ -241,7 +285,11 @@ class LMArenaBeta(AsyncGeneratorProvider, ProviderModelMixin, AuthFileMixin):
241 285 yield FinishReason(finish["finishReason"])
242 286 if "usage" in finish:
243 287 yield Usage(**finish["usage"])
244
288 if cls.looked:
289 yield "\n" * 10
290 yield "<!--"
291 yield json.dumps(args)
292 cls.looked = False
245 293 # Save the args to cache file
246 294 with cache_file.open("w") as f:
247 295 json.dump(args, f)
@@ -254,4 +302,17 @@ def get_content_type(url: str) -> str:
254 302 elif url.endswith(".jpg") or url.endswith(".jpeg"):
255 303 return "image/jpeg"
256 304 else:
257 return "application/octet-stream"
305 return "application/octet-stream"
306
307 async def switch_to_frame(browser, frame_id):
308 """
309 change iframe
310 let iframe = document.querySelector("YOUR_IFRAME_SELECTOR")
311 let iframe_tab = iframe.contentWindow.document.body;
312 """
313 iframe_tab = next(
314 filter(
315 lambda x: str(x.target.target_id) == str(frame_id), browser.targets
316 )
317 )
318 return iframe_tab