Pushpak21 commited on
Commit
be4467e
Β·
verified Β·
1 Parent(s): c4fa469

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +26 -85
app.py CHANGED
@@ -54,105 +54,46 @@ with tab1:
54
  "Store_Establishment_Year": est_year
55
  }
56
 
57
- url = "https://Pushpak21-SuperKart-Sales-Forecast-Backend.hf.space/predict"
58
- response = requests.post(url, json=payload)
59
- result = response.json()
60
-
61
- if response.status_code == 200:
62
- st.success(f"πŸ“ˆ Predicted Sales: β‚Ή{result['prediction'][0]:,.2f}")
63
- else:
64
- st.error(f"❌ Error: {result.get('error', 'Unknown error')}")
65
-
66
- except Exception as e:
67
- st.error(f"⚠️ Request failed: {e}")
68
 
69
  # ----------------- Tab 2: Batch Prediction -----------------
 
70
  with tab2:
71
  st.subheader("πŸ“„ Upload CSV for Batch Prediction")
72
  uploaded_file = st.file_uploader("Upload your CSV file", type=["csv"])
73
 
74
- if uploaded_file is not None:
75
  try:
76
  df = pd.read_csv(uploaded_file)
77
 
78
  if df.empty:
79
- st.warning("Uploaded file is empty!")
80
  else:
81
  st.write("πŸ“‹ Uploaded Data Preview:")
82
  st.dataframe(df.head())
83
 
84
- # Convert DataFrame to list of records
85
- records = df.to_dict(orient="records")
86
-
87
- # Make prediction API call
88
- response = requests.post(
89
- "https://Pushpak21-SuperKart-Sales-Forecast-Backend.hf.space/predict_batch",
90
- json=records
91
- )
92
-
93
- if response.status_code == 200:
94
- predictions = response.json().get("predictions", [])
95
- df["Predicted_Sales"] = predictions
96
-
97
- # Reorder columns: move actual & predicted sales to front
98
- priority_cols = ["Product_Store_Sales_Total", "Predicted_Sales"]
99
- other_cols = [col for col in df.columns if col not in priority_cols]
100
- df = df[priority_cols + other_cols]
101
-
102
- st.success("βœ… Batch prediction complete!")
103
-
104
- # Header + Download button aligned
105
- col1, col2 = st.columns([6, 1])
106
- with col1:
107
- st.subheader("πŸ“ˆ Prediction Results:")
108
- with col2:
109
- csv = df.to_csv(index=False).encode('utf-8')
110
- st.download_button(
111
- label="πŸ“₯ Download CSV",
112
- data=csv,
113
- file_name="batch_predictions.csv",
114
- mime="text/csv",
115
- use_container_width=True
116
- )
117
-
118
- # Display result table
119
- st.dataframe(df)
120
-
121
- # πŸ“‰ Altair Line Chart: Actual vs Predicted
122
- if "Product_Store_Sales_Total" in df.columns:
123
- plot_df = df[["Product_Store_Sales_Total", "Predicted_Sales"]].dropna()
124
-
125
- if not plot_df.empty:
126
- st.subheader("πŸ“‰ Actual vs Predicted Sales")
127
-
128
- # Prepare long-format dataframe for Altair
129
- plot_df = plot_df.reset_index().rename(columns={
130
- "Product_Store_Sales_Total": "Actual Sales",
131
- "Predicted_Sales": "Predicted Sales",
132
- "index": "Index"
133
- })
134
-
135
- plot_df_melted = plot_df.melt(
136
- id_vars="Index",
137
- var_name="Type",
138
- value_name="Sales"
139
- )
140
-
141
- line_chart = alt.Chart(plot_df_melted).mark_line(point=True).encode(
142
- x=alt.X("Index:O", title="Record Index"),
143
- y=alt.Y("Sales:Q", title="Sales Value"),
144
- color=alt.Color("Type:N", title="Sales Type"),
145
- tooltip=["Index", "Type", "Sales"]
146
- ).properties(
147
- width=700,
148
- height=400
149
- )
150
-
151
- st.altair_chart(line_chart, use_container_width=True)
152
- else:
153
- st.info("ℹ️ Not enough valid rows for plotting.")
154
- else:
155
- st.error(f"❌ API Error {response.status_code}: {response.text}")
156
 
157
  except Exception as e:
158
  st.error(f"⚠️ Error while processing the file: {e}")
 
54
  "Store_Establishment_Year": est_year
55
  }
56
 
57
+ try:
58
+ prediction = get_single_prediction(payload)
59
+ st.success(f"βœ… Predicted Sales: β‚Ή{prediction:,.2f}")
60
+ st.json({**payload, "Predicted_Sales": prediction})
61
+ except Exception as e:
62
+ st.error(f"⚠️ Error during prediction: {e}")
 
 
 
 
 
63
 
64
  # ----------------- Tab 2: Batch Prediction -----------------
65
+ # πŸ“„ TAB 2: Batch Prediction
66
  with tab2:
67
  st.subheader("πŸ“„ Upload CSV for Batch Prediction")
68
  uploaded_file = st.file_uploader("Upload your CSV file", type=["csv"])
69
 
70
+ if uploaded_file:
71
  try:
72
  df = pd.read_csv(uploaded_file)
73
 
74
  if df.empty:
75
+ st.warning("Uploaded file is empty.")
76
  else:
77
  st.write("πŸ“‹ Uploaded Data Preview:")
78
  st.dataframe(df.head())
79
 
80
+ df = get_predictions(df)
81
+ df = reorder_columns(df, ["Product_Store_Sales_Total", "Predicted_Sales"])
82
+
83
+ col1, col2 = st.columns([6, 1])
84
+ with col1:
85
+ st.subheader("πŸ“ˆ Prediction Results:")
86
+ with col2:
87
+ st.download_button(
88
+ label="πŸ“₯ Download CSV",
89
+ data=get_csv_download(df),
90
+ file_name="batch_predictions.csv",
91
+ mime="text/csv",
92
+ use_container_width=True
93
+ )
94
+
95
+ st.dataframe(df)
96
+ plot_actual_vs_predicted(df)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
  except Exception as e:
99
  st.error(f"⚠️ Error while processing the file: {e}")