jadechoghari commited on
Commit
1add98c
1 Parent(s): b7fdbe0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -23
app.py CHANGED
@@ -27,27 +27,19 @@ CACHE_DIR = "gradio_cached_examples"
27
 
28
  #for local cache
29
  def load_cached_example_outputs(example_index: int) -> Tuple[str, str]:
30
- # Search for any folder that contains the example index in its name
31
- for root, dirs, files in os.walk(CACHE_DIR):
32
- for dir_name in dirs:
33
- if dir_name.endswith(str(example_index)): # Search for folder ending with the example index
34
- cached_dir = os.path.join(root, dir_name)
35
- cached_image_path = os.path.join(cached_dir, "processed_image.png")
36
- cached_audio_path = os.path.join(cached_dir, "audio.wav")
37
 
38
- # Debugging: Print the paths being checked
39
- print(f"Looking for cached image at: {cached_image_path}")
40
- print(f"Looking for cached audio at: {cached_audio_path}")
 
 
41
 
42
- # Check if the cached files exist
43
- if os.path.exists(cached_image_path) and os.path.exists(cached_audio_path):
44
- return cached_image_path, cached_audio_path
45
-
46
- raise FileNotFoundError(f"Cached outputs not found for example {example_index}")
47
-
48
- # Function to handle the example click, it now accepts arbitrary arguments and returns the appropriate cached outputs
49
- def on_example_click(*args, example_index=1, **kwargs):
50
- return load_cached_example_outputs(example_index)
51
 
52
  # # to handle the example click, it now accepts arbitrary arguments
53
  # def on_example_click(*args, **kwargs):
@@ -142,13 +134,17 @@ with gr.Blocks(css=css) as demo:
142
  example_index = example_map.get(example_image, 1) # Default to 1 if not found
143
  return load_cached_example_outputs(example_index)
144
 
145
- # Add the examples to Gradio
146
  gr.Examples(
147
- examples=example_inputs,
 
 
 
 
148
  inputs=[image, num_audios, prompt, steps],
149
  outputs=[processed_image, generated_audio],
150
- cache_examples=True,
151
- fn=on_example_click
 
152
  )
153
 
154
  gr.on(
 
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
 
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, based on index
41
+ def on_example_click(index: int, *args, **kwargs):
42
+ return load_cached_example_outputs(index)
 
 
 
 
 
 
43
 
44
  # # to handle the example click, it now accepts arbitrary arguments
45
  # def on_example_click(*args, **kwargs):
 
134
  example_index = example_map.get(example_image, 1) # Default to 1 if not found
135
  return load_cached_example_outputs(example_index)
136
 
 
137
  gr.Examples(
138
+ examples=[
139
+ ["examples/1.png", 3, "A scenic mountain view", 500], # Example 1
140
+ ["examples/2.png", 2, "A forest with birds", 500], # Example 2
141
+ ["examples/3.png", 1, "A crowded city", 500] # Example 3
142
+ ],
143
  inputs=[image, num_audios, prompt, steps],
144
  outputs=[processed_image, generated_audio],
145
+ cache_examples=True, # Cache examples to avoid running the model
146
+ fn=update_examples, # Call function that loads example based on index
147
+ inputs=[index_selector] # Use the dropdown to select the example index
148
  )
149
 
150
  gr.on(