Spaces:
Sleeping
Sleeping
Download app.py from indraw111/rainpredict_LSTMG_EMMA_2: direct link, hf CLI and curl.
- Browser
- Download file 3.21 kB
-
https://huggingface.co/spaces/indraw111/rainpredict_LSTMG_EMMA_2/resolve/main/app.py
- Command line
-
hf download hf://spaces/indraw111/rainpredict_LSTMG_EMMA_2/app.py
-
curl -L -o app.py https://huggingface.co/spaces/indraw111/rainpredict_LSTMG_EMMA_2/resolve/main/app.py
3.21 kB
| import gradio as gr | |
| import numpy as np | |
| import tensorflow as tf | |
| import joblib | |
| import os | |
| from huggingface_hub import InferenceClient | |
| # === Load model & scaler === | |
| lstm_model = tf.keras.models.load_model('retrained_lstm_model.h5', compile=False) | |
| lstm_model.compile(optimizer='adam', loss='mse', metrics=['mae']) | |
| scaler_X = joblib.load('scaler_X.joblib') | |
| scaler_y = joblib.load('scaler_y.joblib') | |
| window_size = 7 | |
| # === Setup Inference Client for Gemma 2-27B === | |
| api_key = os.environ.get("hf_") # Pastikan HF_TOKEN diset di Secrets | |
| client = InferenceClient( | |
| provider="nebius", | |
| api_key=api_key | |
| ) | |
| def interpret_rainfall_with_gemma2(tavg, rh_avg, rainfall_mm): | |
| prompt = f""" | |
| Data cuaca: | |
| - Temperatur Rata-rata: {tavg}°C | |
| - Kelembaban Rata-rata: {rh_avg}% | |
| - Prediksi Curah Hujan: {rainfall_mm:.2f} mm | |
| Berikan analisis cuaca berdasarkan data di atas. Apakah hujan tergolong ringan, sedang, atau lebat? | |
| Apa rekomendasi aktivitas harian, terutama jika saya ingin keluar rumah? | |
| """ | |
| try: | |
| completion = client.chat.completions.create( | |
| model="google/gemma-2-27b-it", | |
| messages=[{"role": "user", "content": prompt}], | |
| max_tokens=300 | |
| ) | |
| return completion.choices[0].message.content | |
| except Exception as e: | |
| return f"Gagal mendapatkan interpretasi: {str(e)}" | |
| # === Gradio App === | |
| with gr.Blocks(fill_height=True, title="Prediksi Curah Hujan & Interpretasi AI") as demo: | |
| gr.Markdown("# Prediksi Curah Hujan dengan LSTM + Interpretasi Gemma 2") | |
| gr.Markdown("Masukkan data cuaca harian dan dapatkan prediksi serta analisis bahasa alami.") | |
| with gr.Row(): | |
| with gr.Column(): | |
| tavg_input = gr.Number(label="Temperatur Rata-rata (°C)", value=25.0) | |
| rh_avg_input = gr.Number(label="Kelembaban Rata-rata (%)", value=80.0) | |
| ss_input = gr.Number(label="Durasi Sinar Matahari (jam)", value=5.0) | |
| predict_btn = gr.Button("Prediksi Curah Hujan") | |
| with gr.Column(): | |
| rainfall_output = gr.Textbox(label="Hasil Prediksi (mm)") | |
| interpretation_output = gr.Textbox(label="Interpretasi AI", lines=12) | |
| def predict_and_interpret(tavg, rh_avg, ss): | |
| user_input = np.array([[tavg, rh_avg, ss]] * window_size) | |
| user_input_scaled = scaler_X.transform(user_input) | |
| user_input_scaled = user_input_scaled.reshape(1, window_size, 3) | |
| predicted_scaled = lstm_model.predict(user_input_scaled) | |
| predicted_rainfall = scaler_y.inverse_transform(predicted_scaled) | |
| rainfall_mm = float(predicted_rainfall[0][0]) | |
| explanation = interpret_rainfall_with_gemma2(tavg, rh_avg, rainfall_mm) | |
| return f"{rainfall_mm:.2f}", explanation | |
| predict_btn.click( | |
| predict_and_interpret, | |
| inputs=[tavg_input, rh_avg_input, ss_input], | |
| outputs=[rainfall_output, interpretation_output] | |
| ) | |
| gr.Markdown("### Tentang Aplikasi") | |
| gr.Markdown(""" | |
| Aplikasi ini memanfaatkan: | |
| - Model LSTM untuk prediksi curah hujan berdasarkan data suhu, kelembaban, dan durasi sinar matahari | |
| - Model bahasa besar Gemma 2 (27B) untuk menjelaskan hasil prediksi | |
| """) | |
| demo.launch() | |