Spaces:
Sleeping
Sleeping
from fastapi import FastAPI, File, UploadFile | |
from fastapi.responses import JSONResponse | |
import shutil | |
import os | |
app = FastAPI() | |
# Carpeta para almacenar las imágenes | |
UPLOAD_FOLDER = "uploaded_images" | |
os.makedirs(UPLOAD_FOLDER, exist_ok=True) | |
async def upload_image(file: UploadFile = File(...)): | |
""" | |
Sube una imagen y devuelve una URL pública accesible desde Hugging Face. | |
""" | |
file_path = os.path.join(UPLOAD_FOLDER, file.filename) | |
with open(file_path, "wb") as buffer: | |
shutil.copyfileobj(file.file, buffer) | |
space_name = "img" | |
base_url = f"https://jairodanielmt-{space_name}.hf.space" | |
image_url = f"{base_url}/uploaded_images/{file.filename}" | |
return JSONResponse(content={"url": image_url}) | |