Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|