Adityadn commited on
Commit
02c7e8f
·
verified ·
1 Parent(s): cf6d608

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -16
app.py CHANGED
@@ -34,32 +34,30 @@ def clean_cache():
34
  if os.path.isfile(file_path) and now - os.path.getmtime(file_path) > 900: # 15 minutes
35
  os.remove(file_path)
36
 
37
- def convert_image_ffmpeg(image, target_format):
38
  try:
39
- # Create a temporary directory for the uploaded file
40
- temp_dir = tempfile.mkdtemp()
 
41
 
42
- # Sanitize the filename and save the uploaded video to the temp directory
43
- base_name = os.path.splitext(os.path.basename(image.name))[0]
44
- sanitized_base_name = sanitize_filename(base_name)
45
- image_path = os.path.join(temp_dir, f"{sanitized_base_name}.png") # Saving as mp4 by default for now
46
 
47
- # Nama file keluaran dengan awalan yang diinginkan
48
- output_file = f"flowly_ai_image_converter_{sanitized_base_name}.{target_format.lower()}"
49
 
50
- with open(image_path, "wb") as f:
51
- f.write(image.getbuffer()) # Save the uploaded video to a local file
52
-
53
  # Gunakan ffmpeg untuk konversi
54
- ffmpeg.input(image_path).output(output_file, format=target_format).overwrite_output().run(capture_stdout=True, capture_stderr=True)
 
 
55
 
56
- return output_file
57
  except Exception as e:
58
  return f"Error: {e}"
59
-
60
  # Antarmuka Gradio
61
  interface = gr.Interface(
62
- fn=convert_image_ffmpeg,
63
  inputs=[
64
  gr.Image(label="Upload Image", type="filepath", height=512), # Input gambar
65
  gr.Dropdown(label="Select Target Format", choices=supported_formats) # Pilih format
 
34
  if os.path.isfile(file_path) and now - os.path.getmtime(file_path) > 900: # 15 minutes
35
  os.remove(file_path)
36
 
37
+ def convert_image(image_path, target_format):
38
  try:
39
+ # Pastikan target format valid
40
+ if target_format.lower() not in supported_formats:
41
+ return "Error: Unsupported target format."
42
 
43
+ # Sanitize file path
44
+ sanitized_base_name = sanitize_filename(os.path.splitext(os.path.basename(image_path))[0])
 
 
45
 
46
+ # Nama file keluaran
47
+ output_file = os.path.join(CACHE_DIR, f"flowly_ai_image_converter_{sanitized_base_name}.{target_format.lower()}")
48
 
 
 
 
49
  # Gunakan ffmpeg untuk konversi
50
+ ffmpeg.input(image_path).output(output_file, format=target_format).overwrite_output().run(
51
+ capture_stdout=True, capture_stderr=True
52
+ )
53
 
54
+ return output_file # Return path ke file hasil
55
  except Exception as e:
56
  return f"Error: {e}"
57
+
58
  # Antarmuka Gradio
59
  interface = gr.Interface(
60
+ fn=convert_image,
61
  inputs=[
62
  gr.Image(label="Upload Image", type="filepath", height=512), # Input gambar
63
  gr.Dropdown(label="Select Target Format", choices=supported_formats) # Pilih format