inference / app.py
mateoluksenberg's picture
Update app.py
2d54649 verified
raw
history blame
1.89 kB
from fastapi import FastAPI, File, UploadFile, HTTPException
from roboflow import Roboflow
import shutil
from pathlib import Path
app = FastAPI()
# Inicializaci贸n de Roboflow
rf = Roboflow(api_key="z15djNx8oHjsud3dWL4A")
project = rf.workspace().project("stine")
model = project.version(3).model
# Definir la carpeta temporal para guardar im谩genes
UPLOAD_DIR = Path("uploads")
UPLOAD_DIR.mkdir(exist_ok=True) # Crear la carpeta si no existe
@app.post("/predict/")
async def predict_image(file: UploadFile = File(...)):
# 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
try:
with temp_file.open("wb") as buffer:
shutil.copyfileobj(file.file, buffer)
# Realizar la predicci贸n
prediction = model.predict(str(temp_file), confidence=40, overlap=30).json()
except Exception as e:
# Eliminar archivo temporal si algo sale mal
if temp_file.exists():
temp_file.unlink()
raise HTTPException(status_code=500, detail=f"Error al realizar la predicci贸n: {e}")
# Limpiar archivo temporal despu茅s de procesar
if temp_file.exists():
temp_file.unlink()
# Devolver la predicci贸n como respuesta
return prediction
if __name__ == "__main__":
app = gr.mount_gradio_app(app, demo, "/")
uvicorn.run(app, host="0.0.0.0", port=7860)
#app.mount("/static", StaticFiles(directory="static", html=True), name="static")
# app = gr.mount_gradio_app(app, block, "/", gradio_api_url="http://localhost:7860/")
# uvicorn.run(app, host="0.0.0.0", port=7860)
demo.queue(api_open=False).launch(show_api=False, share=False, )#server_name="0.0.0.0", )