prakashkota's picture
Added initial project files
0c01b91
raw
history blame
2.19 kB
# Everything here goes into the app.py file
# Define Gradio inference function
# 15 March 2025
# Prakash Kota
# East Greenbush
import numpy as np
import tensorflow as tf
import os
import gradio as gr
import joblib
# Fixed reactor parameters
V = 100 # Reactor volume (L)
k = 0.1 # Reaction rate constant (1/min)
delta_H = -50000 # Heat of reaction (J/mol)
rho = 1 # Density in kg/L
Cp = 4184 # Heat capacity in J/kg·K
# Define Gradio inference function
def predict_cstr(CA_in, T_in, F):
# Load scalers and model
scaler_X = joblib.load(f"{model_dir}/scaler_X.pkl")
scaler_Y = joblib.load(f"{model_dir}/scaler_Y.pkl")
model = tf.keras.models.load_model(f"{model_dir}/cstr_model.keras")
# Scale input
input_scaled = scaler_X.transform([[CA_in, T_in, F]])
# Predict using the model
prediction_scaled = model.predict(input_scaled)
prediction_original = scaler_Y.inverse_transform(prediction_scaled)
CA_ss_pred, T_ss_pred = prediction_original[0]
# Compute analytical solution
CA_ss_analytical = (CA_in * (F / V)) / ((F / V) + k)
T_ss_analytical = T_in + ((-delta_H * k * CA_ss_analytical) / (rho * Cp)) * (V / F)
# Compute % Error and % Accuracy
percent_error = abs((CA_ss_pred - CA_ss_analytical) / CA_ss_analytical) * 100
percent_accuracy = (1 - abs(CA_ss_pred - CA_ss_analytical) / CA_ss_analytical) * 100
return (f"Predicted CA_ss: {CA_ss_pred:.4f} mol/L\n"
f"Predicted T_ss: {T_ss_pred:.2f} K\n"
f"Analytical CA_ss: {CA_ss_analytical:.4f} mol/L\n"
f"Analytical T_ss: {T_ss_analytical:.2f} K\n"
f"% Error: {percent_error:.2f}%\n"
f"% Accuracy: {percent_accuracy:.2f}%")
# Deploy using Gradio
iface = gr.Interface(
fn=predict_cstr,
inputs=[
gr.Number(label="Input Concentration CA_in - Range [0.5-2.0] mol/L"),
gr.Number(label="Input Temperature T_in - Range [300-350] K"),
gr.Number(label="CSTR Flow Rate F - Range [5-20] L/min")
],
outputs="text",
title="CSTR Surrogate Model Inference",
description="Enter the input values to predict steady-state concentration and temperature."