Spaces:
Sleeping
Sleeping
Upload 11 files
Browse files- .env +1 -0
- .gitattributes +2 -0
- .idea/.gitignore +8 -0
- .idea/workspace.xml +8 -0
- Dockerfile +20 -0
- __pycache__/main.cpython-311.pyc +0 -0
- ipc-data/Indian Penal Code Book.pdf +3 -0
- ipc_vector_db/index.faiss +3 -0
- ipc_vector_db/index.pkl +3 -0
- main.py +105 -0
- requirements.txt +13 -0
- test_main.http +11 -0
.env
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
TOGETHER_AI=92b5eec828c7ac0c34b60bf5daf97aebc2594196c5005e6c0d9ec2826e3eee11
|
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
ipc_vector_db/index.faiss filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
ipc-data/Indian[[:space:]]Penal[[:space:]]Code[[:space:]]Book.pdf filter=lfs diff=lfs merge=lfs -text
|
.idea/.gitignore
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Default ignored files
|
| 2 |
+
/shelf/
|
| 3 |
+
/workspace.xml
|
| 4 |
+
# Editor-based HTTP Client requests
|
| 5 |
+
/httpRequests/
|
| 6 |
+
# Datasource local storage ignored files
|
| 7 |
+
/dataSources/
|
| 8 |
+
/dataSources.local.xml
|
.idea/workspace.xml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="ProjectViewState">
|
| 4 |
+
<option name="hideEmptyMiddlePackages" value="true" />
|
| 5 |
+
<option name="showLibraryContents" value="true" />
|
| 6 |
+
</component>
|
| 7 |
+
<component name="PropertiesComponent">{}</component>
|
| 8 |
+
</project>
|
Dockerfile
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Base image
|
| 2 |
+
FROM python:3.10-slim
|
| 3 |
+
|
| 4 |
+
# Set working directory
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy project files
|
| 8 |
+
COPY . .
|
| 9 |
+
|
| 10 |
+
# Install system dependencies
|
| 11 |
+
RUN apt-get update && apt-get install -y libfaiss-dev && rm -rf /var/lib/apt/lists/*
|
| 12 |
+
|
| 13 |
+
# Install Python dependencies
|
| 14 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 15 |
+
|
| 16 |
+
# Expose the application port
|
| 17 |
+
EXPOSE 8000
|
| 18 |
+
|
| 19 |
+
# Run the application
|
| 20 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|
__pycache__/main.cpython-311.pyc
ADDED
|
Binary file (6.07 kB). View file
|
|
|
ipc-data/Indian Penal Code Book.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5706a1b995df774c4c4ea1868223e18a13ba619977d323d3cab76a1cc095e237
|
| 3 |
+
size 20095787
|
ipc_vector_db/index.faiss
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d95e6cc1b7e77537e6bff530fa443dfcf2638efcb858eccf44de03185fc52c7c
|
| 3 |
+
size 18284589
|
ipc_vector_db/index.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:88316941d341e914e168e058c26609184e62baf5223b96b510ec65a1da8313cd
|
| 3 |
+
size 5879209
|
main.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
from langchain_community.document_loaders import PyPDFLoader, DirectoryLoader
|
| 3 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 6 |
+
from langchain_community.vectorstores import FAISS
|
| 7 |
+
from langchain.prompts import PromptTemplate
|
| 8 |
+
from langchain_together import Together
|
| 9 |
+
from langchain.memory import ConversationBufferMemory
|
| 10 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 11 |
+
from fastapi import FastAPI, HTTPException
|
| 12 |
+
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)
|
| 19 |
+
logger = logging.getLogger(__name__)
|
| 20 |
+
logger.debug("Starting FastAPI app...")
|
| 21 |
+
|
| 22 |
+
# Suppress warnings
|
| 23 |
+
warnings.filterwarnings("ignore", message="You are using `torch.load` with `weights_only=False`")
|
| 24 |
+
warnings.filterwarnings("ignore", message="Tried to instantiate class '__path__._path'")
|
| 25 |
+
warnings.filterwarnings("ignore", category=FutureWarning)
|
| 26 |
+
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
| 27 |
+
|
| 28 |
+
# Load environment variables
|
| 29 |
+
load_dotenv()
|
| 30 |
+
TOGETHER_AI_API = os.getenv("TOGETHER_AI")
|
| 31 |
+
|
| 32 |
+
if not TOGETHER_AI_API:
|
| 33 |
+
raise ValueError("Environment variable TOGETHER_AI_API is missing. Please set it in your .env file.")
|
| 34 |
+
|
| 35 |
+
# Initialize embeddings and vectorstore
|
| 36 |
+
embeddings = HuggingFaceEmbeddings(
|
| 37 |
+
model_name="nomic-ai/nomic-embed-text-v1",
|
| 38 |
+
model_kwargs={"trust_remote_code": True, "revision": "289f532e14dbbbd5a04753fa58739e9ba766f3c7"},
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Ensure FAISS vectorstore is loaded properly
|
| 42 |
+
try:
|
| 43 |
+
db = FAISS.load_local("ipc_vector_db", embeddings, allow_dangerous_deserialization=True)
|
| 44 |
+
db_retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 2, "max_length": 512})
|
| 45 |
+
except Exception as e:
|
| 46 |
+
logger.error(f"Error loading FAISS vectorstore: {e}")
|
| 47 |
+
raise RuntimeError("FAISS vectorstore could not be loaded. Ensure the vector database exists.")
|
| 48 |
+
|
| 49 |
+
# Define the prompt template
|
| 50 |
+
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."
|
| 51 |
+
CONTEXT: {context}
|
| 52 |
+
CHAT HISTORY: {chat_history}
|
| 53 |
+
QUESTION: {question}
|
| 54 |
+
ANSWER:
|
| 55 |
+
</s>[INST]
|
| 56 |
+
"""
|
| 57 |
+
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question", "chat_history"])
|
| 58 |
+
|
| 59 |
+
# Initialize the Together API
|
| 60 |
+
try:
|
| 61 |
+
llm = Together(
|
| 62 |
+
model="mistralai/Mistral-7B-Instruct-v0.2",
|
| 63 |
+
temperature=0.5,
|
| 64 |
+
max_tokens=1024,
|
| 65 |
+
together_api_key=TOGETHER_AI_API,
|
| 66 |
+
)
|
| 67 |
+
except Exception as e:
|
| 68 |
+
logger.error(f"Error initializing Together API: {e}")
|
| 69 |
+
raise RuntimeError("Together API could not be initialized. Check your API key and network connection.")
|
| 70 |
+
|
| 71 |
+
# Initialize conversational retrieval chain
|
| 72 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 73 |
+
qa = ConversationalRetrievalChain.from_llm(
|
| 74 |
+
llm=llm,
|
| 75 |
+
memory=memory,
|
| 76 |
+
retriever=db_retriever,
|
| 77 |
+
combine_docs_chain_kwargs={"prompt": prompt},
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
# Initialize FastAPI app
|
| 81 |
+
app = FastAPI()
|
| 82 |
+
|
| 83 |
+
# Define request and response models
|
| 84 |
+
class ChatRequest(BaseModel):
|
| 85 |
+
question: str
|
| 86 |
+
|
| 87 |
+
class ChatResponse(BaseModel):
|
| 88 |
+
answer: str
|
| 89 |
+
|
| 90 |
+
# Health check endpoint
|
| 91 |
+
@app.get("/")
|
| 92 |
+
async def root():
|
| 93 |
+
return {"message": "Hello, World!"}
|
| 94 |
+
|
| 95 |
+
# Chat endpoint
|
| 96 |
+
@app.post("/chat", response_model=ChatResponse)
|
| 97 |
+
async def chat(request: ChatRequest):
|
| 98 |
+
try:
|
| 99 |
+
# Pass the user question
|
| 100 |
+
result = qa.invoke(input=request.question)
|
| 101 |
+
answer = result.get("answer", "The chatbot could not generate a response.")
|
| 102 |
+
return ChatResponse(answer=answer)
|
| 103 |
+
except Exception as e:
|
| 104 |
+
logger.error(f"Error during chat invocation: {e}")
|
| 105 |
+
raise HTTPException(status_code=500, detail="Internal server error")
|
requirements.txt
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.115.6
|
| 2 |
+
langchain==0.3.14
|
| 3 |
+
langchain_community==0.3.14
|
| 4 |
+
langchain_together==0.2.0
|
| 5 |
+
python-dotenv==1.0.1
|
| 6 |
+
transformers==4.47.1
|
| 7 |
+
uvicorn==0.34.0
|
| 8 |
+
sentence-transformers==3.3.1
|
| 9 |
+
einops==0.8.0
|
| 10 |
+
faiss-cpu==1.9.0.post1
|
| 11 |
+
langchain_huggingface==0.1.2
|
| 12 |
+
torch==2.5.1
|
| 13 |
+
requests==2.32.3
|
test_main.http
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Test your FastAPI endpoints
|
| 2 |
+
|
| 3 |
+
GET http://127.0.0.1:8000/
|
| 4 |
+
Accept: application/json
|
| 5 |
+
|
| 6 |
+
###
|
| 7 |
+
|
| 8 |
+
GET http://127.0.0.1:8000/hello/User
|
| 9 |
+
Accept: application/json
|
| 10 |
+
|
| 11 |
+
###
|