Spaces:
Running
Running
| import gradio as gr | |
| import fal_client | |
| import os | |
| from huggingface_hub import HfApi, whoami | |
| from gradio.processing_utils import file_to_temp_file | |
| try: | |
| FAL_KEY = os.getenv("FAL_KEY") | |
| except AttributeError: | |
| FAL_KEY = "" | |
| def get_fal_key(): | |
| if not FAL_KEY: | |
| raise gr.Error("FAL_KEY is not set. Please add it to your secrets.") | |
| return FAL_KEY | |
| def generate_image(prompt, image=None): | |
| """ | |
| Generates an image from a text prompt or edits an existing image. | |
| If an image is provided, it performs image-to-image generation. | |
| Otherwise, it performs text-to-image generation. | |
| """ | |
| get_fal_key() | |
| if image: | |
| # Image-to-Image | |
| temp_file_path = file_to_temp_file(image, dir=".") | |
| result = fal_client.run( | |
| "fal-ai/nano-banana/edit", | |
| arguments={ | |
| "prompt": prompt, | |
| "image_url": temp_file_path, | |
| }, | |
| ) | |
| else: | |
| # Text-to-Image | |
| result = fal_client.run( | |
| "fal-ai/nano-banana", | |
| arguments={ | |
| "prompt": prompt, | |
| }, | |
| ) | |
| return result["images"][0]["url"] | |
| def multi_image_edit(prompt, images): | |
| """ | |
| Edits multiple images based on a text prompt. | |
| """ | |
| get_fal_key() | |
| if not images: | |
| raise gr.Error("Please upload at least one image.") | |
| output_images = [] | |
| for image in images: | |
| temp_file_path = file_to_temp_file(image, dir=".") | |
| result = fal_client.run( | |
| "fal-ai/nano-banana/edit", | |
| arguments={ | |
| "prompt": prompt, | |
| "image_url": temp_file_path, | |
| }, | |
| ) | |
| output_images.append(result["images"][0]["url"]) | |
| return output_images | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Nano Banana Image Generation") | |
| gr.Markdown( | |
| "Generate or edit images using the FAL Nano Banana model. " | |
| "Login with Hugging Face to use the app." | |
| ) | |
| login_button = gr.LoginButton() | |
| main_interface = gr.Blocks(visible=False) | |
| with main_interface: | |
| with gr.Tabs(): | |
| with gr.TabItem("Generate"): | |
| with gr.Row(): | |
| with gr.Column(): | |
| prompt_input = gr.Textbox(label="Prompt") | |
| image_input = gr.Image(type="filepath", label="Input Image (Optional)") | |
| generate_button = gr.Button("Generate") | |
| with gr.Column(): | |
| generate_output = gr.Image(label="Output") | |
| generate_button.click( | |
| generate_image, | |
| inputs=[prompt_input, image_input], | |
| outputs=[generate_output], | |
| ) | |
| with gr.TabItem("Multi-Image Edit"): | |
| with gr.Row(): | |
| with gr.Column(): | |
| multi_prompt_input = gr.Textbox(label="Prompt") | |
| multi_image_input = gr.Gallery(label="Input Images") | |
| multi_edit_button = gr.Button("Edit Images") | |
| with gr.Column(): | |
| multi_image_output = gr.Gallery(label="Output Images") | |
| multi_edit_button.click( | |
| multi_image_edit, | |
| inputs=[multi_prompt_input, multi_image_input], | |
| outputs=[multi_image_output], | |
| ) | |
| def show_interface(token): | |
| if token: | |
| try: | |
| whoami(token) | |
| return gr.update(visible=True) | |
| except Exception: | |
| return gr.update(visible=False) | |
| return gr.update(visible=False) | |
| login_button.login(show_interface, None, main_interface) | |
| if __name__ == "__main__": | |
| demo.launch() |