HOOK777 / app.py
Clone04's picture
Upload 3 files
cc00fa2 verified
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
import urllib.request
import io
app = FastAPI()
@app.get("/download-image")
async def download_image(path: str):
BASE_URL = "https://kwai-kolors-kolors-virtual-try-on.hf.space/file="
try:
# Download image
with urllib.request.urlopen(BASE_URL+path) as response:
image_data = response.read()
# Create a BytesIO object for streaming
image_stream = io.BytesIO(image_data)
# Return StreamingResponse with appropriate media type
return StreamingResponse(
image_stream,
media_type="image/webp",
headers={
"Content-Disposition": "inline; filename=image.webp"
}
)
except Exception as e:
return {"error": str(e)}
'''import urllib.request
url = 'https://kwai-kolors-kolors-virtual-try-on.hf.space/file=/tmp/gradio/e3f14f9d34b839a2b2a338b8e4a1827f6a1902663ddb031a8bb2e296b1bdcbf7/image.webp'
def download_image_and_log_bytes(url, save_as):
with urllib.request.urlopen(url) as response:
image_data = response.read()
print(f"[LOG] Image byte length: {len(image_data)}")
print(f"[LOG] First 100 bytes: {image_data[:100]}")
with open(save_as, 'wb') as f:
f.write(image_data)
save_as = 'imager.jpg'
download_image_and_log_bytes(url, save_as)'''