Spaces:
Sleeping
Sleeping
Sai809701
commited on
Commit
Β·
ddffd31
1
Parent(s):
5d7c99a
fixed main.py
Browse files
main.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
from fastapi import FastAPI
|
| 5 |
from pydantic import BaseModel
|
|
@@ -10,7 +8,6 @@ from sentence_transformers import SentenceTransformer
|
|
| 10 |
from typing import List, Optional
|
| 11 |
|
| 12 |
# --- Configuration ---
|
| 13 |
-
# Using environment variables is best practice for production.
|
| 14 |
MONGO_URI = os.getenv("MONGO_URI", "mongodb+srv://saisunil22ecs:9m2ajd0GxVn43Fbu@majorproject.g0g1as0.mongodb.net/?retryWrites=true&w=majority&appName=MajorProject")
|
| 15 |
DB_NAME = os.getenv("MONGO_DB", "legal_chatbot_db")
|
| 16 |
COLLECTION_NAME = os.getenv("MONGO_COLLECTION", "datasets")
|
|
@@ -19,7 +16,6 @@ EMBED_MODEL = os.getenv("EMBED_MODEL", "sentence-transformers/all-MiniLM-L6-v2")
|
|
| 19 |
|
| 20 |
# --- Resource Loading ---
|
| 21 |
def load_resources():
|
| 22 |
-
"""A single function to load all models and the database connection at startup."""
|
| 23 |
try:
|
| 24 |
print("π Loading intent classification model...")
|
| 25 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
|
@@ -27,7 +23,13 @@ def load_resources():
|
|
| 27 |
print("β
Intent model loaded.")
|
| 28 |
|
| 29 |
print("π Loading embedding model for vector search...")
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
print("β
Embedding model loaded.")
|
| 32 |
|
| 33 |
print("π Connecting to MongoDB Atlas...")
|
|
@@ -43,7 +45,6 @@ def load_resources():
|
|
| 43 |
return None, None, None, None
|
| 44 |
|
| 45 |
tokenizer, intent_model, embedding_model, collection = load_resources()
|
| 46 |
-
|
| 47 |
# --- FastAPI App ---
|
| 48 |
app = FastAPI(title="Legal Aid Chatbot API")
|
| 49 |
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from fastapi import FastAPI
|
| 3 |
from pydantic import BaseModel
|
|
|
|
| 8 |
from typing import List, Optional
|
| 9 |
|
| 10 |
# --- Configuration ---
|
|
|
|
| 11 |
MONGO_URI = os.getenv("MONGO_URI", "mongodb+srv://saisunil22ecs:9m2ajd0GxVn43Fbu@majorproject.g0g1as0.mongodb.net/?retryWrites=true&w=majority&appName=MajorProject")
|
| 12 |
DB_NAME = os.getenv("MONGO_DB", "legal_chatbot_db")
|
| 13 |
COLLECTION_NAME = os.getenv("MONGO_COLLECTION", "datasets")
|
|
|
|
| 16 |
|
| 17 |
# --- Resource Loading ---
|
| 18 |
def load_resources():
|
|
|
|
| 19 |
try:
|
| 20 |
print("π Loading intent classification model...")
|
| 21 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
|
|
|
| 23 |
print("β
Intent model loaded.")
|
| 24 |
|
| 25 |
print("π Loading embedding model for vector search...")
|
| 26 |
+
|
| 27 |
+
# --- THIS IS THE FIX ---
|
| 28 |
+
# We specify a local cache directory where the app has write permissions.
|
| 29 |
+
cache_dir = "./model_cache"
|
| 30 |
+
embedding_model = SentenceTransformer(EMBED_MODEL, cache_folder=cache_dir)
|
| 31 |
+
# ---
|
| 32 |
+
|
| 33 |
print("β
Embedding model loaded.")
|
| 34 |
|
| 35 |
print("π Connecting to MongoDB Atlas...")
|
|
|
|
| 45 |
return None, None, None, None
|
| 46 |
|
| 47 |
tokenizer, intent_model, embedding_model, collection = load_resources()
|
|
|
|
| 48 |
# --- FastAPI App ---
|
| 49 |
app = FastAPI(title="Legal Aid Chatbot API")
|
| 50 |
|