Spaces:
Sleeping
Sleeping
Paladugula Siva Satyanarayana commited on
Commit ·
859f8b3
1
Parent(s): 6d69182
Db Deployment handles
Browse files
backend/app/db/db_connector.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
from sqlalchemy import create_engine
|
| 2 |
from sqlalchemy.engine import Engine
|
| 3 |
from sqlalchemy.orm import sessionmaker
|
|
|
|
| 4 |
import sqlite3
|
| 5 |
from pathlib import Path
|
| 6 |
import os
|
|
@@ -9,10 +10,11 @@ load_dotenv()
|
|
| 9 |
|
| 10 |
# Path to SQLite DB file, configurable via environment variable or .env
|
| 11 |
print("DB_PATH from env:", os.getenv("DB_PATH"))
|
| 12 |
-
DB_PATH = os.getenv("DB_PATH")
|
|
|
|
| 13 |
if not DB_PATH:
|
| 14 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 15 |
-
DB_PATH = os.path.join(BASE_DIR, "
|
| 16 |
|
| 17 |
DATABASE_URL = f"sqlite:///{DB_PATH}"
|
| 18 |
|
|
|
|
| 1 |
from sqlalchemy import create_engine
|
| 2 |
from sqlalchemy.engine import Engine
|
| 3 |
from sqlalchemy.orm import sessionmaker
|
| 4 |
+
from app.services.utility import UtilityClass
|
| 5 |
import sqlite3
|
| 6 |
from pathlib import Path
|
| 7 |
import os
|
|
|
|
| 10 |
|
| 11 |
# Path to SQLite DB file, configurable via environment variable or .env
|
| 12 |
print("DB_PATH from env:", os.getenv("DB_PATH"))
|
| 13 |
+
# DB_PATH = os.getenv("DB_PATH")
|
| 14 |
+
DB_PATH = UtilityClass.download_sqlite_db_from_dropbox(os.getenv("DROPBOX_TOKEN"), os.getenv("DROPBOX_DB_PATH"))
|
| 15 |
if not DB_PATH:
|
| 16 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 17 |
+
DB_PATH = os.path.join(BASE_DIR, "Banking.db")
|
| 18 |
|
| 19 |
DATABASE_URL = f"sqlite:///{DB_PATH}"
|
| 20 |
|
backend/app/services/utility.py
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
|
|
| 1 |
import openai
|
| 2 |
from config.settings import settings
|
| 3 |
from langchain_openai import ChatOpenAI
|
| 4 |
import sqlparse
|
| 5 |
import re
|
|
|
|
| 6 |
class UtilityClass:
|
| 7 |
"""
|
| 8 |
Classifies user input as 'sql' (SQL/data context) or 'chat' (normal conversation) using an LLM.
|
|
@@ -102,4 +104,19 @@ class UtilityClass:
|
|
| 102 |
|
| 103 |
return False
|
| 104 |
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import dropbox
|
| 2 |
import openai
|
| 3 |
from config.settings import settings
|
| 4 |
from langchain_openai import ChatOpenAI
|
| 5 |
import sqlparse
|
| 6 |
import re
|
| 7 |
+
import os
|
| 8 |
class UtilityClass:
|
| 9 |
"""
|
| 10 |
Classifies user input as 'sql' (SQL/data context) or 'chat' (normal conversation) using an LLM.
|
|
|
|
| 104 |
|
| 105 |
return False
|
| 106 |
|
| 107 |
+
@staticmethod
|
| 108 |
+
def download_sqlite_db_from_dropbox(dropbox_token: str, dropbox_db_path: str, local_db_path: str = None) -> str:
|
| 109 |
+
"""
|
| 110 |
+
Downloads a SQLite DB file from Dropbox and returns the local file path.
|
| 111 |
+
:param dropbox_token: Dropbox API access token.
|
| 112 |
+
:param dropbox_db_path: Path to the DB file in Dropbox (e.g., '/folder/mydb.db').
|
| 113 |
+
:param local_db_path: Local path to save the file. If None, saves to './downloaded_db.db'.
|
| 114 |
+
:return: Local file path of the downloaded DB.
|
| 115 |
+
"""
|
| 116 |
+
if local_db_path is None:
|
| 117 |
+
local_db_path = os.path.join(os.getcwd(), "downloaded_db.db")
|
| 118 |
+
dbx = dropbox.Dropbox(dropbox_token)
|
| 119 |
+
with open(local_db_path, 'wb') as f:
|
| 120 |
+
metadata, res = dbx.files_download(path=dropbox_db_path)
|
| 121 |
+
f.write(res.content)
|
| 122 |
+
return local_db_path
|
backend/requirements.txt
CHANGED
|
@@ -12,4 +12,4 @@ faiss-cpu # optional for embeddings
|
|
| 12 |
pandas
|
| 13 |
matplotlib
|
| 14 |
python-dotenv
|
| 15 |
-
|
|
|
|
| 12 |
pandas
|
| 13 |
matplotlib
|
| 14 |
python-dotenv
|
| 15 |
+
dropbox
|