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", "")
fal_client.api_key = FAL_KEY
def verify_pro_status(token: Optional[gr.OAuthToken]) -> bool:
"""Verifies if the user is a Hugging Face PRO user or part of an enterprise org."""
if not token:
return False
try:
user_info = whoami(token=token.token)
# Case 1: User is PRO
if user_info.get("isPro", False):
return True
# Case 2: User is in any enterprise org
orgs = user_info.get("orgs", [])
if any(org.get("isEnterprise", False) for org in orgs):
return True
return False
except Exception as e:
print(f"Could not verify user's PRO/Enterprise status: {e}")
return False
# --- Backend Generation Functions ---
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."""
if image:
image_url = fal_client.upload_file(image)
result = fal_client.run(
"fal-ai/nano-banana/edit",
# CORRECTED: The 'edit' endpoint always expects 'image_urls' as a list.
arguments={"prompt": prompt, "image_urls": [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:
"""
Handles multi-image editing by sending a list of URLs in a single API call.
"""
if not images:
raise gr.Error("Please upload at least one image in the 'Multiple Images' tab.")
print(images)
image_urls = [fal_client.upload_file(image_path[0]) 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"]
# --- Gradio App UI ---
css = '''
#sub_title{margin-top: -35px !important}
.tab-wrapper{margin-bottom: -33px !important}
.tabitem{padding: 0px !important}
#output{margin-top: 25px}
.fillable{max-width: 980px !important}
.dark .progress-text {color: white}
.logo-dark{display: none}
.dark .logo-dark{display: block !important}
.dark .logo-light{display: none}
'''
with gr.Blocks(theme=gr.themes.Citrus(), css=css) as demo:
gr.HTML('''
''')
gr.HTML("