返回提交历史
Modified
g4f/cli/client.py
+13
-12
XFEstudio/gpt4free
Update CLI client
c1c00eee
代码差异
1 个文件
+13
-12
@@ -136,6 +136,7 @@ async def stream_response(
136
136
except (IOError, BrokenPipeError) as e:
137
137
print(f"\nError writing to stdout: {e}", file=sys.stderr)
138
138
break
139
print("\n", end="")
139
140
140
141
conversation.conversation = getattr(last_chunk, 'conversation', None)
141
142
response_content = response_content[0] if len(response_content) == 1 else "".join([str(chunk) for chunk in response_content])
@@ -168,7 +169,7 @@ def save_content(content, filepath: str, allowed_types = None):
168
169
with open(filepath, "wb") as f:
169
170
f.write(response.content)
170
171
return True
171
content = filter_markdown(content, allowed_types)
172
content = filter_markdown(content, allowed_types, content)
172
173
if content:
173
174
with open(filepath, "w") as f:
174
175
f.write(content)
@@ -216,7 +217,7 @@ def get_parser():
216
217
help="File to store/load conversation state"
217
218
)
218
219
parser.add_argument(
219
'--clear-history',
220
'-C', '--clear-history',
220
221
action='store_true',
221
222
help="Clear conversation history before starting"
222
223
)
@@ -257,9 +258,7 @@ async def run_args(input_text: str, args):
257
258
258
259
# Save conversation state
259
260
conversation.save()
260
261
print() # Ensure final newline
262
261
263
262
except Exception as e:
264
263
print(traceback.format_exc(), file=sys.stderr)
265
264
sys.exit(1)
@@ -268,14 +267,16 @@ def run_client_args(args):
268
267
input_text = ""
269
268
if args.input and os.path.isfile(args.input[0]):
270
269
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:
270
try:
271
if is_accepted_format(f.read(12)):
272
input_text = (Path(args.input[0]), " ".join(args.input[1:]))
273
except ValueError:
274
# If not a valid image, read as text
275
with open(args.input[0], 'r', encoding='utf-8') as f:
275
276
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
277
if len(args.input) > 1:
278
input_text = " ".join(args.input[1:]) + "\n"
279
input_text += f"```{os.path.basename(args.input[0])}\n" + file_content + "\n```"
279
280
elif args.input:
280
281
input_text = " ".join(args.input)
281
282
if not input_text: