Saditiya commited on
Commit
7b2bbe0
·
verified ·
1 Parent(s): 06d5ac3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -78
app.py CHANGED
@@ -1,82 +1,98 @@
1
  import pandas as pd
2
- import matplotlib
3
- matplotlib.use("Agg")
4
- import matplotlib.pyplot as plt
5
  import gradio as gr
6
- import os
7
- import tempfile
8
-
9
- def plot_top_100_mak(excel_file):
10
- # Baca file Excel dari path
11
- df = pd.read_excel(excel_file)
12
-
13
- # Tangani nilai kosong
14
- df["MAK"] = df["MAK"].fillna("Unknown")
15
- df["Nominal"] = pd.to_numeric(df["Nominal"], errors="coerce").fillna(0)
16
-
17
- # Ambil 100 MAK dengan nominal terbesar
18
- top_100 = (
19
- df.groupby("MAK", as_index=False)["Nominal"]
20
- .sum()
21
- .sort_values("Nominal", ascending=False)
22
- .head(100)
23
- )
24
-
25
- # Konversi ke juta
26
- top_100["Nominal"] = top_100["Nominal"] / 1_000_000
27
-
28
- # Plot
29
- plt.figure(figsize=(16, 9))
30
- bars = plt.bar(top_100["MAK"], top_100["Nominal"])
31
- plt.xlabel("MAK")
32
- plt.ylabel("Nominal (Juta)")
33
- plt.title("Top 100 MAK dengan Nominal Terbesar")
34
- plt.xticks(rotation=90)
35
- plt.tight_layout()
36
-
37
- # Label nilai
38
- for bar in bars:
39
- height = bar.get_height()
40
- plt.text(
41
- bar.get_x() + bar.get_width() / 2,
42
- height,
43
- f"{height:,.2f}",
44
- ha="center",
45
- va="bottom",
46
- fontsize=7,
47
- )
48
-
49
- # Simpan ke file sementara
50
- tmp_dir = tempfile.mkdtemp()
51
- output_path = os.path.join(tmp_dir, "top_100_mak.png")
52
- plt.savefig(output_path, dpi=150)
53
- plt.close()
54
-
55
- return "Bar chart berhasil dibuat.", output_path
56
-
57
-
58
- with gr.Blocks() as demo:
59
- gr.Markdown(
60
- """
61
- ## 📊 Top 100 MAK dengan Nominal Terbesar
62
- Unggah file Excel yang memiliki kolom **MAK** dan **Nominal**
63
- """
64
- )
65
-
66
- file_input = gr.File(
67
- label="Unggah File Excel",
68
- file_types=[".xlsx", ".xls"],
69
- )
70
-
71
- result_text = gr.Textbox(label="Status")
72
- result_image = gr.Image(label="Bar Chart")
73
-
74
- submit_btn = gr.Button("Proses")
75
-
76
- submit_btn.click(
77
- fn=plot_top_100_mak,
78
- inputs=file_input,
79
- outputs=[result_text, result_image],
80
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
  demo.launch()
 
1
  import pandas as pd
 
 
 
2
  import gradio as gr
3
+
4
+ def process_data(file):
5
+ try:
6
+ # file adalah path di Hugging Face Spaces
7
+ df = pd.read_excel(file)
8
+
9
+ output_data = []
10
+ no = 1
11
+
12
+ char1 = char2 = char3 = char4 = ""
13
+ previous_char3 = ""
14
+ nominal = tipe = 0
15
+ anggaran1 = anggaran2 = anggaran3 = 0
16
+ total_nominal = 0
17
+
18
+ column_names = df.columns.tolist()
19
+
20
+ for _, row in df.iterrows():
21
+ kode = str(row[column_names[0]]).strip()
22
+ anggaran = row[column_names[3]]
23
+
24
+ if kode and kode[0] in ["3", "6"]:
25
+ char1 = kode
26
+ anggaran3 = anggaran
27
+
28
+ elif kode.startswith("0"):
29
+ char2 = kode
30
+
31
+ elif kode != "nan" and kode.isalpha():
32
+ char3 = kode
33
+ previous_char3 = char3
34
+ anggaran1 = anggaran
35
+
36
+ elif kode.startswith("5"):
37
+ char4 = kode
38
+ anggaran2 = anggaran
39
+
40
+ if pd.isna(row[column_names[9]]):
41
+ nominal = row[column_names[10]]
42
+ tipe = row[column_names[13]]
43
+ else:
44
+ nominal = row[column_names[9]]
45
+ tipe = row[column_names[11]]
46
+
47
+ if char3 == "":
48
+ char3 = previous_char3
49
+
50
+ mak = f"{char1}.{char2}.{char3}.{char4}"
51
+
52
+ output_data.append({
53
+ "No": no,
54
+ "MAK": mak,
55
+ "Anggaran3": anggaran3,
56
+ "Anggaran1": anggaran1,
57
+ "Anggaran2": anggaran2,
58
+ "Nominal": nominal,
59
+ "Tipe": tipe
60
+ })
61
+
62
+ no += 1
63
+ char3 = ""
64
+
65
+ if pd.notna(nominal):
66
+ total_nominal += float(nominal)
67
+
68
+ output_data.append({
69
+ "No": "",
70
+ "MAK": "",
71
+ "Anggaran3": "",
72
+ "Anggaran1": "",
73
+ "Anggaran2": "",
74
+ "Nominal": f"Total Nominal: {total_nominal:.0f}",
75
+ "Tipe": ""
76
+ })
77
+
78
+ output_df = pd.DataFrame(output_data)
79
+
80
+ output_file = "/tmp/processed_data.xlsx"
81
+ output_df.to_excel(output_file, index=False)
82
+
83
+ return output_file
84
+
85
+ except Exception as e:
86
+ return f"Error: {e}"
87
+
88
+ with gr.Blocks(title="Konvert Mata Anggaran Sakti") as demo:
89
+ gr.Markdown("### Konvert Mata Anggaran Sakti")
90
+ gr.Markdown("Unggah file Excel, sistem akan mengonversi dan menghasilkan file Excel baru.")
91
+
92
+ input_file = gr.File(label="Upload Excel File", file_types=[".xlsx"])
93
+ output_file = gr.File(label="Download Hasil")
94
+
95
+ btn = gr.Button("Proses")
96
+ btn.click(process_data, inputs=input_file, outputs=output_file)
97
 
98
  demo.launch()