AhmadYarAI commited on
Commit
805175a
·
1 Parent(s): d491cd7

Setup the Hugging face deployment

Browse files
Files changed (2) hide show
  1. .env +1 -0
  2. main.py +16 -13
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ DATABASE_URL='postgresql://neondb_owner:npg_0ZThyfD5xJGw@ep-polished-base-ab35nufp.eu-west-2.aws.neon.tech/neondb?sslmode=require&channel_binding=require'
main.py CHANGED
@@ -1,20 +1,23 @@
 
1
  from fastapi import FastAPI
2
- from fastapi.middleware.cors import CORSMiddleware
 
3
  from dotenv import load_dotenv
4
- import os
5
 
6
- # Load variables from .env
7
  load_dotenv()
8
 
9
- app = FastAPI()
10
 
11
- app.add_middleware(
12
- CORSMiddleware,
13
- allow_origins=["*"],
14
- allow_methods=["*"],
15
- allow_headers=["*"],
16
- )
17
 
18
- @app.get("/")
19
- def read_root():
20
- return {"status": "SmiloCAD API is Live", "db_connected": bool(os.getenv("DATABASE_URL"))}
 
 
 
 
1
+ import os
2
  from fastapi import FastAPI
3
+ from sqlalchemy import create_engine
4
+ from sqlalchemy.orm import sessionmaker
5
  from dotenv import load_dotenv
 
6
 
7
+ # This will pick up the secret you saved in Hugging Face
8
  load_dotenv()
9
 
10
+ DATABASE_URL = os.getenv("DATABASE_URL")
11
 
12
+ # Create the SQLAlchemy engine using the modern psycopg driver
13
+ engine = create_engine(DATABASE_URL)
14
+ SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
15
+
16
+ app = FastAPI()
 
17
 
18
+ @app.get("/health")
19
+ def health_check():
20
+ # This checks if the URL was loaded correctly from the secrets
21
+ if DATABASE_URL:
22
+ return {"status": "Database URL is loaded"}
23
+ return {"status": "Error: DATABASE_URL not found"}