Spaces:
Runtime error
Runtime error
from fastapi import FastAPI, File, UploadFile, HTTPException | |
from roboflow import Roboflow | |
import shutil | |
from pathlib import Path | |
# Inicializar la aplicaci贸n FastAPI | |
app = FastAPI() | |
# Inicializar Roboflow | |
rf = Roboflow(api_key="z15djNx8oHjsud3dWL4A") | |
project = rf.workspace().project("stine") | |
model = project.version(3).model | |
# Carpeta temporal para guardar im谩genes cargadas | |
UPLOAD_DIR = Path("uploads") | |
UPLOAD_DIR.mkdir(exist_ok=True) | |
async def predict_image(file: UploadFile = File(...)): | |
""" | |
Endpoint para predecir una imagen usando el modelo de Roboflow. | |
""" | |
# Verificar el tipo de archivo | |
if file.content_type not in ["image/jpeg", "image/png"]: | |
raise HTTPException(status_code=400, detail="El archivo debe ser una imagen (JPEG o PNG)") | |
# Guardar el archivo temporalmente | |
temp_file = UPLOAD_DIR / file.filename | |
with temp_file.open("wb") as buffer: | |
shutil.copyfileobj(file.file, buffer) | |
# Realizar la predicci贸n | |
try: | |
prediction = model.predict(str(temp_file), confidence=40, overlap=30).json() | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=f"Error al realizar la predicci贸n: {e}") | |
# Devolver la predicci贸n como respuesta | |
return prediction | |