Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
from langchain_groq import ChatGroq
|
4 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
5 |
+
from langchain.chains.combine_documents import create_stuff_documents_chain
|
6 |
+
from langchain_core.prompts import ChatPromptTemplate
|
7 |
+
from langchain.chains import create_retrieval_chain
|
8 |
+
from langchain_community.vectorstores import FAISS
|
9 |
+
from langchain_community.document_loaders import PyPDFDirectoryLoader
|
10 |
+
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
11 |
+
from dotenv import load_dotenv
|
12 |
+
import os
|
13 |
+
load_dotenv()
|
14 |
+
|
15 |
+
## load the GROQ And OpenAI API KEY
|
16 |
+
groq_api_key=os.getenv('GROQ_API_KEY')
|
17 |
+
os.environ["GOOGLE_API_KEY"]=os.getenv("GOOGLE_API_KEY")
|
18 |
+
|
19 |
+
st.title("Gemma Model Document Q&A")
|
20 |
+
|
21 |
+
llm=ChatGroq(groq_api_key=groq_api_key,
|
22 |
+
model_name="Llama3-8b-8192")
|
23 |
+
prompt=ChatPromptTemplate.from_template(
|
24 |
+
"""
|
25 |
+
1. Check Bharatiya Nyaya Sanhita Section: First, analyze the given input and determine which section of the Bharatiya Nyaya Sanhita (BNS) it fits into. Check input properly because some words may not match with Bharatiya Nyaya Sanhita terminology, so you still need to understand the context.
|
26 |
+
2. Register FIR: After determining the appropriate BNS section, confirm the registration of the First Information Report (FIR) under that section.
|
27 |
+
3. Outline Procedure: Provide a brief outline of the procedure that will follow after the FIR registration, based on the identified BNS section.
|
28 |
+
4. Punishment: Specify the punishment associated with the identified BNS section.
|
29 |
+
5. Additional Recommendations: Suggest any specific actions or precautions that the police should take based on the nature of the case.
|
30 |
+
In final response Don't use IPC or indian penal code always use BNS, Bharatiya Nyaya Sanhita and you will only provide in details: {input}, Crime input, FIR Register under which section, Outline Procedure, Punishment, and Additional Recommendations.
|
31 |
+
<context>
|
32 |
+
{context}
|
33 |
+
<context>
|
34 |
+
|
35 |
+
Questions: {input}
|
36 |
+
"""
|
37 |
+
)
|
38 |
+
|
39 |
+
|
40 |
+
def vector_embedding():
|
41 |
+
|
42 |
+
if "vectors" not in st.session_state:
|
43 |
+
|
44 |
+
st.session_state.embeddings=GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
|
45 |
+
st.session_state.loader=PyPDFDirectoryLoader("./pdf_data") ## Data Ingestion
|
46 |
+
st.session_state.docs=st.session_state.loader.load() ## Document Loading
|
47 |
+
st.session_state.text_splitter=RecursiveCharacterTextSplitter(chunk_size=1000,chunk_overlap=200) ## Chunk Creation
|
48 |
+
st.session_state.final_documents=st.session_state.text_splitter.split_documents(st.session_state.docs[:20]) #splitting
|
49 |
+
st.session_state.vectors=FAISS.from_documents(st.session_state.final_documents,st.session_state.embeddings) #vector OpenAI embeddings
|
50 |
+
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
+
|
55 |
+
prompt1=st.text_input("Enter Your Question From Doduments")
|
56 |
+
|
57 |
+
|
58 |
+
if st.button("Documents Embedding"):
|
59 |
+
vector_embedding()
|
60 |
+
st.write("Vector Store DB Is Ready")
|
61 |
+
|
62 |
+
import time
|
63 |
+
|
64 |
+
|
65 |
+
|
66 |
+
if prompt1:
|
67 |
+
document_chain=create_stuff_documents_chain(llm,prompt)
|
68 |
+
retriever=st.session_state.vectors.as_retriever()
|
69 |
+
retrieval_chain=create_retrieval_chain(retriever,document_chain)
|
70 |
+
start=time.process_time()
|
71 |
+
response=retrieval_chain.invoke({'input':prompt1})
|
72 |
+
print("Response time :",time.process_time()-start)
|
73 |
+
st.write(response['answer'])
|
74 |
+
|
75 |
+
# With a streamlit expander
|
76 |
+
with st.expander("Document Similarity Search"):
|
77 |
+
# Find the relevant chunks
|
78 |
+
for i, doc in enumerate(response["context"]):
|
79 |
+
st.write(doc.page_content)
|
80 |
+
st.write("--------------------------------")
|
81 |
+
|
82 |
+
|
83 |
+
|