Spaces:
Sleeping
Sleeping
Commit
ยท
36fa734
1
Parent(s):
c1885de
Delete app.py
Browse files
app.py
DELETED
@@ -1,151 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from dotenv import load_dotenv
|
3 |
-
from PyPDF2 import PdfReader
|
4 |
-
from langchain.text_splitter import CharacterTextSplitter, RecursiveCharacterTextSplitter
|
5 |
-
from langchain.embeddings import OpenAIEmbeddings, HuggingFaceInstructEmbeddings
|
6 |
-
from langchain.vectorstores import FAISS, Chroma
|
7 |
-
from langchain.embeddings import HuggingFaceEmbeddings # General embeddings from HuggingFace models.
|
8 |
-
from langchain.chat_models import ChatOpenAI
|
9 |
-
from langchain.memory import ConversationBufferMemory
|
10 |
-
from langchain.chains import ConversationalRetrievalChain
|
11 |
-
from htmlTemplates import css, bot_template, user_template
|
12 |
-
from langchain.llms import HuggingFaceHub, LlamaCpp, CTransformers # For loading transformer models.
|
13 |
-
from langchain.document_loaders import PyPDFLoader, TextLoader, JSONLoader, CSVLoader
|
14 |
-
import tempfile # ์์ ํ์ผ์ ์์ฑํ๊ธฐ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค.
|
15 |
-
import os
|
16 |
-
|
17 |
-
|
18 |
-
# PDF ๋ฌธ์๋ก๋ถํฐ ํ
์คํธ๋ฅผ ์ถ์ถํ๋ ํจ์์
๋๋ค.
|
19 |
-
def get_pdf_text(pdf_docs):
|
20 |
-
temp_dir = tempfile.TemporaryDirectory() # ์์ ๋๋ ํ ๋ฆฌ๋ฅผ ์์ฑํฉ๋๋ค.
|
21 |
-
temp_filepath = os.path.join(temp_dir.name, pdf_docs.name) # ์์ ํ์ผ ๊ฒฝ๋ก๋ฅผ ์์ฑํฉ๋๋ค.
|
22 |
-
with open(temp_filepath, "wb") as f: # ์์ ํ์ผ์ ๋ฐ์ด๋๋ฆฌ ์ฐ๊ธฐ ๋ชจ๋๋ก ์ฝ๋๋ค.
|
23 |
-
f.write(pdf_docs.getvalue()) # PDF ๋ฌธ์์ ๋ด์ฉ์ ์์ ํ์ผ์ ์๋๋ค.
|
24 |
-
pdf_loader = PyPDFLoader(temp_filepath) # PyPDFLoader๋ฅผ ์ฌ์ฉํด PDF๋ฅผ ๋ก๋ํฉ๋๋ค.
|
25 |
-
pdf_doc = pdf_loader.load() # ํ
์คํธ๋ฅผ ์ถ์ถํฉ๋๋ค.
|
26 |
-
return pdf_doc # ์ถ์ถํ ํ
์คํธ๋ฅผ ๋ฐํํฉ๋๋ค.
|
27 |
-
|
28 |
-
# ๊ณผ์
|
29 |
-
# ์๋ ํ
์คํธ ์ถ์ถ ํจ์๋ฅผ ์์ฑ
|
30 |
-
|
31 |
-
def get_text_file(docs):
|
32 |
-
pass
|
33 |
-
|
34 |
-
|
35 |
-
def get_csv_file(docs):
|
36 |
-
pass
|
37 |
-
|
38 |
-
def get_json_file(docs):
|
39 |
-
pass
|
40 |
-
|
41 |
-
|
42 |
-
# ๋ฌธ์๋ค์ ์ฒ๋ฆฌํ์ฌ ํ
์คํธ ์ฒญํฌ๋ก ๋๋๋ ํจ์์
๋๋ค.
|
43 |
-
def get_text_chunks(documents):
|
44 |
-
text_splitter = RecursiveCharacterTextSplitter(
|
45 |
-
chunk_size=1000, # ์ฒญํฌ์ ํฌ๊ธฐ๋ฅผ ์ง์ ํฉ๋๋ค.
|
46 |
-
chunk_overlap=200, # ์ฒญํฌ ์ฌ์ด์ ์ค๋ณต์ ์ง์ ํฉ๋๋ค.
|
47 |
-
length_function=len # ํ
์คํธ์ ๊ธธ์ด๋ฅผ ์ธก์ ํ๋ ํจ์๋ฅผ ์ง์ ํฉ๋๋ค.
|
48 |
-
)
|
49 |
-
|
50 |
-
documents = text_splitter.split_documents(documents) # ๋ฌธ์๋ค์ ์ฒญํฌ๋ก ๋๋๋๋ค
|
51 |
-
return documents # ๋๋ ์ฒญํฌ๋ฅผ ๋ฐํํฉ๋๋ค.
|
52 |
-
|
53 |
-
|
54 |
-
# ํ
์คํธ ์ฒญํฌ๋ค๋ก๋ถํฐ ๋ฒกํฐ ์คํ ์ด๋ฅผ ์์ฑํ๋ ํจ์์
๋๋ค.
|
55 |
-
def get_vectorstore(text_chunks):
|
56 |
-
# OpenAI ์๋ฒ ๋ฉ ๋ชจ๋ธ์ ๋ก๋ํฉ๋๋ค. (Embedding models - Ada v2)
|
57 |
-
|
58 |
-
embeddings = OpenAIEmbeddings()
|
59 |
-
vectorstore = FAISS.from_documents(text_chunks, embeddings) # FAISS ๋ฒกํฐ ์คํ ์ด๋ฅผ ์์ฑํฉ๋๋ค.
|
60 |
-
|
61 |
-
return vectorstore # ์์ฑ๋ ๋ฒกํฐ ์คํ ์ด๋ฅผ ๋ฐํํฉ๋๋ค.
|
62 |
-
|
63 |
-
|
64 |
-
def get_conversation_chain(vectorstore):
|
65 |
-
gpt_model_name = 'gpt-3.5-turbo'
|
66 |
-
llm = ChatOpenAI(model_name = gpt_model_name) #gpt-3.5 ๋ชจ๋ธ ๋ก๋
|
67 |
-
|
68 |
-
# ๋ํ ๊ธฐ๋ก์ ์ ์ฅํ๊ธฐ ์ํ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์์ฑํฉ๋๋ค.
|
69 |
-
memory = ConversationBufferMemory(
|
70 |
-
memory_key='chat_history', return_messages=True)
|
71 |
-
# ๋ํ ๊ฒ์ ์ฒด์ธ์ ์์ฑํฉ๋๋ค.
|
72 |
-
conversation_chain = ConversationalRetrievalChain.from_llm(
|
73 |
-
llm=llm,
|
74 |
-
retriever=vectorstore.as_retriever(),
|
75 |
-
memory=memory
|
76 |
-
)
|
77 |
-
return conversation_chain
|
78 |
-
|
79 |
-
# ์ฌ์ฉ์ ์
๋ ฅ์ ์ฒ๋ฆฌํ๋ ํจ์์
๋๋ค.
|
80 |
-
def handle_userinput(user_question):
|
81 |
-
# ๋ํ ์ฒด์ธ์ ์ฌ์ฉํ์ฌ ์ฌ์ฉ์ ์ง๋ฌธ์ ๋ํ ์๋ต์ ์์ฑํฉ๋๋ค.
|
82 |
-
response = st.session_state.conversation({'question': user_question})
|
83 |
-
# ๋ํ ๊ธฐ๋ก์ ์ ์ฅํฉ๋๋ค.
|
84 |
-
st.session_state.chat_history = response['chat_history']
|
85 |
-
|
86 |
-
for i, message in enumerate(st.session_state.chat_history):
|
87 |
-
if i % 2 == 0:
|
88 |
-
st.write(user_template.replace(
|
89 |
-
"{{MSG}}", message.content), unsafe_allow_html=True)
|
90 |
-
else:
|
91 |
-
st.write(bot_template.replace(
|
92 |
-
"{{MSG}}", message.content), unsafe_allow_html=True)
|
93 |
-
|
94 |
-
|
95 |
-
def main():
|
96 |
-
load_dotenv()
|
97 |
-
st.set_page_config(page_title="Chat with multiple Files",
|
98 |
-
page_icon=":books:")
|
99 |
-
st.write(css, unsafe_allow_html=True)
|
100 |
-
|
101 |
-
if "conversation" not in st.session_state:
|
102 |
-
st.session_state.conversation = None
|
103 |
-
if "chat_history" not in st.session_state:
|
104 |
-
st.session_state.chat_history = None
|
105 |
-
|
106 |
-
st.header("Chat with multiple Files :")
|
107 |
-
user_question = st.text_input("Ask a question about your documents:")
|
108 |
-
if user_question:
|
109 |
-
handle_userinput(user_question)
|
110 |
-
|
111 |
-
with st.sidebar:
|
112 |
-
openai_key = st.text_input("Paste your OpenAI API key (sk-...)")
|
113 |
-
if openai_key:
|
114 |
-
os.environ["OPENAI_API_KEY"] = openai_key
|
115 |
-
|
116 |
-
st.subheader("Your documents")
|
117 |
-
docs = st.file_uploader(
|
118 |
-
"Upload your PDFs here and click on 'Process'", accept_multiple_files=True)
|
119 |
-
if st.button("Process"):
|
120 |
-
with st.spinner("Processing"):
|
121 |
-
# get pdf text
|
122 |
-
doc_list = []
|
123 |
-
|
124 |
-
for file in docs:
|
125 |
-
print('file - type : ', file.type)
|
126 |
-
if file.type == 'text/plain':
|
127 |
-
# file is .txt
|
128 |
-
doc_list.extend(get_text_file(file))
|
129 |
-
elif file.type in ['application/octet-stream', 'application/pdf']:
|
130 |
-
# file is .pdf
|
131 |
-
doc_list.extend(get_pdf_text(file))
|
132 |
-
elif file.type == 'text/csv':
|
133 |
-
# file is .csv
|
134 |
-
doc_list.extend(get_csv_file(file))
|
135 |
-
elif file.type == 'application/json':
|
136 |
-
# file is .json
|
137 |
-
doc_list.extend(get_json_file(file))
|
138 |
-
|
139 |
-
# get the text chunks
|
140 |
-
text_chunks = get_text_chunks(doc_list)
|
141 |
-
|
142 |
-
# create vector store
|
143 |
-
vectorstore = get_vectorstore(text_chunks)
|
144 |
-
|
145 |
-
# create conversation chain
|
146 |
-
st.session_state.conversation = get_conversation_chain(
|
147 |
-
vectorstore)
|
148 |
-
|
149 |
-
|
150 |
-
if __name__ == '__main__':
|
151 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|