Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import plotly.express as px
|
| 5 |
+
import plotly.graph_objects as go
|
| 6 |
+
from sklearn.linear_model import LinearRegression
|
| 7 |
+
|
| 8 |
+
# ================= LOAD DATA =================
|
| 9 |
+
try:
|
| 10 |
+
df = pd.read_excel("df_final.xlsx", sheet_name="Sheet1")
|
| 11 |
+
# Fix encoding
|
| 12 |
+
df.columns = df.columns.str.replace("Â", "")
|
| 13 |
+
for col in df.select_dtypes(include='object').columns:
|
| 14 |
+
df[col] = df[col].astype(str).str.replace("Â", "")
|
| 15 |
+
# Parse datetime
|
| 16 |
+
df['Time'] = pd.to_datetime(df['Time'], errors='coerce')
|
| 17 |
+
df = df.dropna(subset=['Time'])
|
| 18 |
+
df['hour'] = df['Time'].dt.hour
|
| 19 |
+
# Alarm flag
|
| 20 |
+
df['is_alarm'] = (~df['Alarm Status'].str.contains('No Alarm', na=False)).astype(int)
|
| 21 |
+
except FileNotFoundError:
|
| 22 |
+
df = pd.DataFrame() # Empty if file not found
|
| 23 |
+
print("⚠️ File df_final.xlsx not found. Running with dummy data.")
|
| 24 |
+
|
| 25 |
+
# ================= SIMULASI PRESSURE BERDASARKAN GRADIEN =================
|
| 26 |
+
# Definisikan rumus gradien berdasarkan tabel Anda
|
| 27 |
+
pressure_formulas = {
|
| 28 |
+
(10, 'High', 'Min'): (76.658370666499, 0.424510125965938),
|
| 29 |
+
(10, 'High', 'Max'): (81.0954797166173, 0.445533183),
|
| 30 |
+
(10, 'Low', 'Min'): (68.02273668, 0.375071782),
|
| 31 |
+
(10, 'Low', 'Max'): (63.6496681312519, 0.351500420678697),
|
| 32 |
+
(11, 'High', 'Min'): (102.275273585809, 0.496334109689563),
|
| 33 |
+
(11, 'High', 'Max'): (108.013017260632, 0.525284752576333),
|
| 34 |
+
(11, 'Low', 'Min'): (90.4977325517091, 0.446382251463767),
|
| 35 |
+
(11, 'Low', 'Max'): (84.9683663311582, 0.410110214756436),
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
def calculate_pressure(tyre_type, temp, condition, min_max):
|
| 39 |
+
if (tyre_type, condition, min_max) in pressure_formulas:
|
| 40 |
+
intercept, gradient = pressure_formulas[(tyre_type, condition, min_max)]
|
| 41 |
+
return intercept + gradient * temp
|
| 42 |
+
return None
|
| 43 |
+
|
| 44 |
+
# ================= GRADIO INTERFACE =================
|
| 45 |
+
with gr.Blocks(title="Michelin Mining Tyre Analytics - Simulation") as demo:
|
| 46 |
+
gr.Markdown("""
|
| 47 |
+
# 🛞 Michelin Mining Tyre Analytics — Objective 6
|
| 48 |
+
### Simulasi Tekanan Ban Berdasarkan Suhu & Jenis Ban
|
| 49 |
+
Masukkan suhu saat ini dan pilih jenis ban untuk mendapatkan rekomendasi tekanan ideal.
|
| 50 |
+
""")
|
| 51 |
+
|
| 52 |
+
with gr.Row():
|
| 53 |
+
tyre_type = gr.Radio(["10 (Depan)", "11 (Belakang)"], label="Jenis Ban", value="10 (Depan)")
|
| 54 |
+
temperature = gr.Slider(40, 70, value=54, step=0.5, label="Suhu Saat Ini (°C)")
|
| 55 |
+
|
| 56 |
+
with gr.Row():
|
| 57 |
+
btn_simulate = gr.Button("Simulasikan Tekanan")
|
| 58 |
+
|
| 59 |
+
with gr.Row():
|
| 60 |
+
output = gr.HTML(label="Hasil Simulasi")
|
| 61 |
+
|
| 62 |
+
def simulate_pressure(tyres, temp):
|
| 63 |
+
tyre_num = int(tyres.split()[0])
|
| 64 |
+
p_min_high = calculate_pressure(tyre_num, temp, 'High', 'Min')
|
| 65 |
+
p_max_high = calculate_pressure(tyre_num, temp, 'High', 'Max')
|
| 66 |
+
p_min_low = calculate_pressure(tyre_num, temp, 'Low', 'Min')
|
| 67 |
+
p_max_low = calculate_pressure(tyre_num, temp, 'Low', 'Max')
|
| 68 |
+
|
| 69 |
+
if p_min_high is None or p_max_high is None or p_min_low is None or p_max_low is None:
|
| 70 |
+
return "<p style='color:red;'>❌ Tidak dapat menghitung tekanan. Periksa input.</p>"
|
| 71 |
+
|
| 72 |
+
# Rekomendasi: Jaga tekanan antara P Min Low dan P Max High
|
| 73 |
+
recommendation = f"""
|
| 74 |
+
<div style="background:#f9f9f9; padding:15px; border-radius:8px; font-family:Segoe UI;">
|
| 75 |
+
<h4>📊 Hasil Simulasi untuk Ban {tyres} pada Suhu {temp}°C</h4>
|
| 76 |
+
<p><strong>P Min saat Low Pressure:</strong> {p_min_low:.1f} psi</p>
|
| 77 |
+
<p><strong>P Max saat Low Pressure:</strong> {p_max_low:.1f} psi</p>
|
| 78 |
+
<p><strong>P Min saat High Pressure:</strong> {p_min_high:.1f} psi</p>
|
| 79 |
+
<p><strong>P Max saat High Pressure:</strong> {p_max_high:.1f} psi</p>
|
| 80 |
+
<hr>
|
| 81 |
+
<p>✅ <strong>Rekomendasi Tekanan Ideal:</strong> Isi angin antara <strong>{p_min_low:.1f} – {p_max_high:.1f} psi</strong> untuk menghindari alarm Amber/Red.</p>
|
| 82 |
+
<p>⚠️ Jika tekanan saat ini < {p_min_low:.1f} psi → risiko <strong>Low Pressure</strong>.</p>
|
| 83 |
+
<p>⚠️ Jika tekanan saat ini > {p_max_high:.1f} psi → risiko <strong>High Pressure</strong>.</p>
|
| 84 |
+
</div>
|
| 85 |
+
"""
|
| 86 |
+
return recommendation
|
| 87 |
+
|
| 88 |
+
btn_simulate.click(simulate_pressure, inputs=[tyre_type, temperature], outputs=output)
|
| 89 |
+
|
| 90 |
+
demo.launch()
|