Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
| 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()
|