Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,12 @@
|
|
1 |
from fastapi import FastAPI, File, UploadFile, Request, HTTPException
|
2 |
-
from fastapi.responses import JSONResponse
|
3 |
from pydantic import BaseModel
|
4 |
import httpx
|
5 |
import os
|
6 |
from sse_starlette.sse import EventSourceResponse
|
7 |
import asyncio
|
|
|
|
|
8 |
|
9 |
app = FastAPI(title="Virtual Try-On API", description="API to forward images and handle virtual try-on requests")
|
10 |
|
@@ -118,4 +120,30 @@ async def event_generator(session_hash: str):
|
|
118 |
@app.get("/sse")
|
119 |
async def sse_proxy(session_hash: str, request: Request):
|
120 |
"""Proxy Server-Sent Events for the virtual try-on process."""
|
121 |
-
return EventSourceResponse(event_generator(session_hash))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from fastapi import FastAPI, File, UploadFile, Request, HTTPException
|
2 |
+
from fastapi.responses import JSONResponse, StreamingResponse
|
3 |
from pydantic import BaseModel
|
4 |
import httpx
|
5 |
import os
|
6 |
from sse_starlette.sse import EventSourceResponse
|
7 |
import asyncio
|
8 |
+
import urllib.request
|
9 |
+
import io
|
10 |
|
11 |
app = FastAPI(title="Virtual Try-On API", description="API to forward images and handle virtual try-on requests")
|
12 |
|
|
|
120 |
@app.get("/sse")
|
121 |
async def sse_proxy(session_hash: str, request: Request):
|
122 |
"""Proxy Server-Sent Events for the virtual try-on process."""
|
123 |
+
return EventSourceResponse(event_generator(session_hash))
|
124 |
+
|
125 |
+
@app.get("/download-image")
|
126 |
+
async def download_image(path: str):
|
127 |
+
"""Download and stream an image from the external API."""
|
128 |
+
try:
|
129 |
+
async with httpx.AsyncClient() as client:
|
130 |
+
response = await client.get("https://kwai-kolors-kolors-virtual-try-on.hf.space/file="+path)
|
131 |
+
response.raise_for_status()
|
132 |
+
|
133 |
+
# Create a BytesIO object for streaming
|
134 |
+
image_stream = io.BytesIO(response.content)
|
135 |
+
|
136 |
+
# Return StreamingResponse with appropriate media type
|
137 |
+
return StreamingResponse(
|
138 |
+
image_stream,
|
139 |
+
media_type="image/webp",
|
140 |
+
headers={
|
141 |
+
"Content-Disposition": "inline; filename=image.webp"
|
142 |
+
}
|
143 |
+
)
|
144 |
+
except httpx.HTTPStatusError as e:
|
145 |
+
raise HTTPException(status_code=e.response.status_code, detail=str(e.response.text))
|
146 |
+
except httpx.HTTPError as e:
|
147 |
+
raise HTTPException(status_code=500, detail=f"HTTP error occurred: {str(e)}")
|
148 |
+
except Exception as e:
|
149 |
+
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
|