返回提交历史
Modified
g4f/cli/client.py
+25
-10
Modified
g4f/client/__init__.py
+1
-1
XFEstudio/gpt4free
Support images input
94d5b486
代码差异
2 个文件
+26
-11
@@ -12,7 +12,7 @@ from g4f.client import AsyncClient
12
12
from g4f.providers.response import JsonConversation, is_content
13
13
from g4f.cookies import set_cookies_dir, read_cookie_files
14
14
from g4f.Provider import ProviderUtils
15
from g4f.image import extract_data_uri
15
from g4f.image import extract_data_uri, is_accepted_format
16
16
from g4f.image.copy_images import get_media_dir
17
17
from g4f.client.helper import filter_markdown
18
18
from g4f import debug
@@ -65,6 +65,9 @@ class ConversationManager:
65
65
66
66
def save(self) -> None:
67
67
"""Save conversation to file."""
68
if not self.file_path:
69
return
70
68
71
try:
69
72
with open(self.file_path, 'w', encoding='utf-8') as f:
70
73
if self.conversation and self.provider:
@@ -96,9 +99,10 @@ async def stream_response(
96
99
instructions: Optional[str] = None
97
100
) -> None:
98
101
"""Stream the response from the API and update conversation."""
102
image = None
103
if isinstance(input_text, tuple):
104
image, input_text = input_text
99
105
input_text = input_text.strip()
100
if not input_text:
101
raise ValueError("Input text cannot be empty")
102
106
103
107
if instructions:
104
108
# Add system instructions to conversation if provided
@@ -109,7 +113,8 @@ async def stream_response(
109
113
110
114
create_args = {
111
115
"messages": conversation.get_messages(),
112
"stream": True
116
"stream": True,
117
"image": image
113
118
}
114
119
115
120
if conversation.model:
@@ -150,7 +155,7 @@ def save_content(content, filepath: str, allowed_types = None):
150
155
elif hasattr(content, "data"):
151
156
content = content.data
152
157
if content.startswith("/media/"):
153
os.rename(content.replace("/media", get_media_dir()), filepath)
158
os.rename(content.replace("/media", get_media_dir()).split("?")[0], filepath)
154
159
return True
155
160
elif content.startswith("data:"):
156
161
with open(filepath, "wb") as f:
@@ -226,7 +231,8 @@ def get_parser():
226
231
async def run_args(input_text: str, args):
227
232
try:
228
233
# Ensure directories exist
229
args.output.parent.mkdir(parents=True, exist_ok=True)
234
if args.output:
235
args.output.parent.mkdir(parents=True, exist_ok=True)
230
236
args.conversation_file.parent.mkdir(parents=True, exist_ok=True)
231
237
args.cookies_dir.mkdir(parents=True, exist_ok=True)
232
238
@@ -259,10 +265,19 @@ async def run_args(input_text: str, args):
259
265
sys.exit(1)
260
266
261
267
def run_client_args(args):
262
input_text = " ".join(args.input)
263
if os.path.isfile(input_text):
264
with open(input_text, 'r', encoding='utf-8') as f:
265
input_text = f.read().strip()
268
input_text = ""
269
if args.input and os.path.isfile(args.input[0]):
270
with open(args.input[0], 'rb') as f:
271
if is_accepted_format(f.read(12)):
272
input_text = (Path(args.input[0]), " ".join(args.input[1:]))
273
else:
274
with open(input_text, 'r', encoding='utf-8') as f:
275
file_content = f.read().strip()
276
if len(input_text) > 1:
277
input_text = " ".join(input_text[1:])
278
input_text = f"```{os.path.basename(input_text)}\n" + file_content + "\n```" + input_text
279
elif args.input:
280
input_text = " ".join(args.input)
266
281
if not input_text:
267
282
input_text = sys.stdin.read().strip()
268
283
if not input_text:
@@ -47,7 +47,7 @@ def add_chunk(content, chunk):
47
47
48
48
def resolve_media(kwargs: dict, image = None, image_name: str = None) -> None:
49
49
if image is not None:
50
kwargs["media"] = [(image, image_name)]
50
kwargs["media"] = [(image, getattr(image, "name", image_name))]
51
51
elif "images" in kwargs:
52
52
kwargs["media"] = kwargs.pop("images")
53
53
if "media" in kwargs and not isinstance(kwargs["media"], list):