chaithanyashaji commited on
Commit
b7ff65c
·
verified ·
1 Parent(s): 1fc09b0

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +41 -19
main.py CHANGED
@@ -13,6 +13,7 @@ from pydantic import BaseModel
13
  import os
14
  from dotenv import load_dotenv
15
  import warnings
 
16
 
17
  # Logging configuration
18
  logging.basicConfig(level=logging.DEBUG)
@@ -27,41 +28,56 @@ warnings.filterwarnings("ignore", category=DeprecationWarning)
27
 
28
  # Load environment variables
29
  load_dotenv()
 
 
30
 
31
- # Set HF_HOME for Hugging Face cache directory
32
- HF_HOME = os.getenv("HF_HOME", "/tmp/cache")
33
  os.environ["HF_HOME"] = HF_HOME
34
 
35
- # Ensure the directory exists
36
- os.makedirs(HF_HOME, exist_ok=True)
 
37
 
38
- # Validate Together API environment variable
39
- TOGETHER_AI_API = os.getenv("TOGETHER_AI")
40
  if not TOGETHER_AI_API:
41
  raise ValueError("Environment variable TOGETHER_AI_API is missing. Please set it in your .env file.")
42
 
43
- # Initialize embeddings and vectorstore
44
- embeddings = HuggingFaceEmbeddings(
45
- model_name="nomic-ai/nomic-embed-text-v1",
46
- model_kwargs={"trust_remote_code": True, "revision": "289f532e14dbbbd5a04753fa58739e9ba766f3c7"},
47
- )
 
 
 
 
48
 
49
- # Ensure FAISS vectorstore is loaded properly
50
  try:
51
  db = FAISS.load_local("ipc_vector_db", embeddings, allow_dangerous_deserialization=True)
52
  db_retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 2, "max_length": 512})
53
  except Exception as e:
54
  logger.error(f"Error loading FAISS vectorstore: {e}")
55
- raise RuntimeError("FAISS vectorstore could not be loaded. Ensure the vector database exists.")
 
 
 
 
 
 
 
 
 
 
56
 
57
  # Define the prompt template
58
  prompt_template = """<s>[INST]As a legal chatbot specializing in the Indian Penal Code, provide a concise and accurate answer based on the given context. Avoid unnecessary details or unrelated content. Only respond if the answer can be derived from the provided context; otherwise, say "The information is not available in the provided context."
59
- CONTEXT: {context}
60
- CHAT HISTORY: {chat_history}
61
- QUESTION: {question}
62
- ANSWER:
63
- </s>[INST]
64
- """
65
  prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question", "chat_history"])
66
 
67
  # Initialize the Together API
@@ -111,3 +127,9 @@ async def chat(request: ChatRequest):
111
  except Exception as e:
112
  logger.error(f"Error during chat invocation: {e}")
113
  raise HTTPException(status_code=500, detail="Internal server error")
 
 
 
 
 
 
 
13
  import os
14
  from dotenv import load_dotenv
15
  import warnings
16
+ import uvicorn
17
 
18
  # Logging configuration
19
  logging.basicConfig(level=logging.DEBUG)
 
28
 
29
  # Load environment variables
30
  load_dotenv()
31
+ TOGETHER_AI_API = os.getenv("TOGETHER_AI")
32
+ HF_HOME = os.getenv("HF_HOME", "./cache")
33
 
34
+ # Set cache directory for Hugging Face
 
35
  os.environ["HF_HOME"] = HF_HOME
36
 
37
+ # Ensure HF_HOME exists and is writable
38
+ if not os.path.exists(HF_HOME):
39
+ os.makedirs(HF_HOME, exist_ok=True)
40
 
41
+ # Validate environment variables
 
42
  if not TOGETHER_AI_API:
43
  raise ValueError("Environment variable TOGETHER_AI_API is missing. Please set it in your .env file.")
44
 
45
+ # Initialize embeddings
46
+ try:
47
+ embeddings = HuggingFaceEmbeddings(
48
+ model_name="nomic-ai/nomic-embed-text-v1",
49
+ model_kwargs={"trust_remote_code": True},
50
+ )
51
+ except Exception as e:
52
+ logger.error(f"Error loading embeddings: {e}")
53
+ raise RuntimeError("Error initializing HuggingFaceEmbeddings.")
54
 
55
+ # Ensure FAISS vectorstore is loaded or created
56
  try:
57
  db = FAISS.load_local("ipc_vector_db", embeddings, allow_dangerous_deserialization=True)
58
  db_retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 2, "max_length": 512})
59
  except Exception as e:
60
  logger.error(f"Error loading FAISS vectorstore: {e}")
61
+ # If not found, create a new vectorstore
62
+ try:
63
+ loader = DirectoryLoader('./data')
64
+ text_splitter = RecursiveCharacterTextSplitter()
65
+ documents = text_splitter.split_documents(loader.load())
66
+ db = FAISS.from_documents(documents, embeddings)
67
+ db.save_local("ipc_vector_db")
68
+ db_retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 2, "max_length": 512})
69
+ except Exception as inner_e:
70
+ logger.error(f"Error creating FAISS vectorstore: {inner_e}")
71
+ raise RuntimeError("FAISS vectorstore could not be created or loaded.")
72
 
73
  # Define the prompt template
74
  prompt_template = """<s>[INST]As a legal chatbot specializing in the Indian Penal Code, provide a concise and accurate answer based on the given context. Avoid unnecessary details or unrelated content. Only respond if the answer can be derived from the provided context; otherwise, say "The information is not available in the provided context."
75
+ CONTEXT: {context}
76
+ CHAT HISTORY: {chat_history}
77
+ QUESTION: {question}
78
+ ANSWER:
79
+ </s>[INST]
80
+ """
81
  prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question", "chat_history"])
82
 
83
  # Initialize the Together API
 
127
  except Exception as e:
128
  logger.error(f"Error during chat invocation: {e}")
129
  raise HTTPException(status_code=500, detail="Internal server error")
130
+
131
+ # Start Uvicorn server if run directly
132
+ if __name__ == "__main__":
133
+ ENV = os.getenv("ENV", "prod")
134
+ PORT = int(os.environ.get("PORT", 10000)) # Use the default port 10000 or the environment port
135
+ uvicorn.run("main:app", host="0.0.0.0", port=PORT, reload=(ENV == "dev"))