Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -158,8 +158,10 @@ def run_single_image_logic(prompt: str, image_path: Optional[str] = None, progre
|
|
| 158 |
progress(0.5, desc="β¨ Generating...")
|
| 159 |
|
| 160 |
# Run the model on Replicate
|
|
|
|
|
|
|
| 161 |
output = replicate.run(
|
| 162 |
-
"google/nano-banana",
|
| 163 |
input=input_data
|
| 164 |
)
|
| 165 |
|
|
@@ -171,6 +173,13 @@ def run_single_image_logic(prompt: str, image_path: Optional[str] = None, progre
|
|
| 171 |
else:
|
| 172 |
raise ValueError("No output received from Replicate API")
|
| 173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
except Exception as e:
|
| 175 |
print(f"Error details: {e}")
|
| 176 |
print(f"Error type: {type(e)}")
|
|
@@ -182,6 +191,8 @@ def run_single_image_logic(prompt: str, image_path: Optional[str] = None, progre
|
|
| 182 |
def run_multi_image_logic(prompt: str, images: List[str], progress=gr.Progress()) -> str:
|
| 183 |
"""
|
| 184 |
Handles multi-image editing by sending a list of images and a prompt.
|
|
|
|
|
|
|
| 185 |
"""
|
| 186 |
if not images:
|
| 187 |
raise gr.Error("Please upload at least one image in the 'Multiple Images' tab.")
|
|
@@ -189,27 +200,34 @@ def run_multi_image_logic(prompt: str, images: List[str], progress=gr.Progress()
|
|
| 189 |
try:
|
| 190 |
progress(0.2, desc="π¨ Preparing images...")
|
| 191 |
|
| 192 |
-
#
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
progress(0.2 + (0.2 * idx / len(images)), desc=f"π€ Uploading image {idx+1}/{len(images)}...")
|
| 199 |
-
image_url = upload_image_to_hosting(image_path)
|
| 200 |
-
image_urls.append(image_url)
|
| 201 |
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
input_data = {
|
| 204 |
"prompt": prompt,
|
| 205 |
-
"image_input":
|
| 206 |
}
|
| 207 |
|
| 208 |
progress(0.5, desc="β¨ Generating...")
|
| 209 |
|
| 210 |
# Run the model on Replicate
|
|
|
|
|
|
|
| 211 |
output = replicate.run(
|
| 212 |
-
"google/nano-banana",
|
| 213 |
input=input_data
|
| 214 |
)
|
| 215 |
|
|
@@ -221,11 +239,20 @@ def run_multi_image_logic(prompt: str, images: List[str], progress=gr.Progress()
|
|
| 221 |
else:
|
| 222 |
raise ValueError("No output received from Replicate API")
|
| 223 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
except Exception as e:
|
| 225 |
print(f"Multi-image error details: {e}")
|
|
|
|
| 226 |
print(f"Output value: {output if 'output' in locals() else 'Not set'}")
|
| 227 |
-
|
| 228 |
-
raise gr.Error(f"Image generation failed: {e}")
|
| 229 |
|
| 230 |
# --- Gradio App UI ---
|
| 231 |
css = '''
|
|
|
|
| 158 |
progress(0.5, desc="β¨ Generating...")
|
| 159 |
|
| 160 |
# Run the model on Replicate
|
| 161 |
+
# Note: Replace "google/nano-banana" with actual model name if it doesn't exist
|
| 162 |
+
# Examples of real models: "stability-ai/stable-diffusion", "tencentarc/photomaker", etc.
|
| 163 |
output = replicate.run(
|
| 164 |
+
"google/nano-banana", # This might need to be changed to a real model
|
| 165 |
input=input_data
|
| 166 |
)
|
| 167 |
|
|
|
|
| 173 |
else:
|
| 174 |
raise ValueError("No output received from Replicate API")
|
| 175 |
|
| 176 |
+
except replicate.exceptions.ModelError as e:
|
| 177 |
+
print(f"Replicate Model Error: {e}")
|
| 178 |
+
error_msg = str(e)
|
| 179 |
+
if "does not exist" in error_msg.lower() or "not found" in error_msg.lower():
|
| 180 |
+
raise gr.Error("The specified model 'google/nano-banana' was not found. Please check the model name and ensure your Replicate API token has access.")
|
| 181 |
+
else:
|
| 182 |
+
raise gr.Error(f"Model error: {error_msg[:200]}")
|
| 183 |
except Exception as e:
|
| 184 |
print(f"Error details: {e}")
|
| 185 |
print(f"Error type: {type(e)}")
|
|
|
|
| 191 |
def run_multi_image_logic(prompt: str, images: List[str], progress=gr.Progress()) -> str:
|
| 192 |
"""
|
| 193 |
Handles multi-image editing by sending a list of images and a prompt.
|
| 194 |
+
Note: Since the actual model might not support multiple images,
|
| 195 |
+
we'll process only the first image or combine them.
|
| 196 |
"""
|
| 197 |
if not images:
|
| 198 |
raise gr.Error("Please upload at least one image in the 'Multiple Images' tab.")
|
|
|
|
| 200 |
try:
|
| 201 |
progress(0.2, desc="π¨ Preparing images...")
|
| 202 |
|
| 203 |
+
# For now, we'll use only the first image since the model might not support multiple
|
| 204 |
+
# You can modify this based on the actual model's capabilities
|
| 205 |
+
image_path = images[0]
|
| 206 |
+
if isinstance(image_path, (list, tuple)):
|
| 207 |
+
image_path = image_path[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
+
progress(0.3, desc="π€ Uploading image...")
|
| 210 |
+
image_url = upload_image_to_hosting(image_path)
|
| 211 |
+
|
| 212 |
+
if image_url.startswith('http'):
|
| 213 |
+
print(f"Image uploaded successfully: {image_url[:50]}...")
|
| 214 |
+
else:
|
| 215 |
+
print("Using data URI fallback")
|
| 216 |
+
|
| 217 |
+
# Prepare input for Replicate API
|
| 218 |
+
# Using single image format since model might not support multiple
|
| 219 |
input_data = {
|
| 220 |
"prompt": prompt,
|
| 221 |
+
"image_input": [image_url] # Send as array with single image
|
| 222 |
}
|
| 223 |
|
| 224 |
progress(0.5, desc="β¨ Generating...")
|
| 225 |
|
| 226 |
# Run the model on Replicate
|
| 227 |
+
# Note: Replace "google/nano-banana" with actual model name
|
| 228 |
+
# Examples of real models: "stability-ai/stable-diffusion", "tencentarc/photomaker", etc.
|
| 229 |
output = replicate.run(
|
| 230 |
+
"google/nano-banana", # This might need to be changed to a real model
|
| 231 |
input=input_data
|
| 232 |
)
|
| 233 |
|
|
|
|
| 239 |
else:
|
| 240 |
raise ValueError("No output received from Replicate API")
|
| 241 |
|
| 242 |
+
except replicate.exceptions.ModelError as e:
|
| 243 |
+
print(f"Replicate Model Error: {e}")
|
| 244 |
+
error_msg = str(e)
|
| 245 |
+
if "does not exist" in error_msg.lower() or "not found" in error_msg.lower():
|
| 246 |
+
raise gr.Error("The specified model 'google/nano-banana' was not found. Please check the model name.")
|
| 247 |
+
elif "no image content" in error_msg.lower():
|
| 248 |
+
raise gr.Error("Failed to process images. The model may not support the provided image format or multiple images.")
|
| 249 |
+
else:
|
| 250 |
+
raise gr.Error(f"Model error: {error_msg[:200]}")
|
| 251 |
except Exception as e:
|
| 252 |
print(f"Multi-image error details: {e}")
|
| 253 |
+
print(f"Input data sent: {input_data if 'input_data' in locals() else 'Not set'}")
|
| 254 |
print(f"Output value: {output if 'output' in locals() else 'Not set'}")
|
| 255 |
+
raise gr.Error(f"Image generation failed: {str(e)[:200]}")
|
|
|
|
| 256 |
|
| 257 |
# --- Gradio App UI ---
|
| 258 |
css = '''
|