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

XFEstudio/gpt4free

add headers, robust sign-out detection, fix section window

bcec2d4a
Desel72 <pedroluiscolmenares722@gmail.com>
提交于

代码差异

1 个文件 +15 -3
Modified g4f/Provider/local/Ollama.py +15 -3
@@ -47,11 +47,23 @@ class Ollama(OpenaiTemplate):
47 47 async with session.get(
48 48 "https://ollama.com/settings",
49 49 cookies=cookies,
50 headers={"User-Agent": "Mozilla/5.0"},
50 headers={
51 "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
52 "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
53 "Accept-Language": "en-US,en;q=0.9",
54 },
51 55 ) as response:
52 56 await raise_for_status(response)
53 57 html = await response.text()
54 if "sign in" in html.lower() or "/signin" in html.lower():
58 lower = html.lower()
59 has_sign_in = "sign in to ollama" in lower or "log in to ollama" in lower
60 has_auth_endpoint = "/api/auth/signin" in lower or "/auth/signin" in lower or 'href="/signin"' in lower or 'href="/login"' in lower
61 has_form = "<form" in lower
62 has_password = 'type="password"' in lower or 'name="password"' in lower
63 has_email = 'type="email"' in lower or 'name="email"' in lower
64 if (has_sign_in and has_form and (has_email or has_password or has_auth_endpoint)) \
65 or (has_form and has_auth_endpoint) \
66 or (has_form and has_password and has_email):
55 67 debug.error("Ollama session cookie is invalid or expired")
56 68 return None
57 69 quota = {}
@@ -59,7 +71,7 @@ class Ollama(OpenaiTemplate):
59 71 idx = html.find(label)
60 72 if idx == -1:
61 73 continue
62 section = html[idx:idx + 500]
74 section = html[idx + len(label):idx + len(label) + 800]
63 75 pct_match = re.search(r'(\d+(?:\.\d+)?)%\s*used', section)
64 76 if not pct_match:
65 77 width_match = re.search(r'width:\s*(\d+(?:\.\d+)?)%', section)