Skip to content

Commit a358e84

Browse files
committed
chat.py: Add (SVG) save option
1 parent ef07708 commit a358e84

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

examples/chat.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,21 @@ def get_input_ids(_prefix):
274274
# Add response to context
275275
response = s.all_text.strip()
276276

277+
# Optionally save output
278+
if args.save:
279+
sr = response
280+
if args.save_svg:
281+
m = re.search(r"<svg\b[^>]*>.*?</svg>", sr, flags = re.IGNORECASE | re.DOTALL)
282+
sr = m.group(0).strip() if m else None
283+
if sr: print_info(f"Found SVG: {len(sr)} characters")
284+
else: print_error(f"No SVG block found")
285+
if sr:
286+
print_info(f"Writing response to: {args.save}")
287+
with open(args.save, "w") as f:
288+
f.write(sr)
289+
else:
290+
print_info(f"Nothing to write")
291+
277292
context[-1] = (user_prompt, response)
278293

279294

@@ -303,5 +318,7 @@ def get_input_ids(_prefix):
303318
parser.add_argument("-topp", "--top_p", type = float, help = "Top-P truncation, 1 to disable (default: disabled)", default = 1.0)
304319
parser.add_argument("-tps", "--show_tps", action = "store_true", help = "Show tokens/second after every reply")
305320
parser.add_argument("-prompt", "--prompt", type = str, help = "Run single prompt, then exit")
321+
parser.add_argument("-save", "--save", type = str, help = "Save output to file (use with --prompt)")
322+
parser.add_argument("-save_svg", "--save_svg", action = "store_true", help = "Extract SVG from response (use with --save)")
306323
_args = parser.parse_args()
307324
main(_args)

0 commit comments

Comments
 (0)