Starchik commited on
Commit
247c70e
·
verified ·
1 Parent(s): 34c9ff7

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -2
app.py CHANGED
@@ -18,6 +18,7 @@ Requirements (requirements.txt):
18
  import os
19
  import re
20
  import tempfile
 
21
  from pathlib import Path
22
 
23
  import gradio as gr
@@ -102,6 +103,23 @@ def extract_files(text: str, tag: str):
102
  return files, html_source
103
 
104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  def build_client(model: str) -> InferenceClient:
106
  kwargs = {"provider": "auto"}
107
  if HF_TOKEN:
@@ -166,10 +184,20 @@ def respond(message, chat_history, model, system_prompt, temperature, max_tokens
166
 
167
  tag = f"turn{len(chat_history)}"
168
  files, html_source = extract_files(partial, tag)
 
 
 
 
 
 
 
 
 
 
169
  yield (
170
  chat_history,
171
  "",
172
- gr.update(value=files if files else None, visible=bool(files)),
173
  gr.update(value=make_preview_html(html_source)),
174
  )
175
 
@@ -230,7 +258,10 @@ with gr.Blocks(title="Chatterbox", theme=gr.themes.Soft()) as demo:
230
 
231
  with gr.Column(scale=2):
232
  with gr.Tab("📄 Файлы"):
233
- files_out = gr.File(label="Сгенерированные файлы", file_count="multiple")
 
 
 
234
  with gr.Tab("👁 Превью (HTML)"):
235
  preview = gr.HTML(value=make_preview_html(None))
236
 
 
18
  import os
19
  import re
20
  import tempfile
21
+ import zipfile
22
  from pathlib import Path
23
 
24
  import gradio as gr
 
103
  return files, html_source
104
 
105
 
106
+ def zip_files(file_paths, tag: str) -> str:
107
+ """Bundle several generated files into a single .zip for one-click download.
108
+
109
+ Files are stored under their original name (the shared `tag_` prefix used
110
+ on disk to avoid collisions between turns is stripped inside the zip).
111
+ """
112
+ zip_path = OUT_DIR / f"{tag}_bundle.zip"
113
+ prefix = f"{tag}_"
114
+ with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
115
+ for fp in file_paths:
116
+ arcname = Path(fp).name
117
+ if arcname.startswith(prefix):
118
+ arcname = arcname[len(prefix):]
119
+ zf.write(fp, arcname=arcname)
120
+ return str(zip_path)
121
+
122
+
123
  def build_client(model: str) -> InferenceClient:
124
  kwargs = {"provider": "auto"}
125
  if HF_TOKEN:
 
184
 
185
  tag = f"turn{len(chat_history)}"
186
  files, html_source = extract_files(partial, tag)
187
+
188
+ if len(files) > 1:
189
+ # Multiple files: bundle them so it's one click, not one download
190
+ # per file.
191
+ download_value = zip_files(files, tag)
192
+ elif files:
193
+ download_value = files[0]
194
+ else:
195
+ download_value = None
196
+
197
  yield (
198
  chat_history,
199
  "",
200
+ gr.update(value=download_value, visible=bool(download_value)),
201
  gr.update(value=make_preview_html(html_source)),
202
  )
203
 
 
258
 
259
  with gr.Column(scale=2):
260
  with gr.Tab("📄 Файлы"):
261
+ files_out = gr.File(
262
+ label="Сгенерированные файлы (несколько — упаковываются в .zip)",
263
+ file_count="single",
264
+ )
265
  with gr.Tab("👁 Превью (HTML)"):
266
  preview = gr.HTML(value=make_preview_html(None))
267