Spaces:
Sleeping
Sleeping
| import numpy as np | |
| import gradio as gr | |
| import joblib | |
| from pytorch_tabnet.tab_model import TabNetClassifier | |
| # Load model, scaler, and encoder | |
| model = TabNetClassifier() | |
| model.load_model('tabnet_model.zip') # Must be .zip file | |
| scaler = joblib.load('scaler.save') | |
| encoder = joblib.load('encoder.save') | |
| # Full form trait mapping | |
| trait_prefixes = { | |
| 'Extraversion': 'EXT', | |
| 'Emotional Stability': 'EST', | |
| 'Agreeableness': 'AGR', | |
| 'Conscientiousness': 'CSN', | |
| 'Openness': 'OPN' | |
| } | |
| # Example IPIP-FFM questionnaire questions (replace with actual questions) | |
| questions = [ | |
| "I am the life of the party.", "I feel comfortable around people.", "I often feel blue.", "I am easily disturbed.", | |
| "I enjoy trying new things.", "I am talkative.", "I don't mind being the center of attention.", "I often get upset.", | |
| "I am interested in abstract ideas.", "I am full of energy.", "I would rather sit at home than go out.", | |
| "I don't like to draw attention to myself.", "I am sometimes easily angered.", "I have frequent mood swings.", | |
| "I like to travel to new places.", "I like to meet new people.", "I enjoy having a wide variety of friends.", | |
| "I am good at handling stress.", "I often feel like I’m not in control of my emotions.", "I prefer variety to routine.", | |
| "I am a very anxious person.", "I prefer to stick to one activity at a time.", "I enjoy being active in social settings.", | |
| "I don’t like to take risks.", "I get along with most people.", "I get bored easily.", "I tend to be impulsive.", | |
| "I like being organized.", "I feel uncomfortable around strangers.", "I avoid conflict with others.", "I have a lot of energy.", | |
| "I find it difficult to express my emotions.", "I often feel lonely.", "I like to keep my thoughts and feelings to myself.", | |
| "I find it difficult to relax.", "I am always prepared.", "I sometimes feel down.", "I find it difficult to focus on one task.", | |
| "I enjoy the company of others.", "I like to talk about my feelings.", "I can’t stand being interrupted.", | |
| "I often forget to do things.", "I am good at understanding other people’s feelings.", "I enjoy taking on challenges.", | |
| "I often feel overwhelmed.", "I like to take my time making decisions.", "I enjoy being in charge.", "I am easily distracted.", | |
| "I get along well with others.", "I enjoy being the center of attention." | |
| ] | |
| # Create sliders with questions | |
| inputs = [gr.Slider(1.0, 5.0, value=3.0, label=q) for q in questions] | |
| output = gr.Textbox(label="Prediction") | |
| # Inference function | |
| def predict_personality(*inputs): | |
| input_array = np.array(inputs).reshape(1, -1) | |
| scaled_input = scaler.transform(input_array) | |
| pred = model.predict(scaled_input) | |
| personality = encoder.inverse_transform(pred)[0] | |
| return f"Predicted Personality: **{personality}**" | |
| # Gradio UI with 50 sliders and full question labels | |
| demo = gr.Interface( | |
| fn=predict_personality, | |
| inputs=inputs, | |
| outputs=output, | |
| title="Personality Type Classifier (Introvert vs. Extrovert)", | |
| 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." | |
| ) | |
| demo.launch() | |