返回提交历史
Modified
g4f/Provider/needs_auth/PuterJS.py
+1
-0
Modified
g4f/api/__init__.py
+4
-2
Modified
g4f/cli/__init__.py
+7
-2
Modified
g4f/requests/__init__.py
+6
-2
XFEstudio/gpt4free
Enhance PuterJS provider to track live model instances and update BrowserConfig for automation tool settings
933fff98
代码差异
4 个文件
+18
-6
@@ -264,6 +264,7 @@ class PuterJS(AsyncGeneratorProvider, ProviderModelMixin):
264
264
url = "https://api.puter.com/puterai/chat/models/"
265
265
cls.models = requests.get(url).json().get("models", [])
266
266
cls.models = [model for model in cls.models if "/" not in model and model not in ["abuse", "costly", "fake", "model-fallback-test-1"]]
267
cls.live += 1
267
268
except Exception as e:
268
269
debug.log(f"PuterJS: Failed to fetch models from API: {e}")
269
270
cls.models = []
@@ -71,6 +71,7 @@ from g4f.providers.types import ProviderType
71
71
from g4f.providers.response import AudioResponse
72
72
from g4f.providers.any_provider import AnyProvider
73
73
from g4f.providers.any_model_map import model_map, vision_models, image_models, audio_models, video_models
74
from g4f.requests import BrowserConfig
74
75
from g4f import Provider
75
76
from g4f.gui import get_gui_app
76
77
from .stubs import (
@@ -78,8 +79,7 @@ from .stubs import (
78
79
ProviderResponseModel, ModelResponseModel,
79
80
ErrorResponseModel, ProviderResponseDetailModel,
80
81
FileResponseModel,
81
TranscriptionResponseModel, AudioSpeechConfig,
82
ResponsesConfig
82
TranscriptionResponseModel, AudioSpeechConfig
83
83
)
84
84
from g4f import debug
85
85
@@ -103,6 +103,8 @@ async def lifespan(app: FastAPI):
103
103
AppConfig.g4f_api_key = os.environ.get("G4F_API_KEY", AppConfig.g4f_api_key)
104
104
AppConfig.timeout = os.environ.get("G4F_TIMEOUT", AppConfig.timeout)
105
105
AppConfig.stream_timeout = os.environ.get("G4F_STREAM_TIMEOUT", AppConfig.stream_timeout)
106
BrowserConfig.port = os.environ.get("G4F_BROWSER_PORT", BrowserConfig.port)
107
BrowserConfig.host = os.environ.get("G4F_BROWSER_HOST", BrowserConfig.host)
106
108
yield
107
109
if has_nodriver:
108
110
for browser in util.get_registered_instances():
@@ -1,9 +1,9 @@
1
1
from __future__ import annotations
2
2
3
import sys
4
3
import argparse
5
4
from argparse import ArgumentParser
6
5
from .client import get_parser, run_client_args
6
from ..requests import BrowserConfig
7
7
8
8
from g4f import Provider
9
9
from g4f.gui.run import gui_parser, run_gui_args
@@ -36,7 +36,9 @@ def get_api_parser():
36
36
api_parser.add_argument("--ssl-keyfile", type=str, default=None, help="Path to SSL key file for HTTPS.")
37
37
api_parser.add_argument("--ssl-certfile", type=str, default=None, help="Path to SSL certificate file for HTTPS.")
38
38
api_parser.add_argument("--log-config", type=str, default=None, help="Custom log config.")
39
39
api_parser.add_argument("--browser-port", type=int, help="Port for the browser automation tool.")
40
api_parser.add_argument("--browser-host", type=str, default="127.0.0.1", help="Host for the browser automation tool.")
41
40
42
return api_parser
41
43
42
44
def run_api_args(args):
@@ -54,6 +56,9 @@ def run_api_args(args):
54
56
demo=args.demo,
55
57
timeout=args.timeout,
56
58
)
59
if args.browser_port:
60
BrowserConfig.port = args.browser_port
61
BrowserConfig.host = args.browser_host
57
62
if args.cookie_browsers:
58
63
g4f.cookies.BROWSERS = [g4f.cookies[browser] for browser in args.cookie_browsers]
59
64
run_api(
@@ -46,6 +46,8 @@ from ..cookies import get_cookies_dir
46
46
from .defaults import DEFAULT_HEADERS, WEBVIEW_HAEDERS
47
47
48
48
class BrowserConfig:
49
port: int = None
50
host: str = None
49
51
stop_browser = lambda: None
50
52
browser_executable_path: str = None
51
53
@@ -168,8 +170,8 @@ async def get_nodriver(
168
170
if not os.path.exists(browser_executable_path):
169
171
browser_executable_path = None
170
172
debug.log(f"Browser executable path: {browser_executable_path}")
173
lock_file = Path(get_cookies_dir()) / ".nodriver_is_open"
171
174
if user_data_dir:
172
lock_file = Path(get_cookies_dir()) / ".nodriver_is_open"
173
175
lock_file.parent.mkdir(exist_ok=True)
174
176
# Implement a short delay (milliseconds) to prevent race conditions.
175
177
await asyncio.sleep(0.1 * random.randint(0, 50))
@@ -199,6 +201,8 @@ async def get_nodriver(
199
201
user_data_dir=user_data_dir,
200
202
browser_args=[*browser_args, f"--proxy-server={proxy}"] if proxy else browser_args,
201
203
browser_executable_path=browser_executable_path,
204
port=BrowserConfig.port,
205
host=BrowserConfig.host,
202
206
**kwargs
203
207
)
204
208
except FileNotFoundError as e:
@@ -211,7 +215,7 @@ async def get_nodriver(
211
215
raise
212
216
def on_stop():
213
217
try:
214
if browser.connection:
218
if BrowserConfig.port is None and browser.connection:
215
219
browser.stop()
216
220
except:
217
221
pass