File size: 2,212 Bytes
d035171
 
 
 
 
 
 
 
 
 
cfccc3d
 
d035171
 
 
 
 
 
 
864731c
d035171
 
 
 
 
 
 
 
 
6f3fde2
 
d035171
 
 
 
 
923da65
d035171
cfccc3d
 
 
 
d035171
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93206c9
d035171
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
@app.get("/")
def home():
    return "Welcome to the Introvert-Extrovert Prediction API!"

@app.post("/v1/personality/predict")
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})

@app.post("/v1/personality/predictbatch")
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)