Download app.py from HSSHETTY01/Extraa-Learn-backend2: direct link, hf CLI and curl.
- Browser
- Download file 2.64 kB
-
https://huggingface.co/spaces/HSSHETTY01/Extraa-Learn-backend2/resolve/main/app.py
- Command line
-
hf download hf://spaces/HSSHETTY01/Extraa-Learn-backend2/app.py
-
curl -L -o app.py https://huggingface.co/spaces/HSSHETTY01/Extraa-Learn-backend2/resolve/main/app.py
2.64 kB
| import numpy as np | |
| import pandas as pd | |
| import joblib | |
| from flask import Flask, request, jsonify | |
| from flask_cors import CORS | |
| # Initialize Flask app | |
| Extraa_Learn_api = Flask("Extraa_Learn_api") | |
| CORS(Extraa_Learn_api) | |
| # Load the trained model pipeline (preprocessing + model) | |
| model = joblib.load("/content/drive/My Drive/Colab Notebooks/deployment_files/Extraa_Learn_model_v1_0.joblib") | |
| # Health check route | |
| def home(): | |
| return "✅ Welcome to the Extraa-Learn_API" | |
| # Prediction route | |
| def predict_status(): | |
| try: | |
| # Parse JSON payload | |
| data = request.get_json() | |
| print("Raw incoming data:", data) | |
| # Validate expected fields | |
| required_fields = [ | |
| 'age', | |
| 'current_occupation', | |
| 'first_interaction', | |
| 'profile_completed', | |
| 'website_visits', | |
| 'time_spent_on_website', | |
| 'page_views_per_visit', | |
| 'last_activity', | |
| 'print_media_type1', | |
| 'print_media_type2', | |
| 'digital_media', | |
| 'educational_channels', | |
| 'referral' | |
| ] | |
| missing_fields = [f for f in required_fields if f not in data] | |
| if missing_fields: | |
| return jsonify({'error': f"Missing fields: {missing_fields}"}), 400 | |
| # Convert and transform input | |
| sample = { | |
| 'age': float(data['age']), | |
| 'current_occupation': data['current_occupation'], | |
| 'first_interaction': data['first_interaction'], | |
| 'profile_completed': data['profile_completed'], | |
| 'website_visits': data['website_visits'], | |
| 'time_spent_on_website': ['time_spent_on_website']), | |
| 'page_views_per_visit': data['page_views_per_visit'], | |
| 'last_activity': data['last_activity'], | |
| 'print_media_type1': data['print_media_type1'], | |
| 'print_media_type2': data['print_media_type2']), | |
| 'digital_media': data['digital_media'], | |
| 'educational_channels': data['educational_channels'], | |
| 'referral': data['referral'] | |
| } | |
| input_df = pd.DataFrame([sample]) | |
| print("Transformed input for model: | |
| ", input_df) | |
| # Make prediction | |
| prediction = model.predict(input_df).tolist()[0] | |
| return jsonify({'Status': prediction}) | |
| except Exception as e: | |
| print("❌ Error during prediction:", str(e)) | |
| return jsonify({'error': f"Prediction failed: {str(e)}"}), 500 | |
| # Run the app (for local testing only) | |
| if __name__ == '__main__': | |
| Extraa_Learn_api.run(debug=True) | |