| | import gradio as gr |
| | import pyrebase |
| | import pandas as pd |
| | import torch |
| | import matplotlib.pyplot as plt |
| | from chronos import ChronosPipeline |
| |
|
| | |
| | |
| | model_id = "amazon/chronos-t5-tiny" |
| | config = { |
| | "apiKey": "AIzaSyApZpMn8lNOVMLO7bUAoe3sIpRR7C9Te90", |
| | "authDomain": "pertamina-power-optimizer.firebaseapp.com", |
| | "databaseURL": "https://pertamina-power-optimizer-default-rtdb.asia-southeast1.firebasedatabase.app", |
| | "storageBucket": "pertamina-power-optimizer.appspot.com" |
| | } |
| |
|
| | |
| | firebase = pyrebase.initialize_app(config) |
| | auth = firebase.auth() |
| | db = firebase.database() |
| | pipeline = ChronosPipeline.from_pretrained(model_id, device_map="cpu", dtype=torch.float32) |
| |
|
| | |
| | user_session = auth.sign_in_with_email_and_password("user@pertamina.com", "Pertamina45_") |
| |
|
| | |
| | def analyze_energy(safe_limit): |
| | try: |
| | |
| | token = user_session['idToken'] |
| | data = db.child("data_efisiensi").get(token).val() |
| | |
| | if not data: |
| | return "Menunggu data dari ESP32...", None, 0 |
| |
|
| | |
| | df = pd.DataFrame(list(data.values())) |
| | |
| | |
| | |
| | context = torch.tensor(df['w'].tail(50).values) |
| | |
| | |
| | forecast = pipeline.predict(context, prediction_length=12) |
| | forecast_median = forecast[0].median(dim=0).values |
| |
|
| | |
| | plt.figure(figsize=(10, 5)) |
| | plt.plot(df['w'].tail(50).values, label="Daya Aktual (W)", color="#0047AB", linewidth=2) |
| | plt.plot(range(50, 62), forecast_median, color='#FF0000', linestyle='--', label="Prediksi AI") |
| | |
| | |
| | plt.axhline(y=safe_limit, color='green', linestyle=':', label="Batas Aman User") |
| | |
| | plt.title("Integrated Terminal Semarang - Live Energy Forecast") |
| | plt.xlabel("Urutan Data") |
| | plt.ylabel("Daya (Watt)") |
| | plt.legend() |
| | plt.grid(True, alpha=0.3) |
| | |
| | plot_path = "energy_live.png" |
| | plt.savefig(plot_path) |
| | plt.close() |
| |
|
| | |
| | last_w = df['w'].iloc[-1] |
| | next_w = float(forecast_median[0]) |
| | |
| | |
| | if next_w > safe_limit: |
| | status = "⚠️ BAHAYA: PREDIKSI MELEBIHI BATAS AMAN" |
| | else: |
| | status = "✅ OPERASIONAL STABIL" |
| |
|
| | report = (f"📊 LIVE AI ANALYSIS\n" |
| | f"----------------------------------\n" |
| | f"Daya Sekarang : {last_w:.2f} W\n" |
| | f"Batas Aman User: {safe_limit:.2f} W\n" |
| | f"Prediksi AI : {next_w:.2f} W\n" |
| | f"Status Sistem : {status}\n" |
| | f"Update Terakhir: {pd.Timestamp.now().strftime('%H:%M:%S')}") |
| |
|
| | return report, plot_path, round(last_w, 2) |
| |
|
| | except Exception as e: |
| | return f"Koneksi/Error: {str(e)}", None, 0 |
| |
|
| | |
| | with gr.Blocks() as demo: |
| | gr.Markdown("# ⚡ Pertamina Power Optimizer") |
| | gr.Markdown("Monitoring Otomatis berbasis AI") |
| | |
| | with gr.Row(): |
| | with gr.Column(scale=1): |
| | |
| | input_limit = gr.Number(label="SET BATAS AMAN (WATT)", value=3500) |
| | |
| | watt_display = gr.Number(label="DAYA SAAT INI (WATT)") |
| | output_text = gr.Textbox(label="Status Analisis (Auto-update 5s)", lines=10) |
| | |
| | with gr.Column(scale=2): |
| | output_plot = gr.Image(label="Grafik Tren Efisiensi") |
| |
|
| | |
| | timer = gr.Timer(5) |
| | |
| | |
| | timer.tick( |
| | fn=analyze_energy, |
| | inputs=[input_limit], |
| | outputs=[output_text, output_plot, watt_display] |
| | ) |
| |
|
| | |
| | demo.load( |
| | fn=analyze_energy, |
| | inputs=[input_limit], |
| | outputs=[output_text, output_plot, watt_display] |
| | ) |
| |
|
| | |
| | if __name__ == "__main__": |
| | |
| | demo.queue().launch(theme=gr.themes.Soft()) |