Download app.py from cladyles/BABF_ec_ML: direct link, hf CLI and curl.
- Browser
- Download file 1.09 kB
-
https://huggingface.co/cladyles/BABF_ec_ML/resolve/main/app.py
- Command line
-
hf download hf://cladyles/BABF_ec_ML/app.py
-
curl -L -o app.py https://huggingface.co/cladyles/BABF_ec_ML/resolve/main/app.py
1.09 kB
| import gradio as gr | |
| import pandas as pd | |
| import joblib | |
| import numpy as np | |
| from sklearn.ensemble import RandomForestRegressor | |
| # ——— 加载你训练好的模型和特征名 ——— | |
| model = joblib.load("rf_model.joblib") | |
| feature_names = joblib.load("rf_features.joblib") # 应当是像 ["feature1", "feature2", ...] | |
| # ——— 定义预测函数 ——— | |
| def predict_energy(*inputs): | |
| # 将输入打包成 DataFrame | |
| df = pd.DataFrame([inputs], columns=feature_names) | |
| # 调用模型预测 | |
| pred = model.predict(df) | |
| return f"预测能耗为:{pred[0]:.2f}" | |
| # ——— 构建 Gradio 界面 ——— | |
| # 动态为每个特征创建一个 Number 输入框 | |
| input_components = [ | |
| gr.Number(label=name) for name in feature_names | |
| ] | |
| demo = gr.Interface( | |
| fn=predict_energy, | |
| inputs=input_components, | |
| outputs="text", | |
| title="🏡 Energy Usage Predictor", | |
| description="基于 RandomForest 已训练模型 (rf_model.joblib),输入若干特征,输出能耗预测结果" | |
| ) | |
| # ——— 启动 ——— | |
| if __name__ == "__main__": | |
| demo.launch() |