hariqueen commited on
Commit
675f467
ยท
verified ยท
1 Parent(s): 4b1cee2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -22
app.py CHANGED
@@ -2,28 +2,61 @@ import gradio as gr
2
  import main # main.py์— ์ž‘์„ฑ๋œ ์ „์ฒ˜๋ฆฌ ๋กœ์ง ํ˜ธ์ถœ
3
  import os
4
  import pandas as pd
 
 
 
5
 
6
  def process_file(file_path, voucher_number):
 
 
 
 
 
 
 
7
  if file_path is None:
8
- return None
9
 
10
- # ํŒŒ์ผ ํ™•์žฅ์ž ํ™•์ธ
11
- ext = os.path.splitext(file_path)[1].lower()
 
 
 
 
 
12
 
13
- if ext == ".xlsx":
14
- # ์—‘์…€ ํŒŒ์ผ์„ CSV๋กœ ๋ณ€ํ™˜
15
- df = pd.read_excel(file_path)
16
- csv_path = file_path.replace(".xlsx", ".csv")
17
- df.to_csv(csv_path, index=False)
18
- input_path = csv_path
19
- else:
20
- # ์ด๋ฏธ CSV ํŒŒ์ผ์ด๋ฉด ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ
21
- input_path = file_path
22
 
23
- # ๋ฉ”์ธ ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜ ํ˜ธ์ถœ (์ „ํ‘œ๋ฒˆํ˜ธ ๋„˜๊ฒจ์ฃผ๊ธฐ)
24
- output_path = main.process_rental_company_with_voucher(input_path, voucher_number)
25
-
26
- return output_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌ์„ฑ
29
  with gr.Blocks() as demo:
@@ -33,7 +66,7 @@ with gr.Blocks() as demo:
33
  file_input = gr.File(
34
  label="๋ Œํƒˆ๋ฃŒ ํŒŒ์ผ ์—…๋กœ๋“œ (CSV ๋˜๋Š” Excel)",
35
  file_types=[".csv", ".xlsx"],
36
- type="filepath" # โญ ์—ฌ๊ธฐ๊ฐ€ ์ค‘์š”!
37
  )
38
  voucher_input = gr.Textbox(
39
  label="์ „ํ‘œ๋ฒˆํ˜ธ ์ž…๋ ฅ",
@@ -41,22 +74,31 @@ with gr.Blocks() as demo:
41
  )
42
 
43
  with gr.Row():
44
- submit_btn = gr.Button("์ œ์ถœ")
45
  clear_btn = gr.Button("์ง€์šฐ๊ธฐ")
46
 
 
 
 
 
 
 
 
47
  output_file = gr.File(label="์ „์ฒ˜๋ฆฌ ์™„๋ฃŒ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ")
48
 
49
  # ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ
50
  submit_btn.click(
51
  fn=process_file,
52
  inputs=[file_input, voucher_input],
53
- outputs=output_file
54
  )
55
 
56
  clear_btn.click(
57
- fn=lambda: (None, ""),
58
  inputs=[],
59
- outputs=[file_input, voucher_input]
60
  )
61
 
62
- demo.launch()
 
 
 
2
  import main # main.py์— ์ž‘์„ฑ๋œ ์ „์ฒ˜๋ฆฌ ๋กœ์ง ํ˜ธ์ถœ
3
  import os
4
  import pandas as pd
5
+ import traceback
6
+ import sys
7
+ from io import StringIO
8
 
9
  def process_file(file_path, voucher_number):
10
+ # ์ƒํƒœ ๋ฉ”์‹œ์ง€์™€ ๊ฒฐ๊ณผ๋ฅผ ํ•จ๊ป˜ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”
11
+ status_message = ""
12
+ output_file_path = None
13
+
14
+ # ๋กœ๊ทธ๋ฅผ ์บก์ฒ˜ํ•˜๊ธฐ ์œ„ํ•œ StringIO ๊ฐ์ฒด
15
+ log_capture = StringIO()
16
+
17
  if file_path is None:
18
+ return None, "ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•ด์ฃผ์„ธ์š”."
19
 
20
+ try:
21
+ # ํ‘œ์ค€ ์ถœ๋ ฅ์„ ์บก์ฒ˜
22
+ original_stdout = sys.stdout
23
+ sys.stdout = log_capture
24
+
25
+ # ํŒŒ์ผ ํ™•์žฅ์ž ํ™•์ธ
26
+ ext = os.path.splitext(file_path)[1].lower()
27
 
