File size: 1,257 Bytes
64634ff
12e58a1
3639cfb
 
 
 
 
 
 
e20fb06
 
 
f2dd97d
 
 
 
 
3639cfb
e20fb06
3639cfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# app.py

from fastapi import FastAPI, Request
import csv
import logging
import os
# Configuración de logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = FastAPI()

if __name__ == "__main__":
    import uvicorn, os
    port = int(os.environ.get("PORT", 7860))
    uvicorn.run("app:app", host="0.0.0.0", port=port, log_level="info")

# Ruta raíz: mensaje de bienvenida
@app.get("/")
async def read_root():
    return {"message": "GreenVision API en marcha"}

# Endpoint de feedback
@app.post("/feedback")
async def feedback(request: Request):
    try:
        data = await request.json()
        file_path = "feedback_log.csv"
        file_exists = os.path.isfile(file_path)
        # Crear el archivo con encabezados si no existe
        with open(file_path, "a", newline="") as csvfile:
            writer = csv.writer(csvfile)
            if not file_exists:
                writer.writerow(["predicted", "correct"])
            writer.writerow([data.get("predicted"), data.get("correct")])
        logger.info(f"Feedback recibido: {data}")
        return {"status": "success"}
    except Exception as e:
        logger.error(f"Error guardando feedback: {e}")
        return {"status": "error", "detail": str(e)}