alperugurcan commited on
Commit
4b9787b
·
verified ·
1 Parent(s): aa8d328

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -61
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 (changed from City Group)
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 load_model():
29
- try:
30
- # Download the model file from Hugging Face
31
- url = "https://huggingface.co/alperugurcan/restaurant_revenue_model/resolve/main/restaurant_revenue_model.joblib"
32
- response = requests.get(url)
33
- if response.status_code == 200:
34
- model_bytes = BytesIO(response.content)
35
- model = joblib.load(model_bytes)
36
- return model
37
- else:
38
- st.error(f"Failed to download model: HTTP {response.status_code}")
39
- return None
40
- except Exception as e:
41
- st.error(f"Error loading model: {str(e)}")
42
- return None
 
 
 
 
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
- # Average values for P1-P37
49
- avg_values = {
50
- 'P1': 5.380, 'P2': 3.961, 'P3': 4.372, 'P4': 3.219, 'P5': 2.743,
51
- 'P6': 4.508, 'P7': 4.064, 'P8': 3.801, 'P9': 3.915, 'P10': 4.116,
52
- 'P11': 3.049, 'P12': 2.995, 'P13': 4.606, 'P14': 4.275, 'P15': 3.901,
53
- 'P16': 3.925, 'P17': 4.142, 'P18': 3.664, 'P19': 3.321, 'P20': 4.093,
54
- 'P21': 3.833, 'P22': 3.667, 'P23': 3.916, 'P24': 4.089, 'P25': 3.743,
55
- 'P26': 3.464, 'P27': 4.233, 'P28': 3.967, 'P29': 3.813, 'P30': 3.901,
56
- 'P31': 4.108, 'P32': 3.739, 'P33': 3.438, 'P34': 4.117, 'P35': 3.865,
57
- 'P36': 3.728, 'P37': 3.896
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
- # Load model
88
- model = load_model()
89
 
90
  # Make prediction
91
- prediction = model.predict(df)[0]
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
- st.success(f'Predicted Revenue: ${prediction:,.2f}')
94
  except Exception as e:
95
- st.error(f"Error loading model or making prediction: {str(e)}")
 
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)}")