Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -30,13 +30,26 @@ 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 |
|
34 |
-
# Ensure cached files exist
|
35 |
if os.path.exists(cached_image_path) and os.path.exists(cached_audio_path):
|
36 |
return cached_image_path, cached_audio_path
|
|
|
|
|
37 |
else:
|
38 |
raise FileNotFoundError(f"Cached outputs not found for example {example_index}")
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
# Function to handle the example click, it now accepts arbitrary arguments
|
41 |
def on_example_click(*args, **kwargs):
|
42 |
return load_cached_example_outputs(1) # Always load example 1 for now
|
@@ -109,12 +122,19 @@ with gr.Blocks(css=css) as demo:
|
|
109 |
),
|
110 |
)
|
111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
gr.Examples(
|
113 |
-
examples=
|
114 |
-
inputs=[image
|
115 |
-
outputs=[
|
116 |
-
cache_examples=True,
|
117 |
-
fn=
|
118 |
)
|
119 |
|
120 |
gr.on(
|
|
|
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
|
|
|
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(
|