File size: 2,406 Bytes
bf8e026 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | 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() |