Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,7 @@
|
|
|
|
|
|
|
|
|
|
1 |
from langchain.vectorstores import Chroma
|
2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
from transformers import pipeline
|
@@ -5,111 +9,76 @@ import torch
|
|
5 |
from langchain.llms import HuggingFacePipeline
|
6 |
from langchain.embeddings import SentenceTransformerEmbeddings
|
7 |
from langchain.chains import RetrievalQA
|
8 |
-
from langchain_community.document_loaders import UnstructuredFileLoader
|
9 |
-
from langchain.text_splitter import CharacterTextSplitter
|
10 |
import streamlit as st
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
return qa_chain
|
84 |
-
|
85 |
-
st.title("Document Chatbot")
|
86 |
-
st.write("Upload a pdf file to get started")
|
87 |
-
|
88 |
-
uploaded_file = st.file_uploader("Choose a file", type=["pdf"])
|
89 |
-
|
90 |
-
if uploaded_file is not None:
|
91 |
-
qa_chain = main_process(uploaded_file)
|
92 |
-
if "messages" not in st.session_state:
|
93 |
-
st.session_state.messages = []
|
94 |
-
|
95 |
-
# Display chat messages from history on app rerun
|
96 |
-
for message in st.session_state.messages:
|
97 |
-
with st.chat_message(message["role"]):
|
98 |
-
st.markdown(message["content"])
|
99 |
-
|
100 |
-
# Accept user input
|
101 |
-
if prompt := st.chat_input("What is up?"):
|
102 |
-
# Add user message to chat history
|
103 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
104 |
-
# Display user message in chat message container
|
105 |
-
with st.chat_message("user"):
|
106 |
-
st.markdown(prompt)
|
107 |
-
# Get response from chatbot
|
108 |
-
with st.chat_message("assitant"):
|
109 |
-
response = qa_chain(prompt)
|
110 |
-
st.markdown(response)
|
111 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
|
|
1 |
+
# !pip install accelerate
|
2 |
+
# !pip install chromadb
|
3 |
+
# !pip install "unstructured[all-docs]"
|
4 |
+
|
5 |
from langchain.vectorstores import Chroma
|
6 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
7 |
from transformers import pipeline
|
|
|
9 |
from langchain.llms import HuggingFacePipeline
|
10 |
from langchain.embeddings import SentenceTransformerEmbeddings
|
11 |
from langchain.chains import RetrievalQA
|
|
|
|
|
12 |
import streamlit as st
|
13 |
+
|
14 |
+
|
15 |
+
embeddings = SentenceTransformerEmbeddings(model_name="multi-qa-mpnet-base-dot-v1")
|
16 |
+
persist_directory = "chroma"
|
17 |
+
|
18 |
+
# Persist the database to disk
|
19 |
+
db = Chroma(persist_directory,embeddings)
|
20 |
+
|
21 |
+
# To save and load the saved vector db (if needed in the future)
|
22 |
+
# Persist the database to disk
|
23 |
+
# db.persist()
|
24 |
+
# db = Chroma(persist_directory="db", embedding_function=embeddings)
|
25 |
+
|
26 |
+
checkpoint = "MBZUAI/LaMini-Flan-T5-783M"
|
27 |
+
|
28 |
+
# Initialize the tokenizer and base model for text generation
|
29 |
+
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
30 |
+
base_model = AutoModelForSeq2SeqLM.from_pretrained(
|
31 |
+
checkpoint,
|
32 |
+
device_map="auto",
|
33 |
+
torch_dtype=torch.float32
|
34 |
+
)
|
35 |
+
|
36 |
+
|
37 |
+
|
38 |
+
pipe = pipeline(
|
39 |
+
'text2text-generation',
|
40 |
+
model = base_model,
|
41 |
+
tokenizer = tokenizer,
|
42 |
+
max_length = 512,
|
43 |
+
do_sample = True,
|
44 |
+
temperature = 0.3,
|
45 |
+
top_p= 0.95
|
46 |
+
)
|
47 |
+
|
48 |
+
|
49 |
+
# Initialize a local language model pipeline
|
50 |
+
local_llm = HuggingFacePipeline(pipeline=pipe)
|
51 |
+
# Create a RetrievalQA chain
|
52 |
+
qa_chain = RetrievalQA.from_chain_type(
|
53 |
+
llm=local_llm,
|
54 |
+
chain_type='stuff',
|
55 |
+
retriever=db.as_retriever(search_type="similarity", search_kwargs={"k": 2}),
|
56 |
+
return_source_documents=True,
|
57 |
+
)
|
58 |
+
|
59 |
+
|
60 |
+
st.title("Lawyer Bot")
|
61 |
+
st.subheader("A chatbot to answer your legal questions trained on IPC")
|
62 |
+
if "messages" not in st.session_state:
|
63 |
+
st.session_state.messages = []
|
64 |
+
|
65 |
+
# Display chat messages from history on app rerun
|
66 |
+
for message in st.session_state.messages:
|
67 |
+
with st.chat_message(message["role"]):
|
68 |
+
st.markdown(message["content"])
|
69 |
+
|
70 |
+
# Accept user input
|
71 |
+
if prompt := st.chat_input("What is up?"):
|
72 |
+
# Add user message to chat history
|
73 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
74 |
+
# Display user message in chat message container
|
75 |
+
with st.chat_message("user"):
|
76 |
+
st.markdown(prompt)
|
77 |
+
# Get response from chatbot
|
78 |
+
with st.chat_message("assistant"):
|
79 |
+
response = qa_chain(prompt)
|
80 |
+
print(response['result'])
|
81 |
+
st.markdown(response["result"])
|
82 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
83 |
+
|
84 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|