Spaces:
Sleeping
Sleeping
File size: 19,814 Bytes
4ca8a57 732fc9e 4ca8a57 732fc9e 4ca8a57 ea8a32b 4ca8a57 732fc9e 4ca8a57 ea8a32b 4ca8a57 732fc9e 4ca8a57 ea8a32b 4ca8a57 ea8a32b 4ca8a57 732fc9e 4ca8a57 732fc9e ea8a32b 4ca8a57 732fc9e 4ca8a57 732fc9e 4ca8a57 732fc9e ea8a32b 4ca8a57 732fc9e 4ca8a57 732fc9e 4ca8a57 732fc9e ea8a32b 4ca8a57 732fc9e 4ca8a57 732fc9e 4ca8a57 732fc9e 4ca8a57 732fc9e 4ca8a57 ea8a32b 4ca8a57 ea8a32b 4ca8a57 ea8a32b 4ca8a57 ea8a32b 4ca8a57 732fc9e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 | from contextlib import asynccontextmanager
from typing import List, Optional
import uvicorn
import json
import logging
from fastapi import FastAPI, HTTPException, UploadFile, File, Form, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from typing import Dict, Any
# from mangum import Mangum
from app.services.banner_service import generate_banner
from app.services.custom_banner_desktop_service import generate_desktop_banner_v2
from app.services.custom_banner_mobile_service import generate_mobile_banner_v2
from app.services.custom_banner_ipad_service import generate_ipad_banner_v2
from app.services.bestnetwork_service import generate_network_banner
from app.services.byop_service import generate_byop_banner
from app.services.byop_unsplash_service import get_unsplash_byop_images
from app.services.custom_banner_service import generate_custom_banner
from app.services.custom_byop_service import generate_custom_byop_banner
from app.services.custom_network_service import generate_custom_network_banner
from app.services.logo_service import generate_logos
from app.services.network_unsplash_service import get_unsplash_network_images
from app.services.text_logo_service import generate_text_logos
from app.services.unsplash_service import get_unsplash_images
from app.services.ico_convert import convert_to_ico
from app.services.health_service import get_system_health
from app.services.single_icon_logo_service import generate_single_icon_logo
from app.services.single_text_logo_service import generate_single_text_logo
from app.services.upload_image_service import upload_file_as_is
from app.services.byop_unsplash_desktop_service import get_unsplash_byop_desktop_images
from app.services.byop_unsplash_mobile_service import get_unsplash_byop_mobile_images
from app.services.byop_unsplash_ipad_service import get_unsplash_byop_ipad_images
from app.services.network_unsplash_desktop_service import get_unsplash_network_desktop_images
from app.services.network_unsplash_mobile_service import get_unsplash_network_mobile_images
from app.services.network_unsplash_ipad_service import get_unsplash_network_ipad_images
from app.services.banner_unsplash_desktop_service import get_unsplash_desktop_banners
from app.services.banner_unsplash_ipad_service import get_unsplash_ipad_banners
from app.services.banner_unsplash_mobile_service import get_unsplash_mobile_banners
# Configure logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
@asynccontextmanager
async def lifespan(app: FastAPI):
yield
app = FastAPI(
title="Logo Generator API",
description="API for generating logos using Google Generative AI",
lifespan=lifespan,
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
)
# handler = Mangum(
# app,
# lifespan="off",
# api_gateway_base_path="/",
# text_mime_types=["application/json", "multipart/form-data"]
# )
class LogoRequest(BaseModel):
brand_name: str
domain: str
primary_color: str
secondary_color: Optional[str] = None
tertiary_color: Optional[str] = None
class IconRequest(BaseModel):
svg_url: str
class BannerRequest(BaseModel):
brand_details: str
primary_color: str
class CustomBannerDeviceRequest(BaseModel):
prompt: str
brand_details: str
primary_color: str
class GeneralBannerRequest(BaseModel):
brand_details: str
class CustomBannerBaseRequest(BaseModel):
brand_name: str
domain: str
primary_color: str
description: str
# Specific request models for each device type
class CustomDesktopBannerRequest(CustomBannerBaseRequest):
pass
class CustomMobileBannerRequest(CustomBannerBaseRequest):
pass
class CustomIpadBannerRequest(CustomBannerBaseRequest):
pass
class UnsplashBannerRequest(BaseModel):
search_query: str
class ByopUnsplashRequest(BaseModel):
search_query: str
class NetworkUnsplashRequest(BaseModel):
search_query: str
class CustomBannerRequest(BaseModel):
prompt: str
brand_details: str
class Logo(BaseModel):
id: str
url: str
format: str
name: str
class LogoResponse(BaseModel):
logos: List[Logo]
class IcoConvertRequest(BaseModel):
filename: str
class HealthResponse(BaseModel):
"""Response model for health check endpoints"""
status: str
version: str
details: Dict[str, Any]
# @app.get("/")
# async def root():
# return {
# "message": "Logo Generator API is running. Use POST endpoints to create logos and banners."
# }
@app.get("/health", tags=["Health"])
async def def_health_check():
return {
"message": "Logo Generator API is running. Use POST endpoints to create logos and banners."
}
@app.post("/generate-icon-logo")
async def create_logo(request: LogoRequest):
try:
logo_results = generate_logos(
request.brand_name,
request.domain,
request.primary_color,
request.secondary_color,
request.tertiary_color,
)
return JSONResponse(content={"logos": logo_results})
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error generating logo: {str(e)}")
@app.post("/generate-text-logo")
async def create_text_logo(request: LogoRequest):
try:
logo_results = generate_text_logos(
request.brand_name,
request.domain,
request.primary_color,
request.secondary_color,
request.tertiary_color,
)
return JSONResponse(content={"logos": logo_results})
except Exception as e:
raise HTTPException(
status_code=500, detail=f"Error generating text logo: {str(e)}"
)
@app.post("/generate-banner")
async def create_banner(request: BannerRequest):
try:
banner_result = generate_banner(request.brand_details, request.primary_color)
return JSONResponse(content=banner_result)
except Exception as e:
raise HTTPException(
status_code=500, detail=f"Error generating banner: {str(e)}"
)
@app.post("/desktop-banner")
async def create_desktop_banner(request: CustomBannerDeviceRequest):
try:
banner_result = generate_desktop_banner_v2(request.prompt, request.brand_details, request.primary_color)
return JSONResponse(content=banner_result)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error generating desktop banner: {str(e)}")
@app.post("/mobile-banner")
async def create_mobile_banner(request: CustomBannerDeviceRequest):
try:
banner_result = generate_mobile_banner_v2(request.prompt, request.brand_details, request.primary_color)
return JSONResponse(content=banner_result)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error generating mobile banner: {str(e)}")
@app.post("/ipad-banner")
async def create_ipad_banner(request: CustomBannerDeviceRequest):
try:
banner_result = generate_ipad_banner_v2(request.prompt, request.brand_details, request.primary_color)
return JSONResponse(content=banner_result)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error generating iPad banner: {str(e)}")
@app.post("/get-unsplash-banner-desktop")
async def get_unsplash_banner_desktop(request: UnsplashBannerRequest):
try:
result = get_unsplash_desktop_banners(request.search_query)
return JSONResponse(content=result)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error fetching Unsplash desktop banner: {str(e)}")
@app.post("/get-unsplash-banner-ipad")
async def get_unsplash_banner_ipad(request: UnsplashBannerRequest):
try:
result = get_unsplash_ipad_banners(request.search_query)
return JSONResponse(content=result)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error fetching Unsplash iPad banner: {str(e)}")
@app.post("/get-unsplash-banner-mobile")
async def get_unsplash_banner_mobile(request: UnsplashBannerRequest):
try:
result = get_unsplash_mobile_banners(request.search_query)
return JSONResponse(content=result)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error fetching Unsplash mobile banner: {str(e)}")
# @app.post("/generate-custom-banner")
# async def create_custom_banner(request: CustomBannerRequest):
# try:
# banner_result = generate_custom_banner(
# request.brand_name, request.domain, request.description
# )
# return JSONResponse(content={"banner": banner_result})
# except Exception as e:
# raise HTTPException(
# status_code=500, detail=f"Error generating custom banner: {str(e)}"
# )
@app.post("/generate-network-banner")
async def create_network_banner(request: GeneralBannerRequest):
try:
banner_result = generate_network_banner(request.brand_details)
return JSONResponse(content=banner_result)
except Exception as e:
raise HTTPException(
status_code=500, detail=f"Error generating network banner: {str(e)}"
)
@app.post("/get-network-banner-unsplash")
async def get_network_banner_images(request: NetworkUnsplashRequest):
try:
images = get_unsplash_network_images(request.search_query)
return JSONResponse(content=images)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/generate-custom-network-banner")
async def create_custom_network_banner(request: CustomBannerRequest):
try:
banner_result = generate_custom_network_banner(request.prompt, request.brand_details)
return JSONResponse(content=banner_result)
except Exception as e:
raise HTTPException(
status_code=500, detail=f"Error generating custom network banner: {str(e)}"
)
@app.post("/generate-byop-banner")
async def create_byop_banner(request: GeneralBannerRequest):
try:
banner_result = generate_byop_banner(request.brand_details)
return JSONResponse(content=banner_result)
except Exception as e:
raise HTTPException(
status_code=500, detail=f"Error generating BYOP banner: {str(e)}"
)
@app.post("/get-byop-banner-unsplash")
async def get_byop_banner_images(request: ByopUnsplashRequest):
try:
images = get_unsplash_byop_images(request.search_query)
return JSONResponse(content=images)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/generate-custom-byop-banner")
async def create_custom_byop_banner(request: CustomBannerRequest):
try:
banner_result = generate_custom_byop_banner(request.prompt, request.brand_details)
return JSONResponse(content=banner_result)
except Exception as e:
raise HTTPException(
status_code=500, detail=f"Error generating custom BYOP banner: {str(e)}"
)
@app.post("/convert-to-ico")
async def convert_image_to_ico(file: UploadFile = File(...)):
try:
result = await convert_to_ico(file)
return JSONResponse(content={"url": result["url"]})
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"Error converting image to ICO: {str(e)}"
)
@app.get("/health/logo-service", tags=["Health"])
async def logo_service_health():
"""
Health check for logo generation service
"""
try:
health_status = await get_system_health()
required_services = ["google_ai", "image_processing", "reach_upload"]
service_status = all(
health_status["details"][service]["status"] == "healthy"
for service in required_services
)
if not service_status:
raise HTTPException(status_code=503, detail="Logo service dependencies unhealthy")
return {
"status": "healthy",
"service": "logo-generation",
"dependencies": {
service: health_status["details"][service]
for service in required_services
}
}
except Exception as e:
logger.error(f"Logo service health check failed: {str(e)}")
raise HTTPException(status_code=503, detail="Logo service unavailable")
@app.get("/health/banner-service", tags=["Health"])
async def banner_service_health():
"""
Health check for banner generation service
"""
try:
health_status = await get_system_health()
required_services = ["google_ai", "image_processing", "reach_upload"]
service_status = all(
health_status["details"][service]["status"] == "healthy"
for service in required_services
)
if not service_status:
raise HTTPException(status_code=503, detail="Banner service dependencies unhealthy")
return {
"status": "healthy",
"service": "banner-generation",
"dependencies": {
service: health_status["details"][service]
for service in required_services
}
}
except Exception as e:
logger.error(f"Banner service health check failed: {str(e)}")
raise HTTPException(status_code=503, detail="Banner service unavailable")
@app.get("/health/unsplash-service", tags=["Health"])
async def unsplash_service_health():
"""
Health check for Unsplash integration service
"""
try:
health_status = await get_system_health()
required_services = ["unsplash", "reach_upload"]
# Get detailed status of each required service
service_details = {
service: health_status["details"][service]
for service in required_services
}
# Check if any service is unhealthy
unhealthy_services = [
service for service, details in service_details.items()
if details["status"] != "healthy"
]
if unhealthy_services:
return JSONResponse(
status_code=503,
content={
"status": "unhealthy",
"service": "unsplash-integration",
"unhealthy_dependencies": unhealthy_services,
"details": service_details
}
)
return {
"status": "healthy",
"service": "unsplash-integration",
"dependencies": service_details,
"rate_limits": service_details["unsplash"].get("rate_limit_remaining", "unknown")
}
except Exception as e:
logger.error(f"Unsplash service health check failed: {str(e)}")
return JSONResponse(
status_code=503,
content={
"status": "unhealthy",
"service": "unsplash-integration",
"error": str(e),
"message": "Unsplash service health check failed"
}
)
@app.get("/health/ico-service", tags=["Health"])
async def ico_conversion_service_health():
"""
Health check for ICO conversion service
"""
try:
health_status = await get_system_health()
required_services = ["image_processing", "reach_upload"]
service_status = all(
health_status["details"][service]["status"] == "healthy"
for service in required_services
)
if not service_status:
raise HTTPException(status_code=503, detail="ICO conversion service dependencies unhealthy")
return {
"status": "healthy",
"service": "ico-conversion",
"dependencies": {
service: health_status["details"][service]
for service in required_services
}
}
except Exception as e:
logger.error(f"ICO conversion service health check failed: {str(e)}")
raise HTTPException(status_code=503, detail="ICO conversion service unavailable")
@app.post("/generate-single-icon-logo")
async def create_single_icon_logo(request: LogoRequest):
try:
logo_results = generate_single_icon_logo(
request.brand_name,
request.domain,
request.primary_color,
request.secondary_color,
request.tertiary_color,
)
return JSONResponse(content={"logos": logo_results["logos"]})
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error generating single icon logo: {str(e)}")
@app.post("/generate-single-text-logo")
async def create_single_text_logo(request: LogoRequest):
try:
logo_results = generate_single_text_logo(
request.brand_name,
request.domain,
request.primary_color,
request.secondary_color,
request.tertiary_color,
)
return JSONResponse(content={"logos": logo_results["logos"]})
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error generating single text logo: {str(e)}")
@app.post("/upload-image")
async def upload_image(file: UploadFile = File(...), file_type: str = Form(...)):
try:
result = await upload_file_as_is(file, file_type)
return JSONResponse(content=result)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error uploading image: {str(e)}")
@app.post("/get-byop-banner-unsplash-desktop")
async def get_byop_banner_unsplash_desktop(request: ByopUnsplashRequest):
try:
images = get_unsplash_byop_desktop_images(request.search_query)
return JSONResponse(content=images)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/get-byop-banner-unsplash-mobile")
async def get_byop_banner_unsplash_mobile(request: ByopUnsplashRequest):
try:
images = get_unsplash_byop_mobile_images(request.search_query)
return JSONResponse(content=images)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/get-byop-banner-unsplash-ipad")
async def get_byop_banner_unsplash_ipad(request: ByopUnsplashRequest):
try:
images = get_unsplash_byop_ipad_images(request.search_query)
return JSONResponse(content=images)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/get-network-banner-unsplash-desktop")
async def get_network_banner_unsplash_desktop(request: NetworkUnsplashRequest):
try:
images = get_unsplash_network_desktop_images(request.search_query)
return JSONResponse(content=images)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/get-network-banner-unsplash-mobile")
async def get_network_banner_unsplash_mobile(request: NetworkUnsplashRequest):
try:
images = get_unsplash_network_mobile_images(request.search_query)
return JSONResponse(content=images)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/get-network-banner-unsplash-ipad")
async def get_network_banner_unsplash_ipad(request: NetworkUnsplashRequest):
try:
images = get_unsplash_network_ipad_images(request.search_query)
return JSONResponse(content=images)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
|