Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ from PIL import Image
|
|
| 7 |
import requests
|
| 8 |
from io import BytesIO
|
| 9 |
import tempfile
|
|
|
|
| 10 |
|
| 11 |
# --- Replicate API Configuration ---
|
| 12 |
REPLICATE_API_TOKEN = os.getenv("REPLICATE_API_TOKEN")
|
|
@@ -28,6 +29,20 @@ def verify_login_status(token: Optional[gr.OAuthToken]) -> bool:
|
|
| 28 |
print(f"Could not verify user's login status: {e}")
|
| 29 |
return False
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
def run_single_image_logic(prompt: str, image_path: Optional[str] = None, progress=gr.Progress()) -> str:
|
| 32 |
"""Handles text-to-image or single image-to-image using Replicate's Nano Banana."""
|
| 33 |
try:
|
|
@@ -38,12 +53,11 @@ def run_single_image_logic(prompt: str, image_path: Optional[str] = None, progre
|
|
| 38 |
"prompt": prompt
|
| 39 |
}
|
| 40 |
|
| 41 |
-
# If there's an input image,
|
| 42 |
if image_path:
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
input_data["image_input"] = [image_path]
|
| 47 |
|
| 48 |
progress(0.5, desc="✨ 생성 중...")
|
| 49 |
|
|
@@ -95,10 +109,18 @@ def run_multi_image_logic(prompt: str, images: List[str], progress=gr.Progress()
|
|
| 95 |
try:
|
| 96 |
progress(0.2, desc="🎨 이미지 준비 중...")
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
# Prepare input for Replicate API with multiple images
|
| 99 |
input_data = {
|
| 100 |
"prompt": prompt,
|
| 101 |
-
"image_input":
|
| 102 |
}
|
| 103 |
|
| 104 |
progress(0.5, desc="✨ 생성 중...")
|
|
|
|
| 7 |
import requests
|
| 8 |
from io import BytesIO
|
| 9 |
import tempfile
|
| 10 |
+
import base64
|
| 11 |
|
| 12 |
# --- Replicate API Configuration ---
|
| 13 |
REPLICATE_API_TOKEN = os.getenv("REPLICATE_API_TOKEN")
|
|
|
|
| 29 |
print(f"Could not verify user's login status: {e}")
|
| 30 |
return False
|
| 31 |
|
| 32 |
+
def image_to_data_uri(image_path: str) -> str:
|
| 33 |
+
"""Convert local image file to data URI format."""
|
| 34 |
+
with open(image_path, "rb") as img_file:
|
| 35 |
+
img_data = img_file.read()
|
| 36 |
+
img_base64 = base64.b64encode(img_data).decode('utf-8')
|
| 37 |
+
|
| 38 |
+
# Get the image format
|
| 39 |
+
img = Image.open(image_path)
|
| 40 |
+
img_format = img.format.lower() if img.format else 'png'
|
| 41 |
+
|
| 42 |
+
# Create data URI
|
| 43 |
+
data_uri = f"data:image/{img_format};base64,{img_base64}"
|
| 44 |
+
return data_uri
|
| 45 |
+
|
| 46 |
def run_single_image_logic(prompt: str, image_path: Optional[str] = None, progress=gr.Progress()) -> str:
|
| 47 |
"""Handles text-to-image or single image-to-image using Replicate's Nano Banana."""
|
| 48 |
try:
|
|
|
|
| 53 |
"prompt": prompt
|
| 54 |
}
|
| 55 |
|
| 56 |
+
# If there's an input image, convert it to data URI
|
| 57 |
if image_path:
|
| 58 |
+
# Convert local image to data URI format
|
| 59 |
+
data_uri = image_to_data_uri(image_path)
|
| 60 |
+
input_data["image_input"] = [data_uri]
|
|
|
|
| 61 |
|
| 62 |
progress(0.5, desc="✨ 생성 중...")
|
| 63 |
|
|
|
|
| 109 |
try:
|
| 110 |
progress(0.2, desc="🎨 이미지 준비 중...")
|
| 111 |
|
| 112 |
+
# Convert all images to data URI format
|
| 113 |
+
data_uris = []
|
| 114 |
+
for image_path in images:
|
| 115 |
+
if isinstance(image_path, (list, tuple)):
|
| 116 |
+
image_path = image_path[0]
|
| 117 |
+
data_uri = image_to_data_uri(image_path)
|
| 118 |
+
data_uris.append(data_uri)
|
| 119 |
+
|
| 120 |
# Prepare input for Replicate API with multiple images
|
| 121 |
input_data = {
|
| 122 |
"prompt": prompt,
|
| 123 |
+
"image_input": data_uris
|
| 124 |
}
|
| 125 |
|
| 126 |
progress(0.5, desc="✨ 생성 중...")
|