LogicGoInfotechSpaces commited on
Commit
c82aa52
·
verified ·
1 Parent(s): 72b7090

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -2
app.py CHANGED
@@ -13,7 +13,8 @@ from fastapi import FastAPI, UploadFile, File, HTTPException, Header
13
  from rapidocr_onnxruntime import RapidOCR
14
  from openai import OpenAI
15
  from pymongo import MongoClient
16
-
 
17
  # ---------------- ENV CONFIG ----------------
18
  DO_KEY_ID = os.getenv("DO_SPACES_KEY_ID")
19
  DO_SECRET_KEY = os.getenv("DO_SPACES_SECRET_KEY")
@@ -84,7 +85,50 @@ def log_api_event(
84
  # ---------------- ROUTES ----------------
85
  @app.get("/health")
86
  async def health():
87
- return {"status": "ok"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
  @app.post("/upload")
90
  async def upload_image(file: UploadFile = File(...)):
 
13
  from rapidocr_onnxruntime import RapidOCR
14
  from openai import OpenAI
15
  from pymongo import MongoClient
16
+ from pymongo.errors import PyMongoError
17
+ from botocore.exceptions import BotoCoreError, ClientError
18
  # ---------------- ENV CONFIG ----------------
19
  DO_KEY_ID = os.getenv("DO_SPACES_KEY_ID")
20
  DO_SECRET_KEY = os.getenv("DO_SPACES_SECRET_KEY")
 
85
  # ---------------- ROUTES ----------------
86
  @app.get("/health")
87
  async def health():
88
+ health_report = {
89
+ "service": "Receipt Scanner API",
90
+ "status": "healthy",
91
+ "checks": {}
92
+ }
93
+
94
+ # ---------------- MongoDB ----------------
95
+ try:
96
+ mongo_client.admin.command("ping")
97
+ health_report["checks"]["mongodb"] = "ok"
98
+ except PyMongoError as e:
99
+ health_report["checks"]["mongodb"] = f"fail: {str(e)}"
100
+ health_report["status"] = "degraded"
101
+
102
+ # ---------------- OpenAI ----------------
103
+ try:
104
+ # very light call, does not consume tokens
105
+ client.models.list()
106
+ health_report["checks"]["openai"] = "ok"
107
+ except Exception as e:
108
+ health_report["checks"]["openai"] = f"fail: {str(e)}"
109
+ health_report["status"] = "degraded"
110
+
111
+ # ---------------- DO Spaces / S3 ----------------
112
+ try:
113
+ s3.head_bucket(Bucket=DO_BUCKET)
114
+ health_report["checks"]["object_storage"] = "ok"
115
+ except (BotoCoreError, ClientError) as e:
116
+ health_report["checks"]["object_storage"] = f"fail: {str(e)}"
117
+ health_report["status"] = "degraded"
118
+
119
+ # ---------------- OCR Engine ----------------
120
+ try:
121
+ if ocr_engine is None:
122
+ raise RuntimeError("OCR engine not initialized")
123
+ health_report["checks"]["ocr"] = "ok"
124
+ except Exception as e:
125
+ health_report["checks"]["ocr"] = f"fail: {str(e)}"
126
+ health_report["status"] = "degraded"
127
+
128
+ # ---------------- Overall ----------------
129
+ health_report["timestamp"] = datetime.utcnow().isoformat()
130
+
131
+ return health_report
132
 
133
  @app.post("/upload")
134
  async def upload_image(file: UploadFile = File(...)):