Spaces:
Sleeping
Sleeping
Download src/predictor.py from InduwaraR/Interest_Predictions: direct link, hf CLI and curl.
- Browser
- Download file 3.19 kB
-
https://huggingface.co/spaces/InduwaraR/Interest_Predictions/resolve/main/src/predictor.py
- Command line
-
hf download hf://spaces/InduwaraR/Interest_Predictions/src/predictor.py
-
curl -L -o predictor.py https://huggingface.co/spaces/InduwaraR/Interest_Predictions/resolve/main/src/predictor.py
3.19 kB
| 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 |