tlsgy
commited on
Commit
Β·
29dd511
1
Parent(s):
cbe988a
dd
Browse files- app.py +0 -175
- htmlTemplates.py +0 -44
- requirements.txt +0 -14
app.py
DELETED
@@ -1,175 +0,0 @@
|
|
1 |
-
import json
|
2 |
-
|
3 |
-
import streamlit as st
|
4 |
-
from dotenv import load_dotenv
|
5 |
-
from PyPDF2 import PdfReader
|
6 |
-
from langchain.text_splitter import CharacterTextSplitter, RecursiveCharacterTextSplitter
|
7 |
-
from langchain.embeddings import OpenAIEmbeddings, HuggingFaceInstructEmbeddings
|
8 |
-
from langchain.vectorstores import FAISS, Chroma
|
9 |
-
from langchain.embeddings import HuggingFaceEmbeddings # General embeddings from HuggingFace models.
|
10 |
-
from langchain.chat_models import ChatOpenAI
|
11 |
-
from langchain.memory import ConversationBufferMemory
|
12 |
-
from langchain.chains import ConversationalRetrievalChain
|
13 |
-
from htmlTemplates import css, bot_template, user_template
|
14 |
-
from langchain.llms import HuggingFaceHub, LlamaCpp, CTransformers # For loading transformer models.
|
15 |
-
from langchain.document_loaders import PyPDFLoader, TextLoader, JSONLoader, CSVLoader
|
16 |
-
import tempfile # μμ νμΌμ μμ±νκΈ° μν λΌμ΄λΈλ¬λ¦¬μ
λλ€.
|
17 |
-
import os
|
18 |
-
|
19 |
-
|
20 |
-
# PDF λ¬Έμλ‘λΆν° ν
μ€νΈλ₯Ό μΆμΆνλ ν¨μμ
λλ€.
|
21 |
-
def get_pdf_text(pdf_docs):
|
22 |
-
temp_dir = tempfile.TemporaryDirectory() # μμ λλ ν 리λ₯Ό μμ±ν©λλ€.
|
23 |
-
temp_filepath = os.path.join(temp_dir.name, pdf_docs.name) # μμ νμΌ κ²½λ‘λ₯Ό μμ±ν©λλ€.
|
24 |
-
with open(temp_filepath, "wb") as f: # μμ νμΌμ λ°μ΄λ리 μ°κΈ° λͺ¨λλ‘ μ½λλ€.
|
25 |
-
f.write(pdf_docs.getvalue()) # PDF λ¬Έμμ λ΄μ©μ μμ νμΌμ μλλ€.
|
26 |
-
pdf_loader = PyPDFLoader(temp_filepath) # PyPDFLoaderλ₯Ό μ¬μ©ν΄ PDFλ₯Ό λ‘λν©λλ€.
|
27 |
-
pdf_doc = pdf_loader.load() # ν
μ€νΈλ₯Ό μΆμΆν©λλ€.
|
28 |
-
return pdf_doc # μΆμΆν ν
μ€νΈλ₯Ό λ°νν©λλ€.
|
29 |
-
|
30 |
-
# κ³Όμ
|
31 |
-
# μλ ν
μ€νΈ μΆμΆ ν¨μλ₯Ό μμ±
|
32 |
-
|
33 |
-
def get_text_file(docs):
|
34 |
-
temp_dir = tempfile.TemporaryDirectory() # μμ λλ ν 리λ₯Ό μμ±ν©λλ€.
|
35 |
-
temp_filepath = os.path.join(temp_dir.name, docs.name) # μμ νμΌ κ²½λ‘λ₯Ό μμ±ν©λλ€.
|
36 |
-
with open(temp_filepath, "wb") as f: # μμ νμΌμ λ°μ΄λ리 μ°κΈ° λͺ¨λλ‘ μ½λλ€.
|
37 |
-
f.write(docs.getvalue())
|
38 |
-
loader = TextLoader(temp_filepath)
|
39 |
-
data = loader.load()
|
40 |
-
return data
|
41 |
-
|
42 |
-
|
43 |
-
def get_csv_file(docs):
|
44 |
-
temp_dir = tempfile.TemporaryDirectory() # μμ λλ ν 리λ₯Ό μμ±ν©λλ€.
|
45 |
-
temp_filepath = os.path.join(temp_dir.name, docs.name) # μμ νμΌ κ²½λ‘λ₯Ό μμ±ν©λλ€.
|
46 |
-
with open(temp_filepath, "wb") as f: # μμ νμΌμ λ°μ΄λ리 μ°κΈ° λͺ¨λλ‘ μ½λλ€.
|
47 |
-
f.write(docs.getvalue())
|
48 |
-
loader = CSVLoader(file_path=temp_filepath)
|
49 |
-
data = loader.load()
|
50 |
-
return data
|
51 |
-
|
52 |
-
|
53 |
-
def get_json_file(docs):
|
54 |
-
temp_dir = tempfile.TemporaryDirectory() # μμ λλ ν 리λ₯Ό μμ±ν©λλ€.
|
55 |
-
temp_filepath = os.path.join(temp_dir.name, docs.name) # μμ νμΌ κ²½λ‘λ₯Ό μμ±ν©λλ€.
|
56 |
-
with open(temp_filepath, "wb") as f: # μμ νμΌμ λ°μ΄λ리 μ°κΈ° λͺ¨λλ‘ μ½λλ€.
|
57 |
-
f.write(docs.getvalue())
|
58 |
-
|
59 |
-
loader = JSONLoader(file_path=temp_filepath,
|
60 |
-
jq_schema='.messages.[].content',
|
61 |
-
text_content=False)
|
62 |
-
data = loader.load()
|
63 |
-
|
64 |
-
return data
|
65 |
-
|
66 |
-
# λ¬Έμλ€μ μ²λ¦¬νμ¬ ν
μ€νΈ μ²ν¬λ‘ λλλ ν¨μμ
λλ€.
|
67 |
-
def get_text_chunks(documents):
|
68 |
-
text_splitter = RecursiveCharacterTextSplitter(
|
69 |
-
chunk_size=1000, # μ²ν¬μ ν¬κΈ°λ₯Ό μ§μ ν©λλ€.
|
70 |
-
chunk_overlap=200, # μ²ν¬ μ¬μ΄μ μ€λ³΅μ μ§μ ν©λλ€.
|
71 |
-
length_function=len # ν
μ€νΈμ κΈΈμ΄λ₯Ό μΈ‘μ νλ ν¨μλ₯Ό μ§μ ν©λλ€.
|
72 |
-
)
|
73 |
-
|
74 |
-
documents = text_splitter.split_documents(documents) # λ¬Έμλ€μ μ²ν¬λ‘ λλλλ€
|
75 |
-
return documents # λλ μ²ν¬λ₯Ό λ°νν©λλ€.
|
76 |
-
|
77 |
-
|
78 |
-
# ν
μ€νΈ μ²ν¬λ€λ‘λΆν° λ²‘ν° μ€ν μ΄λ₯Ό μμ±νλ ν¨μμ
λλ€.
|
79 |
-
def get_vectorstore(text_chunks):
|
80 |
-
# OpenAI μλ² λ© λͺ¨λΈμ λ‘λν©λλ€. (Embedding models - Ada v2)
|
81 |
-
|
82 |
-
embeddings = OpenAIEmbeddings()
|
83 |
-
vectorstore = FAISS.from_documents(text_chunks, embeddings) # FAISS λ²‘ν° μ€ν μ΄λ₯Ό μμ±ν©λλ€.
|
84 |
-
|
85 |
-
return vectorstore # μμ±λ λ²‘ν° μ€ν μ΄λ₯Ό λ°νν©λλ€.
|
86 |
-
|
87 |
-
|
88 |
-
def get_conversation_chain(vectorstore):
|
89 |
-
gpt_model_name = 'gpt-3.5-turbo'
|
90 |
-
llm = ChatOpenAI(model_name = gpt_model_name) #gpt-3.5 λͺ¨λΈ λ‘λ
|
91 |
-
|
92 |
-
# λν κΈ°λ‘μ μ μ₯νκΈ° μν λ©λͺ¨λ¦¬λ₯Ό μμ±ν©λλ€.
|
93 |
-
memory = ConversationBufferMemory(
|
94 |
-
memory_key='chat_history', return_messages=True)
|
95 |
-
# λν κ²μ 체μΈμ μμ±ν©λλ€.
|
96 |
-
conversation_chain = ConversationalRetrievalChain.from_llm(
|
97 |
-
llm=llm,
|
98 |
-
retriever=vectorstore.as_retriever(),
|
99 |
-
memory=memory
|
100 |
-
)
|
101 |
-
return conversation_chain
|
102 |
-
|
103 |
-
# μ¬μ©μ μ
λ ₯μ μ²λ¦¬νλ ν¨μμ
λλ€.
|
104 |
-
def handle_userinput(user_question):
|
105 |
-
# λν 체μΈμ μ¬μ©νμ¬ μ¬μ©μ μ§λ¬Έμ λν μλ΅μ μμ±ν©λλ€.
|
106 |
-
response = st.session_state.conversation({'question': user_question})
|
107 |
-
# λν κΈ°λ‘μ μ μ₯ν©λλ€.
|
108 |
-
st.session_state.chat_history = response['chat_history']
|
109 |
-
|
110 |
-
for i, message in enumerate(st.session_state.chat_history):
|
111 |
-
if i % 2 == 0:
|
112 |
-
st.write(user_template.replace(
|
113 |
-
"{{MSG}}", message.content), unsafe_allow_html=True)
|
114 |
-
else:
|
115 |
-
st.write(bot_template.replace(
|
116 |
-
"{{MSG}}", message.content), unsafe_allow_html=True)
|
117 |
-
|
118 |
-
|
119 |
-
def main():
|
120 |
-
load_dotenv()
|
121 |
-
st.set_page_config(page_title="Chat with multiple Files",
|
122 |
-
page_icon=":books:")
|
123 |
-
st.write(css, unsafe_allow_html=True)
|
124 |
-
|
125 |
-
if "conversation" not in st.session_state:
|
126 |
-
st.session_state.conversation = None
|
127 |
-
if "chat_history" not in st.session_state:
|
128 |
-
st.session_state.chat_history = None
|
129 |
-
|
130 |
-
st.header("Chat with multiple Files :")
|
131 |
-
user_question = st.text_input("Ask a question about your documents:")
|
132 |
-
if user_question:
|
133 |
-
handle_userinput(user_question)
|
134 |
-
|
135 |
-
with st.sidebar:
|
136 |
-
openai_key = st.text_input("Paste your OpenAI API key (sk-...)")
|
137 |
-
if openai_key:
|
138 |
-
os.environ["OPENAI_API_KEY"] = openai_key
|
139 |
-
|
140 |
-
st.subheader("Your documents")
|
141 |
-
docs = st.file_uploader(
|
142 |
-
"Upload your PDFs here and click on 'Process'", accept_multiple_files=True)
|
143 |
-
if st.button("Process"):
|
144 |
-
with st.spinner("Processing"):
|
145 |
-
# get pdf text
|
146 |
-
doc_list = []
|
147 |
-
|
148 |
-
for file in docs:
|
149 |
-
print('file - type : ', file.type)
|
150 |
-
if file.type == 'text/plain':
|
151 |
-
# file is .txt
|
152 |
-
doc_list.extend(get_text_file(file))
|
153 |
-
elif file.type in ['application/octet-stream', 'application/pdf']:
|
154 |
-
# file is .pdf
|
155 |
-
doc_list.extend(get_pdf_text(file))
|
156 |
-
elif file.type == 'text/csv':
|
157 |
-
# file is .csv
|
158 |
-
doc_list.extend(get_csv_file(file))
|
159 |
-
elif file.type == 'application/json':
|
160 |
-
# file is .json
|
161 |
-
doc_list.extend(get_json_file(file))
|
162 |
-
|
163 |
-
# get the text chunks
|
164 |
-
text_chunks = get_text_chunks(doc_list)
|
165 |
-
|
166 |
-
# create vector store
|
167 |
-
vectorstore = get_vectorstore(text_chunks)
|
168 |
-
|
169 |
-
# create conversation chain
|
170 |
-
st.session_state.conversation = get_conversation_chain(
|
171 |
-
vectorstore)
|
172 |
-
|
173 |
-
|
174 |
-
if __name__ == '__main__':
|
175 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
htmlTemplates.py
DELETED
@@ -1,44 +0,0 @@
|
|
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://i.ibb.co/cN0nmSj/Screenshot-2023-05-28-at-02-37-21.png" 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://i.ibb.co/rdZC7LZ/Photo-logo-1.png">
|
41 |
-
</div>
|
42 |
-
<div class="message">{{MSG}}</div>
|
43 |
-
</div>
|
44 |
-
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
DELETED
@@ -1,14 +0,0 @@
|
|
1 |
-
langchain
|
2 |
-
llama-cpp-python
|
3 |
-
PyPDF2==3.0.1
|
4 |
-
faiss-cpu==1.7.4
|
5 |
-
ctransformers
|
6 |
-
pypdf
|
7 |
-
chromadb
|
8 |
-
tiktoken
|
9 |
-
pysqlite3-binary
|
10 |
-
streamlit-extras
|
11 |
-
InstructorEmbedding
|
12 |
-
sentence-transformers
|
13 |
-
jq
|
14 |
-
openai
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|