ositamiles's picture
Update app.py
02bc549 verified
import streamlit as st
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')
# 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]
# Load pre-trained OneHotEncoder and StandardScaler (assuming you have these saved)
ohe = joblib.load('ohe.pkl') # Load the OneHotEncoder
scaler = joblib.load('scaler.pkl') # Load the StandardScaler
# Streamlit application
st.title("Game Price Prediction App")
st.write("""
### Enter the game details below to predict its price.
""")
# Game details form
with st.form("game_details_form"):
genre = st.selectbox('Genre', ['Action', 'RPG', 'Puzzle', 'Adventure', 'Simulation', 'Strategy', 'Horror', 'Fighting', 'Sports', 'Racing', 'Casual', 'MOBA', 'Sandbox'])
target_platform = st.selectbox('Platform', ['PC', 'PlayStation', 'Xbox', 'Mobile', 'Switch', 'Nintendo 3DS', 'VR', 'Web'])
total_sales = st.number_input('Total Sales (units)', min_value=0, value=50000)
initial_price = st.number_input('Initial Price Offering ($)', min_value=0.0, value=29.99, format="%.2f")
revenue = total_sales * initial_price
competitor_pricing = st.number_input('Average Market Price for Similar Games ($)', min_value=0.0, value=30.0, format="%.2f")
currency_fluctuations = st.number_input('Currency Fluctuations', min_value=0.5, max_value=1.5, value=1.0, format="%.2f")
# Submit button
submitted = st.form_submit_button("Predict Price")
# Prediction logic
if submitted:
# Prepare input data
input_data = {
'genre': genre,
'targetPlatform': target_platform,
'gamePlays': revenue,
'competitorPricing': competitor_pricing,
'currencyFluctuations': currency_fluctuations
}
# Make prediction
predicted_price = make_prediction(input_data)
# Display results
st.write(f"### Predicted Game Price: ${predicted_price:.2f}")
# Show the input details for reference
st.write("#### Input Details:")
st.write(f"- **Genre**: {genre}")
st.write(f"- **Platform**: {target_platform}")
st.write(f"- **Total Sales**: {total_sales}")
st.write(f"- **Initial Price**: ${initial_price:.2f}")
st.write(f"- **Current Revenue**: ${revenue:.2f}")
st.write(f"- **Competitor Pricing**: ${competitor_pricing:.2f}")
st.write(f"- **Currency Fluctuations**: {currency_fluctuations}")