File size: 2,660 Bytes
a244ac5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5d7c5a
a244ac5
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
from flask import Flask, request, jsonify, render_template
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
from src.pipelines.predict_pipeline import PredictPipeline, CustomData

application = Flask(__name__, template_folder='templates')
app = application

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/predict', methods=['POST', 'GET'])
def predict():
    if request.method == 'GET':
        return render_template('home.html')
    
    try:
        input_data = request.get_json()
        
        if not input_data:
            return jsonify({'error': 'No JSON data provided'}), 400
        
        # Extract features
        gender = input_data.get('gender')
        race = input_data.get('race')
        parental_education = input_data.get('parental_education')
        lunch = input_data.get('lunch')
        test_preparation_course = input_data.get('test_preparation_course')
        reading_score = input_data.get('reading_score')
        writing_score = input_data.get('writing_score')
        
        # Validate all required fields
        if not all([gender, race, parental_education, lunch, test_preparation_course, 
                   reading_score is not None, writing_score is not None]):
            return jsonify({'error': 'Missing required fields'}), 400
        
        # Convert scores to float
        try:
            reading_score = float(reading_score)
            writing_score = float(writing_score)
        except ValueError:
            return jsonify({'error': 'Scores must be numbers'}), 400
        
        # Create CustomData instance
        data = CustomData(
            gender=gender,
            race=race,
            parental_level_of_education=parental_education,
            lunch=lunch,
            test_preparation_course=test_preparation_course,
            reading_score=reading_score,
            writing_score=writing_score
        )
        
        pred_df = data.get_data_as_data_frame()
        predictor = PredictPipeline()
        results = predictor.predict(pred_df)
        
        return jsonify({
            'predicted_math_score': float(results[0]),
            'reading_score': reading_score,
            'writing_score': writing_score
        }), 200
        
    except Exception as e:
        print(f"Error: {str(e)}")
        return jsonify({'error': f'Prediction failed: {str(e)}'}), 500

if __name__ == '__main__':
    # This won't be used in production (Gunicorn runs it), but good for local testing
    app.run(host='0.0.0.0', port=7860)
# https://huggingface.co/spaces/Aryanjaiswal78231/math-score-predictor