Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,8 +30,8 @@ def verify_pro_status(token: Optional[gr.OAuthToken]) -> bool:
|
|
| 30 |
|
| 31 |
# --- Backend Generation Functions ---
|
| 32 |
|
| 33 |
-
def run_single_image_logic(prompt: str, image: Optional[str] = None) ->
|
| 34 |
-
"""Handles text-to-image or single image-to-image and returns a
|
| 35 |
get_fal_key()
|
| 36 |
if image:
|
| 37 |
image_url = fal_client.upload_file(image)
|
|
@@ -43,26 +43,28 @@ def run_single_image_logic(prompt: str, image: Optional[str] = None) -> List[str
|
|
| 43 |
result = fal_client.run(
|
| 44 |
"fal-ai/nano-banana", arguments={"prompt": prompt}
|
| 45 |
)
|
| 46 |
-
return
|
| 47 |
|
| 48 |
-
def run_multi_image_logic(prompt: str, images: List[str]) ->
|
| 49 |
-
"""
|
|
|
|
|
|
|
| 50 |
get_fal_key()
|
| 51 |
if not images:
|
| 52 |
raise gr.Error("Please upload at least one image in the 'Multiple Images' tab.")
|
| 53 |
|
| 54 |
-
|
| 55 |
for image_path in images:
|
| 56 |
image_url = fal_client.upload_file(image_path)
|
| 57 |
result = fal_client.run(
|
| 58 |
"fal-ai/nano-banana/edit",
|
| 59 |
arguments={"prompt": prompt, "image_url": image_url},
|
| 60 |
)
|
| 61 |
-
|
| 62 |
-
return
|
| 63 |
|
| 64 |
# --- Gradio App UI ---
|
| 65 |
-
with gr.Blocks(theme=gr.themes.
|
| 66 |
gr.Markdown("# Nano Banana Image Generation")
|
| 67 |
gr.Markdown("Generate or edit images with FAL. **Sign in with Hugging Face to begin.**")
|
| 68 |
|
|
@@ -94,9 +96,8 @@ with gr.Blocks(theme=gr.themes.Citrus()) as demo:
|
|
| 94 |
|
| 95 |
# RIGHT COLUMN: Outputs
|
| 96 |
with gr.Column(scale=1):
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
use_image_button = gr.Button("♻️ Use Selected Image for Next Edit")
|
| 100 |
|
| 101 |
# --- Event Handlers ---
|
| 102 |
|
|
@@ -105,8 +106,8 @@ with gr.Blocks(theme=gr.themes.Citrus()) as demo:
|
|
| 105 |
single_image: Optional[str],
|
| 106 |
multi_images: Optional[List[str]],
|
| 107 |
active_tab: str,
|
| 108 |
-
oauth_token: Optional[gr.OAuthToken] = None,
|
| 109 |
-
) ->
|
| 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:
|
|
@@ -119,26 +120,16 @@ with gr.Blocks(theme=gr.themes.Citrus()) as demo:
|
|
| 119 |
|
| 120 |
generate_button.click(
|
| 121 |
unified_generator,
|
| 122 |
-
# CORRECTED: login_button is removed. Gradio will inject the token via the type hint.
|
| 123 |
inputs=[prompt_input, image_input, gallery_input, active_tab_state],
|
| 124 |
-
outputs=[
|
| 125 |
)
|
| 126 |
|
| 127 |
-
#
|
| 128 |
-
|
| 129 |
-
return evt.value['image']['path']
|
| 130 |
-
|
| 131 |
-
def reuse_output_image(selected_image_path):
|
| 132 |
-
if not selected_image_path:
|
| 133 |
-
gr.Warning("Please select an image from the output gallery first!")
|
| 134 |
-
return None, gr.update()
|
| 135 |
-
return selected_image_path, "single"
|
| 136 |
-
|
| 137 |
-
output_gallery.select(store_selected_image, None, selected_output_image_state)
|
| 138 |
use_image_button.click(
|
| 139 |
-
|
| 140 |
-
inputs=[
|
| 141 |
-
outputs=[image_input
|
| 142 |
)
|
| 143 |
|
| 144 |
# --- Access Control Logic ---
|
|
@@ -157,7 +148,7 @@ with gr.Blocks(theme=gr.themes.Citrus()) as demo:
|
|
| 157 |
"To unlock this and many other benefits, please consider upgrading your account.\n\n"
|
| 158 |
"### [**Become a PRO Member Today!**](https://huggingface.co/pro)"
|
| 159 |
)
|
| 160 |
-
return gr.update(visible=False), gr.update(visible=
|
| 161 |
|
| 162 |
demo.load(control_access, inputs=None, outputs=[main_interface, pro_message])
|
| 163 |
|
|
|
|
| 30 |
|
| 31 |
# --- Backend Generation Functions ---
|
| 32 |
|
| 33 |
+
def run_single_image_logic(prompt: str, image: Optional[str] = None) -> str:
|
| 34 |
+
"""Handles text-to-image or single image-to-image and returns a single URL string."""
|
| 35 |
get_fal_key()
|
| 36 |
if image:
|
| 37 |
image_url = fal_client.upload_file(image)
|
|
|
|
| 43 |
result = fal_client.run(
|
| 44 |
"fal-ai/nano-banana", arguments={"prompt": prompt}
|
| 45 |
)
|
| 46 |
+
return result["images"][0]["url"]
|
| 47 |
|
| 48 |
+
def run_multi_image_logic(prompt: str, images: List[str]) -> str:
|
| 49 |
+
"""
|
| 50 |
+
Handles multi-image editing and returns the URL of the LAST generated image.
|
| 51 |
+
"""
|
| 52 |
get_fal_key()
|
| 53 |
if not images:
|
| 54 |
raise gr.Error("Please upload at least one image in the 'Multiple Images' tab.")
|
| 55 |
|
| 56 |
+
last_image_url = ""
|
| 57 |
for image_path in images:
|
| 58 |
image_url = fal_client.upload_file(image_path)
|
| 59 |
result = fal_client.run(
|
| 60 |
"fal-ai/nano-banana/edit",
|
| 61 |
arguments={"prompt": prompt, "image_url": image_url},
|
| 62 |
)
|
| 63 |
+
last_image_url = result["images"][0]["url"]
|
| 64 |
+
return last_image_url
|
| 65 |
|
| 66 |
# --- Gradio App UI ---
|
| 67 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 68 |
gr.Markdown("# Nano Banana Image Generation")
|
| 69 |
gr.Markdown("Generate or edit images with FAL. **Sign in with Hugging Face to begin.**")
|
| 70 |
|
|
|
|
| 96 |
|
| 97 |
# RIGHT COLUMN: Outputs
|
| 98 |
with gr.Column(scale=1):
|
| 99 |
+
output_image = gr.Image(label="Output")
|
| 100 |
+
use_image_button = gr.Button("♻️ Use this Image for Next Edit")
|
|
|
|
| 101 |
|
| 102 |
# --- Event Handlers ---
|
| 103 |
|
|
|
|
| 106 |
single_image: Optional[str],
|
| 107 |
multi_images: Optional[List[str]],
|
| 108 |
active_tab: str,
|
| 109 |
+
oauth_token: Optional[gr.OAuthToken] = None,
|
| 110 |
+
) -> str:
|
| 111 |
if not verify_pro_status(oauth_token):
|
| 112 |
raise gr.Error("Access Denied. This service is for PRO users only.")
|
| 113 |
if active_tab == "multiple" and multi_images:
|
|
|
|
| 120 |
|
| 121 |
generate_button.click(
|
| 122 |
unified_generator,
|
|
|
|
| 123 |
inputs=[prompt_input, image_input, gallery_input, active_tab_state],
|
| 124 |
+
outputs=[output_image],
|
| 125 |
)
|
| 126 |
|
| 127 |
+
# Corrected handler for the continuous editing loop.
|
| 128 |
+
# It takes the output image and directly returns it to be used as the input.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
use_image_button.click(
|
| 130 |
+
lambda img: img, # A simple function that returns its input
|
| 131 |
+
inputs=[output_image],
|
| 132 |
+
outputs=[image_input]
|
| 133 |
)
|
| 134 |
|
| 135 |
# --- Access Control Logic ---
|
|
|
|
| 148 |
"To unlock this and many other benefits, please consider upgrading your account.\n\n"
|
| 149 |
"### [**Become a PRO Member Today!**](https://huggingface.co/pro)"
|
| 150 |
)
|
| 151 |
+
return gr.update(visible=False), gr.update(visible=True, value=message)
|
| 152 |
|
| 153 |
demo.load(control_access, inputs=None, outputs=[main_interface, pro_message])
|
| 154 |
|