Update app.py
Browse files
app.py
CHANGED
@@ -24,9 +24,10 @@ def get_pdf_text(pdf_docs):
|
|
24 |
|
25 |
# ๊ณผ์
|
26 |
# ์๋ ํ
์คํธ ์ถ์ถ ํจ์๋ฅผ ์์ฑ
|
|
|
27 |
def get_text_file(text_docs):
|
28 |
temp_dir = tempfile.TemporaryDirectory() # ์์ ๋๋ ํ ๋ฆฌ๋ฅผ ์์ฑํฉ๋๋ค.
|
29 |
-
temp_filepath = os.path.join(temp_dir.name, temp_file.txt) # ์์ ํ์ผ ๊ฒฝ๋ก๋ฅผ ์์ฑํฉ๋๋ค.
|
30 |
with open(temp_filepath, "wb") as f:
|
31 |
f.write(text_docs.getvalue()) # text ๋ฌธ์์ ๋ด์ฉ์ ์์ ํ์ผ์ ์๋๋ค.
|
32 |
text_loader = TextLoader(temp_filepath) # TextLoader๋ฅผ ์ฌ์ฉํด text๋ฅผ ๋ก๋ํฉ๋๋ค.
|
@@ -35,7 +36,7 @@ def get_text_file(text_docs):
|
|
35 |
|
36 |
def get_csv_file(csv_docs):
|
37 |
temp_dir = tempfile.TemporaryDirectory() # ์์ ๋๋ ํ ๋ฆฌ๋ฅผ ์์ฑํฉ๋๋ค.
|
38 |
-
temp_filepath = os.path.join(temp_dir.name, temp_file.csv) # ์์ ํ์ผ ๊ฒฝ๋ก๋ฅผ ์์ฑํฉ๋๋ค.
|
39 |
with open(temp_filepath, "wb") as f:
|
40 |
f.write(csv_docs.getvalue()) # CSV ๋ฌธ์์ ๋ด์ฉ์ ์์ ํ์ผ์ ์๋๋ค.
|
41 |
csv_loader = CSVLoader(temp_filepath) # CSVLoader๋ฅผ ์ฌ์ฉํด CSV๋ฅผ ๋ก๋ํฉ๋๋ค.
|
@@ -43,14 +44,23 @@ def get_csv_file(csv_docs):
|
|
43 |
return csv_doc # ์ถ์ถํ ํ
์คํธ๋ฅผ ๋ฐํํฉ๋๋ค.
|
44 |
|
45 |
def get_json_file(json_docs):
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
# ๋ฌธ์๋ค์ ์ฒ๋ฆฌํ์ฌ ํ
์คํธ ์ฒญํฌ๋ก ๋๋๋ ํจ์์
๋๋ค.
|
55 |
def get_text_chunks(documents):
|
56 |
text_splitter = RecursiveCharacterTextSplitter(
|
|
|
24 |
|
25 |
# ๊ณผ์
|
26 |
# ์๋ ํ
์คํธ ์ถ์ถ ํจ์๋ฅผ ์์ฑ
|
27 |
+
|
28 |
def get_text_file(text_docs):
|
29 |
temp_dir = tempfile.TemporaryDirectory() # ์์ ๋๋ ํ ๋ฆฌ๋ฅผ ์์ฑํฉ๋๋ค.
|
30 |
+
temp_filepath = os.path.join(temp_dir.name, "temp_file.txt") # ์์ ํ์ผ ๊ฒฝ๋ก๋ฅผ ์์ฑํฉ๋๋ค.
|
31 |
with open(temp_filepath, "wb") as f:
|
32 |
f.write(text_docs.getvalue()) # text ๋ฌธ์์ ๋ด์ฉ์ ์์ ํ์ผ์ ์๋๋ค.
|
33 |
text_loader = TextLoader(temp_filepath) # TextLoader๋ฅผ ์ฌ์ฉํด text๋ฅผ ๋ก๋ํฉ๋๋ค.
|
|
|
36 |
|
37 |
def get_csv_file(csv_docs):
|
38 |
temp_dir = tempfile.TemporaryDirectory() # ์์ ๋๋ ํ ๋ฆฌ๋ฅผ ์์ฑํฉ๋๋ค.
|
39 |
+
temp_filepath = os.path.join(temp_dir.name, "temp_file.csv") # ์์ ํ์ผ ๊ฒฝ๋ก๋ฅผ ์์ฑํฉ๋๋ค.
|
40 |
with open(temp_filepath, "wb") as f:
|
41 |
f.write(csv_docs.getvalue()) # CSV ๋ฌธ์์ ๋ด์ฉ์ ์์ ํ์ผ์ ์๋๋ค.
|
42 |
csv_loader = CSVLoader(temp_filepath) # CSVLoader๋ฅผ ์ฌ์ฉํด CSV๋ฅผ ๋ก๋ํฉ๋๋ค.
|
|
|
44 |
return csv_doc # ์ถ์ถํ ํ
์คํธ๋ฅผ ๋ฐํํฉ๋๋ค.
|
45 |
|
46 |
def get_json_file(json_docs):
|
47 |
+
try:
|
48 |
+
temp_dir = tempfile.TemporaryDirectory() # ์์ ๋๋ ํ ๋ฆฌ๋ฅผ ์์ฑํฉ๋๋ค.
|
49 |
+
temp_filepath = os.path.join(temp_dir.name, "temp_file.json") # ์์ ํ์ผ ๊ฒฝ๋ก๋ฅผ ์์ฑํฉ๋๋ค.
|
50 |
+
with open(temp_filepath, "wb") as f:
|
51 |
+
f.write(json_docs.getvalue()) # JSON ๋ฌธ์์ ๋ด์ฉ์ ์์ ํ์ผ์ ์๋๋ค.
|
52 |
+
json_loader = JSONLoader(temp_filepath, jq_schema='.messages[].content', text_content=False) # JSONLoader๋ฅผ ์ฌ์ฉํด JSON๋ฅผ ๋ก๋ํฉ๋๋ค.
|
53 |
+
json_doc = json_loader.load() # ํ
์คํธ๋ฅผ ์ถ์ถํฉ๋๋ค.
|
54 |
+
return json_doc # ์ถ์ถํ ํ
์คํธ๋ฅผ ๋ฐํํฉ๋๋ค.
|
55 |
+
except Exception as e:
|
56 |
+
st.error(f"Error loading JSON file: {e}")
|
57 |
+
return []
|
58 |
+
|
59 |
+
|
60 |
+
|
61 |
+
|
62 |
+
|
63 |
+
|
64 |
# ๋ฌธ์๋ค์ ์ฒ๋ฆฌํ์ฌ ํ
์คํธ ์ฒญํฌ๋ก ๋๋๋ ํจ์์
๋๋ค.
|
65 |
def get_text_chunks(documents):
|
66 |
text_splitter = RecursiveCharacterTextSplitter(
|