Download introvert_extrovert_predictor.py from udbhav90/Introvert-Extrovert-Predictor: direct link, hf CLI and curl.
- Browser
- Download file 2.21 kB
-
https://huggingface.co/spaces/udbhav90/Introvert-Extrovert-Predictor/resolve/main/introvert_extrovert_predictor.py
- Command line
-
hf download hf://spaces/udbhav90/Introvert-Extrovert-Predictor/introvert_extrovert_predictor.py
-
curl -L -o introvert_extrovert_predictor.py https://huggingface.co/spaces/udbhav90/Introvert-Extrovert-Predictor/resolve/main/introvert_extrovert_predictor.py
2.21 kB
| import joblib | |
| import pandas as pd | |
| from flask import Flask, request, jsonify | |
| # Initialize Flask app | |
| app = Flask("Introvert Extrovert Predictor") | |
| # Load the trained classification model | |
| model = joblib.load("introvert_extrovert_predictor_v1_0.joblib") | |
| api_hit_counter = 0 | |
| # Home route | |
| def home(): | |
| return "Welcome to the Introvert-Extrovert Prediction API!" | |
| def predict_personality_single(): | |
| global api_hit_counter | |
| input_data = request.get_json() | |
| # Prepare input sample | |
| sample = { | |
| "Time_spent_Alone": input_data["Time_spent_Alone"], | |
| "Social_event_attendance": input_data["Social_event_attendance"], | |
| "Going_outside": input_data["Going_outside"], | |
| "Friends_circle_size": input_data["Friends_circle_size"], | |
| "Post_frequency": input_data["Post_frequency"], | |
| "Stage_fear": input_data["Stage_fear"], | |
| "Drained_after_socializing": input_data["Drained_after_socializing"] | |
| } | |
| # Convert to DataFrame and predict | |
| input_df = pd.DataFrame([sample]) | |
| prediction = model.predict(input_df).tolist()[0] | |
| personality = "Extrovert" if prediction == 0 else "Introvert" | |
| # Increment and print the counter | |
| api_hit_counter += 1 | |
| print(f"API hit count: {api_hit_counter}") | |
| return jsonify({'Predicted_Personality': personality}) | |
| def predict_personality_batch(): | |
| # Get uploaded file | |
| file = request.files['file'] | |
| # Read CSV file | |
| input_df = pd.read_csv(file) | |
| # Convert binary features to numerical (Yes → 1, No → 0) | |
| input_df["Stage_fear"] = input_df["Stage_fear"].apply(lambda x: 1 if str(x).lower() == "yes" else 0) | |
| input_df["Drained_after_socializing"] = input_df["Drained_after_socializing"].apply(lambda x: 1 if str(x).lower() == "yes" else 0) | |
| # Predict personality | |
| predictions = model.predict(input_df).tolist() | |
| input_df["Predicted_Personality"] = ["Extrovert" if p == 0 else "Introvert" for p in predictions] | |
| # Convert to dict for JSON output | |
| result = input_df.to_dict(orient="records") | |
| return jsonify(result) | |
| # Run app | |
| if __name__ == '__main__': | |
| app.run(debug=True) | |