jadechoghari commited on
Commit
75c374e
·
verified ·
1 Parent(s): 9309c87

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -32
app.py CHANGED
@@ -26,33 +26,26 @@ model.setup()
26
  CACHE_DIR = "gradio_cached_examples"
27
 
28
  #for local cache
29
- def load_cached_example_outputs(example_index: int) -> Tuple[str, str]:
30
- cached_dir = os.path.join(CACHE_DIR, str(example_index)) # Use the example index to find the directory
31
  cached_image_path = os.path.join(cached_dir, "processed_image.png")
32
  cached_audio_path = os.path.join(cached_dir, "audio.wav")
33
- cached_video_path = os.path.join(cached_dir, "processed_video.mp4") # Support video output too
34
-
35
- # Ensure cached files exist (either image or video)
36
- if os.path.exists(cached_image_path) and os.path.exists(cached_audio_path):
37
- return cached_image_path, cached_audio_path
38
- elif os.path.exists(cached_video_path) and os.path.exists(cached_audio_path):
39
- return cached_video_path, cached_audio_path
40
  else:
41
  raise FileNotFoundError(f"Cached outputs not found for example {example_index}")
42
 
 
 
 
43
 
44
- # detect whether the input is an image or a video
45
- def determine_input_type(filepath: str):
46
- if filepath.endswith((".png", ".jpg", ".jpeg")):
47
- return gr.Image
48
- elif filepath.endswith(".mp4"):
49
- return gr.Video
50
- else:
51
- raise ValueError("Unsupported file type")
52
-
53
- # Function to handle the example click, it now accepts arbitrary arguments
54
- def on_example_click(*args, **kwargs):
55
- return load_cached_example_outputs(1) # Always load example 1 for now
56
 
57
 
58
  @spaces.GPU(duration=280)
@@ -121,20 +114,30 @@ with gr.Blocks(css=css) as demo:
121
  show_controls=True,
122
  ),
123
  )
124
-
125
- def load_example_by_input_type(file_input, *args, example_index):
126
- input_type = determine_input_type(file_input)
127
- if input_type == gr.Video:
128
- return gr.Video(label="Video Example"), on_example_click(example_index=example_index)
 
 
 
 
 
 
 
 
 
 
129
  else:
130
- return gr.Image(label="Image Example"), on_example_click(example_index=example_index)
131
 
 
132
  gr.Examples(
133
- examples=example_inputs,
134
- inputs=[gr.File(label="Select image or video")],
135
- outputs=[processed_media, generated_audio],
136
- cache_examples=True,
137
- fn=lambda *args, **kwargs: load_example_by_input_type(args[0], example_index=args[0][0]), # Dynamically detect and process
138
  )
139
 
140
  gr.on(
 
26
  CACHE_DIR = "gradio_cached_examples"
27
 
28
  #for local cache
29
+ def load_cached_example_outputs(example_index: int) -> Tuple[Union[str, None], str]:
30
+ cached_dir = os.path.join(CACHE_DIR, str(example_index))
31
  cached_image_path = os.path.join(cached_dir, "processed_image.png")
32
  cached_audio_path = os.path.join(cached_dir, "audio.wav")
33
+ cached_video_path = os.path.join(cached_dir, "processed_video.mp4")
34
+
35
+ if os.path.exists(cached_video_path) and os.path.exists(cached_audio_path):
36
+ return cached_video_path, cached_audio_path # Return video output
37
+ elif os.path.exists(cached_image_path) and os.path.exists(cached_audio_path):
38
+ return cached_image_path, cached_audio_path # Return image output
 
39
  else:
40
  raise FileNotFoundError(f"Cached outputs not found for example {example_index}")
41
 
42
+ # to handle the example click
43
+ def on_example_click(example_index: int) -> Tuple[Union[str, None], str]:
44
+ return load_cached_example_outputs(example_index)
45
 
46
+ # # to handle the example click, it now accepts arbitrary arguments
47
+ # def on_example_click(*args, **kwargs):
48
+ # return load_cached_example_outputs(1) # Always load example 1 for now
 
 
 
 
 
 
 
 
 
49
 
50
 
51
  @spaces.GPU(duration=280)
 
114
  show_controls=True,
115
  ),
116
  )
117
+
118
+ # Example inputs, the last two are videos
119
+ examples = [
120
+ ["examples/1.png", 3, "Example 1", 500],
121
+ ["examples/2.png", 3, "Example 2", 500],
122
+ ["examples/3.png", 3, "Example 3", 500],
123
+ ["examples/4.mp4", 3, "Example 4", 500],
124
+ ["examples/5.mp4", 3, "Example 5", 500]
125
+ ]
126
+
127
+ # Function to dynamically switch output visibility based on file type
128
+ def handle_example_click(example):
129
+ output, audio = on_example_click(int(example[0][-5])) # Example index based on file path
130
+ if output.endswith('.mp4'):
131
+ return None, output, audio # Show video and hide image
132
  else:
133
+ return output, None, audio # Show image and hide video
134
 
135
+ # Add the examples to Gradio
136
  gr.Examples(
137
+ examples=examples,
138
+ inputs=[gr.File(label="Select image or video")], # Use gr.File to support both image and video inputs for examples
139
+ outputs=[processed_image, processed_video, generated_audio],
140
+ fn=handle_example_click
 
141
  )
142
 
143
  gr.on(