File size: 1,891 Bytes
0ff0c53
 
 
 
 
eea0f24
0ff0c53
 
eea0f24
0ff0c53
 
 
 
eea0f24
0ff0c53
eea0f24
0ff0c53
 
 
eea0f24
0ff0c53
 
 
 
 
 
 
eea0f24
 
 
 
0ff0c53
eea0f24
0ff0c53
eea0f24
 
 
0ff0c53
 
eea0f24
 
 
 
0ff0c53
 
2d54649
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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", )