vkumartr commited on
Commit
553ae8e
·
verified ·
1 Parent(s): 28043fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -2
app.py CHANGED
@@ -36,14 +36,25 @@ app = FastAPI(docs_url='/')
36
  use_gpu = False
37
  output_dir = 'output'
38
 
39
- if not MONGODB_URI:
40
- raise ValueError("MONGODB_URL is not set. Please add it to Hugging Face secrets.")
 
41
 
42
  # Initialize MongoDB Connection
43
  client = MongoClient(MONGODB_URI)
44
  db = client[DATABASE_NAME]
45
  invoice_collection=db[COLLECTION_NAME]
46
 
 
 
 
 
 
 
 
 
 
 
47
  # Initialize PaddleOCR
48
  ocr = PaddleOCR(use_angle_cls=True, lang='en')
49
 
@@ -166,6 +177,7 @@ This JSON format will be used to store and manage invoices in a structured and u
166
  # return None
167
  # except Exception as e:
168
  # return f"Error in summarization: {str(e)}"
 
169
  # Dependency to check API Key
170
  def verify_api_key(api_key: str = Header(...)):
171
  if api_key != API_KEY:
 
36
  use_gpu = False
37
  output_dir = 'output'
38
 
39
+ # Check if environment variables are set
40
+ if not all([MONGODB_URI, AWS_ACCESS_KEY, AWS_SECRET_KEY, S3_BUCKET_NAME, API_KEY, openai.api_key]):
41
+ raise ValueError("One or more required environment variables are not set.")
42
 
43
  # Initialize MongoDB Connection
44
  client = MongoClient(MONGODB_URI)
45
  db = client[DATABASE_NAME]
46
  invoice_collection=db[COLLECTION_NAME]
47
 
48
+ @app.on_event("startup")
49
+ def startup_db():
50
+ try:
51
+ # Create a MongoDB client and fetch server info to verify connection
52
+ client = MongoClient(MONGODB_URI)
53
+ server_info = client.server_info() # This is a blocking call
54
+ print("MongoDB connection successful:", server_info)
55
+ except Exception as e:
56
+ print("MongoDB connection failed:", str(e))
57
+
58
  # Initialize PaddleOCR
59
  ocr = PaddleOCR(use_angle_cls=True, lang='en')
60
 
 
177
  # return None
178
  # except Exception as e:
179
  # return f"Error in summarization: {str(e)}"
180
+
181
  # Dependency to check API Key
182
  def verify_api_key(api_key: str = Header(...)):
183
  if api_key != API_KEY: