File size: 2,810 Bytes
b2dda9d
 
7b2bbe0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2dda9d
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import pandas as pd
import gradio as gr

def process_data(file):
    try:
        # file adalah path di Hugging Face Spaces
        df = pd.read_excel(file)

        output_data = []
        no = 1

        char1 = char2 = char3 = char4 = ""
        previous_char3 = ""
        nominal = tipe = 0
        anggaran1 = anggaran2 = anggaran3 = 0
        total_nominal = 0

        column_names = df.columns.tolist()

        for _, row in df.iterrows():
            kode = str(row[column_names[0]]).strip()
            anggaran = row[column_names[3]]

            if kode and kode[0] in ["3", "6"]:
                char1 = kode
                anggaran3 = anggaran

            elif kode.startswith("0"):
                char2 = kode

            elif kode != "nan" and kode.isalpha():
                char3 = kode
                previous_char3 = char3
                anggaran1 = anggaran

            elif kode.startswith("5"):
                char4 = kode
                anggaran2 = anggaran

                if pd.isna(row[column_names[9]]):
                    nominal = row[column_names[10]]
                    tipe = row[column_names[13]]
                else:
                    nominal = row[column_names[9]]
                    tipe = row[column_names[11]]

                if char3 == "":
                    char3 = previous_char3

                mak = f"{char1}.{char2}.{char3}.{char4}"

                output_data.append({
                    "No": no,
                    "MAK": mak,
                    "Anggaran3": anggaran3,
                    "Anggaran1": anggaran1,
                    "Anggaran2": anggaran2,
                    "Nominal": nominal,
                    "Tipe": tipe
                })

                no += 1
                char3 = ""

                if pd.notna(nominal):
                    total_nominal += float(nominal)

        output_data.append({
            "No": "",
            "MAK": "",
            "Anggaran3": "",
            "Anggaran1": "",
            "Anggaran2": "",
            "Nominal": f"Total Nominal: {total_nominal:.0f}",
            "Tipe": ""
        })

        output_df = pd.DataFrame(output_data)

        output_file = "/tmp/processed_data.xlsx"
        output_df.to_excel(output_file, index=False)

        return output_file

    except Exception as e:
        return f"Error: {e}"

with gr.Blocks(title="Konvert Mata Anggaran Sakti") as demo:
    gr.Markdown("### Konvert Mata Anggaran Sakti")
    gr.Markdown("Unggah file Excel, sistem akan mengonversi dan menghasilkan file Excel baru.")

    input_file = gr.File(label="Upload Excel File", file_types=[".xlsx"])
    output_file = gr.File(label="Download Hasil")

    btn = gr.Button("Proses")
    btn.click(process_data, inputs=input_file, outputs=output_file)

demo.launch()