Download app.py from amitcoolll/ExtraLearnConversionPredictionBackendAPP: direct link, hf CLI and curl.
- Browser
- Download file 3.41 kB
-
https://huggingface.co/spaces/amitcoolll/ExtraLearnConversionPredictionBackendAPP/resolve/main/app.py
- Command line
-
hf download hf://spaces/amitcoolll/ExtraLearnConversionPredictionBackendAPP/app.py
-
curl -L -o app.py https://huggingface.co/spaces/amitcoolll/ExtraLearnConversionPredictionBackendAPP/resolve/main/app.py
3.41 kB
| # Import necessary libraries | |
| import os | |
| import numpy as np | |
| import joblib # For loading the serialized model | |
| import pandas as pd # For data manipulation | |
| from flask import Flask, request, jsonify # For creating the Flask API | |
| # Initialize the Flask application | |
| rental_price_predictor_api = Flask("Extraa Learn conversion Predictor") | |
| # Load the trained machine learning model | |
| # Use relative path to load the model inside backend_files | |
| model_path = os.path.join(os.path.dirname(__file__), "conversion_prediction_model_v1_0.joblib") | |
| model = joblib.load(model_path) | |
| print("Model loaded successfully.") | |
| # model = joblib.load(model_path) | |
| # Define a route for the home page (GET request) | |
| def home(): | |
| """ | |
| This function handles GET requests to the root URL ('/') of the API. | |
| It returns a simple welcome message. | |
| """ | |
| return "HI, Welcome to the Extraa Learn conversion Predictor API!" | |
| # Define an endpoint for single property prediction (POST request) | |
| def predict_rental_price(): | |
| property_data = request.get_json() | |
| sample = { | |
| 'age': property_data['age'], | |
| 'website_visits': property_data['website_visits'], | |
| 'time_spent_on_website': property_data['time_spent_on_website'], | |
| 'page_views_per_visit': property_data['page_views_per_visit'], | |
| 'current_occupation': property_data['current_occupation'], | |
| 'first_interaction': property_data['first_interaction'], | |
| 'profile_completed': property_data['profile_completed'], | |
| 'last_activity': property_data['last_activity'], | |
| 'print_media_type1': property_data['print_media_type1'], | |
| 'print_media_type2': property_data['print_media_type2'], | |
| 'digital_media': property_data['digital_media'], | |
| 'educational_channels': property_data['educational_channels'], | |
| 'referral': property_data['referral'] | |
| } | |
| input_data = pd.DataFrame([sample]) | |
| # Directly predict class (0 or 1) | |
| predicted_status = int(model.predict(input_data)[0]) | |
| return jsonify({'Predicted Status': predicted_status}) | |
| # Define an endpoint for batch prediction (POST request) | |
| def predict_rental_price_batch(): | |
| """ | |
| This function handles POST requests to the '/v1/conversionbatch' endpoint. | |
| It expects a CSV file containing property details for multiple properties | |
| and returns the predicted rental prices as a dictionary in the JSON response. | |
| """ | |
| # Get the uploaded CSV file from the request | |
| file = request.files['file'] | |
| # Read the CSV file into a Pandas DataFrame | |
| input_data = pd.read_csv(file) | |
| # Make predictions for all properties in the DataFrame (get log_prices) | |
| status_log = model.predict(input_data).tolist() | |
| # Calculate actual prices | |
| status = [round(float(np.exp(log_price)), 2) for log_price in status_log] | |
| # Create a dictionary of predictions with property IDs as keys | |
| property_ids = input_data['ID'].tolist() # Assuming 'id' is the property ID column | |
| output_dict = dict(zip(property_ids, status)) # Use actual prices | |
| # Return the predictions dictionary as a JSON response | |
| return output_dict | |
| # Run the Flask application in debug mode if this script is executed directly | |
| if __name__ == '__main__': | |
| rental_price_predictor_api.run(debug=True) | |