Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -29,26 +29,26 @@ IMAGE_MODELS = {
|
|
| 29 |
"Freepik Gemini 2.5 Flash": {
|
| 30 |
"name": "Freepik Gemini 2.5 Flash Image Preview",
|
| 31 |
"api": "freepik",
|
| 32 |
-
"url": "https://api.freepik.com/v1/ai/
|
| 33 |
-
"description": "π Best for competition - Real Gemini 2.5 Flash generation"
|
| 34 |
},
|
| 35 |
"OpenAI DALL-E 3": {
|
| 36 |
-
"name": "DALL-E 3",
|
| 37 |
"api": "openai",
|
| 38 |
"url": "https://api.openai.com/v1/images/generations",
|
| 39 |
-
"description": "π¨ High-quality creative
|
| 40 |
},
|
| 41 |
"Stable Diffusion XL": {
|
| 42 |
"name": "Stable Diffusion XL",
|
| 43 |
-
"api": "stabilityai",
|
| 44 |
"url": "https://api.stability.ai/v1/generation/stable-diffusion-xl-1024-v1-0/text-to-image",
|
| 45 |
-
"description": "π Open-source
|
| 46 |
},
|
| 47 |
"Gemini Analysis + Demo": {
|
| 48 |
"name": "Gemini Analysis + Visual Demo",
|
| 49 |
"api": "gemini",
|
| 50 |
"url": None,
|
| 51 |
-
"description": "π
|
| 52 |
}
|
| 53 |
}
|
| 54 |
|
|
@@ -135,28 +135,63 @@ class NanoBananaApp:
|
|
| 135 |
|
| 136 |
full_prompt = self._build_enhanced_prompt(prompt, style, editing_mode)
|
| 137 |
|
| 138 |
-
|
|
|
|
| 139 |
payload = {
|
| 140 |
"prompt": full_prompt,
|
| 141 |
-
"
|
| 142 |
-
"
|
|
|
|
|
|
|
| 143 |
}
|
| 144 |
headers = {
|
| 145 |
"x-freepik-api-key": api_key,
|
| 146 |
"Content-Type": "application/json"
|
| 147 |
}
|
| 148 |
|
| 149 |
-
response = requests.post(url, json=payload, headers=headers, timeout=
|
|
|
|
|
|
|
| 150 |
|
| 151 |
if response.status_code == 200:
|
| 152 |
result = response.json()
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
except Exception as e:
|
| 162 |
logger.error(f"Freepik generation failed: {e}")
|
|
@@ -246,11 +281,19 @@ class NanoBananaApp:
|
|
| 246 |
return None, f"Stable Diffusion error: {str(e)}"
|
| 247 |
|
| 248 |
def _generate_with_gemini_demo(self, image, prompt, style, editing_mode, api_key):
|
| 249 |
-
if
|
| 250 |
try:
|
|
|
|
|
|
|
| 251 |
genai.configure(api_key=api_key)
|
| 252 |
-
|
| 253 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
|
| 255 |
return self._fallback_to_gemini_demo(image, prompt, style, editing_mode)
|
| 256 |
|
|
|
|
| 29 |
"Freepik Gemini 2.5 Flash": {
|
| 30 |
"name": "Freepik Gemini 2.5 Flash Image Preview",
|
| 31 |
"api": "freepik",
|
| 32 |
+
"url": "https://api.freepik.com/v1/ai/text-to-image",
|
| 33 |
+
"description": "π <strong>Best for competition</strong> - Real Gemini 2.5 Flash generation<br/>β οΈ <em>Requires paid Freepik subscription + valid API key</em>"
|
| 34 |
},
|
| 35 |
"OpenAI DALL-E 3": {
|
| 36 |
+
"name": "DALL-E 3",
|
| 37 |
"api": "openai",
|
| 38 |
"url": "https://api.openai.com/v1/images/generations",
|
| 39 |
+
"description": "π¨ <strong>High-quality creative images</strong> - Excellent for artistic styles<br/>π³ <em>Requires OpenAI API key + billing account</em>"
|
| 40 |
},
|
| 41 |
"Stable Diffusion XL": {
|
| 42 |
"name": "Stable Diffusion XL",
|
| 43 |
+
"api": "stabilityai",
|
| 44 |
"url": "https://api.stability.ai/v1/generation/stable-diffusion-xl-1024-v1-0/text-to-image",
|
| 45 |
+
"description": "π <strong>Open-source powerhouse</strong> - High-resolution generation<br/>π° <em>Requires Stability AI credits + API key</em>"
|
| 46 |
},
|
| 47 |
"Gemini Analysis + Demo": {
|
| 48 |
"name": "Gemini Analysis + Visual Demo",
|
| 49 |
"api": "gemini",
|
| 50 |
"url": None,
|
| 51 |
+
"description": "π <strong>Smart fallback</strong> - AI analysis + visual demo overlay<br/>β
<em>Works with basic Gemini key or demo mode</em>"
|
| 52 |
}
|
| 53 |
}
|
| 54 |
|
|
|
|
| 135 |
|
| 136 |
full_prompt = self._build_enhanced_prompt(prompt, style, editing_mode)
|
| 137 |
|
| 138 |
+
# Note: This URL may need to be updated based on actual Freepik API documentation
|
| 139 |
+
url = "https://api.freepik.com/v1/ai/text-to-image"
|
| 140 |
payload = {
|
| 141 |
"prompt": full_prompt,
|
| 142 |
+
"num_images": 1,
|
| 143 |
+
"image": {
|
| 144 |
+
"size": "1024x1024"
|
| 145 |
+
}
|
| 146 |
}
|
| 147 |
headers = {
|
| 148 |
"x-freepik-api-key": api_key,
|
| 149 |
"Content-Type": "application/json"
|
| 150 |
}
|
| 151 |
|
| 152 |
+
response = requests.post(url, json=payload, headers=headers, timeout=60)
|
| 153 |
+
|
| 154 |
+
logger.info(f"Freepik API response: {response.status_code}")
|
| 155 |
|
| 156 |
if response.status_code == 200:
|
| 157 |
result = response.json()
|
| 158 |
+
logger.info(f"Freepik response data: {result}")
|
| 159 |
+
|
| 160 |
+
# Handle different possible response formats
|
| 161 |
+
if 'data' in result and len(result['data']) > 0:
|
| 162 |
+
if 'url' in result['data'][0]:
|
| 163 |
+
image_url = result['data'][0]['url']
|
| 164 |
+
elif 'image_url' in result['data'][0]:
|
| 165 |
+
image_url = result['data'][0]['image_url']
|
| 166 |
+
else:
|
| 167 |
+
return None, "Freepik API: No image URL in response"
|
| 168 |
+
elif 'image_url' in result:
|
| 169 |
+
image_url = result['image_url']
|
| 170 |
+
elif 'url' in result:
|
| 171 |
+
image_url = result['url']
|
| 172 |
+
else:
|
| 173 |
+
return None, f"Freepik API: Unexpected response format - {result}"
|
| 174 |
+
|
| 175 |
+
# Download the generated image
|
| 176 |
+
img_response = requests.get(image_url, timeout=30)
|
| 177 |
+
if img_response.status_code == 200:
|
| 178 |
+
generated_image = Image.open(io.BytesIO(img_response.content))
|
| 179 |
+
return generated_image, "π Generated with Freepik API"
|
| 180 |
+
else:
|
| 181 |
+
return None, f"Failed to download image: {img_response.status_code}"
|
| 182 |
+
|
| 183 |
+
elif response.status_code == 401:
|
| 184 |
+
return None, "Invalid or expired Freepik API key"
|
| 185 |
+
elif response.status_code == 403:
|
| 186 |
+
return None, "Freepik API access forbidden - check subscription plan"
|
| 187 |
+
elif response.status_code == 429:
|
| 188 |
+
return None, "Freepik API rate limit exceeded"
|
| 189 |
+
else:
|
| 190 |
+
try:
|
| 191 |
+
error_data = response.json()
|
| 192 |
+
return None, f"Freepik API error: {response.status_code} - {error_data}"
|
| 193 |
+
except:
|
| 194 |
+
return None, f"Freepik API error: {response.status_code} - {response.text[:200]}"
|
| 195 |
|
| 196 |
except Exception as e:
|
| 197 |
logger.error(f"Freepik generation failed: {e}")
|
|
|
|
| 281 |
return None, f"Stable Diffusion error: {str(e)}"
|
| 282 |
|
| 283 |
def _generate_with_gemini_demo(self, image, prompt, style, editing_mode, api_key):
|
| 284 |
+
if api_key:
|
| 285 |
try:
|
| 286 |
+
# Temporarily configure with user's key
|
| 287 |
+
original_key = GEMINI_API_KEY
|
| 288 |
genai.configure(api_key=api_key)
|
| 289 |
+
result = self._fallback_to_gemini_demo(image, prompt, style, editing_mode)
|
| 290 |
+
# Restore original key
|
| 291 |
+
if original_key:
|
| 292 |
+
genai.configure(api_key=original_key)
|
| 293 |
+
return result
|
| 294 |
+
except Exception as e:
|
| 295 |
+
logger.warning(f"User Gemini key failed: {e}")
|
| 296 |
+
return None, f"Invalid Gemini API key: {str(e)}"
|
| 297 |
|
| 298 |
return self._fallback_to_gemini_demo(image, prompt, style, editing_mode)
|
| 299 |
|