seawolf2357 commited on
Commit
ee35541
ยท
verified ยท
1 Parent(s): 1fe8e49

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -8
app.py CHANGED
@@ -98,14 +98,14 @@ def extract_text_with_olefile(input_path):
98
 
99
  def convert_to_html_subprocess(input_path, output_dir):
100
  """subprocess๋กœ hwp5html ์‹คํ–‰"""
101
- output_path = os.path.join(output_dir, "output.html")
 
102
 
103
  # ๊ฐ€๋Šฅํ•œ hwp5html ๊ฒฝ๋กœ๋“ค
104
  hwp5html_paths = [
105
  'hwp5html', # PATH์— ์žˆ๋Š” ๊ฒฝ์šฐ
106
  os.path.join(SCRIPT_DIR, 'bin', 'hwp5html'),
107
  os.path.join(PYHWP_PATH, 'bin', 'hwp5html'),
108
- sys.executable.replace('python', 'hwp5html'),
109
  ]
110
 
111
  for cmd_path in hwp5html_paths:
@@ -119,9 +119,11 @@ def convert_to_html_subprocess(input_path, output_dir):
119
 
120
  if result.returncode == 0 and os.path.exists(output_path):
121
  return output_path, None
 
122
  except (FileNotFoundError, subprocess.TimeoutExpired):
123
  continue
124
  except Exception as e:
 
125
  continue
126
 
127
  return None, "hwp5html ๋ช…๋ น์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
@@ -228,7 +230,7 @@ def convert_hwp(file, output_format, progress=gr.Progress()):
228
  # ๋ณ€ํ™˜ ์ˆ˜ํ–‰
229
  if output_format == "HTML":
230
  output_path, error = convert_to_html_subprocess(input_path, tmp_dir)
231
- ext = ".html"
232
 
233
  elif output_format == "ODT (OpenDocument)":
234
  output_path, error = convert_to_odt_subprocess(input_path, tmp_dir)
@@ -253,10 +255,21 @@ def convert_hwp(file, output_format, progress=gr.Progress()):
253
  return None, f"โŒ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ํ˜•์‹: {output_format}", ""
254
 
255
  # ๋ณ€ํ™˜ ์‹คํŒจ ์ฒ˜๋ฆฌ
256
- if output_path is None or not os.path.exists(output_path):
257
  error_msg = error or "๋ณ€ํ™˜์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค."
258
  return None, f"โŒ {error_msg}", ""
259
 
 
 
 
 
 
 
 
 
 
 
 
260
  progress(0.8, desc="ํŒŒ์ผ ์ค€๋น„ ์ค‘...")
261
 
262
  # ์ตœ์ข… ํŒŒ์ผ๋ช… ์„ค์ •
@@ -264,8 +277,13 @@ def convert_hwp(file, output_format, progress=gr.Progress()):
264
  final_filename = f"{base_name}{ext}"
265
  final_output = os.path.join(tmp_dir, final_filename)
266
 
267
- if output_path != final_output:
268
- shutil.move(output_path, final_output)
 
 
 
 
 
269
 
270
  # ํŒŒ์ผ ํฌ๊ธฐ ํ™•์ธ
271
  file_size = os.path.getsize(final_output)
@@ -275,7 +293,9 @@ def convert_hwp(file, output_format, progress=gr.Progress()):
275
 
276
  # ๋ฏธ๋ฆฌ๋ณด๊ธฐ (ํ…์ŠคํŠธ/HTML์ธ ๊ฒฝ์šฐ)
277
  preview = ""
278
- if ext in ['.txt', '.html']:
 
 
279
  try:
280
  with open(final_output, 'r', encoding='utf-8', errors='ignore') as f:
281
  preview = f.read(5000) # ์ฒ˜์Œ 5000์ž๋งŒ
@@ -283,6 +303,8 @@ def convert_hwp(file, output_format, progress=gr.Progress()):
283
  preview += "\n\n... (๋ฏธ๋ฆฌ๋ณด๊ธฐ ์ƒ๋žต)"
284
  except:
285
  pass
 
 
286
 
287
  return final_output, f"โœ… ๋ณ€ํ™˜ ์™„๋ฃŒ: {final_filename} ({size_str})", preview
288
 
@@ -323,7 +345,9 @@ css = """
323
  """
324
 
325
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค
326
- with gr.Blocks(css=css, title="HWP ๋ณ€ํ™˜๊ธฐ", theme=gr.themes.Soft()) as demo:
 
 
327
  with gr.Column(elem_id="col-container"):
