Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,7 +17,7 @@ st.set_page_config(page_title="Dynamic Game Pricing App", layout="wide")
|
|
| 17 |
@st.cache_data
|
| 18 |
def load_data():
|
| 19 |
if os.path.exists('game_data.csv'):
|
| 20 |
-
|
| 21 |
else:
|
| 22 |
# Sample dataset
|
| 23 |
data = {
|
|
@@ -32,7 +32,11 @@ def load_data():
|
|
| 32 |
}
|
| 33 |
df = pd.DataFrame(data)
|
| 34 |
df.to_csv('game_data.csv', index=False)
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# Load data
|
| 38 |
df = load_data()
|
|
@@ -49,15 +53,22 @@ if page == "Data Explorer":
|
|
| 49 |
|
| 50 |
st.subheader("Data Visualization")
|
| 51 |
fig, ax = plt.subplots(1, 2, figsize=(15, 5))
|
| 52 |
-
ax[0].scatter(df['competitor_price'], df['suggested_price'])
|
| 53 |
-
ax[0].set_xlabel('Competitor Price')
|
| 54 |
-
ax[0].set_ylabel('Suggested Price')
|
| 55 |
-
ax[0].set_title('Competitor Price vs Suggested Price')
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
st.pyplot(fig)
|
| 63 |
|
|
@@ -65,15 +76,20 @@ elif page == "Model Training":
|
|
| 65 |
st.title("Model Training")
|
| 66 |
|
| 67 |
# Data preprocessing
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
y = df['suggested_price']
|
| 77 |
|
| 78 |
scaler = StandardScaler()
|
| 79 |
X_scaled = scaler.fit_transform(X)
|
|
@@ -107,11 +123,11 @@ elif page == "Model Training":
|
|
| 107 |
ax.legend()
|
| 108 |
st.pyplot(fig)
|
| 109 |
|
| 110 |
-
# Save model and
|
| 111 |
model.save('dynamic_pricing_model.h5')
|
| 112 |
joblib.dump(scaler, 'scaler.pkl')
|
| 113 |
-
joblib.dump(
|
| 114 |
-
joblib.dump(
|
| 115 |
|
| 116 |
st.info("Model and preprocessing objects saved.")
|
| 117 |
|
|
@@ -122,28 +138,30 @@ elif page == "Price Prediction":
|
|
| 122 |
if os.path.exists('dynamic_pricing_model.h5'):
|
| 123 |
model = load_model('dynamic_pricing_model.h5')
|
| 124 |
scaler = joblib.load('scaler.pkl')
|
| 125 |
-
|
| 126 |
-
|
| 127 |
|
| 128 |
# User input
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
# Prepare input for prediction
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
le_region.transform([region])[0],
|
| 140 |
-
release_year,
|
| 141 |
-
demand_index,
|
| 142 |
-
competitor_price,
|
| 143 |
-
past_sales
|
| 144 |
-
]])
|
| 145 |
-
|
| 146 |
-
input_scaled = scaler.transform(input_data)
|
| 147 |
|
| 148 |
# Make prediction
|
| 149 |
if st.button("Predict Price"):
|
|
|
|
| 17 |
@st.cache_data
|
| 18 |
def load_data():
|
| 19 |
if os.path.exists('game_data.csv'):
|
| 20 |
+
df = pd.read_csv('game_data.csv')
|
| 21 |
else:
|
| 22 |
# Sample dataset
|
| 23 |
data = {
|
|
|
|
| 32 |
}
|
| 33 |
df = pd.DataFrame(data)
|
| 34 |
df.to_csv('game_data.csv', index=False)
|
| 35 |
+
|
| 36 |
+
# Print column names for debugging
|
| 37 |
+
st.sidebar.write("Available columns:", df.columns.tolist())
|
| 38 |
+
|
| 39 |
+
return df
|
| 40 |
|
| 41 |
# Load data
|
| 42 |
df = load_data()
|
|
|
|
| 53 |
|
| 54 |
st.subheader("Data Visualization")
|
| 55 |
fig, ax = plt.subplots(1, 2, figsize=(15, 5))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
# Use column names dynamically
|
| 58 |
+
price_col = [col for col in df.columns if 'price' in col.lower()]
|
| 59 |
+
demand_col = [col for col in df.columns if 'demand' in col.lower()]
|
| 60 |
+
|
| 61 |
+
if price_col and 'suggested_price' in df.columns:
|
| 62 |
+
ax[0].scatter(df[price_col[0]], df['suggested_price'])
|
| 63 |
+
ax[0].set_xlabel(price_col[0])
|
| 64 |
+
ax[0].set_ylabel('Suggested Price')
|
| 65 |
+
ax[0].set_title(f'{price_col[0]} vs Suggested Price')
|
| 66 |
+
|
| 67 |
+
if demand_col and 'suggested_price' in df.columns:
|
| 68 |
+
ax[1].scatter(df[demand_col[0]], df['suggested_price'])
|
| 69 |
+
ax[1].set_xlabel(demand_col[0])
|
| 70 |
+
ax[1].set_ylabel('Suggested Price')
|
| 71 |
+
ax[1].set_title(f'{demand_col[0]} vs Suggested Price')
|
| 72 |
|
| 73 |
st.pyplot(fig)
|
| 74 |
|
|
|
|
| 76 |
st.title("Model Training")
|
| 77 |
|
| 78 |
# Data preprocessing
|
| 79 |
+
categorical_cols = df.select_dtypes(include=['object']).columns
|
| 80 |
+
numeric_cols = df.select_dtypes(include=['int64', 'float64']).columns
|
| 81 |
+
|
| 82 |
+
# Remove 'suggested_price' from features if it exists
|
| 83 |
+
feature_cols = [col for col in numeric_cols if col != 'suggested_price']
|
| 84 |
|
| 85 |
+
encoders = {}
|
| 86 |
+
for col in categorical_cols:
|
| 87 |
+
encoders[col] = LabelEncoder()
|
| 88 |
+
df[f'{col}_encoded'] = encoders[col].fit_transform(df[col])
|
| 89 |
+
feature_cols.append(f'{col}_encoded')
|
| 90 |
|
| 91 |
+
X = df[feature_cols]
|
| 92 |
+
y = df['suggested_price'] if 'suggested_price' in df.columns else df[price_col[0]]
|
|
|
|
| 93 |
|
| 94 |
scaler = StandardScaler()
|
| 95 |
X_scaled = scaler.fit_transform(X)
|
|
|
|
| 123 |
ax.legend()
|
| 124 |
st.pyplot(fig)
|
| 125 |
|
| 126 |
+
# Save model and preprocessing objects
|
| 127 |
model.save('dynamic_pricing_model.h5')
|
| 128 |
joblib.dump(scaler, 'scaler.pkl')
|
| 129 |
+
joblib.dump(encoders, 'encoders.pkl')
|
| 130 |
+
joblib.dump(feature_cols, 'feature_cols.pkl')
|
| 131 |
|
| 132 |
st.info("Model and preprocessing objects saved.")
|
| 133 |
|
|
|
|
| 138 |
if os.path.exists('dynamic_pricing_model.h5'):
|
| 139 |
model = load_model('dynamic_pricing_model.h5')
|
| 140 |
scaler = joblib.load('scaler.pkl')
|
| 141 |
+
encoders = joblib.load('encoders.pkl')
|
| 142 |
+
feature_cols = joblib.load('feature_cols.pkl')
|
| 143 |
|
| 144 |
# User input
|
| 145 |
+
input_data = {}
|
| 146 |
+
for col in feature_cols:
|
| 147 |
+
if col.endswith('_encoded'):
|
| 148 |
+
original_col = col[:-8] # Remove '_encoded' suffix
|
| 149 |
+
if original_col in encoders:
|
| 150 |
+
value = st.selectbox(f"Select {original_col}", encoders[original_col].classes_)
|
| 151 |
+
input_data[col] = encoders[original_col].transform([value])[0]
|
| 152 |
+
else:
|
| 153 |
+
if 'year' in col.lower():
|
| 154 |
+
input_data[col] = st.slider(f"{col}", 2018, 2024, 2022)
|
| 155 |
+
elif 'price' in col.lower():
|
| 156 |
+
input_data[col] = st.slider(f"{col}", 20.0, 60.0, 40.0)
|
| 157 |
+
elif 'sales' in col.lower():
|
| 158 |
+
input_data[col] = st.slider(f"{col}", 100, 1000, 500)
|
| 159 |
+
else:
|
| 160 |
+
input_data[col] = st.slider(f"{col}", 0.1, 1.0, 0.5)
|
| 161 |
|
| 162 |
# Prepare input for prediction
|
| 163 |
+
input_df = pd.DataFrame([input_data])
|
| 164 |
+
input_scaled = scaler.transform(input_df)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
# Make prediction
|
| 167 |
if st.button("Predict Price"):
|