bin20 commited on
Commit
1044662
ยท
1 Parent(s): f2c1ad0

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -147
app.py DELETED
@@ -1,147 +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
- # PDF ๋ฌธ์„œ๋กœ๋ถ€ํ„ฐ ํ…์ŠคํŠธ๋ฅผ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
16
- def get_pdf_text(pdf_docs):
17
- temp_dir = tempfile.TemporaryDirectory() # ์ž„์‹œ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
18
- temp_filepath = os.path.join(temp_dir.name, pdf_docs.name) # ์ž„์‹œ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
19
- with open(temp_filepath, "wb") as f: # ์ž„์‹œ ํŒŒ์ผ์„ ๋ฐ”์ด๋„ˆ๋ฆฌ ์“ฐ๊ธฐ ๋ชจ๋“œ๋กœ ์—ฝ๋‹ˆ๋‹ค.
20
- f.write(pdf_docs.getvalue()) # PDF ๋ฌธ์„œ์˜ ๋‚ด์šฉ์„ ์ž„์‹œ ํŒŒ์ผ์— ์”๋‹ˆ๋‹ค.
21
- pdf_loader = PyPDFLoader(temp_filepath) # PyPDFLoader๋ฅผ ์‚ฌ์šฉํ•ด PDF๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค.
22
- pdf_doc = pdf_loader.load() # ํ…์ŠคํŠธ๋ฅผ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.
23
- return pdf_doc # ์ถ”์ถœํ•œ ํ…์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
24
-
25
- # ๊ณผ์ œ
26
- # ์•„๋ž˜ ํ…์ŠคํŠธ ์ถ”์ถœ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑ
27
- def get_text_file(docs):
28
- pass
29
-
30
- def get_csv_file(docs):
31
- pass
32
-
33
- def get_json_file(docs):
34
- pass
35
-
36
-
37
- # ๋ฌธ์„œ๋“ค์„ ์ฒ˜๋ฆฌํ•˜์—ฌ ํ…์ŠคํŠธ ์ฒญํฌ๋กœ ๋‚˜๋ˆ„๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
38
- def get_text_chunks(documents):
39
- text_splitter = RecursiveCharacterTextSplitter(
40
- chunk_size=1000, # ์ฒญํฌ์˜ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
41
- chunk_overlap=200, # ์ฒญํฌ ์‚ฌ์ด์˜ ์ค‘๋ณต์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
42
- length_function=len # ํ…์ŠคํŠธ์˜ ๊ธธ์ด๋ฅผ ์ธก์ •ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
43
- )
44
-
45
- documents = text_splitter.split_documents(documents) # ๋ฌธ์„œ๋“ค์„ ์ฒญํฌ๋กœ ๋‚˜๋ˆ•๋‹ˆ๋‹ค.
46
- return documents # ๋‚˜๋ˆˆ ์ฒญํฌ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
47
-
48
-
49
- # ํ…์ŠคํŠธ ์ฒญํฌ๋“ค๋กœ๋ถ€ํ„ฐ ๋ฒกํ„ฐ ์Šคํ† ์–ด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
50
- def get_vectorstore(text_chunks):
51
- # ์›ํ•˜๋Š” ์ž„๋ฒ ๋”ฉ ๋ชจ๋ธ์„ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค.
52
- embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L12-v2',
53
- model_kwargs={'device': 'cpu'}) # ์ž„๋ฒ ๋”ฉ ๋ชจ๋ธ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
54
- vectorstore = FAISS.from_documents(text_chunks, embeddings) # FAISS ๋ฒกํ„ฐ ์Šคํ† ์–ด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
55
- return vectorstore # ์ƒ์„ฑ๋œ ๋ฒกํ„ฐ ์Šคํ† ์–ด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
56
-
57
-
58
- def get_conversation_chain(vectorstore):
59
- model_name_or_path = 'TheBloke/Llama-2-7B-chat-GGUF'
60
- model_basename = 'llama-2-7b-chat.Q2_K.gguf'
61
- model_path = hf_hub_download(repo_id=model_name_or_path, filename=model_basename)
62
-
63
- llm = LlamaCpp(model_path=model_path,
64
- n_ctx=4086,
65
- input={"temperature": 0.75, "max_length": 2000, "top_p": 1},
66
- verbose=True, )
67
- # ๋Œ€ํ™” ๊ธฐ๋ก์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
68
- memory = ConversationBufferMemory(
69
- memory_key='chat_history', return_messages=True)
70
- # ๋Œ€ํ™” ๊ฒ€์ƒ‰ ์ฒด์ธ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
71
- conversation_chain = ConversationalRetrievalChain.from_llm(
72
- llm=llm,
73
- retriever=vectorstore.as_retriever(),
74
- memory=memory
75
- )
76
- return conversation_chain # ์ƒ์„ฑ๋œ ๋Œ€ํ™” ์ฒด์ธ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
77
-
78
- # ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
79
- def handle_userinput(user_question):
80
- print('user_question => ', 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
- st.subheader("Your documents")
113
- docs = st.file_uploader(
114
- "Upload your PDFs here and click on 'Process'", accept_multiple_files=True)
115
- if st.button("Process"):
116
- with st.spinner("Processing"):
117
- # get pdf text
118
- doc_list = []
119
-
120
- for file in docs:
121
- print('file - type : ', file.type)
122
- if file.type == 'text/plain':
123
- # file is .txt
124
- doc_list.extend(get_text_file(file))
125
- elif file.type in ['application/octet-stream', 'application/pdf']:
126
- # file is .pdf
127
- doc_list.extend(get_pdf_text(file))
128
- elif file.type == 'text/csv':
129
- # file is .csv
130
- doc_list.extend(get_csv_file(file))
131
- elif file.type == 'application/json':
132
- # file is .json
133
- doc_list.extend(get_json_file(file))
134
-
135
- # get the text chunks
136
- text_chunks = get_text_chunks(doc_list)
137
-
138
- # create vector store
139
- vectorstore = get_vectorstore(text_chunks)
140
-
141
- # create conversation chain
142
- st.session_state.conversation = get_conversation_chain(
143
- vectorstore)
144
-
145
-
146
- if __name__ == '__main__':
147
- main()