nijoow commited on
Commit
ff6051d
ยท
1 Parent(s): 3bc26b2

Delete import streamlit as st.py

Browse files
Files changed (1) hide show
  1. import streamlit as st.py +0 -175
import streamlit as st.py DELETED
@@ -1,175 +0,0 @@
1
- import streamlit as st
2
- from dotenv import load_dotenv
3
- from langchain.text_splitter import CharacterTextSplitter, RecursiveCharacterTextSplitter
4
- from langchain.vectorstores import FAISS
5
- from langchain.embeddings import HuggingFaceEmbeddings # General embeddings from HuggingFace models.
6
- from langchain.memory import ConversationBufferMemory
7
- from langchain.chains import ConversationalRetrievalChain
8
- from htmlTemplates import css, bot_template, user_template
9
- from langchain.llms import LlamaCpp # For loading transformer models.
10
- from langchain.document_loaders import PyPDFLoader, TextLoader, JSONLoader, CSVLoader
11
- import tempfile # ์ž„์‹œ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค.
12
- import os
13
- from huggingface_hub import hf_hub_download # Hugging Face Hub์—์„œ ๋ชจ๋ธ์„ ๋‹ค์šด๋กœ๋“œํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
14
-
15
-
16
- # PDF ๋ฌธ์„œ๋กœ๋ถ€ํ„ฐ ํ…์ŠคํŠธ๋ฅผ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
17
- def get_pdf_text(pdf_docs):
18
- temp_dir = tempfile.TemporaryDirectory() # ์ž„์‹œ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
19
- temp_filepath = os.path.join(temp_dir.name, pdf_docs.name) # ์ž„์‹œ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
20
- with open(temp_filepath, "wb") as f: # ์ž„์‹œ ํŒŒ์ผ์„ ๋ฐ”์ด๋„ˆ๋ฆฌ ์“ฐ๊ธฐ ๋ชจ๋“œ๋กœ ์—ฝ๋‹ˆ๋‹ค.
21
- f.write(pdf_docs.getvalue()) # PDF ๋ฌธ์„œ์˜ ๋‚ด์šฉ์„ ์ž„์‹œ ํŒŒ์ผ์— ์”๋‹ˆ๋‹ค.
22
- pdf_loader = PyPDFLoader(temp_filepath) # PyPDFLoader๋ฅผ ์‚ฌ์šฉํ•ด PDF๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค.
23
- pdf_doc = pdf_loader.load() # ํ…์ŠคํŠธ๋ฅผ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.
24
- return pdf_doc # ์ถ”์ถœํ•œ ํ…์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
25
-
26
-
27
- # ๊ณผ์ œ
28
- # ์•„๋ž˜ ํ…์ŠคํŠธ ์ถ”์ถœ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑ
29
- def get_text_file(docs):
30
- with NamedTemporaryFile() as temp_file:
31
- temp_file.write(docs.getvalue())
32
- temp_file.seek(0)
33
-
34
- # ํ…์ŠคํŠธ ํŒŒ์ผ์—์„œ ํ…์ŠคํŠธ๋ฅผ ์ถ”์ถœํ•˜๋Š” ๋กœ์ง์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.
35
- text_content = temp_file.read().decode('utf-8')
36
-
37
- return text_content
38
-
39
-
40
- import csv
41
- import json
42
- from tempfile import NamedTemporaryFile
43
-
44
- def get_csv_file(docs):
45
- with NamedTemporaryFile() as temp_file:
46
- temp_file.write(docs.getvalue())
47
- temp_file.seek(0)
48
-
49
- csv_data = []
50
- csv_reader = csv.reader(temp_file)
51
- for row in csv_reader:
52
- csv_data.append(row)
53
- return csv_data
54
-
55
- def get_json_file(docs):
56
- with NamedTemporaryFile() as temp_file:
57
- temp_file.write(docs.getvalue())
58
- temp_file.seek(0)
59
-
60
- json_data = json.load(temp_file)
61
- return json_data
62
-
63
-
64
- # ๋ฌธ์„œ๋“ค์„ ์ฒ˜๋ฆฌํ•˜์—ฌ ํ…์ŠคํŠธ ์ฒญํฌ๋กœ ๋‚˜๋ˆ„๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
65
- def get_text_chunks(documents):
66
- text_splitter = RecursiveCharacterTextSplitter(
67
- chunk_size=1000, # ์ฒญํฌ์˜ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
68
- chunk_overlap=200, # ์ฒญํฌ ์‚ฌ์ด์˜ ์ค‘๋ณต์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
69
- length_function=len # ํ…์ŠคํŠธ์˜ ๊ธธ์ด๋ฅผ ์ธก์ •ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
70
- )
71
-
72
- documents = text_splitter.split_documents(documents) # ๋ฌธ์„œ๋“ค์„ ์ฒญํฌ๋กœ ๋‚˜๋ˆ•๋‹ˆ๋‹ค.
73
- return documents # ๋‚˜๋ˆˆ ์ฒญํฌ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
74
-
75
-
76
- # ํ…์ŠคํŠธ ์ฒญํฌ๋“ค๋กœ๋ถ€ํ„ฐ ๋ฒกํ„ฐ ์Šคํ† ์–ด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
77
- def get_vectorstore(text_chunks):
78
- # ์›ํ•˜๋Š” ์ž„๋ฒ ๋”ฉ ๋ชจ๋ธ์„ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค.
79
- embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L12-v2',
80
- model_kwargs={'device': 'cpu'}) # ์ž„๋ฒ ๋”ฉ ๋ชจ๋ธ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
81
- vectorstore = FAISS.from_documents(text_chunks, embeddings) # FAISS ๋ฒกํ„ฐ ์Šคํ† ์–ด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
82
- return vectorstore # ์ƒ์„ฑ๋œ ๋ฒกํ„ฐ ์Šคํ† ์–ด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
83
-
84
-
85
- def get_conversation_chain(vectorstore):
86
- model_name_or_path = 'TheBloke/Llama-2-7B-chat-GGUF'
87
- model_basename = 'llama-2-7b-chat.Q2_K.gguf'
88
- model_path = hf_hub_download(repo_id=model_name_or_path, filename=model_basename)
89
-
90
- llm = LlamaCpp(model_path=model_path,
91
- n_ctx=4086,
92
- input={"temperature": 0.75, "max_length": 2000, "top_p": 1},
93
- verbose=True, )
94
- # ๋Œ€ํ™” ๊ธฐ๋ก์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
95
- memory = ConversationBufferMemory(
96
- memory_key='chat_history', return_messages=True)
97
- # ๋Œ€ํ™” ๊ฒ€์ƒ‰ ์ฒด์ธ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
98
- conversation_chain = ConversationalRetrievalChain.from_llm(
99
- llm=llm,
100
- retriever=vectorstore.as_retriever(),
101
- memory=memory
102
- )
103
- return conversation_chain # ์ƒ์„ฑ๋œ ๋Œ€ํ™” ์ฒด์ธ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
104
-
105
-
106
- # ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
107
- def handle_userinput(user_question):
108
- print('user_question => ', user_question)
109
- # ๋Œ€ํ™” ์ฒด์ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž ์งˆ๋ฌธ์— ๋Œ€ํ•œ ์‘๋‹ต์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
110
- response = st.session_state.conversation({'question': user_question})
111
- # ๋Œ€ํ™” ๊ธฐ๋ก์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
112
- st.session_state.chat_history = response['chat_history']
113
-
114
- for i, message in enumerate(st.session_state.chat_history):
115
- if i % 2 == 0:
116
- st.write(user_template.replace(
117
- "{{MSG}}", message.content), unsafe_allow_html=True)
118
- else:
119
- st.write(bot_template.replace(
120
- "{{MSG}}", message.content), unsafe_allow_html=True)
121
-
122
-
123
- def main():
124
- load_dotenv()
125
- st.set_page_config(page_title="Chat with multiple Files",
126
- page_icon=":books:")
127
- st.write(css, unsafe_allow_html=True)
128
-
129
- if "conversation" not in st.session_state:
130
- st.session_state.conversation = None
131
- if "chat_history" not in st.session_state:
132
- st.session_state.chat_history = None
133
-
134
- st.header("Chat with multiple Files:")
135
- user_question = st.text_input("Ask a question about your documents:")
136
- if user_question:
137
- handle_userinput(user_question)
138
-
139
- with st.sidebar:
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()