Spaces:
Sleeping
Sleeping
akman12914
commited on
Commit
Β·
e70c205
1
Parent(s):
3dd6761
assignment function write
Browse files
app.py
CHANGED
@@ -11,44 +11,69 @@ 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
|
|
|
|
|
|
|
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())
|
24 |
-
pdf_loader = PyPDFLoader(temp_filepath)
|
25 |
-
pdf_doc = pdf_loader.load()
|
26 |
-
return pdf_doc
|
|
|
27 |
|
28 |
# κ³Όμ
|
29 |
# μλ ν
μ€νΈ μΆμΆ ν¨μλ₯Ό μμ±
|
30 |
|
31 |
def get_text_file(docs):
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
|
35 |
def get_csv_file(docs):
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
def get_json_file(docs):
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
# ν
μ€νΈ μ²ν¬λ€λ‘λΆν° λ²‘ν° μ€ν μ΄λ₯Ό μμ±νλ ν¨μμ
λλ€.
|
@@ -56,15 +81,15 @@ def get_vectorstore(text_chunks):
|
|
56 |
# OpenAI μλ² λ© λͺ¨λΈμ λ‘λν©λλ€. (Embedding models - Ada v2)
|
57 |
|
58 |
embeddings = OpenAIEmbeddings()
|
59 |
-
vectorstore = FAISS.from_documents(text_chunks, embeddings)
|
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
|
67 |
-
|
68 |
# λν κΈ°λ‘μ μ μ₯νκΈ° μν λ©λͺ¨λ¦¬λ₯Ό μμ±ν©λλ€.
|
69 |
memory = ConversationBufferMemory(
|
70 |
memory_key='chat_history', return_messages=True)
|
@@ -76,6 +101,7 @@ def get_conversation_chain(vectorstore):
|
|
76 |
)
|
77 |
return conversation_chain
|
78 |
|
|
|
79 |
# μ¬μ©μ μ
λ ₯μ μ²λ¦¬νλ ν¨μμ
λλ€.
|
80 |
def handle_userinput(user_question):
|
81 |
# λν 체μΈμ μ¬μ©νμ¬ μ¬μ©μ μ§λ¬Έμ λν μλ΅μ μμ±ν©λλ€.
|
|
|
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 json
|
15 |
+
from pathlib import Path
|
16 |
+
from pprint import pprint
|
17 |
+
import tempfile # μμ νμΌμ μμ±νκΈ° μν λΌμ΄λΈλ¬λ¦¬μ
λλ€.
|
18 |
import os
|
19 |
|
20 |
|
21 |
# PDF λ¬Έμλ‘λΆν° ν
μ€νΈλ₯Ό μΆμΆνλ ν¨μμ
λλ€.
|
22 |
def get_pdf_text(pdf_docs):
|
23 |
+
temp_dir = tempfile.TemporaryDirectory() # μμ λλ ν 리λ₯Ό μμ±ν©λλ€.
|
24 |
+
temp_filepath = os.path.join(temp_dir.name, pdf_docs.name) # μμ νμΌ κ²½λ‘λ₯Ό μμ±ν©λλ€.
|
25 |
with open(temp_filepath, "wb") as f: # μμ νμΌμ λ°μ΄λ리 μ°κΈ° λͺ¨λλ‘ μ½λλ€.
|
26 |
+
f.write(pdf_docs.getvalue()) # PDF λ¬Έμμ λ΄μ©μ μμ νμΌμ μλλ€.
|
27 |
+
pdf_loader = PyPDFLoader(temp_filepath) # PyPDFLoaderλ₯Ό μ¬μ©ν΄ PDFλ₯Ό λ‘λν©λλ€.
|
28 |
+
pdf_doc = pdf_loader.load() # ν
μ€νΈλ₯Ό μΆμΆν©λλ€.
|
29 |
+
return pdf_doc # μΆμΆν ν
μ€νΈλ₯Ό λ°νν©λλ€.
|
30 |
+
|
31 |
|
32 |
# κ³Όμ
|
33 |
# μλ ν
μ€νΈ μΆμΆ ν¨μλ₯Ό μμ±
|
34 |
|
35 |
def get_text_file(docs):
|
36 |
+
temp_dir = tempfile.TemporaryDirectory()
|
37 |
+
temp_filepath = os.path.join(temp_dir.name, docs.name)
|
38 |
+
with open(temp_filepath, "wb") as f:
|
39 |
+
f.write(docs.getvalue())
|
40 |
+
text_loader = TextLoader(temp_filepath)
|
41 |
+
text_doc = text_loader.load()
|
42 |
+
return text_doc
|
43 |
|
44 |
|
45 |
def get_csv_file(docs):
|
46 |
+
temp_dir = tempfile.TemporaryDirectory()
|
47 |
+
temp_filepath = os.path.join(temp_dir.name, docs.name)
|
48 |
+
with open(temp_filepath, "wb") as f:
|
49 |
+
f.write(docs.getvalue())
|
50 |
+
csv_loader = CSVLoader(temp_filepath)
|
51 |
+
csv_doc = csv_loader.load()
|
52 |
+
return csv_doc
|
53 |
+
|
54 |
|
55 |
def get_json_file(docs):
|
56 |
+
temp_dir = tempfile.TemporaryDirectory()
|
57 |
+
temp_filepath = os.path.join(temp_dir.name, docs.name)
|
58 |
+
with open(temp_filepath, 'wb') as f:
|
59 |
+
f.write(docs.getvalue())
|
60 |
+
json_loader = JSONLoader(file_path=temp_filepath,
|
61 |
+
jq_schema='.messages[].content',
|
62 |
+
text_content=False)
|
63 |
+
json_doc = json_loader.load()
|
64 |
+
return json_doc
|
65 |
+
|
66 |
|
|
|
67 |
# λ¬Έμλ€μ μ²λ¦¬νμ¬ ν
μ€νΈ μ²ν¬λ‘ λλλ ν¨μμ
λλ€.
|
68 |
def get_text_chunks(documents):
|
69 |
text_splitter = RecursiveCharacterTextSplitter(
|
70 |
+
chunk_size=1000, # μ²ν¬μ ν¬κΈ°λ₯Ό μ§μ ν©λλ€.
|
71 |
+
chunk_overlap=200, # μ²ν¬ μ¬μ΄μ μ€λ³΅μ μ§μ ν©λλ€.
|
72 |
+
length_function=len # ν
μ€νΈμ κΈΈμ΄λ₯Ό μΈ‘μ νλ ν¨μλ₯Ό μ§μ ν©λλ€.
|
73 |
)
|
74 |
|
75 |
+
documents = text_splitter.split_documents(documents) # λ¬Έμλ€μ μ²ν¬λ‘ λλλλ€
|
76 |
+
return documents # λλ μ²ν¬λ₯Ό λ°νν©λλ€.
|
77 |
|
78 |
|
79 |
# ν
μ€νΈ μ²ν¬λ€λ‘λΆν° λ²‘ν° μ€ν μ΄λ₯Ό μμ±νλ ν¨μμ
λλ€.
|
|
|
81 |
# OpenAI μλ² λ© λͺ¨λΈμ λ‘λν©λλ€. (Embedding models - Ada v2)
|
82 |
|
83 |
embeddings = OpenAIEmbeddings()
|
84 |
+
vectorstore = FAISS.from_documents(text_chunks, embeddings) # FAISS λ²‘ν° μ€ν μ΄λ₯Ό μμ±ν©λλ€.
|
85 |
|
86 |
+
return vectorstore # μμ±λ λ²‘ν° μ€ν μ΄λ₯Ό λ°νν©λλ€.
|
87 |
|
88 |
|
89 |
def get_conversation_chain(vectorstore):
|
90 |
gpt_model_name = 'gpt-3.5-turbo'
|
91 |
+
llm = ChatOpenAI(model_name=gpt_model_name) # gpt-3.5 λͺ¨λΈ λ‘λ
|
92 |
+
|
93 |
# λν κΈ°λ‘μ μ μ₯νκΈ° μν λ©λͺ¨λ¦¬λ₯Ό μμ±ν©λλ€.
|
94 |
memory = ConversationBufferMemory(
|
95 |
memory_key='chat_history', return_messages=True)
|
|
|
101 |
)
|
102 |
return conversation_chain
|
103 |
|
104 |
+
|
105 |
# μ¬μ©μ μ
λ ₯μ μ²λ¦¬νλ ν¨μμ
λλ€.
|
106 |
def handle_userinput(user_question):
|
107 |
# λν 체μΈμ μ¬μ©νμ¬ μ¬μ©μ μ§λ¬Έμ λν μλ΅μ μμ±ν©λλ€.
|