Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,8 +4,11 @@ import os
|
|
| 4 |
from typing import Optional, List
|
| 5 |
from huggingface_hub import whoami
|
| 6 |
|
|
|
|
|
|
|
| 7 |
FAL_KEY = os.getenv("FAL_KEY", "")
|
| 8 |
|
|
|
|
| 9 |
if FAL_KEY:
|
| 10 |
fal_client.api_key = FAL_KEY
|
| 11 |
|
|
@@ -31,12 +34,11 @@ def run_single_image_logic(prompt: str, image: Optional[str] = None) -> str:
|
|
| 31 |
"""Handles text-to-image or single image-to-image and returns a single URL string."""
|
| 32 |
get_fal_key()
|
| 33 |
if image:
|
| 34 |
-
print(image)
|
| 35 |
image_url = fal_client.upload_file(image)
|
| 36 |
-
print(image_url)
|
| 37 |
result = fal_client.run(
|
| 38 |
"fal-ai/nano-banana/edit",
|
| 39 |
-
|
|
|
|
| 40 |
)
|
| 41 |
else:
|
| 42 |
result = fal_client.run(
|
|
@@ -46,16 +48,13 @@ def run_single_image_logic(prompt: str, image: Optional[str] = None) -> str:
|
|
| 46 |
|
| 47 |
def run_multi_image_logic(prompt: str, images: List[str]) -> str:
|
| 48 |
"""
|
| 49 |
-
|
| 50 |
"""
|
| 51 |
get_fal_key()
|
| 52 |
if not images:
|
| 53 |
raise gr.Error("Please upload at least one image in the 'Multiple Images' tab.")
|
| 54 |
|
| 55 |
-
# 1. Upload all images and collect their URLs
|
| 56 |
image_urls = [fal_client.upload_file(image_path) for image_path in images]
|
| 57 |
-
|
| 58 |
-
# 2. Make a single API call with the list of URLs
|
| 59 |
result = fal_client.run(
|
| 60 |
"fal-ai/nano-banana/edit",
|
| 61 |
arguments={
|
|
@@ -64,8 +63,6 @@ def run_multi_image_logic(prompt: str, images: List[str]) -> str:
|
|
| 64 |
"num_images": 1
|
| 65 |
},
|
| 66 |
)
|
| 67 |
-
|
| 68 |
-
# 3. Return the single resulting image URL
|
| 69 |
return result["images"][0]["url"]
|
| 70 |
|
| 71 |
# --- Gradio App UI ---
|
|
|
|
| 4 |
from typing import Optional, List
|
| 5 |
from huggingface_hub import whoami
|
| 6 |
|
| 7 |
+
# It is recommended to create this as a Secret on your Hugging Face Space
|
| 8 |
+
# For example: FAL_KEY = "fal_key_..."
|
| 9 |
FAL_KEY = os.getenv("FAL_KEY", "")
|
| 10 |
|
| 11 |
+
# Set the key for the fal_client
|
| 12 |
if FAL_KEY:
|
| 13 |
fal_client.api_key = FAL_KEY
|
| 14 |
|
|
|
|
| 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)
|
|
|
|
| 38 |
result = fal_client.run(
|
| 39 |
"fal-ai/nano-banana/edit",
|
| 40 |
+
# CORRECTED: The 'edit' endpoint always expects 'image_urls' as a list.
|
| 41 |
+
arguments={"prompt": prompt, "image_urls": [image_url]},
|
| 42 |
)
|
| 43 |
else:
|
| 44 |
result = fal_client.run(
|
|
|
|
| 48 |
|
| 49 |
def run_multi_image_logic(prompt: str, images: List[str]) -> str:
|
| 50 |
"""
|
| 51 |
+
Handles multi-image editing by sending a list of URLs in a single API call.
|
| 52 |
"""
|
| 53 |
get_fal_key()
|
| 54 |
if not images:
|
| 55 |
raise gr.Error("Please upload at least one image in the 'Multiple Images' tab.")
|
| 56 |
|
|
|
|
| 57 |
image_urls = [fal_client.upload_file(image_path) for image_path in images]
|
|
|
|
|
|
|
| 58 |
result = fal_client.run(
|
| 59 |
"fal-ai/nano-banana/edit",
|
| 60 |
arguments={
|
|
|
|
| 63 |
"num_images": 1
|
| 64 |
},
|
| 65 |
)
|
|
|
|
|
|
|
| 66 |
return result["images"][0]["url"]
|
| 67 |
|
| 68 |
# --- Gradio App UI ---
|