|
|
import gradio as gr |
|
|
import fal_client |
|
|
import os |
|
|
from typing import Optional, List |
|
|
from huggingface_hub import whoami |
|
|
|
|
|
FAL_KEY = os.getenv("FAL_KEY", "") |
|
|
|
|
|
if FAL_KEY: |
|
|
fal_client.api_key = FAL_KEY |
|
|
|
|
|
def get_fal_key(): |
|
|
"""Checks for the FAL_KEY and raises a Gradio error if it's not set.""" |
|
|
if not FAL_KEY: |
|
|
raise gr.Error("FAL_KEY is not set. Please add it to your Hugging Face Space secrets.") |
|
|
|
|
|
def verify_pro_status(token: Optional[gr.OAuthToken]) -> bool: |
|
|
"""Verifies if the user is a Hugging Face PRO user using their token.""" |
|
|
if not token: |
|
|
return False |
|
|
try: |
|
|
user_info = whoami(token=token.token) |
|
|
return user_info.get("isPro", False) |
|
|
except Exception as e: |
|
|
print(f"Could not verify user's PRO status: {e}") |
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
def run_single_image_logic(prompt: str, image: Optional[str] = None) -> str: |
|
|
"""Handles text-to-image or single image-to-image and returns a single URL string.""" |
|
|
get_fal_key() |
|
|
if image: |
|
|
print(image) |
|
|
image_url = fal_client.upload_file(image) |
|
|
print(image_url) |
|
|
result = fal_client.run( |
|
|
"fal-ai/nano-banana/edit", |
|
|
arguments={"prompt": prompt, "image_url": image_url}, |
|
|
) |
|
|
else: |
|
|
result = fal_client.run( |
|
|
"fal-ai/nano-banana", arguments={"prompt": prompt} |
|
|
) |
|
|
return result["images"][0]["url"] |
|
|
|
|
|
def run_multi_image_logic(prompt: str, images: List[str]) -> str: |
|
|
""" |
|
|
Uploads multiple images |
|
|
""" |
|
|
get_fal_key() |
|
|
if not images: |
|
|
raise gr.Error("Please upload at least one image in the 'Multiple Images' tab.") |
|
|
|
|
|
|
|
|
image_urls = [fal_client.upload_file(image_path) for image_path in images] |
|
|
|
|
|
|
|
|
result = fal_client.run( |
|
|
"fal-ai/nano-banana/edit", |
|
|
arguments={ |
|
|
"prompt": prompt, |
|
|
"image_urls": image_urls, |
|
|
"num_images": 1 |
|
|
}, |
|
|
) |
|
|
|
|
|
|
|
|
return result["images"][0]["url"] |
|
|
|
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Citrus()) as demo: |
|
|
gr.HTML("<h1 style='text-align:center'>Nano Banana for PROs</h1>") |
|
|
gr.Markdown("Hugging Face PRO users can use Google's Nano Banana (Gemini 2.5 Flash Image Preview) on this Space. [Subscribe to PRO](https://huggingface.co/pro)") |
|
|
|
|
|
login_button = gr.LoginButton() |
|
|
pro_message = gr.Markdown(visible=False) |
|
|
main_interface = gr.Column(visible=False) |
|
|
|
|
|
with main_interface: |
|
|
gr.Markdown("## Welcome, PRO User!") |
|
|
with gr.Row(): |
|
|
|
|
|
with gr.Column(scale=1): |
|
|
prompt_input = gr.Textbox( |
|
|
label="Prompt", |
|
|
placeholder="A delicious looking pizza" |
|
|
) |
|
|
active_tab_state = gr.State(value="single") |
|
|
with gr.Tabs() as tabs: |
|
|
with gr.TabItem("Single Image", id="single") as single_tab: |
|
|
image_input = gr.Image( |
|
|
type="filepath", |
|
|
label="Input Image (Leave blank for text-to-image)" |
|
|
) |
|
|
with gr.TabItem("Multiple Images", id="multiple") as multi_tab: |
|
|
gallery_input = gr.Gallery( |
|
|
label="Input Images (drop all images here)", file_types=["image"] |
|
|
) |
|
|
generate_button = gr.Button("Generate", variant="primary") |
|
|
|
|
|
|
|
|
with gr.Column(scale=1): |
|
|
output_image = gr.Image(label="Output", interactive=False) |
|
|
use_image_button = gr.Button("♻️ Use this Image for Next Edit") |
|
|
|
|
|
|
|
|
|
|
|
def unified_generator( |
|
|
prompt: str, |
|
|
single_image: Optional[str], |
|
|
multi_images: Optional[List[str]], |
|
|
active_tab: str, |
|
|
oauth_token: Optional[gr.OAuthToken] = None, |
|
|
) -> str: |
|
|
if not verify_pro_status(oauth_token): |
|
|
raise gr.Error("Access Denied. This service is for PRO users only.") |
|
|
if active_tab == "multiple" and multi_images: |
|
|
return run_multi_image_logic(prompt, multi_images) |
|
|
else: |
|
|
return run_single_image_logic(prompt, single_image) |
|
|
|
|
|
single_tab.select(lambda: "single", None, active_tab_state) |
|
|
multi_tab.select(lambda: "multiple", None, active_tab_state) |
|
|
|
|
|
generate_button.click( |
|
|
unified_generator, |
|
|
inputs=[prompt_input, image_input, gallery_input, active_tab_state], |
|
|
outputs=[output_image], |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
use_image_button.click( |
|
|
lambda img: img, |
|
|
inputs=[output_image], |
|
|
outputs=[image_input] |
|
|
) |
|
|
|
|
|
|
|
|
def control_access( |
|
|
profile: Optional[gr.OAuthProfile] = None, |
|
|
oauth_token: Optional[gr.OAuthToken] = None |
|
|
): |
|
|
if not profile: |
|
|
return gr.update(visible=False), gr.update(visible=False) |
|
|
if verify_pro_status(oauth_token): |
|
|
return gr.update(visible=True), gr.update(visible=False) |
|
|
else: |
|
|
message = ( |
|
|
"## ✨ Exclusive Access for PRO Users\n\n" |
|
|
"Thank you for your interest! This feature is available exclusively for our Hugging Face **PRO** members.\n\n" |
|
|
"To unlock this and many other benefits, please consider upgrading your account.\n\n" |
|
|
"### [**Become a PRO Member Today!**](https://huggingface.co/pro)" |
|
|
) |
|
|
return gr.update(visible=False), gr.update(visible=True, value=message) |
|
|
|
|
|
demo.load(control_access, inputs=None, outputs=[main_interface, pro_message]) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch() |