Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -34,14 +34,12 @@ def run_single_image_logic(prompt: str, image: Optional[str] = None) -> List[str
|
|
| 34 |
"""Handles text-to-image or single image-to-image and returns a list."""
|
| 35 |
get_fal_key()
|
| 36 |
if image:
|
| 37 |
-
# Single Image-to-Image
|
| 38 |
image_url = fal_client.upload_file(image)
|
| 39 |
result = fal_client.run(
|
| 40 |
"fal-ai/nano-banana/edit",
|
| 41 |
arguments={"prompt": prompt, "image_url": image_url},
|
| 42 |
)
|
| 43 |
else:
|
| 44 |
-
# Text-to-Image
|
| 45 |
result = fal_client.run(
|
| 46 |
"fal-ai/nano-banana", arguments={"prompt": prompt}
|
| 47 |
)
|
|
@@ -81,12 +79,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 81 |
label="Prompt",
|
| 82 |
placeholder="A delicious looking pizza"
|
| 83 |
)
|
| 84 |
-
|
| 85 |
-
# Use gr.State to track the active tab
|
| 86 |
active_tab_state = gr.State(value="single")
|
| 87 |
-
|
| 88 |
with gr.Tabs() as tabs:
|
| 89 |
-
# When a tab is selected, its 'select' event updates the active_tab_state
|
| 90 |
with gr.TabItem("Single Image", id="single") as single_tab:
|
| 91 |
image_input = gr.Image(
|
| 92 |
type="filepath",
|
|
@@ -96,16 +90,16 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 96 |
gallery_input = gr.Gallery(
|
| 97 |
label="Input Images", file_types=["image"]
|
| 98 |
)
|
| 99 |
-
|
| 100 |
generate_button = gr.Button("Generate", variant="primary")
|
| 101 |
|
| 102 |
# RIGHT COLUMN: Outputs
|
| 103 |
with gr.Column(scale=1):
|
| 104 |
output_gallery = gr.Gallery(label="Output")
|
|
|
|
|
|
|
| 105 |
|
| 106 |
# --- Event Handlers ---
|
| 107 |
|
| 108 |
-
# A single, unified generator function
|
| 109 |
def unified_generator(
|
| 110 |
prompt: str,
|
| 111 |
single_image: Optional[str],
|
|
@@ -113,40 +107,53 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 113 |
active_tab: str,
|
| 114 |
oauth_token: Optional[gr.OAuthToken] = None,
|
| 115 |
) -> List[str]:
|
| 116 |
-
"""
|
| 117 |
-
A single handler that verifies PRO status and routes to the correct function.
|
| 118 |
-
The `active_tab` state is the primary source of truth for user intent.
|
| 119 |
-
"""
|
| 120 |
-
# Server-side security check before any inference
|
| 121 |
if not verify_pro_status(oauth_token):
|
| 122 |
raise gr.Error("Access Denied. This service is for PRO users only.")
|
| 123 |
-
|
| 124 |
-
# Logic based on the active tab and whether inputs exist
|
| 125 |
if active_tab == "multiple" and multi_images:
|
| 126 |
return run_multi_image_logic(prompt, multi_images)
|
| 127 |
-
else:
|
| 128 |
return run_single_image_logic(prompt, single_image)
|
| 129 |
|
| 130 |
-
# Cleanly update the state when a tab is selected
|
| 131 |
single_tab.select(lambda: "single", None, active_tab_state)
|
| 132 |
multi_tab.select(lambda: "multiple", None, active_tab_state)
|
| 133 |
-
|
| 134 |
-
# Wire the button to the unified generator
|
| 135 |
generate_button.click(
|
| 136 |
unified_generator,
|
| 137 |
inputs=[prompt_input, image_input, gallery_input, active_tab_state, login_button],
|
| 138 |
outputs=[output_gallery],
|
| 139 |
)
|
| 140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
# --- Access Control Logic ---
|
| 142 |
def control_access(
|
| 143 |
profile: Optional[gr.OAuthProfile] = None,
|
| 144 |
oauth_token: Optional[gr.OAuthToken] = None
|
| 145 |
):
|
| 146 |
-
"""Controls UI visibility based on user's PRO status."""
|
| 147 |
if not profile:
|
| 148 |
return gr.update(visible=False), gr.update(visible=False)
|
| 149 |
-
|
| 150 |
if verify_pro_status(oauth_token):
|
| 151 |
return gr.update(visible=True), gr.update(visible=False)
|
| 152 |
else:
|
|
|
|
| 34 |
"""Handles text-to-image or single image-to-image and returns a list."""
|
| 35 |
get_fal_key()
|
| 36 |
if image:
|
|
|
|
| 37 |
image_url = fal_client.upload_file(image)
|
| 38 |
result = fal_client.run(
|
| 39 |
"fal-ai/nano-banana/edit",
|
| 40 |
arguments={"prompt": prompt, "image_url": image_url},
|
| 41 |
)
|
| 42 |
else:
|
|
|
|
| 43 |
result = fal_client.run(
|
| 44 |
"fal-ai/nano-banana", arguments={"prompt": prompt}
|
| 45 |
)
|
|
|
|
| 79 |
label="Prompt",
|
| 80 |
placeholder="A delicious looking pizza"
|
| 81 |
)
|
|
|
|
|
|
|
| 82 |
active_tab_state = gr.State(value="single")
|
|
|
|
| 83 |
with gr.Tabs() as tabs:
|
|
|
|
| 84 |
with gr.TabItem("Single Image", id="single") as single_tab:
|
| 85 |
image_input = gr.Image(
|
| 86 |
type="filepath",
|
|
|
|
| 90 |
gallery_input = gr.Gallery(
|
| 91 |
label="Input Images", file_types=["image"]
|
| 92 |
)
|
|
|
|
| 93 |
generate_button = gr.Button("Generate", variant="primary")
|
| 94 |
|
| 95 |
# RIGHT COLUMN: Outputs
|
| 96 |
with gr.Column(scale=1):
|
| 97 |
output_gallery = gr.Gallery(label="Output")
|
| 98 |
+
selected_output_image_state = gr.State()
|
| 99 |
+
use_image_button = gr.Button("♻️ Use Selected Image for Next Edit")
|
| 100 |
|
| 101 |
# --- Event Handlers ---
|
| 102 |
|
|
|
|
| 103 |
def unified_generator(
|
| 104 |
prompt: str,
|
| 105 |
single_image: Optional[str],
|
|
|
|
| 107 |
active_tab: str,
|
| 108 |
oauth_token: Optional[gr.OAuthToken] = None,
|
| 109 |
) -> List[str]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
if not verify_pro_status(oauth_token):
|
| 111 |
raise gr.Error("Access Denied. This service is for PRO users only.")
|
|
|
|
|
|
|
| 112 |
if active_tab == "multiple" and multi_images:
|
| 113 |
return run_multi_image_logic(prompt, multi_images)
|
| 114 |
+
else:
|
| 115 |
return run_single_image_logic(prompt, single_image)
|
| 116 |
|
|
|
|
| 117 |
single_tab.select(lambda: "single", None, active_tab_state)
|
| 118 |
multi_tab.select(lambda: "multiple", None, active_tab_state)
|
| 119 |
+
|
|
|
|
| 120 |
generate_button.click(
|
| 121 |
unified_generator,
|
| 122 |
inputs=[prompt_input, image_input, gallery_input, active_tab_state, login_button],
|
| 123 |
outputs=[output_gallery],
|
| 124 |
)
|
| 125 |
|
| 126 |
+
# New handlers for the continuous editing loop
|
| 127 |
+
def store_selected_image(evt: gr.SelectData):
|
| 128 |
+
"""When an image is selected in the output gallery, store its path in state."""
|
| 129 |
+
return evt.value['image']
|
| 130 |
+
|
| 131 |
+
def reuse_output_image(selected_image_path):
|
| 132 |
+
"""
|
| 133 |
+
Takes the path from state and sends it to the single image input.
|
| 134 |
+
Also forces the UI to switch to the "Single Image" tab.
|
| 135 |
+
"""
|
| 136 |
+
if not selected_image_path:
|
| 137 |
+
gr.Warning("Please select an image from the output gallery first!")
|
| 138 |
+
return None, gr.update()
|
| 139 |
+
# Output 1: The image path for the gr.Image component
|
| 140 |
+
# Output 2: The ID of the tab to select for the gr.Tabs component
|
| 141 |
+
return selected_image_path, "single"
|
| 142 |
+
|
| 143 |
+
output_gallery.select(store_selected_image, None, selected_output_image_state)
|
| 144 |
+
use_image_button.click(
|
| 145 |
+
reuse_output_image,
|
| 146 |
+
inputs=[selected_output_image_state],
|
| 147 |
+
outputs=[image_input, tabs]
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
# --- Access Control Logic ---
|
| 151 |
def control_access(
|
| 152 |
profile: Optional[gr.OAuthProfile] = None,
|
| 153 |
oauth_token: Optional[gr.OAuthToken] = None
|
| 154 |
):
|
|
|
|
| 155 |
if not profile:
|
| 156 |
return gr.update(visible=False), gr.update(visible=False)
|
|
|
|
| 157 |
if verify_pro_status(oauth_token):
|
| 158 |
return gr.update(visible=True), gr.update(visible=False)
|
| 159 |
else:
|