sahilsingla commited on
Commit
e123a92
·
verified ·
1 Parent(s): 2623a95

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +21 -53
app.py CHANGED
@@ -112,62 +112,30 @@ st.subheader("Batch Prediction")
112
 
113
  st.write("Upload a CSV file with columns: Product_Weight, Product_Sugar_Content, Product_Allocated_Area, Product_Type, Product_MRP, Store_Establishment_Year, Store_Size, Store_Location_City_Type, Store_Type")
114
 
115
- try:
116
- # File uploader
117
- uploaded_file = st.file_uploader("Choose a CSV file", type=["csv"])
118
- except Exception as e:
119
- st.error("An unexpected error occurred during prediction.")
120
- st.exception(e)
121
 
122
  if uploaded_file is not None:
123
- st.success("File uploaded successfully")
124
- # Read CSV for display
125
- try:
126
- # Read the uploaded file into a pandas DataFrame
127
- input_df = pd.read_csv(uploaded_file)
128
- st.write("Uploaded Data Preview:")
129
- st.dataframe(input_df.head())
130
 
131
- # Prepare file for API
132
- # Reset file pointer to start
133
- uploaded_file.seek(0)
134
- files = {"file": (uploaded_file.name, uploaded_file, "text/csv")}
 
135
 
136
- # Send POST request to batch API
137
- model_batch_url = "https://sahilsingla-SuperKartPredictionBackend.hf.space/v1/salesbatch"
138
- try:
139
- response = requests.post(model_batch_url, files=files)
140
- response.raise_for_status()
141
- result = response.json()
142
- predictions = result.get("predictions", None)
143
-
144
- if predictions:
145
- # Create DataFrame with predictions
146
- output_df = input_df.copy()
147
- output_df["Predicted_Sales"] = [float(p) for p in predictions]
148
-
149
- # Display predictions
150
- st.write("Batch Prediction Results:")
151
- st.dataframe(output_df)
152
-
153
- # Provide download button for results
154
- csv_buffer = StringIO()
155
- output_df.to_csv(csv_buffer, index=False)
156
- st.download_button(
157
- label="Download Predictions",
158
- data=csv_buffer.getvalue(),
159
- file_name="batch_predictions.csv",
160
- mime="text/csv"
161
- )
162
  else:
163
- st.error("No predictions returned from API")
164
- except requests.exceptions.RequestException as e:
165
- st.error(f"Error contacting the batch API: {e}")
166
- except ValueError:
167
- st.error("Invalid response format from batch API")
168
- except pd.errors.EmptyDataError:
169
- st.error("Uploaded CSV is empty")
170
  except Exception as e:
171
- st.error(f"Error processing CSV: {e}")
172
- except Exception as e:
173
- st.error(f"Error reading CSV: {e}")
 
112
 
113
  st.write("Upload a CSV file with columns: Product_Weight, Product_Sugar_Content, Product_Allocated_Area, Product_Type, Product_MRP, Store_Establishment_Year, Store_Size, Store_Location_City_Type, Store_Type")
114
 
115
+ # Allow users to upload a CSV file
116
+ uploaded_file = st.file_uploader("Upload CSV file for batch prediction", type=["csv"])
 
 
 
 
117
 
118
  if uploaded_file is not None:
119
+ if st.button("Predict Batch"):
120
+ try:
121
+ # Prepare file payload correctly
122
+ files = {"file": (uploaded_file.name, uploaded_file, "text/csv")}
 
 
 
123
 
124
+ # Send the POST request to the Flask API
125
+ response = requests.post(
126
+ "https://sahilsingla-SuperKartPredictionBackend.hf.space/v1/salesbatch",
127
+ files=files
128
+ )
129
 
130
+ # Check response status
131
+ if response.status_code == 200:
132
+ predictions = response.json()
133
+ st.success("Batch predictions completed!")
134
+ st.write(predictions)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  else:
136
+ st.error(f"Error: {response.status_code}")
137
+ st.error(response.text)
138
+
 
 
 
 
139
  except Exception as e:
140
+ st.error("An unexpected error occurred during prediction.")
141
+ st.exception(e)