Upload 3 files
Browse files- README.md +11 -11
- app.py +84 -0
- requirements.txt +10 -0
README.md
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
-
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
-
sdk: docker
|
7 |
-
pinned: false
|
8 |
-
license:
|
9 |
-
---
|
10 |
-
|
11 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
+
---
|
2 |
+
title: JudicaBOT
|
3 |
+
emoji: 🐢
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: gray
|
6 |
+
sdk: docker
|
7 |
+
pinned: false
|
8 |
+
license: apache-2.0
|
9 |
+
---
|
10 |
+
|
11 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import APIRouter, HTTPException
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from pathlib import Path
|
4 |
+
import os
|
5 |
+
from langchain_community.vectorstores import FAISS
|
6 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
7 |
+
from langchain.prompts import PromptTemplate
|
8 |
+
from langchain_together import Together
|
9 |
+
from langchain.memory import ConversationBufferWindowMemory
|
10 |
+
from langchain.chains import ConversationalRetrievalChain
|
11 |
+
|
12 |
+
# Set the API key for Together.ai
|
13 |
+
TOGETHER_AI_API = os.getenv("TOGETHER_AI_API", "1c27fe0df51a29edee1bec6b4b648b436cc80cf4ccc36f56de17272d9e663cbd")
|
14 |
+
|
15 |
+
# Ensure proper cache directory is available for models
|
16 |
+
os.environ['TRANSFORMERS_CACHE'] = '/tmp/cache'
|
17 |
+
|
18 |
+
# Initialize FastAPI Router
|
19 |
+
app = APIRouter()
|
20 |
+
|
21 |
+
# Lazy loading of large models (only load embeddings and index when required)
|
22 |
+
embeddings = HuggingFaceEmbeddings(
|
23 |
+
model_name="nomic-ai/nomic-embed-text-v1",
|
24 |
+
model_kwargs={"trust_remote_code": True, "revision": "289f532e14dbbbd5a04753fa58739e9ba766f3c7"},
|
25 |
+
)
|
26 |
+
|
27 |
+
index_path = Path("index.faiss")
|
28 |
+
if not index_path.exists():
|
29 |
+
raise FileNotFoundError("FAISS index not found. Please generate it and place it in 'ipc_vector_db'.")
|
30 |
+
|
31 |
+
# Load the FAISS index
|
32 |
+
db = FAISS.load_local("AI LAW CHATBOT", embeddings, allow_dangerous_deserialization=True)
|
33 |
+
db_retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 4})
|
34 |
+
|
35 |
+
# Define the prompt template for the legal chatbot
|
36 |
+
prompt_template = """<s>[INST]This is a chat template and as a legal chatbot specializing in Indian Penal Code queries, your objective is to provide accurate and concise information.
|
37 |
+
CONTEXT: {context}
|
38 |
+
CHAT HISTORY: {chat_history}
|
39 |
+
QUESTION: {question}
|
40 |
+
ANSWER:
|
41 |
+
</s>[INST]"""
|
42 |
+
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question", "chat_history"])
|
43 |
+
|
44 |
+
# Set up the LLM (Large Language Model) for the chatbot
|
45 |
+
llm = Together(
|
46 |
+
model="mistralai/Mistral-7B-Instruct-v0.2",
|
47 |
+
temperature=0.5,
|
48 |
+
max_tokens=1024,
|
49 |
+
together_api_key=TOGETHER_AI_API,
|
50 |
+
)
|
51 |
+
|
52 |
+
# Set up memory for conversational context
|
53 |
+
memory = ConversationBufferWindowMemory(k=2, memory_key="chat_history", return_messages=True)
|
54 |
+
|
55 |
+
# Create the conversational retrieval chain with the LLM and retriever
|
56 |
+
qa_chain = ConversationalRetrievalChain.from_llm(
|
57 |
+
llm=llm,
|
58 |
+
memory=memory,
|
59 |
+
retriever=db_retriever,
|
60 |
+
combine_docs_chain_kwargs={"prompt": prompt},
|
61 |
+
)
|
62 |
+
|
63 |
+
# Input schema for chat requests
|
64 |
+
class ChatRequest(BaseModel):
|
65 |
+
question: str
|
66 |
+
chat_history: str
|
67 |
+
|
68 |
+
# POST endpoint to handle chat requests
|
69 |
+
@app.post("/chat/")
|
70 |
+
async def chat(request: ChatRequest):
|
71 |
+
try:
|
72 |
+
# Prepare the input data
|
73 |
+
inputs = {"question": request.question, "chat_history": request.chat_history}
|
74 |
+
# Run the chain to get the answer
|
75 |
+
result = qa_chain(inputs)
|
76 |
+
return {"answer": result["answer"]}
|
77 |
+
except Exception as e:
|
78 |
+
# Return an error if something goes wrong
|
79 |
+
raise HTTPException(status_code=500, detail=f"Error processing request: {str(e)}")
|
80 |
+
|
81 |
+
# GET endpoint to check if the API is running
|
82 |
+
@app.get("/")
|
83 |
+
async def root():
|
84 |
+
return {"message": "LawGPT API is running."}
|
requirements.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi>=0.100.0
|
2 |
+
uvicorn[standard]>=0.22.0
|
3 |
+
pydantic>=2.0.0
|
4 |
+
langchain_together
|
5 |
+
langchain-huggingface
|
6 |
+
langchain_community
|
7 |
+
einops
|
8 |
+
transformers
|
9 |
+
huggingface_hub
|
10 |
+
faiss-cpu
|