Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,126 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
if
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import fal_client
|
| 3 |
+
import os
|
| 4 |
+
from typing import Optional
|
| 5 |
+
|
| 6 |
+
# It is recommended to create this as a Secret on your Hugging Face Space
|
| 7 |
+
# For example: FAL_KEY = "fal_key_..."
|
| 8 |
+
FAL_KEY = os.getenv("FAL_KEY", "")
|
| 9 |
+
|
| 10 |
+
# Set the key for the fal_client
|
| 11 |
+
if FAL_KEY:
|
| 12 |
+
fal_client.api_key = FAL_KEY
|
| 13 |
+
|
| 14 |
+
def get_fal_key():
|
| 15 |
+
"""Checks for the FAL_KEY and raises a Gradio error if it's not set."""
|
| 16 |
+
if not FAL_KEY:
|
| 17 |
+
raise gr.Error("FAL_KEY is not set. Please add it to your Hugging Face Space secrets.")
|
| 18 |
+
|
| 19 |
+
def generate_image(prompt: str, image: Optional[str] = None) -> str:
|
| 20 |
+
"""
|
| 21 |
+
Generates or edits an image.
|
| 22 |
+
- If an image filepath is provided, it performs image-to-image editing.
|
| 23 |
+
- Otherwise, it performs text-to-image generation.
|
| 24 |
+
"""
|
| 25 |
+
get_fal_key()
|
| 26 |
+
|
| 27 |
+
if image:
|
| 28 |
+
# Image-to-Image: Upload the local file to get a public URL
|
| 29 |
+
image_url = fal_client.upload_file(image)
|
| 30 |
+
result = fal_client.run(
|
| 31 |
+
"fal-ai/nano-banana/edit",
|
| 32 |
+
arguments={
|
| 33 |
+
"prompt": prompt,
|
| 34 |
+
"image_url": image_url,
|
| 35 |
+
},
|
| 36 |
+
)
|
| 37 |
+
else:
|
| 38 |
+
# Text-to-Image
|
| 39 |
+
result = fal_client.run(
|
| 40 |
+
"fal-ai/nano-banana",
|
| 41 |
+
arguments={
|
| 42 |
+
"prompt": prompt,
|
| 43 |
+
},
|
| 44 |
+
)
|
| 45 |
+
return result["images"][0]["url"]
|
| 46 |
+
|
| 47 |
+
def multi_image_edit(prompt: str, images: list[str]) -> list[str]:
|
| 48 |
+
"""Edits multiple images based on a text prompt."""
|
| 49 |
+
get_fal_key()
|
| 50 |
+
|
| 51 |
+
if not images:
|
| 52 |
+
raise gr.Error("Please upload at least one image.")
|
| 53 |
+
|
| 54 |
+
output_images = []
|
| 55 |
+
for image_path in images:
|
| 56 |
+
# Upload each image and get its URL
|
| 57 |
+
image_url = fal_client.upload_file(image_path)
|
| 58 |
+
result = fal_client.run(
|
| 59 |
+
"fal-ai/nano-banana/edit",
|
| 60 |
+
arguments={
|
| 61 |
+
"prompt": prompt,
|
| 62 |
+
"image_url": image_url,
|
| 63 |
+
},
|
| 64 |
+
)
|
| 65 |
+
output_images.append(result["images"][0]["url"])
|
| 66 |
+
|
| 67 |
+
return output_images
|
| 68 |
+
|
| 69 |
+
# --- Gradio App UI ---
|
| 70 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 71 |
+
gr.Markdown("# Nano Banana Image Generation")
|
| 72 |
+
gr.Markdown("Generate or edit images with FAL. **Sign in with Hugging Face to begin.**")
|
| 73 |
+
|
| 74 |
+
login_button = gr.LoginButton()
|
| 75 |
+
|
| 76 |
+
# This entire Column will be hidden until the user logs in
|
| 77 |
+
main_interface = gr.Column(visible=False)
|
| 78 |
+
|
| 79 |
+
with main_interface:
|
| 80 |
+
gr.Markdown("## Welcome! You are logged in.")
|
| 81 |
+
with gr.Tabs():
|
| 82 |
+
with gr.TabItem("Generate"):
|
| 83 |
+
with gr.Row():
|
| 84 |
+
with gr.Column(scale=1):
|
| 85 |
+
prompt_input = gr.Textbox(label="Prompt", placeholder="A delicious looking pizza")
|
| 86 |
+
image_input = gr.Image(type="filepath", label="Input Image (Optional)")
|
| 87 |
+
generate_button = gr.Button("Generate", variant="primary")
|
| 88 |
+
with gr.Column(scale=1):
|
| 89 |
+
generate_output = gr.Image(label="Output")
|
| 90 |
+
|
| 91 |
+
generate_button.click(
|
| 92 |
+
generate_image,
|
| 93 |
+
inputs=[prompt_input, image_input],
|
| 94 |
+
outputs=[generate_output],
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
with gr.TabItem("Multi-Image Edit"):
|
| 98 |
+
with gr.Row():
|
| 99 |
+
with gr.Column(scale=1):
|
| 100 |
+
multi_prompt_input = gr.Textbox(label="Prompt", placeholder="Make it black and white")
|
| 101 |
+
multi_image_input = gr.Gallery(label="Input Images", file_types=["image"])
|
| 102 |
+
multi_edit_button = gr.Button("Edit Images", variant="primary")
|
| 103 |
+
with gr.Column(scale=1):
|
| 104 |
+
multi_image_output = gr.Gallery(label="Output Images")
|
| 105 |
+
|
| 106 |
+
multi_edit_button.click(
|
| 107 |
+
multi_image_edit,
|
| 108 |
+
inputs=[multi_prompt_input, multi_image_input],
|
| 109 |
+
outputs=[multi_image_output],
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
def show_app_on_login(profile: Optional[gr.OAuthProfile] = None):
|
| 113 |
+
"""
|
| 114 |
+
Controls the visibility of the main UI.
|
| 115 |
+
Gradio automatically injects `gr.OAuthProfile` if the user is logged in.
|
| 116 |
+
"""
|
| 117 |
+
if profile:
|
| 118 |
+
return gr.update(visible=True)
|
| 119 |
+
return gr.update(visible=False)
|
| 120 |
+
|
| 121 |
+
# When the app loads or the user logs in/out, this function will be called.
|
| 122 |
+
# It receives the user's profile (or None) and updates the UI visibility.
|
| 123 |
+
demo.load(show_app_on_login, inputs=None, outputs=main_interface)
|
| 124 |
+
|
| 125 |
+
if __name__ == "__main__":
|
| 126 |
+
demo.launch()
|