-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_file.py
154 lines (136 loc) · 6.91 KB
/
process_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import os
from llama_parse import LlamaParse
import rdflib
import textract
import json
import requests
from sql_query import *
from file_utils import save_file
from query_function import query_summarize
def format_str(text):
return str(text)
def process_pdf(input_file, output_folder, old_name, semaphore, socketio, question_prompt, answer_prompt):
file_name = os.path.basename(input_file)
output_file = os.path.join(output_folder, file_name)
create_file_subtask(output_folder, file_name, old_name)
try:
parser = LlamaParse(
api_key=os.getenv("LLAMA_CLOUD_API_KEY"),
result_type="markdown",
num_workers=8,
verbose=True
)
total_text = ""
extra_info = {"file_name": file_name}
with open(input_file, "rb") as f:
documents = parser.load_data(f, extra_info=extra_info)
for data in documents:
total_text += data.text
save_file(total_text, output_file)
length = len(total_text)
update_file_subtask(output_folder, file_name, 1, None, None, length)
if length > 400:
data = query_summarize(total_text, output_file, old_name, semaphore, socketio, question_prompt, answer_prompt)
if data:
update_file_subtask(output_folder, file_name, None, 1)
else:
update_file_subtask(output_folder, file_name, None, 2)
print(f"[log] pdf处理完成: {output_file}")
except Exception as e:
update_file_subtask(output_folder, file_name, 2)
print(f"[error] pdf处理失败! \n 文件名:{output_file} \n 原因: {e}")
def process_doc(input_file, output_folder, old_name, semaphore, socketio, question_prompt, answer_prompt):
file_name = os.path.basename(input_file)
output_file = os.path.join(output_folder, file_name)
create_file_subtask(output_folder, file_name, old_name)
try:
content = textract.process(input_file).decode("utf-8")
save_file(content, output_file)
length = len(content)
update_file_subtask(output_folder, file_name, 1, None, None, length)
if length > 400:
data = query_summarize(content, output_file, old_name, semaphore, socketio, question_prompt, answer_prompt)
if data:
update_file_subtask(output_folder, file_name, None, 1)
else:
update_file_subtask(output_folder, file_name, None, 2)
print(f"[log] doc文件处理完成: {output_file}")
except Exception as e:
update_file_subtask(output_folder, file_name, 2)
print(f"[error] doc处理失败! \n 文件名:{output_file} \n 原因: {e}")
def process_text(input_file, output_folder, old_name, semaphore, socketio, question_prompt, answer_prompt):
file_name = os.path.basename(input_file)
file_name_without_ext, file_extension = os.path.splitext(file_name)
output_file = os.path.join(output_folder, os.path.basename(input_file))
create_file_subtask(output_folder, file_name, old_name)
try:
with open(input_file, "r") as f:
content = f.read()
save_file(content, output_file, file_extension.lstrip('.'))
length = len(content)
update_file_subtask(output_folder, file_name, 1, None, None, length)
if length > 400:
data = query_summarize(content, output_file, old_name, semaphore, socketio, question_prompt, answer_prompt)
if data:
update_file_subtask(output_folder, file_name, None, 1)
else:
update_file_subtask(output_folder, file_name, None, 2)
print(f"[log] text文件处理完成: {output_file}")
except Exception as e:
update_file_subtask(output_folder, file_name, 2)
print(f"[error] txt处理失败! \n 文件名:{output_file} \n 原因: {e}")
def process_ttl(input_file, output_folder, ttl_type, old_name):
file_name = os.path.basename(input_file)
first_name = os.path.splitext(file_name)[0]
create_file_subtask(output_folder, file_name, old_name)
try:
g = rdflib.Graph()
g.parse(input_file, format='turtle')
query = """
SELECT ?subject ?definition ?prefLabel ?comment ?broader WHERE {
?subject skos:prefLabel ?prefLabel .
OPTIONAL { ?subject skos:broader ?broader } .
OPTIONAL { ?subject skos:definition ?definition } .
OPTIONAL { ?subject rdfs:comment ?comment } .
}
"""
results = g.query(query)
broader_list = []
text_list = []
for raw in results:
if ttl_type == "md":
if raw.broader:
broader = raw.broader
if "github.com" in broader:
broader = broader.replace('blob', 'raw')
response = requests.get(broader)
if response.status_code == 200:
md_content = format_str(response.text)
if raw.prefLabel:
broader_list.append({'short_text': format_str(raw.prefLabel), 'full_text': md_content})
if raw.definition:
broader_list.append({'short_text': format_str(raw.definition), 'full_text': md_content})
if raw.comment:
broader_list.append({'short_text': format_str(raw.comment), 'full_text': md_content})
elif ttl_type == "text":
combined_text = (format_str(raw.prefLabel) if raw.prefLabel is not None else "") + (format_str(raw.definition) if raw.definition is not None else "") + (format_str(raw.comment) if raw.comment is not None else "")
if raw.prefLabel:
text_list.append({'short_text': format_str(raw.prefLabel), 'full_text': combined_text})
if raw.definition:
text_list.append({'short_text': format_str(raw.definition), 'full_text': combined_text})
if raw.comment:
text_list.append({'short_text': format_str(raw.comment), 'full_text': combined_text})
if len(broader_list):
broader_name = first_name + "_broader.json"
broader_output_file = os.path.join(output_folder, broader_name)
print(f"[log] ttl文件处理完成: {broader_output_file}")
save_file(json.dumps(broader_list), broader_output_file, "json")
if len(text_list):
text_name = first_name + "_text.json"
text_output_file = os.path.join(output_folder, os.path.basename(text_name))
print(f"[log] ttl文件处理完成: {text_output_file}")
save_file(json.dumps(text_list), text_output_file, "json")
update_file_subtask(output_folder, file_name, 1)
except Exception as e:
update_file_subtask(output_folder, file_name, 2)
print(f"[error] ttl处理失败! \n 文件名:{file_name} \n 原因: {e}")