Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, HTTPException | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from pydantic import BaseModel, Field | |
| from typing import List | |
| from utils import get_images | |
| app = FastAPI(title="Images API", version="1.0.0") | |
| # CORS (open by default; tighten if needed) | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], | |
| ) | |
| class GenerateRequest(BaseModel): | |
| category: str = Field(..., min_length=1) | |
| keyword: str = Field(..., min_length=1) | |
| subkeyword: str = Field(..., min_length=1) | |
| class GenerateResponse(BaseModel): | |
| urls: List[str] | |
| def health(): | |
| return {"ok": True} | |
| def generate(req: GenerateRequest): | |
| try: | |
| urls = get_images(req.category, req.keyword, req.subkeyword) | |
| # Basic sanity check | |
| if not isinstance(urls, list): | |
| raise ValueError("get_images must return a List[str]") | |
| return {"urls": urls} | |
| except Exception as e: | |
| # Log in real app; return sanitized error | |
| raise HTTPException(status_code=500, detail=str(e)) | |