Spaces:
Runtime error
Runtime error
Commit
·
43b8c0f
1
Parent(s):
f01c29b
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,15 @@
|
|
1 |
-
import streamlit as st
|
2 |
import os
|
3 |
import base64
|
4 |
import time
|
5 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
6 |
from transformers import pipeline
|
7 |
-
import torch
|
8 |
-
import textwrap
|
9 |
-
from langchain.
|
10 |
-
from langchain.
|
11 |
-
from langchain.embeddings import SentenceTransformerEmbeddings
|
12 |
-
from langchain.vectorstores import Chroma
|
13 |
from langchain.llms import HuggingFacePipeline
|
14 |
-
from langchain.chains import RetrievalQA
|
15 |
-
from constants import CHROMA_SETTINGS
|
16 |
-
from streamlit_chat import message
|
17 |
|
18 |
st.set_page_config(layout="wide")
|
19 |
|
@@ -28,45 +24,31 @@ base_model = AutoModelForSeq2SeqLM.from_pretrained(
|
|
28 |
torch_dtype=torch.float32
|
29 |
)
|
30 |
|
31 |
-
|
32 |
-
# checkpoint = "LaMini-T5-738M"
|
33 |
-
# tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
34 |
-
# base_model = AutoModelForSeq2SeqLM.from_pretrained(
|
35 |
-
# checkpoint,
|
36 |
-
# device_map="auto",
|
37 |
-
# torch_dtype = torch.float32,
|
38 |
-
# from_tf=True
|
39 |
-
# )
|
40 |
-
|
41 |
persist_directory = "db"
|
42 |
|
43 |
@st.cache_resource
|
44 |
-
def data_ingestion():
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
#create vector store here
|
56 |
-
db = Chroma.from_documents(texts, embeddings, persist_directory=persist_directory, client_settings=CHROMA_SETTINGS)
|
57 |
-
db.persist()
|
58 |
-
db=None
|
59 |
|
60 |
@st.cache_resource
|
61 |
def llm_pipeline():
|
62 |
pipe = pipeline(
|
63 |
'text2text-generation',
|
64 |
-
model
|
65 |
-
tokenizer
|
66 |
-
max_length
|
67 |
-
do_sample
|
68 |
-
temperature
|
69 |
-
top_p=
|
70 |
device=device
|
71 |
)
|
72 |
local_llm = HuggingFacePipeline(pipeline=pipe)
|
@@ -76,12 +58,12 @@ def llm_pipeline():
|
|
76 |
def qa_llm():
|
77 |
llm = llm_pipeline()
|
78 |
embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
|
79 |
-
db = Chroma(persist_directory="db", embedding_function
|
80 |
retriever = db.as_retriever()
|
81 |
qa = RetrievalQA.from_chain_type(
|
82 |
-
llm
|
83 |
-
chain_type
|
84 |
-
retriever
|
85 |
return_source_documents=True
|
86 |
)
|
87 |
return qa
|
@@ -101,7 +83,7 @@ def get_file_size(file):
|
|
101 |
return file_size
|
102 |
|
103 |
@st.cache_data
|
104 |
-
#
|
105 |
def displayPDF(file):
|
106 |
# Opening file from file path
|
107 |
with open(file, "rb") as f:
|
@@ -117,7 +99,7 @@ def displayPDF(file):
|
|
117 |
def display_conversation(history):
|
118 |
for i in range(len(history["generated"])):
|
119 |
message(history["past"][i], is_user=True, key=str(i) + "_user")
|
120 |
-
message(history["generated"][i],key=str(i))
|
121 |
|
122 |
def main():
|
123 |
st.markdown("<h1 style='text-align: center; color: blue;'>Chat with your PDF 🦜📄 </h1>", unsafe_allow_html=True)
|
@@ -132,49 +114,20 @@ def main():
|
|
132 |
"Filename": uploaded_file.name,
|
133 |
"File size": get_file_size(uploaded_file)
|
134 |
}
|
135 |
-
filepath = "docs/"+uploaded_file.name
|
136 |
-
with open(filepath, "wb") as temp_file:
|
137 |
-
temp_file.write(uploaded_file.read())
|
138 |
|
139 |
-
col1, col2= st.columns([1,2])
|
140 |
with col1:
|
141 |
st.markdown("<h4 style color:black;'>File details</h4>", unsafe_allow_html=True)
|
142 |
st.json(file_details)
|
143 |
st.markdown("<h4 style color:black;'>File preview</h4>", unsafe_allow_html=True)
|
144 |
-
pdf_view = displayPDF(
|
145 |
|
146 |
with col2:
|
147 |
-
with st.spinner('
|
148 |
-
|
149 |
-
st.success('
|
150 |
-
st.markdown("<h4 style color:black;'>
|
151 |
-
|
152 |
-
|
153 |
-
user_input = st.text_input("", key="input")
|
154 |
-
|
155 |
-
# Initialize session state for generated responses and past messages
|
156 |
-
if "generated" not in st.session_state:
|
157 |
-
st.session_state["generated"] = ["I am ready to help you"]
|
158 |
-
if "past" not in st.session_state:
|
159 |
-
st.session_state["past"] = ["Hey there!"]
|
160 |
-
|
161 |
-
# Search the database for a response based on user input and update session state
|
162 |
-
if user_input:
|
163 |
-
answer = process_answer({'query': user_input})
|
164 |
-
st.session_state["past"].append(user_input)
|
165 |
-
response = answer
|
166 |
-
st.session_state["generated"].append(response)
|
167 |
-
|
168 |
-
# Display conversation history using Streamlit messages
|
169 |
-
if st.session_state["generated"]:
|
170 |
-
display_conversation(st.session_state)
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
|
178 |
if __name__ == "__main__":
|
179 |
main()
|
180 |
-
|
|
|
1 |
+
import streamlit as st
|
2 |
import os
|
3 |
import base64
|
4 |
import time
|
5 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
6 |
from transformers import pipeline
|
7 |
+
import torch
|
8 |
+
import textwrap
|
9 |
+
from langchain.embeddings import SentenceTransformerEmbeddings
|
10 |
+
from langchain.vectorstores import Chroma
|
|
|
|
|
11 |
from langchain.llms import HuggingFacePipeline
|
12 |
+
from langchain.chains import RetrievalQA
|
|
|
|
|
13 |
|
14 |
st.set_page_config(layout="wide")
|
15 |
|
|
|
24 |
torch_dtype=torch.float32
|
25 |
)
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
persist_directory = "db"
|
28 |
|
29 |
@st.cache_resource
|
30 |
+
def data_ingestion(uploaded_file):
|
31 |
+
with open(uploaded_file.name, "rb") as pdf_file:
|
32 |
+
pdf_content = pdf_file.read()
|
33 |
+
|
34 |
+
# Process the PDF content here and generate a brief summary.
|
35 |
+
# You can use libraries like PyPDF2, pdfminer, or other PDF processing tools.
|
36 |
+
|
37 |
+
# For now, let's assume we have extracted the text from the PDF.
|
38 |
+
pdf_text = "This is a brief summary of the PDF content."
|
39 |
+
|
40 |
+
return pdf_text
|
|
|
|
|
|
|
|
|
41 |
|
42 |
@st.cache_resource
|
43 |
def llm_pipeline():
|
44 |
pipe = pipeline(
|
45 |
'text2text-generation',
|
46 |
+
model=base_model,
|
47 |
+
tokenizer=tokenizer,
|
48 |
+
max_length=256,
|
49 |
+
do_sample=True,
|
50 |
+
temperature=0.3,
|
51 |
+
top_p=0.95,
|
52 |
device=device
|
53 |
)
|
54 |
local_llm = HuggingFacePipeline(pipeline=pipe)
|
|
|
58 |
def qa_llm():
|
59 |
llm = llm_pipeline()
|
60 |
embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
|
61 |
+
db = Chroma(persist_directory="db", embedding_function=embeddings, client_settings=CHROMA_SETTINGS)
|
62 |
retriever = db.as_retriever()
|
63 |
qa = RetrievalQA.from_chain_type(
|
64 |
+
llm=llm,
|
65 |
+
chain_type="stuff",
|
66 |
+
retriever=retriever,
|
67 |
return_source_documents=True
|
68 |
)
|
69 |
return qa
|
|
|
83 |
return file_size
|
84 |
|
85 |
@st.cache_data
|
86 |
+
# Function to display the PDF of a given file
|
87 |
def displayPDF(file):
|
88 |
# Opening file from file path
|
89 |
with open(file, "rb") as f:
|
|
|
99 |
def display_conversation(history):
|
100 |
for i in range(len(history["generated"])):
|
101 |
message(history["past"][i], is_user=True, key=str(i) + "_user")
|
102 |
+
message(history["generated"][i], key=str(i))
|
103 |
|
104 |
def main():
|
105 |
st.markdown("<h1 style='text-align: center; color: blue;'>Chat with your PDF 🦜📄 </h1>", unsafe_allow_html=True)
|
|
|
114 |
"Filename": uploaded_file.name,
|
115 |
"File size": get_file_size(uploaded_file)
|
116 |
}
|
|
|
|
|
|
|
117 |
|
118 |
+
col1, col2 = st.columns([1, 2])
|
119 |
with col1:
|
120 |
st.markdown("<h4 style color:black;'>File details</h4>", unsafe_allow_html=True)
|
121 |
st.json(file_details)
|
122 |
st.markdown("<h4 style color:black;'>File preview</h4>", unsafe_allow_html=True)
|
123 |
+
pdf_view = displayPDF(uploaded_file)
|
124 |
|
125 |
with col2:
|
126 |
+
with st.spinner('Processing the uploaded PDF...'):
|
127 |
+
pdf_summary = data_ingestion(uploaded_file)
|
128 |
+
st.success('PDF processing is complete!')
|
129 |
+
st.markdown("<h4 style color:black;'>PDF Summary</h4>", unsafe_allow_html=True)
|
130 |
+
st.write(pdf_summary)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
|
132 |
if __name__ == "__main__":
|
133 |
main()
|
|