mateoluksenberg commited on
Commit
0ff0c53
verified
1 Parent(s): 5d53b03

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile, HTTPException
2
+ from roboflow import Roboflow
3
+ import shutil
4
+ from pathlib import Path
5
+
6
+ # Inicializar la aplicaci贸n FastAPI
7
+ app = FastAPI()
8
+
9
+ # Inicializar Roboflow
10
+ rf = Roboflow(api_key="z15djNx8oHjsud3dWL4A")
11
+ project = rf.workspace().project("stine")
12
+ model = project.version(3).model
13
+
14
+ # Carpeta temporal para guardar im谩genes cargadas
15
+ UPLOAD_DIR = Path("uploads")
16
+ UPLOAD_DIR.mkdir(exist_ok=True)
17
+
18
+ @app.post("/predict/")
19
+ async def predict_image(file: UploadFile = File(...)):
20
+ """
21
+ Endpoint para predecir una imagen usando el modelo de Roboflow.
22
+ """
23
+ # Verificar el tipo de archivo
24
+ if file.content_type not in ["image/jpeg", "image/png"]:
25
+ raise HTTPException(status_code=400, detail="El archivo debe ser una imagen (JPEG o PNG)")
26
+
27
+ # Guardar el archivo temporalmente
28
+ temp_file = UPLOAD_DIR / file.filename
29
+ with temp_file.open("wb") as buffer:
30
+ shutil.copyfileobj(file.file, buffer)
31
+
32
+ # Realizar la predicci贸n
33
+ try:
34
+ prediction = model.predict(str(temp_file), confidence=40, overlap=30).json()
35
+ except Exception as e:
36
+ raise HTTPException(status_code=500, detail=f"Error al realizar la predicci贸n: {e}")
37
+
38
+ # Devolver la predicci贸n como respuesta
39
+ return prediction