Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import main # main.pyμ μμ±λ μ μ²λ¦¬ λ‘μ§ νΈμΆ | |
| import os | |
| import pandas as pd | |
| import traceback | |
| import sys | |
| import re | |
| from io import StringIO | |
| def process_file(file_path, voucher_number, employee_number): | |
| # μν λ©μμ§μ κ²°κ³Όλ₯Ό ν¨κ» λ°ννκΈ° μν λ³μ μ΄κΈ°ν | |
| status_message = "" | |
| output_file_path = None | |
| # λ‘κ·Έλ₯Ό μΊ‘μ²νκΈ° μν StringIO κ°μ²΄ | |
| log_capture = StringIO() | |
| # μ λ ₯κ° κ²μ¦ | |
| if file_path is None: | |
| return None, "νμΌμ μ λ‘λν΄μ£ΌμΈμ." | |
| if not voucher_number or not voucher_number.strip(): | |
| return None, "μ νλ²νΈλ₯Ό μ λ ₯ν΄μ£ΌμΈμ." | |
| if not employee_number or not employee_number.strip(): | |
| return None, "μ¬μλ²νΈλ₯Ό μ λ ₯ν΄μ£ΌμΈμ. μ¬μλ²νΈλ νμ μ λ ₯κ°μ λλ€." | |
| try: | |
| # νμ€ μΆλ ₯μ μΊ‘μ² | |
| original_stdout = sys.stdout | |
| sys.stdout = log_capture | |
| # νμΌ νμ₯μ νμΈ | |
| ext = os.path.splitext(file_path)[1].lower() | |
| if ext == ".xlsx": | |
| # μμ νμΌμ CSVλ‘ λ³ν | |
| df = pd.read_excel(file_path) | |
| csv_path = file_path.replace(".xlsx", ".csv") | |
| df.to_csv(csv_path, index=False) | |
| input_path = csv_path | |
| else: | |
| # μ΄λ―Έ CSV νμΌμ΄λ©΄ κ·Έλλ‘ μ¬μ© | |
| input_path = file_path | |
| # λ©μΈ μ μ²λ¦¬ ν¨μ νΈμΆ (μ νλ²νΈμ μ¬μλ²νΈ λ겨주기) | |
| output_path = main.process_rental_company_with_voucher(input_path, voucher_number, employee_number) | |
| output_file_path = output_path | |
| # μ±κ³΅ λ©μμ§ μμ± | |
| status_message = "β νμΌ λ³ν μ±κ³΅! μ λ²νΌμ ν΄λ¦νμ¬ λ€μ΄λ‘λνμΈμ." | |
| except Exception as e: | |
| # μ€λ₯ λ°μ μ κ°λ¨ν μ€λ₯ λ©μμ§ | |
| status_message = f"β μ€λ₯ λ°μ: {str(e)}" | |
| finally: | |
| # μΊ‘μ²λ λ‘κ·Έ κ°μ Έμ€κΈ° | |
| log_output = log_capture.getvalue() | |
| sys.stdout = original_stdout # μλ νμ€ μΆλ ₯μΌλ‘ 볡ꡬ | |
| # μ£Όμ μ 보 μΆμΆ (μ€λ₯ λ°μ μ¬λΆμ μκ΄μμ΄) | |
| important_info = [] | |
| # κΈμ‘ νλ μ 보 μΆμΆ | |
| amount_field_match = re.search(r"μ¬μ©ν κΈμ‘ νλ: '([^']*)'", log_output) | |
| if amount_field_match: | |
| important_info.append(f"μ¬μ©ν κΈμ‘ νλ: '{amount_field_match.group(1)}'") | |
| # ν νλ μ 보 μΆμΆ | |
| team_field_match = re.search(r"ν νλλ‘ '([^']*)'λ₯Ό μλ μΈμνμ΅λλ€", log_output) | |
| if team_field_match: | |
| important_info.append(f"ν νλλ‘ '{team_field_match.group(1)}'λ₯Ό μλ μΈμνμ΅λλ€.") | |
| # μ μΈλ νλͺ© μ 보 μΆμΆ | |
| excluded_rows_match = re.search(r"κΈμ‘μ΄ μλ ν\(λ°λ© νλͺ©\) (\d+)κ°λ₯Ό μ μΈν©λλ€", log_output) | |
| if excluded_rows_match: | |
| important_info.append(f"κΈμ‘μ΄ μλ ν(λ°λ© νλͺ©) {excluded_rows_match.group(1)}κ°λ₯Ό μ μΈν©λλ€.") | |
| # κΈμ‘ μ 보 μΆμΆ | |
| amt_info = [] | |
| debit_sum_match = re.search(r"μ°¨λ³ κΈμ‘ ν©κ³: (\d+)", log_output) | |
| credit_sum_match = re.search(r"λλ³ κΈμ‘: (\d+)", log_output) | |
| debit_count_match = re.search(r"μ°¨λ³ κ±΄μ: (\d+)", log_output) | |
| credit_count_match = re.search(r"λλ³ κ±΄μ: (\d+)", log_output) | |
| if debit_sum_match: | |
| amt_info.append(f"μ°¨λ³ κΈμ‘ ν©κ³: {int(debit_sum_match.group(1)):,}") | |
| if credit_sum_match: | |
| amt_info.append(f"λλ³ κΈμ‘: {int(credit_sum_match.group(1)):,}") | |
| if debit_count_match: | |
| amt_info.append(f"μ°¨λ³ κ±΄μ: {debit_count_match.group(1)}") | |
| if credit_count_match: | |
| amt_info.append(f"λλ³ κ±΄μ: {credit_count_match.group(1)}") | |
| if amt_info: | |
| important_info.append("AMT νλ νμΈ:") | |
| important_info.extend(amt_info) | |
| # λ§€ν μ€λ₯ μ 보 μΆμΆ (μ€λ₯κ° μμ κ²½μ°μλ§) | |
| if not output_file_path and "λ§€νλμ§ μμ νλͺ " in log_output: # μ€λ₯ λ°μ μμλ§ λ§€ν μ 보 νμ | |
| unmapped_section = re.search(r"λ§€νλμ§ μμ νλͺ (\d+)κ°:(.*?)(?=\n\n|\Z)", log_output, re.DOTALL) | |
| if unmapped_section: | |
| unmapped_count = unmapped_section.group(1) | |
| unmapped_teams = re.findall(r"- '([^']*)'", unmapped_section.group(2)) | |
| important_info.append(f"λ§€νλμ§ μμ νλͺ {unmapped_count}κ°:") | |
| for team in unmapped_teams: | |
| important_info.append(f"- '{team}'") | |
| # μ£Όμ μ 보λ₯Ό μν λ©μμ§μ μΆκ° (νμ) | |
| if important_info: | |
| status_message += "\n\n" + "\n".join(important_info) | |
| return output_file_path, status_message | |
| # Gradio μΈν°νμ΄μ€ κ΅¬μ± | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# ERP μλ μ ν λ³νκΈ°\n\nμ λ‘λν νμΌκ³Ό μ νλ²νΈ, μ¬μλ²νΈλ₯Ό μ λ ₯νμΈμ.") | |
| with gr.Row(): | |
| file_input = gr.File( | |
| label="λ νλ£ νμΌ μ λ‘λ (CSV λλ Excel)", | |
| file_types=[".csv", ".xlsx"], | |
| type="filepath" | |
| ) | |
| with gr.Row(): | |
| voucher_input = gr.Textbox( | |
| label="μ νλ²νΈ μ λ ₯ (νμ)", | |
| placeholder="μ: 20250427001" | |
| ) | |
| employee_input = gr.Textbox( | |
| label="μ¬μλ²νΈ μ λ ₯ (νμ)", | |
| placeholder="μ: 00616" | |
| ) | |
| with gr.Row(): | |
| submit_btn = gr.Button("μ μΆ", variant="primary") | |
| clear_btn = gr.Button("μ§μ°κΈ°") | |
| output_file = gr.File(label="μ μ²λ¦¬ μλ£ νμΌ λ€μ΄λ‘λ") | |
| # μν λ©μμ§λ₯Ό νμΌ λ€μ΄λ‘λ μμ μλλ‘ μ΄λ | |
| status_output = gr.Textbox( | |
| label="μ²λ¦¬ μν", | |
| placeholder="νμΌμ μ μΆνλ©΄ μ²λ¦¬ μνκ° μ¬κΈ°μ νμλ©λλ€.", | |
| lines=10 | |
| ) | |
| # λ²νΌ ν΄λ¦ μ΄λ²€νΈ μ°κ²° | |
| submit_btn.click( | |
| fn=process_file, | |
| inputs=[file_input, voucher_input, employee_input], | |
| outputs=[output_file, status_output] | |
| ) | |
| clear_btn.click( | |
| fn=lambda: (None, "", "", ""), | |
| inputs=[], | |
| outputs=[file_input, voucher_input, employee_input, status_output] | |
| ) | |
| # μμ λ©μμ§ νμ | |
| print("ERP μλ μ ν λ³νκΈ°κ° μμλμμ΅λλ€.") | |
| demo.launch() |