Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,41 +27,50 @@ def get_pdf_text(pdf_docs):
|
|
| 27 |
|
| 28 |
# ๊ณผ์
|
| 29 |
# ์๋ ํ
์คํธ ์ถ์ถ ํจ์๋ฅผ ์์ฑ
|
| 30 |
-
def get_text_file(docs):
|
| 31 |
-
text_content = ""
|
| 32 |
-
|
| 33 |
-
# Hugging Face ๋ชจ๋ธ์ ๋ก๋ํฉ๋๋ค. (์์๋ก 'bert-base-uncased' ๋ชจ๋ธ์ ์ฌ์ฉํฉ๋๋ค)
|
| 34 |
-
model_name = 'bert-base-uncased'
|
| 35 |
-
model = HuggingFaceEmbeddings(model_name)
|
| 36 |
-
|
| 37 |
-
# ํ
์คํธ ์ถ์ถ์ ์ํด ๋ฌธ์๋ฅผ ๋ก๋ํฉ๋๋ค.
|
| 38 |
-
text_loader = TextLoader(docs.getvalue().decode('utf-8'))
|
| 39 |
-
document = text_loader.load()
|
| 40 |
-
|
| 41 |
import csv
|
| 42 |
import json
|
| 43 |
-
from
|
| 44 |
-
|
| 45 |
-
def get_csv_file(docs):
|
| 46 |
-
with NamedTemporaryFile(delete=False) as temp_file:
|
| 47 |
-
temp_file.write(docs.getvalue().encode('utf-8'))
|
| 48 |
-
temp_file.seek(0)
|
| 49 |
-
|
| 50 |
-
csv_data = []
|
| 51 |
-
csv_reader = csv.reader(temp_file)
|
| 52 |
-
for row in csv_reader:
|
| 53 |
-
csv_data.append(row)
|
| 54 |
-
|
| 55 |
-
return csv_data
|
| 56 |
-
|
| 57 |
-
def get_json_file(docs):
|
| 58 |
-
with NamedTemporaryFile(delete=False) as temp_file:
|
| 59 |
-
temp_file.write(docs.getvalue().encode('utf-8'))
|
| 60 |
-
temp_file.seek(0)
|
| 61 |
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
|
| 67 |
|
|
|
|
| 27 |
|
| 28 |
# ๊ณผ์
|
| 29 |
# ์๋ ํ
์คํธ ์ถ์ถ ํจ์๋ฅผ ์์ฑ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
import csv
|
| 31 |
import json
|
| 32 |
+
from PyPDF2 import PdfReader
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
def get_text_from_document(doc_path):
|
| 35 |
+
# ๋ฌธ์์ ํ์ฅ์์ ๋ฐ๋ผ ๋ค๋ฅธ ์ฒ๋ฆฌ๋ฅผ ์ํ
|
| 36 |
+
if doc_path.endswith('.txt'):
|
| 37 |
+
# ํ
์คํธ ํ์ผ์ ๊ฒฝ์ฐ
|
| 38 |
+
with open(doc_path, 'r', encoding='utf-8') as file:
|
| 39 |
+
text = file.read()
|
| 40 |
+
return text
|
| 41 |
+
elif doc_path.endswith('.csv'):
|
| 42 |
+
# CSV ํ์ผ์ ๊ฒฝ์ฐ
|
| 43 |
+
with open(doc_path, 'r', encoding='utf-8') as file:
|
| 44 |
+
csv_reader = csv.reader(file)
|
| 45 |
+
text = '\n'.join(','.join(row) for row in csv_reader)
|
| 46 |
+
return text
|
| 47 |
+
elif doc_path.endswith('.json'):
|
| 48 |
+
# JSON ํ์ผ์ ๊ฒฝ์ฐ
|
| 49 |
+
with open(doc_path, 'r', encoding='utf-8') as file:
|
| 50 |
+
data = json.load(file)
|
| 51 |
+
text = json.dumps(data, indent=2)
|
| 52 |
+
return text
|
| 53 |
+
elif doc_path.endswith('.pdf'):
|
| 54 |
+
# PDF ํ์ผ์ ๊ฒฝ์ฐ
|
| 55 |
+
with open(doc_path, 'rb') as file:
|
| 56 |
+
pdf_reader = PdfReader(file)
|
| 57 |
+
text = ''
|
| 58 |
+
for page_num in range(len(pdf_reader.pages)):
|
| 59 |
+
text += pdf_reader.pages[page_num].extract_text()
|
| 60 |
+
return text
|
| 61 |
+
else:
|
| 62 |
+
# ์ง์ํ์ง ์๋ ํ์ฅ์์ธ ๊ฒฝ์ฐ
|
| 63 |
+
raise ValueError(f"Unsupported file extension: {doc_path}")
|
| 64 |
|
| 65 |
+
def get_text_file(docs):
|
| 66 |
+
# ๊ฐ ๋ฌธ์์ ๋ํ ์ฒ๋ฆฌ๋ฅผ ์ํ
|
| 67 |
+
for doc in docs:
|
| 68 |
+
try:
|
| 69 |
+
text = get_text_from_document(doc)
|
| 70 |
+
# ์ฌ๊ธฐ์์ text๋ฅผ ์ฌ์ฉํ์ฌ ์ถ๊ฐ์ ์ธ ์ ์ฒ๋ฆฌ ๋ก์ง์ ์ํํ ์ ์์
|
| 71 |
+
print(f"Text from {doc}:\n{text}\n")
|
| 72 |
+
except ValueError as e:
|
| 73 |
+
print(f"Error processing {doc}: {e}")
|
| 74 |
|
| 75 |
|
| 76 |
|