28
+ if ext == ".xlsx":
29
+ # ์—‘์…€ ํŒŒ์ผ์„ CSV๋กœ ๋ณ€ํ™˜
30
+ df = pd.read_excel(file_path)
31
+ csv_path = file_path.replace(".xlsx", ".csv")
32
+ df.to_csv(csv_path, index=False)
33
+ input_path = csv_path
34
+ else:
35
+ # ์ด๋ฏธ CSV ํŒŒ์ผ์ด๋ฉด ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ
36
+ input_path = file_path
37
 
38
+ # ๋ฉ”์ธ ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜ ํ˜ธ์ถœ (์ „ํ‘œ๋ฒˆํ˜ธ ๋„˜๊ฒจ์ฃผ๊ธฐ)
39
+ output_path = main.process_rental_company_with_voucher(input_path, voucher_number)
40
+ output_file_path = output_path
41
+
42
+ # ์„ฑ๊ณต ๋ฉ”์‹œ์ง€ ์ž‘์„ฑ
43
+ status_message = "โœ… ํŒŒ์ผ ๋ณ€ํ™˜ ์„ฑ๊ณต! ์•„๋ž˜ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ๋‹ค์šด๋กœ๋“œํ•˜์„ธ์š”."
44
+
45
+ except Exception as e:
46
+ # ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์‹œ ์ƒ์„ธ ์˜ค๋ฅ˜ ๋‚ด์šฉ ์บก์ฒ˜
47
+ error_detail = traceback.format_exc()
48
+ status_message = f"โŒ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}\n\n๐Ÿ” ์ƒ์„ธ ๋‚ด์šฉ:\n{error_detail}"
49
+
50
+ finally:
51
+ # ์บก์ฒ˜๋œ ๋กœ๊ทธ ๊ฐ€์ ธ์˜ค๊ธฐ
52
+ log_output = log_capture.getvalue()
53
+ sys.stdout = original_stdout # ์›๋ž˜ ํ‘œ์ค€ ์ถœ๋ ฅ์œผ๋กœ ๋ณต๊ตฌ
54
+
55
+ # ๋กœ๊ทธ ์ถœ๋ ฅ์ด ์žˆ์œผ๋ฉด ์ƒํƒœ ๋ฉ”์‹œ์ง€์— ์ถ”๊ฐ€
56
+ if log_output.strip():
57
+ status_message += f"\n\n๐Ÿ“‹ ์ฒ˜๋ฆฌ ๋กœ๊ทธ:\n{log_output}"
58
+
59
+ return output_file_path, status_message
60
 
61
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌ์„ฑ
62
  with gr.Blocks() as demo:
 
66
  file_input = gr.File(
67
  label="๋ Œํƒˆ๋ฃŒ ํŒŒ์ผ ์—…๋กœ๋“œ (CSV ๋˜๋Š” Excel)",
68
  file_types=[".csv", ".xlsx"],
69
+ type="filepath"
70
  )
71
  voucher_input = gr.Textbox(
72
  label="์ „ํ‘œ๋ฒˆํ˜ธ ์ž…๋ ฅ",
 
74
  )
75
 
76
  with gr.Row():
77
+ submit_btn = gr.Button("์ œ์ถœ", variant="primary")
78
  clear_btn = gr.Button("์ง€์šฐ๊ธฐ")
79
 
80
+ # ์ƒํƒœ ๋ฉ”์‹œ์ง€๋ฅผ ํ‘œ์‹œํ•  ํ…์ŠคํŠธ ์˜์—ญ ์ถ”๊ฐ€
81
+ status_output = gr.Textbox(
82
+ label="์ฒ˜๋ฆฌ ์ƒํƒœ",
83
+ placeholder="ํŒŒ์ผ์„ ์ œ์ถœํ•˜๋ฉด ์ฒ˜๋ฆฌ ์ƒํƒœ๊ฐ€ ์—ฌ๊ธฐ์— ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.",
84
+ lines=10
85
+ )
86
+
87
  output_file = gr.File(label="์ „์ฒ˜๋ฆฌ ์™„๋ฃŒ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ")
88
 
89
  # ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ
90
  submit_btn.click(
91
  fn=process_file,
92
  inputs=[file_input, voucher_input],
93
+ outputs=[output_file, status_output]
94
  )
95
 
96
  clear_btn.click(
97
+ fn=lambda: (None, "", ""),
98
  inputs=[],
99
+ outputs=[file_input, voucher_input, status_output]
100
  )
101
 
102
+ # ์‹œ์ž‘ ๋ฉ”์‹œ์ง€ ํ‘œ์‹œ
103
+ print("ERP ์ž๋™ ์ „ํ‘œ ๋ณ€ํ™˜๊ธฐ๊ฐ€ ์‹œ์ž‘๋˜์—ˆ์Šต๋‹ˆ๋‹ค.")
104
+ demo.launch()