Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
from tensorflow import keras
|
| 6 |
+
from sklearn.preprocessing import StandardScaler, LabelEncoder
|
| 7 |
+
import pickle # Missing import
|
| 8 |
+
|
| 9 |
+
# Load model and preprocessing objects (replace paths if needed)
|
| 10 |
+
model = keras.models.load_model('model.h5')
|
| 11 |
+
with open('scaler.pkl', 'rb') as f:
|
| 12 |
+
scaler = pickle.load(f)
|
| 13 |
+
with open('label_encoder.pkl', 'rb') as f:
|
| 14 |
+
label_encoder = pickle.load(f)
|
| 15 |
+
|
| 16 |
+
def predict_eligibility(theory_score, practice_score):
|
| 17 |
+
"""Predicts eligibility given theory and practice scores."""
|
| 18 |
+
try:
|
| 19 |
+
# Preprocess input - create DataFrame to avoid feature name warning
|
| 20 |
+
input_df = pd.DataFrame({
|
| 21 |
+
'theory': [theory_score],
|
| 22 |
+
'practice': [practice_score]
|
| 23 |
+
})
|
| 24 |
+
scaled_input = scaler.transform(input_df)
|
| 25 |
+
|
| 26 |
+
# Predict
|
| 27 |
+
proba = model.predict(scaled_input, verbose=0)[0][0]
|
| 28 |
+
prediction = int(proba > 0.5)
|
| 29 |
+
class_name = label_encoder.inverse_transform([prediction])[0]
|
| 30 |
+
|
| 31 |
+
# Confidence score
|
| 32 |
+
confidence = proba if prediction else (1 - proba)
|
| 33 |
+
|
| 34 |
+
# Return 3 separate values (not a dictionary)
|
| 35 |
+
return (
|
| 36 |
+
class_name, # First output: Prediction label
|
| 37 |
+
f"{confidence * 100:.2f}%", # Second output: Confidence
|
| 38 |
+
"β No" if prediction else "β
Yes" # Third output: Eligibility status
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
except Exception as e:
|
| 42 |
+
# Return error message for all outputs
|
| 43 |
+
error_msg = f"Error: {str(e)}"
|
| 44 |
+
return error_msg, "0%", "β Error"
|
| 45 |
+
|
| 46 |
+
# Gradio Interface
|
| 47 |
+
demo = gr.Interface(
|
| 48 |
+
fn=predict_eligibility,
|
| 49 |
+
inputs=[
|
| 50 |
+
gr.Slider(0, 100, value=50, step=1, label="Theory Score"),
|
| 51 |
+
gr.Slider(0, 100, value=60, step=1, label="Practice Score")
|
| 52 |
+
],
|
| 53 |
+
outputs=[
|
| 54 |
+
gr.Textbox(label="Prediction"), # Changed from gr.Label to gr.Textbox
|
| 55 |
+
gr.Textbox(label="Confidence"),
|
| 56 |
+
gr.Textbox(label="Eligibility Status")
|
| 57 |
+
],
|
| 58 |
+
title="π Eligibility Predictor",
|
| 59 |
+
description="Enter theory and practice scores to check eligibility.",
|
| 60 |
+
examples=[[85, 90], [45, 60], [70, 75]],
|
| 61 |
+
theme="soft"
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# Launch
|
| 65 |
+
demo.launch(share=True)
|