ntam0001 commited on
Commit
dc83180
·
verified ·
1 Parent(s): bffed60

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -28
app.py CHANGED
@@ -15,12 +15,11 @@ logger = logging.getLogger(__name__)
15
  # Initialize model components
16
  model = None
17
  scaler = None
18
- metadata = {}
19
  feature_names = []
20
  model_loaded = False
21
 
22
  def load_model():
23
- global model, scaler, metadata, feature_names, model_loaded
24
 
25
  try:
26
  # Verify all required files exist
@@ -44,7 +43,7 @@ def load_model():
44
  logger.info("Loading metadata...")
45
  with open('metadata.json', 'r') as f:
46
  metadata = json.load(f)
47
- feature_names = metadata.get('feature_names', [])
48
 
49
  model_loaded = True
50
  logger.info("✅ Model loaded successfully!")
@@ -52,6 +51,7 @@ def load_model():
52
 
53
  except Exception as e:
54
  logger.error(f"❌ Model loading failed: {str(e)}")
 
55
  model_loaded = False
56
 
57
  # Load model at startup
@@ -84,15 +84,11 @@ def predict(*args):
84
  prediction = "Eligible" if probability > 0.5 else "Not Eligible"
85
  confidence = abs(probability - 0.5) * 2
86
 
87
- return {
88
- "Prediction": prediction,
89
- "Probability": f"{probability:.4f}",
90
- "Confidence": f"{confidence:.4f}"
91
- }
92
 
93
  except Exception as e:
94
  logger.error(f"Prediction error: {str(e)}")
95
- return {"Error": str(e)}
96
 
97
  # Create Gradio interface
98
  with gr.Blocks(title="Student Eligibility Predictor") as demo:
@@ -101,34 +97,36 @@ with gr.Blocks(title="Student Eligibility Predictor") as demo:
101
 
102
  with gr.Row():
103
  with gr.Column():
104
- input_components = [gr.Number(label=name) for name in feature_names]
 
 
 
 
105
  predict_btn = gr.Button("Predict", variant="primary")
 
106
  with gr.Column():
107
  prediction_output = gr.Textbox(label="Prediction")
108
  probability_output = gr.Textbox(label="Probability")
109
  confidence_output = gr.Textbox(label="Confidence")
110
 
111
- # Add examples if features exist
112
- if len(feature_names) > 0:
113
- examples = []
114
- if len(feature_names) >= 3:
115
- examples.append([75, 80, 85] + [0]*(len(feature_names)-3))
116
- elif len(feature_names) == 2:
117
- examples.append([75, 80])
118
- else:
119
- examples.append([75])
120
-
121
- gr.Examples(
122
- examples=examples,
123
- inputs=input_components,
124
- outputs=[prediction_output, probability_output, confidence_output],
125
- fn=predict,
126
- cache_examples=False
127
- )
128
 
129
  predict_btn.click(
130
  fn=predict,
131
- inputs=input_components,
132
  outputs=[prediction_output, probability_output, confidence_output]
133
  )
134
 
 
15
  # Initialize model components
16
  model = None
17
  scaler = None
 
18
  feature_names = []
19
  model_loaded = False
20
 
21
  def load_model():
22
+ global model, scaler, feature_names, model_loaded
23
 
24
  try:
25
  # Verify all required files exist
 
43
  logger.info("Loading metadata...")
44
  with open('metadata.json', 'r') as f:
45
  metadata = json.load(f)
46
+ feature_names = metadata.get('feature_names', ['Score 1', 'Score 2']) # Default names
47
 
48
  model_loaded = True
49
  logger.info("✅ Model loaded successfully!")
 
51
 
52
  except Exception as e:
53
  logger.error(f"❌ Model loading failed: {str(e)}")
54
+ feature_names = ['Score 1', 'Score 2'] # Default names if loading fails
55
  model_loaded = False
56
 
57
  # Load model at startup
 
84
  prediction = "Eligible" if probability > 0.5 else "Not Eligible"
85
  confidence = abs(probability - 0.5) * 2
86
 
87
+ return prediction, f"{probability:.4f}", f"{confidence:.4f}"
 
 
 
 
88
 
89
  except Exception as e:
90
  logger.error(f"Prediction error: {str(e)}")
91
+ return f"Error: {str(e)}", "N/A", "N/A"
92
 
93
  # Create Gradio interface
94
  with gr.Blocks(title="Student Eligibility Predictor") as demo:
 
97
 
98
  with gr.Row():
99
  with gr.Column():
100
+ # Create input components based on actual features
101
+ inputs = []
102
+ for feature in feature_names:
103
+ inputs.append(gr.Number(label=feature, value=75))
104
+
105
  predict_btn = gr.Button("Predict", variant="primary")
106
+
107
  with gr.Column():
108
  prediction_output = gr.Textbox(label="Prediction")
109
  probability_output = gr.Textbox(label="Probability")
110
  confidence_output = gr.Textbox(label="Confidence")
111
 
112
+ # Setup examples
113
+ examples = []
114
+ if len(feature_names) >= 2:
115
+ examples = [[75, 80]] # Basic example with two features
116
+ else:
117
+ examples = [[75]] # Fallback example
118
+
119
+ gr.Examples(
120
+ examples=examples,
121
+ inputs=inputs,
122
+ outputs=[prediction_output, probability_output, confidence_output],
123
+ fn=predict,
124
+ cache_examples=False
125
+ )
 
 
 
126
 
127
  predict_btn.click(
128
  fn=predict,
129
+ inputs=inputs,
130
  outputs=[prediction_output, probability_output, confidence_output]
131
  )
132