Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -1,30 +1,25 @@
|
|
1 |
import logging
|
2 |
import os
|
3 |
import warnings
|
4 |
-
from dotenv import load_dotenv
|
5 |
from fastapi import FastAPI, HTTPException
|
6 |
from pydantic import BaseModel
|
7 |
-
import
|
8 |
-
from langchain_community.document_loaders import DirectoryLoader
|
9 |
from langchain_huggingface import HuggingFaceEmbeddings
|
10 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
11 |
from langchain_community.vectorstores import FAISS
|
12 |
from langchain.prompts import PromptTemplate
|
13 |
from langchain_together import Together
|
14 |
-
|
15 |
-
from langchain.chains import ConversationalRetrievalChain
|
16 |
|
17 |
# ==========================
|
18 |
-
# Logging
|
19 |
# ==========================
|
20 |
-
logging.basicConfig(level=logging.DEBUG)
|
21 |
-
logger = logging.getLogger(
|
22 |
-
logger.debug("Initializing Legal Chatbot application...")
|
23 |
|
24 |
# ==========================
|
25 |
# Suppress Warnings
|
26 |
# ==========================
|
27 |
-
warnings.filterwarnings("ignore"
|
28 |
|
29 |
# ==========================
|
30 |
# Load Environment Variables
|
@@ -33,16 +28,19 @@ load_dotenv()
|
|
33 |
TOGETHER_AI_API = os.getenv("TOGETHER_AI")
|
34 |
HF_HOME = os.getenv("HF_HOME", "./cache")
|
35 |
os.environ["HF_HOME"] = HF_HOME
|
36 |
-
|
37 |
-
# Ensure the HF_HOME directory exists
|
38 |
os.makedirs(HF_HOME, exist_ok=True)
|
39 |
|
40 |
-
# Validate required environment variables
|
41 |
if not TOGETHER_AI_API:
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
# ==========================
|
45 |
-
#
|
46 |
# ==========================
|
47 |
try:
|
48 |
embeddings = HuggingFaceEmbeddings(
|
@@ -50,33 +48,31 @@ try:
|
|
50 |
model_kwargs={"trust_remote_code": True, "revision": "289f532e14dbbbd5a04753fa58739e9ba766f3c7"},
|
51 |
)
|
52 |
logger.info("Embeddings successfully initialized.")
|
53 |
-
except Exception as e:
|
54 |
-
logger.error(f"Error initializing embeddings: {e}")
|
55 |
-
raise RuntimeError("Oops! Something went wrong while setting up embeddings. Please check the configuration and try again.")
|
56 |
|
57 |
-
#
|
58 |
-
|
59 |
-
# ==========================
|
60 |
-
try:
|
61 |
db = FAISS.load_local("ipc_vector_db", embeddings, allow_dangerous_deserialization=True)
|
62 |
-
|
63 |
-
|
|
|
64 |
except Exception as e:
|
65 |
-
logger.error(f"Error
|
66 |
-
raise RuntimeError("
|
67 |
|
68 |
# ==========================
|
69 |
-
#
|
70 |
# ==========================
|
71 |
-
prompt_template = """<s>[INST]
|
72 |
-
|
|
|
|
|
73 |
CONTEXT: {context}
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
|
79 |
-
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"
|
80 |
|
81 |
# ==========================
|
82 |
# Initialize Together API
|
@@ -91,25 +87,48 @@ try:
|
|
91 |
logger.info("Together API successfully initialized.")
|
92 |
except Exception as e:
|
93 |
logger.error(f"Error initializing Together API: {e}")
|
94 |
-
raise RuntimeError("Something went wrong with the Together API setup. Please verify your API key
|
95 |
|
96 |
# ==========================
|
97 |
-
#
|
98 |
# ==========================
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
)
|
106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
# ==========================
|
109 |
-
# FastAPI
|
110 |
# ==========================
|
111 |
-
app = FastAPI()
|
112 |
-
|
113 |
class ChatRequest(BaseModel):
|
114 |
question: str
|
115 |
|
@@ -118,41 +137,23 @@ class ChatResponse(BaseModel):
|
|
118 |
|
119 |
@app.get("/")
|
120 |
async def root():
|
121 |
-
return {
|
|
|
|
|
122 |
|
123 |
@app.post("/chat", response_model=ChatResponse)
|
124 |
async def chat(request: ChatRequest):
|
125 |
try:
|
126 |
-
logger.debug(f"
|
127 |
-
|
128 |
-
|
129 |
-
retrieved_docs = db_retriever.invoke({"query": request.question})
|
130 |
-
logger.debug("Retrieved Documents and Scores:")
|
131 |
-
for i, doc in enumerate(retrieved_docs["documents"], start=1):
|
132 |
-
logger.debug(f"Document {i}: {doc.page_content[:500]}...")
|
133 |
-
logger.debug(f"Score: {retrieved_docs['scores'][i-1]}")
|
134 |
-
|
135 |
-
# Invoke the QA chain with the user question
|
136 |
-
result = qa.invoke(input=request.question)
|
137 |
-
if isinstance(result, dict) and "answer" in result:
|
138 |
-
answer = result["answer"]
|
139 |
-
else:
|
140 |
-
answer = "I'm sorry, I couldn't find relevant information for your query."
|
141 |
-
|
142 |
-
|
143 |
-
if not answer or "The information is not available in the provided context" in answer:
|
144 |
-
answer = "I'm sorry, I couldn't find relevant information for your query. Please try rephrasing or providing more details."
|
145 |
-
|
146 |
-
# Log the final answer
|
147 |
-
logger.debug(f"Chatbot Answer: {answer}")
|
148 |
-
|
149 |
return ChatResponse(answer=answer)
|
150 |
except Exception as e:
|
151 |
-
logger.error(f"Error
|
152 |
-
raise HTTPException(status_code=500, detail="
|
153 |
|
154 |
# ==========================
|
155 |
# Run Uvicorn Server
|
156 |
# ==========================
|
157 |
if __name__ == "__main__":
|
158 |
-
uvicorn.run("main:app", host="0.0.0.0", port=7860)
|
|
|
1 |
import logging
|
2 |
import os
|
3 |
import warnings
|
|
|
4 |
from fastapi import FastAPI, HTTPException
|
5 |
from pydantic import BaseModel
|
6 |
+
from dotenv import load_dotenv
|
|
|
7 |
from langchain_huggingface import HuggingFaceEmbeddings
|
|
|
8 |
from langchain_community.vectorstores import FAISS
|
9 |
from langchain.prompts import PromptTemplate
|
10 |
from langchain_together import Together
|
11 |
+
import uvicorn
|
|
|
12 |
|
13 |
# ==========================
|
14 |
+
# Logging Setup
|
15 |
# ==========================
|
16 |
+
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s")
|
17 |
+
logger = logging.getLogger(__name__)
|
|
|
18 |
|
19 |
# ==========================
|
20 |
# Suppress Warnings
|
21 |
# ==========================
|
22 |
+
warnings.filterwarnings("ignore")
|
23 |
|
24 |
# ==========================
|
25 |
# Load Environment Variables
|
|
|
28 |
TOGETHER_AI_API = os.getenv("TOGETHER_AI")
|
29 |
HF_HOME = os.getenv("HF_HOME", "./cache")
|
30 |
os.environ["HF_HOME"] = HF_HOME
|
|
|
|
|
31 |
os.makedirs(HF_HOME, exist_ok=True)
|
32 |
|
|
|
33 |
if not TOGETHER_AI_API:
|
34 |
+
logger.error("TOGETHER_AI_API key is missing. Please set it in the environment variables.")
|
35 |
+
raise RuntimeError("API key not found. Set TOGETHER_AI_API in .env.")
|
36 |
+
|
37 |
+
# ==========================
|
38 |
+
# App Initialization
|
39 |
+
# ==========================
|
40 |
+
app = FastAPI()
|
41 |
|
42 |
# ==========================
|
43 |
+
# Load Existing IPC Vectorstore
|
44 |
# ==========================
|
45 |
try:
|
46 |
embeddings = HuggingFaceEmbeddings(
|
|
|
48 |
model_kwargs={"trust_remote_code": True, "revision": "289f532e14dbbbd5a04753fa58739e9ba766f3c7"},
|
49 |
)
|
50 |
logger.info("Embeddings successfully initialized.")
|
|
|
|
|
|
|
51 |
|
52 |
+
# Load the pre-existing IPC vector store directly
|
53 |
+
logger.info("Loading existing IPC vectorstore.")
|
|
|
|
|
54 |
db = FAISS.load_local("ipc_vector_db", embeddings, allow_dangerous_deserialization=True)
|
55 |
+
|
56 |
+
db_retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 5})
|
57 |
+
logger.info("IPC Vectorstore successfully loaded.")
|
58 |
except Exception as e:
|
59 |
+
logger.error(f"Error during vectorstore setup: {e}")
|
60 |
+
raise RuntimeError("Initialization failed. Please check your embeddings or vectorstore setup.")
|
61 |
|
62 |
# ==========================
|
63 |
+
# Prompt Template (Context-Only)
|
64 |
# ==========================
|
65 |
+
prompt_template = """<s>[INST]
|
66 |
+
You are a legal assistant specializing in the Indian Penal Code (IPC). Use only the provided CONTEXT to answer questions.
|
67 |
+
If the information is not found in the CONTEXT, respond with: "I don't have enough information yet."
|
68 |
+
Do not use any outside knowledge.
|
69 |
CONTEXT: {context}
|
70 |
+
USER QUERY: {question}
|
71 |
+
RESPONSE:
|
72 |
+
</s>[INST]
|
73 |
+
"""
|
74 |
|
75 |
+
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
76 |
|
77 |
# ==========================
|
78 |
# Initialize Together API
|
|
|
87 |
logger.info("Together API successfully initialized.")
|
88 |
except Exception as e:
|
89 |
logger.error(f"Error initializing Together API: {e}")
|
90 |
+
raise RuntimeError("Something went wrong with the Together API setup. Please verify your API key.")
|
91 |
|
92 |
# ==========================
|
93 |
+
# Chat Processing Function
|
94 |
# ==========================
|
95 |
+
def generate_response(user_query: str) -> str:
|
96 |
+
try:
|
97 |
+
# Retrieve relevant documents
|
98 |
+
retrieved_docs = db_retriever.get_relevant_documents(user_query)
|
99 |
+
|
100 |
+
# Log retrieved documents
|
101 |
+
logger.info(f"User Query: {user_query}")
|
102 |
+
for i, doc in enumerate(retrieved_docs):
|
103 |
+
logger.info(f"Document {i + 1}: {doc.page_content[:500]}...")
|
104 |
+
|
105 |
+
# Prepare context for the LLM
|
106 |
+
context = "\n\n".join(doc.page_content for doc in retrieved_docs)
|
107 |
+
|
108 |
+
# Check if context is empty
|
109 |
+
if not context.strip():
|
110 |
+
return "I don't have enough information yet."
|
111 |
+
|
112 |
+
# Construct LLM prompt input
|
113 |
+
prompt_input = {"context": context, "question": user_query}
|
114 |
+
logger.debug(f"Payload sent to LLM: {prompt_input}")
|
115 |
+
|
116 |
+
# Generate response using the LLM
|
117 |
+
response = llm(prompt.format(**prompt_input))
|
118 |
+
|
119 |
+
# Check if response is empty
|
120 |
+
if not response.strip():
|
121 |
+
return "I don't have enough information yet."
|
122 |
+
|
123 |
+
return response
|
124 |
+
|
125 |
+
except Exception as e:
|
126 |
+
logger.error(f"Error generating response: {e}")
|
127 |
+
return "An error occurred while generating the response."
|
128 |
|
129 |
# ==========================
|
130 |
+
# FastAPI Models and Endpoints
|
131 |
# ==========================
|
|
|
|
|
132 |
class ChatRequest(BaseModel):
|
133 |
question: str
|
134 |
|
|
|
137 |
|
138 |
@app.get("/")
|
139 |
async def root():
|
140 |
+
return {
|
141 |
+
"message": "Welcome to the Legal Chatbot! Ask me questions about the Indian Penal Code (IPC)."
|
142 |
+
}
|
143 |
|
144 |
@app.post("/chat", response_model=ChatResponse)
|
145 |
async def chat(request: ChatRequest):
|
146 |
try:
|
147 |
+
logger.debug(f"User question received: {request.question}")
|
148 |
+
answer = generate_response(request.question)
|
149 |
+
logger.debug(f"Chatbot response: {answer}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
return ChatResponse(answer=answer)
|
151 |
except Exception as e:
|
152 |
+
logger.error(f"Error processing chat request: {e}")
|
153 |
+
raise HTTPException(status_code=500, detail="An internal error occurred. Please try again later.")
|
154 |
|
155 |
# ==========================
|
156 |
# Run Uvicorn Server
|
157 |
# ==========================
|
158 |
if __name__ == "__main__":
|
159 |
+
uvicorn.run("main:app", host="0.0.0.0", port=7860, reload=True)
|