-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhwpx_flask.py
More file actions
64 lines (53 loc) · 1.73 KB
/
Copy pathhwpx_flask.py
File metadata and controls
64 lines (53 loc) · 1.73 KB
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
from flask import Flask, request, jsonify
import subprocess
import os
app = Flask(__name__)
# hwp loader
def process_hwp(file_path, hwp_jar_path, work_dir):
try:
if work_dir == '/app/python-hwpxlib':
result = subprocess.run(
["python3", "hwpx_loader.py",
"--hwpx_jar_path", hwp_jar_path,
"--file_path", file_path],
capture_output=True,
text=True,
encoding='utf-8'
)
return result.stdout
else:
result = subprocess.run(
["python", "hwpx_loader.py",
"--hwpx_jar_path", hwp_jar_path,
"--file_path", file_path],
capture_output=True,
text=True,
encoding='utf-8'
)
return result.stdout
except Exception as e:
return str(e)
@app.route('/extract-text', methods=['POST'])
def extract_text():
# 파일 업로드 처리
if 'file' not in request.files:
return jsonify({"error": "No file uploaded"}), 400
file = request.files['file']
if file.filename == '':
return jsonify({"error": "Empty filename"}), 400
work_dir = os.getcwd()
# 업로드 파일 저장
file_name = file.filename
file.save(file_name)
# HWP 텍스트 추출 실행
hwp_jar_path = "./hwpxlib-1.0.5.jar"
text = process_hwp(file_name, hwp_jar_path, work_dir)
# 임시 파일 삭제 docker 에서만
if work_dir == '/app/python-hwpxlib':
os.remove(file_name)
return jsonify({
"filename": file_name,
"text": text
})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860)