Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,41 +2,31 @@ import gradio as gr
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
from sklearn.preprocessing import MinMaxScaler
|
| 5 |
-
|
| 6 |
-
import zipfile
|
| 7 |
-
import os
|
| 8 |
|
| 9 |
-
# Step 1:
|
| 10 |
-
|
| 11 |
-
unzip_dir = "lstm_saved_model"
|
| 12 |
-
|
| 13 |
-
# Unzip the model if not already unzipped
|
| 14 |
-
if not os.path.exists(unzip_dir):
|
| 15 |
-
with zipfile.ZipFile(model_zip_path, "r") as zip_ref:
|
| 16 |
-
zip_ref.extractall(unzip_dir)
|
| 17 |
-
|
| 18 |
-
# Step 2: Load the TensorFlow SavedModel
|
| 19 |
try:
|
| 20 |
-
model =
|
| 21 |
print("Model loaded successfully!")
|
| 22 |
except Exception as e:
|
| 23 |
print(f"Error loading model: {e}")
|
| 24 |
raise
|
| 25 |
|
| 26 |
-
# Step
|
| 27 |
def predict_performance(file):
|
| 28 |
try:
|
| 29 |
-
# Step
|
| 30 |
data = pd.read_excel(file)
|
| 31 |
|
| 32 |
-
# Step
|
| 33 |
if "Time" in data.columns:
|
| 34 |
data = data.drop(columns=["Time"]) # Drop 'Time' column if it exists
|
| 35 |
features = data.drop(columns=["Student_ID", "Performance"], errors="ignore")
|
| 36 |
scaler = MinMaxScaler()
|
| 37 |
features_scaled = scaler.fit_transform(features)
|
| 38 |
|
| 39 |
-
# Step
|
| 40 |
num_time_steps = 440 # Expected number of time steps
|
| 41 |
if features_scaled.shape[0] != num_time_steps:
|
| 42 |
return "Error: Input data does not have the required time steps."
|
|
@@ -44,11 +34,11 @@ def predict_performance(file):
|
|
| 44 |
# Reshape the data for prediction
|
| 45 |
X_test = np.expand_dims(features_scaled, axis=0)
|
| 46 |
|
| 47 |
-
# Step
|
| 48 |
predictions = model.predict(X_test)
|
| 49 |
predicted_label = np.argmax(predictions, axis=1)[0]
|
| 50 |
|
| 51 |
-
# Step
|
| 52 |
label_mapping = {0: "Low", 1: "Medium", 2: "High"}
|
| 53 |
interpretation_messages = {
|
| 54 |
0: "The simulation exercise needs major improvement.",
|
|
@@ -63,7 +53,7 @@ def predict_performance(file):
|
|
| 63 |
except Exception as e:
|
| 64 |
return f"Error during prediction: {e}"
|
| 65 |
|
| 66 |
-
# Step
|
| 67 |
interface = gr.Interface(
|
| 68 |
fn=predict_performance,
|
| 69 |
inputs=gr.inputs.File(label="Upload Excel File"),
|
|
@@ -72,6 +62,6 @@ interface = gr.Interface(
|
|
| 72 |
description="Upload an Excel file containing student simulation data to predict performance."
|
| 73 |
)
|
| 74 |
|
| 75 |
-
# Step
|
| 76 |
if __name__ == "__main__":
|
| 77 |
interface.launch()
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
from sklearn.preprocessing import MinMaxScaler
|
| 5 |
+
from tensorflow.keras.models import load_model
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Step 1: Load the HDF5 model
|
| 8 |
+
model_path = "lstm_model.h5" # Adjust this if your model file name differs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
try:
|
| 10 |
+
model = load_model(model_path)
|
| 11 |
print("Model loaded successfully!")
|
| 12 |
except Exception as e:
|
| 13 |
print(f"Error loading model: {e}")
|
| 14 |
raise
|
| 15 |
|
| 16 |
+
# Step 2: Define the prediction function
|
| 17 |
def predict_performance(file):
|
| 18 |
try:
|
| 19 |
+
# Step 2.1: Load the Excel file
|
| 20 |
data = pd.read_excel(file)
|
| 21 |
|
| 22 |
+
# Step 2.2: Preprocess the data
|
| 23 |
if "Time" in data.columns:
|
| 24 |
data = data.drop(columns=["Time"]) # Drop 'Time' column if it exists
|
| 25 |
features = data.drop(columns=["Student_ID", "Performance"], errors="ignore")
|
| 26 |
scaler = MinMaxScaler()
|
| 27 |
features_scaled = scaler.fit_transform(features)
|
| 28 |
|
| 29 |
+
# Step 2.3: Ensure the shape is correct (time-series data reshaped per student)
|
| 30 |
num_time_steps = 440 # Expected number of time steps
|
| 31 |
if features_scaled.shape[0] != num_time_steps:
|
| 32 |
return "Error: Input data does not have the required time steps."
|
|
|
|
| 34 |
# Reshape the data for prediction
|
| 35 |
X_test = np.expand_dims(features_scaled, axis=0)
|
| 36 |
|
| 37 |
+
# Step 2.4: Make predictions
|
| 38 |
predictions = model.predict(X_test)
|
| 39 |
predicted_label = np.argmax(predictions, axis=1)[0]
|
| 40 |
|
| 41 |
+
# Step 2.5: Interpret the predictions
|
| 42 |
label_mapping = {0: "Low", 1: "Medium", 2: "High"}
|
| 43 |
interpretation_messages = {
|
| 44 |
0: "The simulation exercise needs major improvement.",
|
|
|
|
| 53 |
except Exception as e:
|
| 54 |
return f"Error during prediction: {e}"
|
| 55 |
|
| 56 |
+
# Step 3: Create the Gradio interface
|
| 57 |
interface = gr.Interface(
|
| 58 |
fn=predict_performance,
|
| 59 |
inputs=gr.inputs.File(label="Upload Excel File"),
|
|
|
|
| 62 |
description="Upload an Excel file containing student simulation data to predict performance."
|
| 63 |
)
|
| 64 |
|
| 65 |
+
# Step 4: Launch the app
|
| 66 |
if __name__ == "__main__":
|
| 67 |
interface.launch()
|