Spaces:
Sleeping
Sleeping
๊น์์ค
commited on
Commit
ยท
a3dbf6a
1
Parent(s):
1d0d99f
modify segmentation
Browse files
app.py
CHANGED
@@ -1,5 +1,3 @@
|
|
1 |
-
import json
|
2 |
-
|
3 |
import streamlit as st
|
4 |
from dotenv import load_dotenv
|
5 |
from PyPDF2 import PdfReader
|
@@ -13,20 +11,18 @@ 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())
|
26 |
-
pdf_loader = PyPDFLoader(temp_filepath)
|
27 |
-
pdf_doc = pdf_loader.load()
|
28 |
-
return pdf_doc
|
29 |
-
|
30 |
|
31 |
# ๊ณผ์
|
32 |
# ์๋ ํ
์คํธ ์ถ์ถ ํจ์๋ฅผ ์์ฑ
|
@@ -50,35 +46,26 @@ def get_csv_file(csv_docs):
|
|
50 |
csv_doc = csv_loader.load() # ํ
์คํธ๋ฅผ ์ถ์ถํฉ๋๋ค.
|
51 |
return csv_doc # ์ถ์ถํ ํ
์คํธ๋ฅผ ๋ฐํํฉ๋๋ค.
|
52 |
|
53 |
-
|
54 |
def get_json_file(json_docs):
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
json_lines=True) # Loader๋ฅผ ์ฌ์ฉํด json๋ฅผ ๋ก๋ํฉ๋๋ค.
|
63 |
-
json_doc = json_loader.load() # ํ
์คํธ๋ฅผ ์ถ์ถํฉ๋๋ค.
|
64 |
-
|
65 |
-
return json_doc # ์ถ์ถํ ํ
์คํธ๋ฅผ ๋ฐํํฉ๋๋ค.
|
66 |
-
except json.JSONDecodeError as e:
|
67 |
-
print(f"Error decoding JSON: {e}")
|
68 |
-
# Handle the exception, raise or return a default value as needed
|
69 |
-
return None # ์์ธ๊ฐ ๋ฐ์ํ๋ฉด None์ ๋ฐํํ๊ฑฐ๋ ๋ค๋ฅธ ์ฒ๋ฆฌ๋ฅผ ์ํํ ์ ์์ต๋๋ค.
|
70 |
|
71 |
|
72 |
# ๋ฌธ์๋ค์ ์ฒ๋ฆฌํ์ฌ ํ
์คํธ ์ฒญํฌ๋ก ๋๋๋ ํจ์์
๋๋ค.
|
73 |
def get_text_chunks(documents):
|
74 |
text_splitter = RecursiveCharacterTextSplitter(
|
75 |
-
chunk_size=1000,
|
76 |
-
chunk_overlap=200,
|
77 |
-
length_function=len
|
78 |
)
|
79 |
|
80 |
-
documents = text_splitter.split_documents(documents)
|
81 |
-
return documents
|
82 |
|
83 |
|
84 |
# ํ
์คํธ ์ฒญํฌ๋ค๋ก๋ถํฐ ๋ฒกํฐ ์คํ ์ด๋ฅผ ์์ฑํ๋ ํจ์์
๋๋ค.
|
@@ -86,14 +73,14 @@ def get_vectorstore(text_chunks):
|
|
86 |
# OpenAI ์๋ฒ ๋ฉ ๋ชจ๋ธ์ ๋ก๋ํฉ๋๋ค. (Embedding models - Ada v2)
|
87 |
|
88 |
embeddings = OpenAIEmbeddings()
|
89 |
-
vectorstore = FAISS.from_documents(text_chunks, embeddings)
|
90 |
|
91 |
-
return vectorstore
|
92 |
|
93 |
|
94 |
def get_conversation_chain(vectorstore):
|
95 |
gpt_model_name = 'gpt-3.5-turbo'
|
96 |
-
llm = ChatOpenAI(model_name=gpt_model_name)
|
97 |
|
98 |
# ๋ํ ๊ธฐ๋ก์ ์ ์ฅํ๊ธฐ ์ํ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์์ฑํฉ๋๋ค.
|
99 |
memory = ConversationBufferMemory(
|
@@ -106,7 +93,6 @@ def get_conversation_chain(vectorstore):
|
|
106 |
)
|
107 |
return conversation_chain
|
108 |
|
109 |
-
|
110 |
# ์ฌ์ฉ์ ์
๋ ฅ์ ์ฒ๋ฆฌํ๋ ํจ์์
๋๋ค.
|
111 |
def handle_userinput(user_question):
|
112 |
# ๋ํ ์ฒด์ธ์ ์ฌ์ฉํ์ฌ ์ฌ์ฉ์ ์ง๋ฌธ์ ๋ํ ์๋ต์ ์์ฑํฉ๋๋ค.
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from dotenv import load_dotenv
|
3 |
from PyPDF2 import PdfReader
|
|
|
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 |
# PDF ๋ฌธ์๋ก๋ถํฐ ํ
์คํธ๋ฅผ ์ถ์ถํ๋ ํจ์์
๋๋ค.
|
18 |
def get_pdf_text(pdf_docs):
|
19 |
+
temp_dir = tempfile.TemporaryDirectory() # ์์ ๋๋ ํ ๋ฆฌ๋ฅผ ์์ฑํฉ๋๋ค.
|
20 |
+
temp_filepath = os.path.join(temp_dir.name, pdf_docs.name) # ์์ ํ์ผ ๊ฒฝ๋ก๋ฅผ ์์ฑํฉ๋๋ค.
|
21 |
with open(temp_filepath, "wb") as f: # ์์ ํ์ผ์ ๋ฐ์ด๋๋ฆฌ ์ฐ๊ธฐ ๋ชจ๋๋ก ์ฝ๋๋ค.
|
22 |
+
f.write(pdf_docs.getvalue()) # PDF ๋ฌธ์์ ๋ด์ฉ์ ์์ ํ์ผ์ ์๋๋ค.
|
23 |
+
pdf_loader = PyPDFLoader(temp_filepath) # PyPDFLoader๋ฅผ ์ฌ์ฉํด PDF๋ฅผ ๋ก๋ํฉ๋๋ค.
|
24 |
+
pdf_doc = pdf_loader.load() # ํ
์คํธ๋ฅผ ์ถ์ถํฉ๋๋ค.
|
25 |
+
return pdf_doc # ์ถ์ถํ ํ
์คํธ๋ฅผ ๋ฐํํฉ๋๋ค
|
|
|
26 |
|
27 |
# ๊ณผ์
|
28 |
# ์๋ ํ
์คํธ ์ถ์ถ ํจ์๋ฅผ ์์ฑ
|
|
|
46 |
csv_doc = csv_loader.load() # ํ
์คํธ๋ฅผ ์ถ์ถํฉ๋๋ค.
|
47 |
return csv_doc # ์ถ์ถํ ํ
์คํธ๋ฅผ ๋ฐํํฉ๋๋ค.
|
48 |
|
|
|
49 |
def get_json_file(json_docs):
|
50 |
+
temp_dir = tempfile.TemporaryDirectory() # ์์ ๋๋ ํ ๋ฆฌ๋ฅผ ์์ฑํฉ๋๋ค.
|
51 |
+
temp_filepath = os.path.join(temp_dir.name, json_docs.name) # ์์ ํ์ผ ๊ฒฝ๋ก๋ฅผ ์์ฑํฉ๋๋ค.
|
52 |
+
with open(temp_filepath, "wb") as f: # ์์ ํ์ผ์ ๋ฐ์ด๋๋ฆฌ ์ฐ๊ธฐ ๋ชจ๋๋ก ์ฝ๋๋ค.
|
53 |
+
f.write(json_docs.getvalue()) # json ๋ฌธ์์ ๋ด์ฉ์ ์์ ํ์ผ์ ์๋๋ค.
|
54 |
+
json_loader = JSONLoader(temp_filepath, jq_schema='.[].text') # Loader๋ฅผ ์ฌ์ฉํด json๋ฅผ ๋ก๋ํฉ๋๋ค.
|
55 |
+
json_doc = json_loader.load() # ํ
์คํธ๋ฅผ ์ถ์ถํฉ๋๋ค.
|
56 |
+
return json_doc # ์ถ์ถํ ํ
์คํธ๋ฅผ ๋ฐํํฉ๋๋ค.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
|
59 |
# ๋ฌธ์๋ค์ ์ฒ๋ฆฌํ์ฌ ํ
์คํธ ์ฒญํฌ๋ก ๋๋๋ ํจ์์
๋๋ค.
|
60 |
def get_text_chunks(documents):
|
61 |
text_splitter = RecursiveCharacterTextSplitter(
|
62 |
+
chunk_size=1000, # ์ฒญํฌ์ ํฌ๊ธฐ๋ฅผ ์ง์ ํฉ๋๋ค.
|
63 |
+
chunk_overlap=200, # ์ฒญํฌ ์ฌ์ด์ ์ค๋ณต์ ์ง์ ํฉ๋๋ค.
|
64 |
+
length_function=len # ํ
์คํธ์ ๊ธธ์ด๋ฅผ ์ธก์ ํ๋ ํจ์๋ฅผ ์ง์ ํฉ๋๋ค.
|
65 |
)
|
66 |
|
67 |
+
documents = text_splitter.split_documents(documents) # ๋ฌธ์๋ค์ ์ฒญํฌ๋ก ๋๋๋๋ค
|
68 |
+
return documents # ๋๋ ์ฒญํฌ๋ฅผ ๋ฐํํฉ๋๋ค.
|
69 |
|
70 |
|
71 |
# ํ
์คํธ ์ฒญํฌ๋ค๋ก๋ถํฐ ๋ฒกํฐ ์คํ ์ด๋ฅผ ์์ฑํ๋ ํจ์์
๋๋ค.
|
|
|
73 |
# OpenAI ์๋ฒ ๋ฉ ๋ชจ๋ธ์ ๋ก๋ํฉ๋๋ค. (Embedding models - Ada v2)
|
74 |
|
75 |
embeddings = OpenAIEmbeddings()
|
76 |
+
vectorstore = FAISS.from_documents(text_chunks, embeddings) # FAISS ๋ฒกํฐ ์คํ ์ด๋ฅผ ์์ฑํฉ๋๋ค.
|
77 |
|
78 |
+
return vectorstore # ์์ฑ๋ ๋ฒกํฐ ์คํ ์ด๋ฅผ ๋ฐํํฉ๋๋ค.
|
79 |
|
80 |
|
81 |
def get_conversation_chain(vectorstore):
|
82 |
gpt_model_name = 'gpt-3.5-turbo'
|
83 |
+
llm = ChatOpenAI(model_name = gpt_model_name) #gpt-3.5 ๋ชจ๋ธ ๋ก๋
|
84 |
|
85 |
# ๋ํ ๊ธฐ๋ก์ ์ ์ฅํ๊ธฐ ์ํ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์์ฑํฉ๋๋ค.
|
86 |
memory = ConversationBufferMemory(
|
|
|
93 |
)
|
94 |
return conversation_chain
|
95 |
|
|
|
96 |
# ์ฌ์ฉ์ ์
๋ ฅ์ ์ฒ๋ฆฌํ๋ ํจ์์
๋๋ค.
|
97 |
def handle_userinput(user_question):
|
98 |
# ๋ํ ์ฒด์ธ์ ์ฌ์ฉํ์ฌ ์ฌ์ฉ์ ์ง๋ฌธ์ ๋ํ ์๋ต์ ์์ฑํฉ๋๋ค.
|