userIdc2024 commited on
Commit
54ffbd3
·
verified ·
1 Parent(s): 53e2f35

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from pydantic import BaseModel, Field
4
+ from typing import List
5
+
6
+ from shashwat_work.main import get_images
7
+
8
+ app = FastAPI(title="Images API", version="1.0.0")
9
+
10
+ # CORS (open by default; tighten if needed)
11
+ app.add_middleware(
12
+ CORSMiddleware,
13
+ allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"],
14
+ )
15
+ class ImageItem(BaseModel):
16
+ ref_url: str
17
+ num: int
18
+
19
+ class GenerateRequest(BaseModel):
20
+ items: List[ImageItem]
21
+
22
+ class ImageResult(BaseModel):
23
+ ref_url: "str"
24
+ urls: List["str"]
25
+
26
+ class GenerateResponse(BaseModel):
27
+ urls: List[ImageResult]
28
+
29
+ @app.get("/health")
30
+ def health():
31
+ return {"ok": True}
32
+
33
+ @app.post("/generate", response_model=GenerateResponse)
34
+ def generate(req: GenerateRequest):
35
+ try:
36
+ urls = get_images(req.items)
37
+ # Basic sanity check
38
+ if not isinstance(urls, list):
39
+ raise ValueError("get_images must return a List[str]")
40
+ return {"urls": urls}
41
+ except Exception as e:
42
+ # Log in real app; return sanitized error
43
+ raise HTTPException(status_code=500, detail=str(e))