Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,31 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from streamlit_chat import message
|
| 3 |
from langchain.chains import ConversationalRetrievalChain
|
| 4 |
-
from langchain.chains import RetrievalQA
|
| 5 |
from langchain.embeddings import HuggingFaceEmbeddings
|
| 6 |
from langchain.llms import HuggingFacePipeline
|
| 7 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 8 |
from langchain.vectorstores import FAISS
|
| 9 |
from langchain.memory import ConversationBufferMemory
|
| 10 |
from langchain_community.document_loaders import PyPDFLoader
|
| 11 |
-
from transformers import
|
| 12 |
import torch
|
| 13 |
from transformers import pipeline
|
| 14 |
import os
|
| 15 |
import tempfile
|
| 16 |
|
| 17 |
-
#model = AutoModelForSeq2SeqLM.from_pretrained("LaMini-Flan-T5-783M")
|
| 18 |
-
#tokenizer = AutoTokenizer.from_pretrained("LaMini-Flan-T5-783M", device_map = 'auto')
|
| 19 |
|
| 20 |
-
|
| 21 |
-
checkpoint = "LaMini-Flan-T5-783M"
|
| 22 |
tokenizer = T5Tokenizer.from_pretrained(checkpoint)
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
def llm_pipeline():
|
| 26 |
pipe = pipeline(
|
| 27 |
'text2text-generation',
|
| 28 |
-
model =
|
| 29 |
tokenizer = tokenizer,
|
| 30 |
do_sample = True,
|
| 31 |
temperature = 0.5,
|
| 32 |
-
max_length = 250
|
| 33 |
)
|
| 34 |
local_llm = HuggingFacePipeline(pipeline=pipe)
|
| 35 |
return local_llm
|
|
@@ -58,7 +54,6 @@ def display_chat_history(chain):
|
|
| 58 |
with st.form(key='my_form', clear_on_submit=True):
|
| 59 |
user_input = st.text_input("Question:", placeholder="Ask about your PDF", key='input')
|
| 60 |
submit_button = st.form_submit_button(label='Send')
|
| 61 |
-
|
| 62 |
|
| 63 |
if submit_button and user_input:
|
| 64 |
with st.spinner('Generating response...'):
|
|
@@ -81,17 +76,13 @@ def create_conversational_chain(vector_store):
|
|
| 81 |
|
| 82 |
chain = ConversationalRetrievalChain.from_llm(llm=llm, chain_type='stuff',
|
| 83 |
retriever=vector_store.as_retriever(search_kwargs={"k": 2}),
|
| 84 |
-
memory=memory)
|
| 85 |
-
|
| 86 |
-
chain = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff",
|
| 87 |
-
retriever=vector_store.as_retriever(search_kwargs={"k": 2}),
|
| 88 |
-
memory = memory)
|
| 89 |
-
return chain """
|
| 90 |
|
| 91 |
def main():
|
| 92 |
# Initialize session state
|
| 93 |
initialize_session_state()
|
| 94 |
-
st.title("Multi-PDF ChatBot :books:")
|
| 95 |
# Initialize Streamlit
|
| 96 |
st.sidebar.title("Document Processing")
|
| 97 |
uploaded_files = st.sidebar.file_uploader("Upload files", accept_multiple_files=True)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from streamlit_chat import message
|
| 3 |
from langchain.chains import ConversationalRetrievalChain
|
|
|
|
| 4 |
from langchain.embeddings import HuggingFaceEmbeddings
|
| 5 |
from langchain.llms import HuggingFacePipeline
|
| 6 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 7 |
from langchain.vectorstores import FAISS
|
| 8 |
from langchain.memory import ConversationBufferMemory
|
| 9 |
from langchain_community.document_loaders import PyPDFLoader
|
| 10 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
| 11 |
import torch
|
| 12 |
from transformers import pipeline
|
| 13 |
import os
|
| 14 |
import tempfile
|
| 15 |
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
checkpoint = "LaMini-Flan-T5-248M"
|
|
|
|
| 18 |
tokenizer = T5Tokenizer.from_pretrained(checkpoint)
|
| 19 |
+
|
| 20 |
+
base_model = T5ForConditionalGeneration.from_pretrained( checkpoint, device_map = 'auto', torch_dtype = torch.float32 )
|
| 21 |
|
| 22 |
def llm_pipeline():
|
| 23 |
pipe = pipeline(
|
| 24 |
'text2text-generation',
|
| 25 |
+
model = base_model,
|
| 26 |
tokenizer = tokenizer,
|
| 27 |
do_sample = True,
|
| 28 |
temperature = 0.5,
|
|
|
|
| 29 |
)
|
| 30 |
local_llm = HuggingFacePipeline(pipeline=pipe)
|
| 31 |
return local_llm
|
|
|
|
| 54 |
with st.form(key='my_form', clear_on_submit=True):
|
| 55 |
user_input = st.text_input("Question:", placeholder="Ask about your PDF", key='input')
|
| 56 |
submit_button = st.form_submit_button(label='Send')
|
|
|
|
| 57 |
|
| 58 |
if submit_button and user_input:
|
| 59 |
with st.spinner('Generating response...'):
|
|
|
|
| 76 |
|
| 77 |
chain = ConversationalRetrievalChain.from_llm(llm=llm, chain_type='stuff',
|
| 78 |
retriever=vector_store.as_retriever(search_kwargs={"k": 2}),
|
| 79 |
+
memory=memory)
|
| 80 |
+
return chain
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
def main():
|
| 83 |
# Initialize session state
|
| 84 |
initialize_session_state()
|
| 85 |
+
st.title("Multi-PDF ChatBot using Mistral-7B-Instruct :books:")
|
| 86 |
# Initialize Streamlit
|
| 87 |
st.sidebar.title("Document Processing")
|
| 88 |
uploaded_files = st.sidebar.file_uploader("Upload files", accept_multiple_files=True)
|