File size: 3,189 Bytes
523415f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import joblib
import pandas as pd
from typing import List, Optional
import os

# Load the trained model and define the categories and questions
MODEL: Optional[object] = None

# Try to load the model at module initialization
try:
    model_path = os.path.join(os.path.dirname(__file__), '..', 'random_forest_model.joblib')
    MODEL = joblib.load(model_path)
    print("Model loaded successfully!")
except FileNotFoundError:
    print("Warning: Model file 'random_forest_model.joblib' not found.")
    print("Please place the model file in the project root directory.")
    MODEL = None
except Exception as e:
    print(f"Error loading model: {e}")
    MODEL = None

QUESTIONS = [
    "1. How enthusiastic are you about attending live music festivals or concerts?",
    "2. To what extent do you enjoy watching or participating in sporting events?",
    "3. How much do you enjoy exploring new restaurants and culinary experiences?",
    "4. How interested are you in visiting museums, art galleries, or historical sites?",
    "5. How much do you enjoy outdoor activities like hiking, camping, or visiting parks?",
    "6. How enthusiastic are you about experiencing a city's nightlife and social scene?",
    "7. To what extent are you interested in the latest technological innovations and gadgets?",
    "8. How important is it for your travel to include opportunities for professional networking?",
    "9. How much do you prioritize activities that promote wellness and relaxation (e.g., yoga)?",
    "10. How interested are you in attending workshops, lectures, or educational tours?"
]


def predict_interest(answers: List[int]) -> str:
    """
    Predicts a user's interest category based on their answers.

    Args:
        answers (List[int]): A list of 10 integers (from 1 to 5)
                           corresponding to the user's answers.

    Returns:
        str: The predicted interest category or error message.
    """
    if MODEL is None:
        return "Model is not loaded. Cannot make a prediction."

    if len(answers) != 10:
        return f"Error: Please provide exactly 10 answers. You provided {len(answers)}."
    
    # Validate that all answers are in the correct range
    for i, answer in enumerate(answers, 1):
        if not isinstance(answer, int) or not 1 <= answer <= 5:
            return f"Error: Answer {i} must be an integer between 1 and 5. Got: {answer}"
    
    try:
        # Create a DataFrame from the answers with the correct column names
        columns = [f"Q{i+1}" for i in range(10)]
        input_df = pd.DataFrame([answers], columns=columns)
        
        # Make the prediction
        prediction = MODEL.predict(input_df)
        
        return prediction[0]
    except Exception as e:
        return f"Error making prediction: {str(e)}"


def get_questions() -> List[str]:
    """
    Returns the list of questions for the survey.
    
    Returns:
        List[str]: List of survey questions.
    """
    return QUESTIONS


def is_model_loaded() -> bool:
    """
    Check if the model is successfully loaded.
    
    Returns:
        bool: True if model is loaded, False otherwise.
    """
    return MODEL is not None