Spaces:
Runtime error
Runtime error
Upload 5 files
Browse files- .env +1 -0
- Dockerfile +34 -0
- Groq_app.py +121 -0
- htmlTemplates.py +44 -0
- requirements.txt +13 -0
.env
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
GROQ_API_KEY = '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'
|
Dockerfile
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use the official Python 3.9 image as the base image
|
| 2 |
+
FROM python:3.9
|
| 3 |
+
|
| 4 |
+
# Set the working directory inside the container
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy the requirements file to the working directory
|
| 8 |
+
COPY requirements.txt /app/requirements.txt
|
| 9 |
+
|
| 10 |
+
# Install the required dependencies
|
| 11 |
+
RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt
|
| 12 |
+
|
| 13 |
+
# Set environment variables for Streamlit and other configurations
|
| 14 |
+
ENV PYTHONUNBUFFERED=1 \
|
| 15 |
+
PATH="/home/user/.local/bin:$PATH" \
|
| 16 |
+
HOME=/home/user
|
| 17 |
+
|
| 18 |
+
# Set up a new user named "user"
|
| 19 |
+
RUN useradd user
|
| 20 |
+
|
| 21 |
+
# Switch to the "user" user
|
| 22 |
+
USER user
|
| 23 |
+
|
| 24 |
+
# Set the working directory to the user's home directory
|
| 25 |
+
WORKDIR /home/user/app
|
| 26 |
+
|
| 27 |
+
# Copy the app content to the container and change ownership to the user
|
| 28 |
+
COPY --chown=user . /home/user/app
|
| 29 |
+
|
| 30 |
+
# Expose the port Streamlit will use
|
| 31 |
+
EXPOSE 8501
|
| 32 |
+
|
| 33 |
+
# Run the Streamlit app
|
| 34 |
+
CMD ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]
|
Groq_app.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
from PyPDF2 import PdfReader
|
| 4 |
+
from langchain.text_splitter import CharacterTextSplitter
|
| 5 |
+
from langchain.embeddings import HuggingFaceInstructEmbeddings
|
| 6 |
+
from langchain.vectorstores import FAISS
|
| 7 |
+
from langchain.memory import ConversationBufferMemory
|
| 8 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 9 |
+
from htmlTemplates import css, bot_template, user_template
|
| 10 |
+
from langchain.llms import HuggingFaceHub
|
| 11 |
+
from langchain_community.llms import Ollama
|
| 12 |
+
from langchain_groq import ChatGroq
|
| 13 |
+
import os
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
#extraction of the text from the pdfs
|
| 19 |
+
def get_pdf_text(pdf_docs):
|
| 20 |
+
text = ""
|
| 21 |
+
for pdf in pdf_docs:
|
| 22 |
+
pdf_reader = PdfReader(pdf)
|
| 23 |
+
for page in pdf_reader.pages:
|
| 24 |
+
text += page.extract_text()
|
| 25 |
+
|
| 26 |
+
return text
|
| 27 |
+
|
| 28 |
+
#dividing the raw text in different chunks
|
| 29 |
+
def get_text_chunks(text):
|
| 30 |
+
text_splitter = CharacterTextSplitter(
|
| 31 |
+
separator= "\n" ,
|
| 32 |
+
chunk_size=1000,
|
| 33 |
+
chunk_overlap=200,
|
| 34 |
+
length_function= len
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
chunks = text_splitter.split_text(text)
|
| 38 |
+
return chunks
|
| 39 |
+
|
| 40 |
+
#creating a vector store embeddings from huggingface
|
| 41 |
+
def get_vectorstore(text_chunks):
|
| 42 |
+
# embeddings = OpenAIEmbeddings()
|
| 43 |
+
embeddings = HuggingFaceInstructEmbeddings(model_name="hkunlp/instructor-xl")
|
| 44 |
+
vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
|
| 45 |
+
return vectorstore
|
| 46 |
+
|
| 47 |
+
#creating a conversation chain to store the context for follow up question
|
| 48 |
+
def get_conversation_chain(vectorstore, groq_api_key):
|
| 49 |
+
|
| 50 |
+
#llm = HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature":0.5, "max_length":512})
|
| 51 |
+
#llm = Ollama(model="llama2")
|
| 52 |
+
llm=ChatGroq(groq_api_key=groq_api_key,
|
| 53 |
+
model_name="llama3-70b-8192")
|
| 54 |
+
memory = ConversationBufferMemory(
|
| 55 |
+
memory_key='chat_history', return_messages=True)
|
| 56 |
+
conversation_chain = ConversationalRetrievalChain.from_llm(
|
| 57 |
+
llm=llm,
|
| 58 |
+
retriever=vectorstore.as_retriever(),
|
| 59 |
+
memory=memory
|
| 60 |
+
)
|
| 61 |
+
return conversation_chain
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
#handling the user input
|
| 65 |
+
def handle_userinput(user_question):
|
| 66 |
+
response = st.session_state.conversation({'question' : user_question})
|
| 67 |
+
#st.write(response)
|
| 68 |
+
st.session_state.chat_history = response['chat_history']
|
| 69 |
+
|
| 70 |
+
for i , message in enumerate(st.session_state.chat_history):
|
| 71 |
+
if i % 2 == 0:
|
| 72 |
+
st.write(user_template.replace("{{MSG}}", message.content), unsafe_allow_html= True)
|
| 73 |
+
else:
|
| 74 |
+
st.write(bot_template.replace("{{MSG}}", message.content), unsafe_allow_html= True)
|
| 75 |
+
|
| 76 |
+
def main():
|
| 77 |
+
load_dotenv()
|
| 78 |
+
#os.environ['OPENAI_API_KEY']=os.getenv("OPENAI_API_KEY")
|
| 79 |
+
groq_api_key=os.getenv('GROQ_API_KEY')
|
| 80 |
+
|
| 81 |
+
st.set_page_config("Chat with your pdf!!!!", page_icon=":books:")
|
| 82 |
+
|
| 83 |
+
st.write(css, unsafe_allow_html=True)
|
| 84 |
+
|
| 85 |
+
if "conversation" not in st.session_state:
|
| 86 |
+
st.session_state.conversation = None
|
| 87 |
+
|
| 88 |
+
if "chat_history" not in st.session_state:
|
| 89 |
+
st.session_state.chat_history = None
|
| 90 |
+
|
| 91 |
+
st.header("Chat with your pdf!!! :books:")
|
| 92 |
+
|
| 93 |
+
#question section
|
| 94 |
+
user_question = st.text_input("Wanna ask something???")
|
| 95 |
+
|
| 96 |
+
if user_question:
|
| 97 |
+
handle_userinput(user_question)
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
with st.sidebar:
|
| 102 |
+
st.subheader("Your documents")
|
| 103 |
+
#generally supports single file at a time. Need the enable the option to access multiple files
|
| 104 |
+
pdf_docs = st.file_uploader("Upload your pdf file", type=["pdf"], accept_multiple_files=True)
|
| 105 |
+
if st.button("Process"):
|
| 106 |
+
with st.spinner("Processing"):
|
| 107 |
+
#get the pdf text
|
| 108 |
+
raw_text = get_pdf_text(pdf_docs)
|
| 109 |
+
|
| 110 |
+
#get the text chunks
|
| 111 |
+
text_chunks = get_text_chunks(raw_text)
|
| 112 |
+
|
| 113 |
+
#create the vector store with embeddings
|
| 114 |
+
vectorstore = get_vectorstore(text_chunks)
|
| 115 |
+
|
| 116 |
+
#create the conversation chain
|
| 117 |
+
st.session_state.conversation = get_conversation_chain(vectorstore, groq_api_key)
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
if __name__ == '__main__':
|
| 121 |
+
main()
|
htmlTemplates.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
css = '''
|
| 2 |
+
<style>
|
| 3 |
+
.chat-message {
|
| 4 |
+
padding: 1.5rem; border-radius: 0.5rem; margin-bottom: 1rem; display: flex
|
| 5 |
+
}
|
| 6 |
+
.chat-message.user {
|
| 7 |
+
background-color: #2b313e
|
| 8 |
+
}
|
| 9 |
+
.chat-message.bot {
|
| 10 |
+
background-color: #475063
|
| 11 |
+
}
|
| 12 |
+
.chat-message .avatar {
|
| 13 |
+
width: 20%;
|
| 14 |
+
}
|
| 15 |
+
.chat-message .avatar img {
|
| 16 |
+
max-width: 78px;
|
| 17 |
+
max-height: 78px;
|
| 18 |
+
border-radius: 50%;
|
| 19 |
+
object-fit: cover;
|
| 20 |
+
}
|
| 21 |
+
.chat-message .message {
|
| 22 |
+
width: 80%;
|
| 23 |
+
padding: 0 1.5rem;
|
| 24 |
+
color: #fff;
|
| 25 |
+
}
|
| 26 |
+
'''
|
| 27 |
+
|
| 28 |
+
bot_template = '''
|
| 29 |
+
<div class="chat-message bot">
|
| 30 |
+
<div class="avatar">
|
| 31 |
+
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ2ovj66Fvluoa25tuClVVp4-caGbDPxVfdfg&s" style="max-height: 78px; max-width: 78px; border-radius: 50%; object-fit: cover;">
|
| 32 |
+
</div>
|
| 33 |
+
<div class="message">{{MSG}}</div>
|
| 34 |
+
</div>
|
| 35 |
+
'''
|
| 36 |
+
|
| 37 |
+
user_template = '''
|
| 38 |
+
<div class="chat-message user">
|
| 39 |
+
<div class="avatar">
|
| 40 |
+
<img src="https://media.npr.org/assets/img/2013/05/06/tonystark_wide-92e2d9abcce4413d58f728f2b5f126cef71afd97.jpg">
|
| 41 |
+
</div>
|
| 42 |
+
<div class="message">{{MSG}}</div>
|
| 43 |
+
</div>
|
| 44 |
+
'''
|
requirements.txt
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
pypdf2
|
| 3 |
+
langchain
|
| 4 |
+
langchain_core
|
| 5 |
+
langchain_community
|
| 6 |
+
langchain-groq
|
| 7 |
+
python-dotenv
|
| 8 |
+
faiss-cpu
|
| 9 |
+
huggingface_hub
|
| 10 |
+
openai
|
| 11 |
+
InstructorEmbedding
|
| 12 |
+
sentence-transformers==2.2.2
|
| 13 |
+
torch
|