Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,15 +3,13 @@ import pandas as pd
|
|
3 |
import numpy as np
|
4 |
from sklearn.ensemble import RandomForestRegressor
|
5 |
import joblib
|
6 |
-
import requests
|
7 |
-
from io import BytesIO
|
8 |
|
9 |
st.title('Restaurant Revenue Predictor')
|
10 |
-
|
11 |
# Create input form
|
12 |
st.write('Enter restaurant details:')
|
13 |
|
14 |
-
# City selection
|
15 |
city = st.selectbox('City', ['Istanbul', 'Ankara', 'Izmir', 'Other Cities'])
|
16 |
|
17 |
# Adding description for Type
|
@@ -24,72 +22,61 @@ st.write("""
|
|
24 |
""")
|
25 |
type = st.selectbox('Type', ['FC', 'IL', 'DT', 'MB'])
|
26 |
|
|
|
27 |
@st.cache_resource
|
28 |
-
def
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
43 |
|
44 |
if st.button('Predict Revenue'):
|
45 |
# Map city to City Group
|
46 |
city_group = 'Big Cities' if city in ['Istanbul', 'Ankara', 'Izmir'] else 'Other'
|
47 |
|
48 |
-
#
|
49 |
-
|
50 |
-
'
|
51 |
-
'
|
52 |
-
'
|
53 |
-
'
|
54 |
-
'
|
55 |
-
'
|
56 |
-
'
|
57 |
-
|
58 |
-
}
|
59 |
-
|
60 |
-
# Create input dataframe with average values for other features
|
61 |
-
input_data = {
|
62 |
-
'City Group': city_group,
|
63 |
-
'Type': type,
|
64 |
-
'Open.Date': pd.Timestamp('2015-01-01'), # Default date
|
65 |
-
**avg_values # Unpack the average values
|
66 |
-
}
|
67 |
-
|
68 |
-
df = pd.DataFrame([input_data])
|
69 |
-
|
70 |
-
# Process dates
|
71 |
-
df['date'] = pd.to_datetime(df['Open.Date'])
|
72 |
-
df['day'] = df['date'].dt.day
|
73 |
-
df['month'] = df['date'].dt.month.astype('category')
|
74 |
-
df['year'] = df['date'].dt.year.astype('category')
|
75 |
-
|
76 |
-
# Calculate days open
|
77 |
-
end_date = pd.to_datetime('2020-08-13')
|
78 |
-
df['days'] = (end_date - df['date']).dt.days
|
79 |
-
|
80 |
-
# Drop unnecessary columns
|
81 |
-
df = df.drop(['Open.Date', 'date'], axis=1)
|
82 |
-
|
83 |
-
# Convert categorical variables
|
84 |
-
df = pd.get_dummies(df)
|
85 |
|
86 |
try:
|
87 |
-
#
|
88 |
-
model =
|
89 |
|
90 |
# Make prediction
|
91 |
-
prediction = model.predict(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
-
st.success(f'Predicted Revenue: ${prediction:,.2f}')
|
94 |
except Exception as e:
|
95 |
-
st.error(f"Error
|
|
|
3 |
import numpy as np
|
4 |
from sklearn.ensemble import RandomForestRegressor
|
5 |
import joblib
|
|
|
|
|
6 |
|
7 |
st.title('Restaurant Revenue Predictor')
|
8 |
+
|
9 |
# Create input form
|
10 |
st.write('Enter restaurant details:')
|
11 |
|
12 |
+
# City selection
|
13 |
city = st.selectbox('City', ['Istanbul', 'Ankara', 'Izmir', 'Other Cities'])
|
14 |
|
15 |
# Adding description for Type
|
|
|
22 |
""")
|
23 |
type = st.selectbox('Type', ['FC', 'IL', 'DT', 'MB'])
|
24 |
|
25 |
+
# Create a simple model for demonstration
|
26 |
@st.cache_resource
|
27 |
+
def create_model():
|
28 |
+
model = RandomForestRegressor(
|
29 |
+
n_estimators=100,
|
30 |
+
max_depth=10,
|
31 |
+
random_state=42
|
32 |
+
)
|
33 |
+
# Create some sample training data
|
34 |
+
X_train = pd.DataFrame({
|
35 |
+
'City Group_Big Cities': [1, 0, 1, 0],
|
36 |
+
'City Group_Other': [0, 1, 0, 1],
|
37 |
+
'Type_DT': [1, 0, 0, 0],
|
38 |
+
'Type_FC': [0, 1, 0, 0],
|
39 |
+
'Type_IL': [0, 0, 1, 0],
|
40 |
+
'Type_MB': [0, 0, 0, 1],
|
41 |
+
'days': [1000, 800, 600, 400]
|
42 |
+
})
|
43 |
+
y_train = np.array([1500000, 1000000, 800000, 500000])
|
44 |
+
model.fit(X_train, y_train)
|
45 |
+
return model
|
46 |
|
47 |
if st.button('Predict Revenue'):
|
48 |
# Map city to City Group
|
49 |
city_group = 'Big Cities' if city in ['Istanbul', 'Ankara', 'Izmir'] else 'Other'
|
50 |
|
51 |
+
# Create input dataframe
|
52 |
+
input_data = pd.DataFrame({
|
53 |
+
'City Group_Big Cities': [1 if city_group == 'Big Cities' else 0],
|
54 |
+
'City Group_Other': [1 if city_group == 'Other' else 0],
|
55 |
+
'Type_DT': [1 if type == 'DT' else 0],
|
56 |
+
'Type_FC': [1 if type == 'FC' else 0],
|
57 |
+
'Type_IL': [1 if type == 'IL' else 0],
|
58 |
+
'Type_MB': [1 if type == 'MB' else 0],
|
59 |
+
'days': [500] # default value
|
60 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
try:
|
63 |
+
# Get model
|
64 |
+
model = create_model()
|
65 |
|
66 |
# Make prediction
|
67 |
+
prediction = model.predict(input_data)[0]
|
68 |
+
|
69 |
+
# Format prediction
|
70 |
+
formatted_prediction = f"${prediction:,.2f}"
|
71 |
+
|
72 |
+
# Display prediction with additional context
|
73 |
+
st.success(f'Predicted Revenue: {formatted_prediction}')
|
74 |
+
|
75 |
+
# Add some context about the prediction
|
76 |
+
st.info("""
|
77 |
+
Note: This is a simplified model for demonstration purposes.
|
78 |
+
The prediction is based on limited training data and should be used as a rough estimate only.
|
79 |
+
""")
|
80 |
|
|
|
81 |
except Exception as e:
|
82 |
+
st.error(f"Error making prediction: {str(e)}")
|