gaialive's picture
Update app.py
0dc505c verified
Raw
History Blame Contribute Delete
2.9 kB
import gradio as gr
import pandas as pd
import joblib
# โหลดโมเดลและ label encoder
model = joblib.load("biochar_lightgbm_model.pkl")
le = joblib.load("biochar_label_encoder.pkl")
# Danh sách các tính năng
feature_names = ['SurfaceArea', 'Fixed Carbon', 'pH', 'Pore Volume', 'Pore Size', 'Energy Content']
# Chức năng dự đoán
def recommend_with_probs(surface_area, fixed_carbon, pH, pore_volume, pore_size, energy_content):
input_data = pd.DataFrame([[surface_area, fixed_carbon, pH, pore_volume, pore_size, energy_content]],
columns=feature_names)
pred_class = model.predict(input_data)[0]
pred_label = le.inverse_transform([pred_class])[0]
# Dự đoán xác suất
probs = model.predict_proba(input_data)[0]
prob_labels = le.inverse_transform(range(len(probs)))
prob_df = pd.DataFrame({
'Application': prob_labels,
'Probability (%)': (probs * 100).round(2)
}).sort_values(by='Probability (%)', ascending=False).reset_index(drop=True)
return pred_label, prob_df
# Custom CSS
css = """
.gradio-container {background-color: #f4f9f4; font-family: 'Arial', sans-serif;}
h1, h2, h3, h4 {text-align: center;}
.output-class {font-size: 1.5em; color: #1b5e20; font-weight: bold;}
.gr-button {background-color: #66bb6a !important; color: white !important; border: none;}
"""
with gr.Blocks(css=css, title="Biochar Application Recommender") as demo:
gr.Markdown("# 🌱 Chuyên gia AI tư vấn ứng dụng Than sinh học")
gr.Markdown("** Hệ thống khuyến nghị ứng dụng than sinh học dựa trên tính chất hóa học và vật lý **")
with gr.Row():
with gr.Column():
surface_area = gr.Number(label="🧪 Surface Area (m²/g)", interactive=True)
fixed_carbon = gr.Number(label="🌑 Fixed Carbon (%)", interactive=True)
pH = gr.Number(label="⚗️ pH", interactive=True)
with gr.Column():
pore_volume = gr.Number(label="🔬 Pore Volume (cm³/g)", interactive=True)
pore_size = gr.Number(label="📏 Pore Size (nm)", interactive=True)
energy_content = gr.Number(label="🔥 Energy Content (kJ/kg)", interactive=True)
submit_btn = gr.Button("🚀 Submit")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("")
with gr.Column(scale=6):
output_text = gr.Textbox(label="✅ Recommended Application", elem_classes="output-class", interactive=False)
output_table = gr.Dataframe(label="📊 Prediction Probabilities", interactive=False)
with gr.Column(scale=1):
gr.Markdown("")
submit_btn.click(
fn=recommend_with_probs,
inputs=[surface_area, fixed_carbon, pH, pore_volume, pore_size, energy_content],
outputs=[output_text, output_table]
)
demo.launch()