Spaces:
Sleeping
Sleeping
Tuchuanhuhuhu
commited on
Commit
·
c87878a
1
Parent(s):
aebda89
支持Excel文件
Browse files- modules/llama_func.py +18 -12
- modules/utils.py +33 -0
- requirements.txt +1 -0
modules/llama_func.py
CHANGED
@@ -40,34 +40,40 @@ def get_documents(file_src):
|
|
40 |
logging.debug("Loading documents...")
|
41 |
logging.debug(f"file_src: {file_src}")
|
42 |
for file in file_src:
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
45 |
logging.debug("Loading PDF...")
|
46 |
try:
|
47 |
from modules.pdf_func import parse_pdf
|
48 |
from modules.config import advance_docs
|
49 |
two_column = advance_docs["pdf"].get("two_column", False)
|
50 |
-
pdftext = parse_pdf(
|
51 |
except:
|
52 |
pdftext = ""
|
53 |
-
with open(
|
54 |
pdfReader = PyPDF2.PdfReader(pdfFileObj)
|
55 |
for page in tqdm(pdfReader.pages):
|
56 |
pdftext += page.extract_text()
|
57 |
text_raw = pdftext
|
58 |
-
elif
|
59 |
-
logging.debug("Loading
|
60 |
DocxReader = download_loader("DocxReader")
|
61 |
loader = DocxReader()
|
62 |
-
text_raw = loader.load_data(file=
|
63 |
-
elif
|
64 |
logging.debug("Loading EPUB...")
|
65 |
EpubReader = download_loader("EpubReader")
|
66 |
loader = EpubReader()
|
67 |
-
text_raw = loader.load_data(file=
|
|
|
|
|
|
|
68 |
else:
|
69 |
logging.debug("Loading text file...")
|
70 |
-
with open(
|
71 |
text_raw = f.read()
|
72 |
text = add_space(text_raw)
|
73 |
# text = block_split(text)
|
@@ -89,7 +95,7 @@ def construct_index(
|
|
89 |
):
|
90 |
from langchain.chat_models import ChatOpenAI
|
91 |
from llama_index import GPTSimpleVectorIndex, ServiceContext
|
92 |
-
|
93 |
os.environ["OPENAI_API_KEY"] = api_key
|
94 |
chunk_size_limit = None if chunk_size_limit == 0 else chunk_size_limit
|
95 |
embedding_limit = None if embedding_limit == 0 else embedding_limit
|
@@ -122,7 +128,7 @@ def construct_index(
|
|
122 |
logging.error("索引构建失败!", e)
|
123 |
print(e)
|
124 |
return None
|
125 |
-
|
126 |
|
127 |
def add_space(text):
|
128 |
punctuations = {",": ", ", "。": "。 ", "?": "? ", "!": "! ", ":": ": ", ";": "; "}
|
|
|
40 |
logging.debug("Loading documents...")
|
41 |
logging.debug(f"file_src: {file_src}")
|
42 |
for file in file_src:
|
43 |
+
filepath = file.name
|
44 |
+
filename = os.path.basename(filepath)
|
45 |
+
file_type = os.path.splitext(filepath)[1]
|
46 |
+
logging.info(f"loading file: {filename}")
|
47 |
+
if file_type == ".pdf":
|
48 |
logging.debug("Loading PDF...")
|
49 |
try:
|
50 |
from modules.pdf_func import parse_pdf
|
51 |
from modules.config import advance_docs
|
52 |
two_column = advance_docs["pdf"].get("two_column", False)
|
53 |
+
pdftext = parse_pdf(filepath, two_column).text
|
54 |
except:
|
55 |
pdftext = ""
|
56 |
+
with open(filepath, 'rb') as pdfFileObj:
|
57 |
pdfReader = PyPDF2.PdfReader(pdfFileObj)
|
58 |
for page in tqdm(pdfReader.pages):
|
59 |
pdftext += page.extract_text()
|
60 |
text_raw = pdftext
|
61 |
+
elif file_type == ".docx":
|
62 |
+
logging.debug("Loading Word...")
|
63 |
DocxReader = download_loader("DocxReader")
|
64 |
loader = DocxReader()
|
65 |
+
text_raw = loader.load_data(file=filepath)[0].text
|
66 |
+
elif file_type == ".epub":
|
67 |
logging.debug("Loading EPUB...")
|
68 |
EpubReader = download_loader("EpubReader")
|
69 |
loader = EpubReader()
|
70 |
+
text_raw = loader.load_data(file=filepath)[0].text
|
71 |
+
elif file_type == ".xlsx":
|
72 |
+
logging.debug("Loading Excel...")
|
73 |
+
text_raw = excel_to_string(filepath)
|
74 |
else:
|
75 |
logging.debug("Loading text file...")
|
76 |
+
with open(filepath, "r", encoding="utf-8") as f:
|
77 |
text_raw = f.read()
|
78 |
text = add_space(text_raw)
|
79 |
# text = block_split(text)
|
|
|
95 |
):
|
96 |
from langchain.chat_models import ChatOpenAI
|
97 |
from llama_index import GPTSimpleVectorIndex, ServiceContext
|
98 |
+
|
99 |
os.environ["OPENAI_API_KEY"] = api_key
|
100 |
chunk_size_limit = None if chunk_size_limit == 0 else chunk_size_limit
|
101 |
embedding_limit = None if embedding_limit == 0 else embedding_limit
|
|
|
128 |
logging.error("索引构建失败!", e)
|
129 |
print(e)
|
130 |
return None
|
131 |
+
|
132 |
|
133 |
def add_space(text):
|
134 |
punctuations = {",": ", ", "。": "。 ", "?": "? ", "!": "! ", ":": ": ", ";": "; "}
|
modules/utils.py
CHANGED
@@ -21,6 +21,7 @@ from markdown import markdown
|
|
21 |
from pygments import highlight
|
22 |
from pygments.lexers import get_lexer_by_name
|
23 |
from pygments.formatters import HtmlFormatter
|
|
|
24 |
|
25 |
from modules.presets import *
|
26 |
from . import shared
|
@@ -498,3 +499,35 @@ def add_details(lst):
|
|
498 |
f"<details><summary>{brief}...</summary><p>{txt}</p></details>"
|
499 |
)
|
500 |
return nodes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
from pygments import highlight
|
22 |
from pygments.lexers import get_lexer_by_name
|
23 |
from pygments.formatters import HtmlFormatter
|
24 |
+
import pandas as pd
|
25 |
|
26 |
from modules.presets import *
|
27 |
from . import shared
|
|
|
499 |
f"<details><summary>{brief}...</summary><p>{txt}</p></details>"
|
500 |
)
|
501 |
return nodes
|
502 |
+
|
503 |
+
|
504 |
+
def sheet_to_string(sheet):
|
505 |
+
result = ""
|
506 |
+
for index, row in sheet.iterrows():
|
507 |
+
row_string = ""
|
508 |
+
for column in sheet.columns:
|
509 |
+
row_string += f"{column}: {row[column]}, "
|
510 |
+
row_string = row_string.rstrip(", ")
|
511 |
+
row_string += "."
|
512 |
+
result += row_string + "\n"
|
513 |
+
return result
|
514 |
+
|
515 |
+
def excel_to_string(file_path):
|
516 |
+
# 读取Excel文件中的所有工作表
|
517 |
+
excel_file = pd.read_excel(file_path, engine='openpyxl', sheet_name=None)
|
518 |
+
|
519 |
+
# 初始化结果字符串
|
520 |
+
result = ""
|
521 |
+
|
522 |
+
# 遍历每一个工作表
|
523 |
+
for sheet_name, sheet_data in excel_file.items():
|
524 |
+
# 将工作表名称添加到结果字符串
|
525 |
+
result += f"Sheet: {sheet_name}\n"
|
526 |
+
|
527 |
+
# 处理当前工作表并添加到结果字符串
|
528 |
+
result += sheet_to_string(sheet_data)
|
529 |
+
|
530 |
+
# 在不同工作表之间添加分隔符
|
531 |
+
result += "\n" + ("-" * 20) + "\n\n"
|
532 |
+
|
533 |
+
return result
|
requirements.txt
CHANGED
@@ -12,3 +12,4 @@ langchain
|
|
12 |
markdown
|
13 |
PyPDF2
|
14 |
pdfplumber
|
|
|
|
12 |
markdown
|
13 |
PyPDF2
|
14 |
pdfplumber
|
15 |
+
pandas
|