img / main.py
JairoDanielMT's picture
Upload 3 files
7507656 verified
raw
history blame
792 Bytes
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)
@app.post("/upload/")
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})