ID0M commited on
Commit
7b3419f
·
verified ·
1 Parent(s): ceb5e3c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -7
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  import subprocess
3
  import os
4
  import shutil
 
5
 
6
 
7
  def setup_avif_decode():
@@ -20,7 +21,7 @@ def convert_avif_to_png(avif_files):
20
  output_paths = []
21
  for avif_file in avif_files:
22
  avif_path = avif_file.name
23
- png_path = avif_path.rsplit('.', 1)[0] + '.png'
24
  result = subprocess.run(["avif-decode/target/release/avif_decode", "-f", avif_path, png_path], capture_output=True, text=True)
25
  if result.returncode == 0:
26
  output_paths.append(png_path)
@@ -29,29 +30,57 @@ def convert_avif_to_png(avif_files):
29
  return output_paths
30
 
31
 
 
 
 
 
 
 
 
 
 
32
  css = """
33
  #col-container {
34
  margin: 0 auto;
35
  max-width: 520px;
36
  }
37
  """
38
- with gr.Blocks(css=css) as demo:
39
  with gr.Column(elem_id="col-container"):
40
  gr.Markdown("""
41
  # AVIF to PNG Converter
42
  Upload your AVIF files and get them converted to PNG.
43
- """)
44
- with gr.Row():
45
  avif_file = gr.File(
46
  label="Upload AVIF File",
47
  file_types=[".avif"],
48
  file_count="multiple"
49
- )
50
- run_button = gr.Button("Convert", scale=0)
51
  result = gr.Gallery(label="Result")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  run_button.click(
53
- fn=convert_avif_to_png,
54
  inputs=[avif_file],
55
  outputs=[result]
56
  )
 
 
 
 
 
57
  demo.queue().launch()
 
2
  import subprocess
3
  import os
4
  import shutil
5
+ import zipfile
6
 
7
 
8
  def setup_avif_decode():
 
21
  output_paths = []
22
  for avif_file in avif_files:
23
  avif_path = avif_file.name
24
+ png_path = avif_path.rsplit('.', 1)[0] + '.png'
25
  result = subprocess.run(["avif-decode/target/release/avif_decode", "-f", avif_path, png_path], capture_output=True, text=True)
26
  if result.returncode == 0:
27
  output_paths.append(png_path)
 
30
  return output_paths
31
 
32
 
33
+ def zip_files(file_paths):
34
+ zip_path = "/tmp/converted_avif.zip"
35
+ with zipfile.ZipFile(zip_path, 'w') as zipf:
36
+ for file_path in file_paths:
37
+ if os.path.isfile(file_path):
38
+ zipf.write(file_path, os.path.basename(file_path))
39
+ return zip_path
40
+
41
+
42
  css = """
43
  #col-container {
44
  margin: 0 auto;
45
  max-width: 520px;
46
  }
47
  """
48
+ with gr.Blocks(css=css) as demo:
49
  with gr.Column(elem_id="col-container"):
50
  gr.Markdown("""
51
  # AVIF to PNG Converter
52
  Upload your AVIF files and get them converted to PNG.
53
+ """)
54
+ with gr.Row():
55
  avif_file = gr.File(
56
  label="Upload AVIF File",
57
  file_types=[".avif"],
58
  file_count="multiple"
59
+ )
60
+ run_button = gr.Button("Convert", scale=0)
61
  result = gr.Gallery(label="Result")
62
+ download_button = gr.Button("Download All as ZIP")
63
+ error_message = gr.Textbox(label="Message", interactive=False, visible=False)
64
+ converted_files = []
65
+
66
+ def handle_conversion(avif_files):
67
+ global converted_files
68
+ converted_files = convert_avif_to_png(avif_files)
69
+ return converted_files
70
+
71
+ def handle_download():
72
+ if not converted_files:
73
+ return None, "No files to download. Please upload and convert AVIF files first."
74
+ return zip_files(converted_files), ""
75
+
76
  run_button.click(
77
+ fn=handle_conversion,
78
  inputs=[avif_file],
79
  outputs=[result]
80
  )
81
+ download_button.click(
82
+ fn=handle_download,
83
+ inputs=[],
84
+ outputs=[gr.File(), error_message]
85
+ )
86
  demo.queue().launch()