File size: 2,746 Bytes
dd1b94d
e13ae45
 
 
 
 
 
dd1b94d
e13ae45
 
 
 
 
 
 
 
dd1b94d
 
e13ae45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd1b94d
 
2ed0106
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
from fastapi import FastAPI
from pydantic import BaseModel
import numpy as np
import pandas as pd
import tensorflow as tf
from sklearn.preprocessing import OneHotEncoder, StandardScaler
import joblib

# Load the trained model
model = tf.keras.models.load_model('trained_game_price_model.h5')

# Load pre-trained OneHotEncoder and StandardScaler
ohe = joblib.load('ohe.pkl')
scaler = joblib.load('scaler.pkl')

# FastAPI app
app = FastAPI()

# Pydantic model for input validation
class GameDetails(BaseModel):
    genre: str
    targetPlatform: str
    gamePlays: int
    competitorPricing: float
    currencyFluctuations: float

# Function to preprocess the input data
def preprocess_input(data, ohe, scaler):
    # Convert input into DataFrame for processing
    input_data = pd.DataFrame([data], columns=['genre', 'targetPlatform', 'gamePlays', 'competitorPricing', 'currencyFluctuations'])
    
    # Apply OneHotEncoder for categorical features
    input_data_transformed = ohe.transform(input_data[['genre', 'targetPlatform']])
    
    # Ensure numerical features are 2D
    numerical_features = input_data[['gamePlays', 'competitorPricing', 'currencyFluctuations']].values.reshape(1, -1)
    
    # Merge with numerical features
    input_data = np.hstack((input_data_transformed.toarray(), numerical_features))
    
    # Scale the features
    input_data_scaled = scaler.transform(input_data)
    
    return input_data_scaled

# Function to make a prediction
def make_prediction(input_data):
    # Preprocess the data for the model
    input_data_scaled = preprocess_input(input_data, ohe, scaler)
    
    # Make prediction
    prediction = model.predict(input_data_scaled)
    
    return prediction[0][0]

# API endpoint for price prediction
@app.post("/predict_price/")
def predict_price(game_details: GameDetails):
    # Prepare input data for prediction
    input_data = {
        'genre': game_details.genre,
        'targetPlatform': game_details.targetPlatform,
        'gamePlays': game_details.gamePlays,
        'competitorPricing': game_details.competitorPricing,
        'currencyFluctuations': game_details.currencyFluctuations
    }
    
    # Make prediction
    predicted_price = make_prediction(input_data)
    
    # Return the predicted price
    return {
        "predicted_price": f"${predicted_price:.2f}",
        "input_details": {
            "genre": game_details.genre,
            "platform": game_details.targetPlatform,
            "game_plays": game_details.gamePlays,
            "competitor_pricing": game_details.competitorPricing,
            "currency_fluctuations": game_details.currencyFluctuations
        }
    }

@app.get("/")
def greet_json():
    return {"Hello": "Blackhards♠️♣️!"}