Nils Durner commited on
Commit
a467dc2
·
1 Parent(s): 02f1bd2

multi-file upload

Browse files
Files changed (1) hide show
  1. app.py +21 -19
app.py CHANGED
@@ -49,31 +49,33 @@ def add_text(history, text):
49
  history = history + [(text, None)]
50
  return history, gr.Textbox(value="", interactive=False)
51
 
52
- def add_file(history, file):
53
- if file.name.endswith(".docx"):
54
- content = process_docx(file.name)
55
- else:
56
- with open(file.name, mode="rb") as f:
57
- content = f.read()
 
58
 
59
- if isinstance(content, bytes):
60
- content = content.decode('utf-8', 'replace')
61
- else:
62
- content = str(content)
63
 
64
- fn = os.path.basename(file.name)
65
- history = history + [(f'```{fn}\n{content}\n```', None)]
66
 
67
- gr.Info(f"File added as {fn}")
68
 
69
  return history
70
 
71
- def add_img(history, file):
72
- if log_to_console:
73
- print(f"add_img {file.name}")
74
- history = history + [(image_embed_prefix + file.name, None)]
 
75
 
76
- gr.Info(f"Image added as {file.name}")
77
 
78
  return history
79
 
@@ -224,7 +226,7 @@ with gr.Blocks() as demo:
224
  )
225
 
226
  with gr.Row():
227
- btn = gr.UploadButton("📁 Upload", size="sm")
228
  img_btn = gr.UploadButton("🖼️ Upload", size="sm", file_types=["image"])
229
  undo_btn = gr.Button("↩️ Undo")
230
  undo_btn.click(undo, inputs=[chatbot], outputs=[chatbot])
 
49
  history = history + [(text, None)]
50
  return history, gr.Textbox(value="", interactive=False)
51
 
52
+ def add_file(history, files):
53
+ for file in files:
54
+ if file.name.endswith(".docx"):
55
+ content = process_docx(file.name)
56
+ else:
57
+ with open(file.name, mode="rb") as f:
58
+ content = f.read()
59
 
60
+ if isinstance(content, bytes):
61
+ content = content.decode('utf-8', 'replace')
62
+ else:
63
+ content = str(content)
64
 
65
+ fn = os.path.basename(file.name)
66
+ history = history + [(f'```{fn}\n{content}\n```', None)]
67
 
68
+ gr.Info(f"File added as {fn}")
69
 
70
  return history
71
 
72
+ def add_img(history, files):
73
+ for file in files:
74
+ if log_to_console:
75
+ print(f"add_img {file.name}")
76
+ history = history + [(image_embed_prefix + file.name, None)]
77
 
78
+ gr.Info(f"Image added as {file.name}")
79
 
80
  return history
81
 
 
226
  )
227
 
228
  with gr.Row():
229
+ btn = gr.UploadButton("📁 Upload", size="sm", file_count="multiple")
230
  img_btn = gr.UploadButton("🖼️ Upload", size="sm", file_types=["image"])
231
  undo_btn = gr.Button("↩️ Undo")
232
  undo_btn.click(undo, inputs=[chatbot], outputs=[chatbot])