328
  gr.Markdown("""
329
  # ๐Ÿ“„ HWP ํŒŒ์ผ ๋ณ€ํ™˜๊ธฐ
 
98
 
99
  def convert_to_html_subprocess(input_path, output_dir):
100
  """subprocess๋กœ hwp5html ์‹คํ–‰"""
101
+ output_name = "output.html"
102
+ output_path = os.path.join(output_dir, output_name)
103
 
104
  # ๊ฐ€๋Šฅํ•œ hwp5html ๊ฒฝ๋กœ๋“ค
105
  hwp5html_paths = [
106
  'hwp5html', # PATH์— ์žˆ๋Š” ๊ฒฝ์šฐ
107
  os.path.join(SCRIPT_DIR, 'bin', 'hwp5html'),
108
  os.path.join(PYHWP_PATH, 'bin', 'hwp5html'),
 
109
  ]
110
 
111
  for cmd_path in hwp5html_paths:
 
119
 
120
  if result.returncode == 0 and os.path.exists(output_path):
121
  return output_path, None
122
+
123
  except (FileNotFoundError, subprocess.TimeoutExpired):
124
  continue
125
  except Exception as e:
126
+ print(f"hwp5html error: {e}")
127
  continue
128
 
129
  return None, "hwp5html ๋ช…๋ น์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
 
230
  # ๋ณ€ํ™˜ ์ˆ˜ํ–‰
231
  if output_format == "HTML":
232
  output_path, error = convert_to_html_subprocess(input_path, tmp_dir)
233
+ ext = ".html" # ๋””๋ ‰ํ† ๋ฆฌ์ธ ๊ฒฝ์šฐ ์•„๋ž˜์—์„œ .zip์œผ๋กœ ๋ณ€๊ฒฝ๋จ
234
 
235
  elif output_format == "ODT (OpenDocument)":
236
  output_path, error = convert_to_odt_subprocess(input_path, tmp_dir)
 
255
  return None, f"โŒ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ํ˜•์‹: {output_format}", ""
256
 
257
  # ๋ณ€ํ™˜ ์‹คํŒจ ์ฒ˜๋ฆฌ
258
+ if output_path is None:
259
  error_msg = error or "๋ณ€ํ™˜์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค."
260
  return None, f"โŒ {error_msg}", ""
261
 
262
+ # output_path๊ฐ€ ๋””๋ ‰ํ† ๋ฆฌ์ธ์ง€ ํŒŒ์ผ์ธ์ง€ ํ™•์ธ
263
+ if os.path.isdir(output_path):
264
+ # ๋””๋ ‰ํ† ๋ฆฌ์ธ ๊ฒฝ์šฐ (HTML ๋ณ€ํ™˜ ์‹œ ๋ฐœ์ƒ ๊ฐ€๋Šฅ) - ZIP ์••์ถ•
265
+ zip_base = os.path.join(tmp_dir, "output_archive")
266
+ zip_path = shutil.make_archive(zip_base, 'zip', output_path)
267
+ output_path = zip_path
268
+ ext = ".zip"
269
+
270
+ if not os.path.exists(output_path):
271
+ return None, "โŒ ๋ณ€ํ™˜๋œ ํŒŒ์ผ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.", ""
272
+
273
  progress(0.8, desc="ํŒŒ์ผ ์ค€๋น„ ์ค‘...")
274
 
275
  # ์ตœ์ข… ํŒŒ์ผ๋ช… ์„ค์ •
 
277
  final_filename = f"{base_name}{ext}"
278
  final_output = os.path.join(tmp_dir, final_filename)
279
 
280
+ # ํŒŒ์ผ ์ด๋™ (๋‹ค๋ฅธ ๊ฒฝ๋กœ์ผ ๋•Œ๋งŒ)
281
+ if output_path != final_output and os.path.isfile(output_path):
282
+ shutil.copy2(output_path, final_output)
283
+ elif output_path == final_output:
284
+ pass # ์ด๋ฏธ ๊ฐ™์€ ๊ฒฝ๋กœ
285
+ else:
286
+ final_output = output_path # ์ด๋™ ์‹คํŒจ ์‹œ ์›๋ณธ ์‚ฌ์šฉ
287
 
288
  # ํŒŒ์ผ ํฌ๊ธฐ ํ™•์ธ
289
  file_size = os.path.getsize(final_output)
 
293
 
294
  # ๋ฏธ๋ฆฌ๋ณด๊ธฐ (ํ…์ŠคํŠธ/HTML์ธ ๊ฒฝ์šฐ)
295
  preview = ""
296
+
297
+ # TXT ํŒŒ์ผ๋งŒ ๋ฏธ๋ฆฌ๋ณด๊ธฐ (HTML์€ ZIP์ด๊ฑฐ๋‚˜ ๋””๋ ‰ํ† ๋ฆฌ์ผ ์ˆ˜ ์žˆ์Œ)
298
+ if ext == '.txt' and os.path.isfile(final_output):
299
  try:
300
  with open(final_output, 'r', encoding='utf-8', errors='ignore') as f:
301
  preview = f.read(5000) # ์ฒ˜์Œ 5000์ž๋งŒ
 
303
  preview += "\n\n... (๋ฏธ๋ฆฌ๋ณด๊ธฐ ์ƒ๋žต)"
304
  except:
305
  pass
306
+ elif ext == '.zip':
307
+ preview = "๐Ÿ“ฆ HTML ๋ณ€ํ™˜ ๊ฒฐ๊ณผ๊ฐ€ ZIP ํŒŒ์ผ๋กœ ์••์ถ•๋˜์—ˆ์Šต๋‹ˆ๋‹ค.\n๋‹ค์šด๋กœ๋“œ ํ›„ ์••์ถ•์„ ํ’€์–ด์„œ HTML ํŒŒ์ผ์„ ํ™•์ธํ•˜์„ธ์š”."
308
 
309
  return final_output, f"โœ… ๋ณ€ํ™˜ ์™„๋ฃŒ: {final_filename} ({size_str})", preview
310
 
 
345
  """
346
 
347
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค
348
+ with gr.Blocks(title="HWP ๋ณ€ํ™˜๊ธฐ") as demo:
349
+ # CSS ์ ์šฉ
350
+ gr.HTML(f"<style>{css}</style>")
351
  with gr.Column(elem_id="col-container"):
352
  gr.Markdown("""
353
  # ๐Ÿ“„ HWP ํŒŒ์ผ ๋ณ€ํ™˜๊ธฐ