MuhammadHananKhan123's picture
Update app.py
26b4dd5 verified
import gradio as gr
import pandas as pd
import numpy as np
# =========================================================
# Load trained model
# =========================================================
# Upload your trained model file in the same Hugging Face Space
# Example model name: biomass_model.pkl
try:
model = joblib.load("biomass_model.pkl")
model_loaded = True
except:
model_loaded = False
# =========================================================
# Prediction Function
# =========================================================
def predict_composition(cellulose, hemicellulose, lignin, moisture, ash):
# Input array
input_data = np.array([
[cellulose, hemicellulose, lignin, moisture, ash]
])
# If model exists
if model_loaded:
prediction = model.predict(input_data)
return {
"Predicted Biomass Composition": float(prediction[0])
}
# Dummy calculation if model not uploaded yet
biomass_score = (
0.35 * cellulose +
0.25 * hemicellulose +
0.20 * lignin -
0.10 * moisture -
0.10 * ash
)
return {
"Estimated Biomass Score": round(biomass_score, 2)
}
# =========================================================
# Gradio Interface
# =========================================================
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown(
"""
# Biomass Composition Finder
Enter biomass properties to estimate biomass composition.
"""
)
with gr.Row():
cellulose = gr.Slider(0, 100, value=40, label="Cellulose (%)")
hemicellulose = gr.Slider(0, 100, value=25, label="Hemicellulose (%)")
with gr.Row():
lignin = gr.Slider(0, 100, value=20, label="Lignin (%)")
moisture = gr.Slider(0, 100, value=10, label="Moisture Content (%)")
ash = gr.Slider(0, 50, value=5, label="Ash Content (%)")
output = gr.JSON(label="Prediction Result")
predict_btn = gr.Button("Predict")
predict_btn.click(
fn=predict_composition,
inputs=[
cellulose,
hemicellulose,
lignin,
moisture,
ash
],
outputs=output
)
# =========================================================
# Launch App
# =========================================================
demo.launch()