MoinulwithAI commited on
Commit
2aafbf4
·
verified ·
1 Parent(s): a957d0a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -17
app.py CHANGED
@@ -1,33 +1,49 @@
1
- import gradio as gr
2
  import numpy as np
 
3
  import joblib
4
  from pytorch_tabnet.tab_model import TabNetClassifier
5
 
6
- # Load model and preprocessing tools
7
  model = TabNetClassifier()
8
- model.load_model("tabnet_model.zip")
9
- scaler = joblib.load("scaler.save")
10
- encoder = joblib.load("encoder.save")
 
11
 
12
- # Features used in the model
13
- features = [f"{trait}{i}" for trait in ["EXT", "EST", "AGR", "CSN", "OPN"] for i in range(1, 11)]
 
 
 
 
 
 
14
 
 
 
 
 
 
 
 
 
 
15
  def predict_personality(*inputs):
16
- X = np.array(inputs).reshape(1, -1).astype(np.float32)
17
- X_scaled = scaler.transform(X)
18
- y_pred = model.predict(X_scaled)
19
- label = encoder.inverse_transform(y_pred)[0]
20
- return f"Predicted Personality Type: {label}"
21
 
22
- # Create Gradio interface
23
- inputs = [gr.Slider(1, 5, step=0.1, label=f) for f in features]
 
24
 
25
  demo = gr.Interface(
26
  fn=predict_personality,
27
  inputs=inputs,
28
- outputs=gr.Text(label="Personality Prediction"),
29
  title="Personality Type Classifier (Introvert vs. Extrovert)",
30
- description="This model predicts if a person is Introvert or Extrovert based on their IPIP-FFM scores."
31
  )
32
-
33
  demo.launch()
 
 
1
  import numpy as np
2
+ import gradio as gr
3
  import joblib
4
  from pytorch_tabnet.tab_model import TabNetClassifier
5
 
6
+ # Load model, scaler, and encoder
7
  model = TabNetClassifier()
8
+ model.load_model('tabnet_model')
9
+
10
+ scaler = joblib.load('D:/Dataset/IPIP-FFM-data-8Nov2018/scaler.save')
11
+ encoder = joblib.load('D:/Dataset/IPIP-FFM-data-8Nov2018/encoder.save')
12
 
13
+ # Full form trait mapping
14
+ trait_prefixes = {
15
+ 'Extraversion': 'EXT',
16
+ 'Emotional Stability': 'EST',
17
+ 'Agreeableness': 'AGR',
18
+ 'Conscientiousness': 'CSN',
19
+ 'Openness': 'OPN'
20
+ }
21
 
22
+ # Create full feature names with full form labels
23
+ feature_labels = []
24
+ feature_keys = []
25
+ for trait, abbrev in trait_prefixes.items():
26
+ for i in range(10):
27
+ feature_labels.append(f"{trait} Q{i+1}")
28
+ feature_keys.append(f"{abbrev}{i+1}")
29
+
30
+ # Inference function
31
  def predict_personality(*inputs):
32
+ input_array = np.array(inputs).reshape(1, -1)
33
+ scaled_input = scaler.transform(input_array)
34
+ pred = model.predict(scaled_input)
35
+ personality = encoder.inverse_transform(pred)[0]
36
+ return f"Predicted Personality: **{personality}**"
37
 
38
+ # Gradio UI: 50 sliders with full trait names
39
+ inputs = [gr.Slider(1.0, 5.0, value=3.0, label=label) for label in feature_labels]
40
+ output = gr.Textbox(label="Prediction")
41
 
42
  demo = gr.Interface(
43
  fn=predict_personality,
44
  inputs=inputs,
45
+ outputs=output,
46
  title="Personality Type Classifier (Introvert vs. Extrovert)",
47
+ description="Provide scores (1–5) for 50 questions from the IPIP-FFM questionnaire. The model will predict whether the person is an Introvert or an Extrovert."
48
  )
 
49
  demo.launch()