Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -52,18 +52,35 @@ input_data = {
|
|
| 52 |
# Make prediction when the "Predict" button is clicked
|
| 53 |
if st.button("Predict Sales"):
|
| 54 |
try:
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
if response.status_code == 200:
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
else:
|
| 64 |
-
st.error(f"Error making prediction
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
st.error(f"Connection error: {str(e)}")
|
|
|
|
| 67 |
|
| 68 |
# Section for batch prediction
|
| 69 |
st.subheader("Batch Prediction")
|
|
@@ -84,28 +101,46 @@ uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
|
|
| 84 |
if uploaded_file is not None:
|
| 85 |
if st.button("Predict Batch Sales"):
|
| 86 |
try:
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
if response.status_code == 200:
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
else:
|
| 106 |
-
st.error(f"Error making predictions
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
st.error(f"Connection error: {str(e)}")
|
|
|
|
| 109 |
|
| 110 |
# Add sample data section
|
| 111 |
st.sidebar.markdown("### Sample Data")
|
|
@@ -123,4 +158,9 @@ if st.sidebar.button("Show Sample Input"):
|
|
| 123 |
}
|
| 124 |
|
| 125 |
st.sidebar.json(sample_data)
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
# Make prediction when the "Predict" button is clicked
|
| 53 |
if st.button("Predict Sales"):
|
| 54 |
try:
|
| 55 |
+
# Updated API endpoint
|
| 56 |
+
backend_url = "https://d009-superkartbackend.hf.space" # Corrected URL
|
| 57 |
+
endpoint = f"{backend_url}/v1/sales"
|
| 58 |
+
|
| 59 |
+
st.info(f"Connecting to: {endpoint}") # Show the endpoint being called
|
| 60 |
+
|
| 61 |
+
response = requests.post(endpoint, json=input_data, timeout=10)
|
| 62 |
+
|
| 63 |
if response.status_code == 200:
|
| 64 |
+
try:
|
| 65 |
+
result = response.json()
|
| 66 |
+
st.success(f"Predicted Sales Total: ${result['predicted_sales']:.2f}")
|
| 67 |
+
|
| 68 |
+
# Display features used
|
| 69 |
+
st.markdown("**Features Used**")
|
| 70 |
+
st.write(", ".join(result['features_used']))
|
| 71 |
+
except ValueError:
|
| 72 |
+
st.error("Could not decode JSON response from server")
|
| 73 |
+
st.text(f"Raw response: {response.text}")
|
| 74 |
else:
|
| 75 |
+
st.error(f"Error making prediction (Status {response.status_code})")
|
| 76 |
+
try:
|
| 77 |
+
error_details = response.json()
|
| 78 |
+
st.json(error_details)
|
| 79 |
+
except ValueError:
|
| 80 |
+
st.text(f"Raw response: {response.text}")
|
| 81 |
+
except requests.exceptions.RequestException as e:
|
| 82 |
st.error(f"Connection error: {str(e)}")
|
| 83 |
+
st.info("Please check if the backend server is running and accessible")
|
| 84 |
|
| 85 |
# Section for batch prediction
|
| 86 |
st.subheader("Batch Prediction")
|
|
|
|
| 101 |
if uploaded_file is not None:
|
| 102 |
if st.button("Predict Batch Sales"):
|
| 103 |
try:
|
| 104 |
+
backend_url = "https://d009-superkartbackend.hf.space" # Corrected URL
|
| 105 |
+
endpoint = f"{backend_url}/v1/salesbatch"
|
| 106 |
+
|
| 107 |
+
st.info(f"Connecting to: {endpoint}") # Show the endpoint being called
|
| 108 |
+
|
| 109 |
+
files = {'file': (uploaded_file.name, uploaded_file, 'text/csv')}
|
| 110 |
+
response = requests.post(endpoint, files=files, timeout=30)
|
| 111 |
|
| 112 |
if response.status_code == 200:
|
| 113 |
+
try:
|
| 114 |
+
results = response.json()
|
| 115 |
+
if 'predictions' in results:
|
| 116 |
+
results_df = pd.DataFrame(results['predictions'])
|
| 117 |
+
st.success("Batch predictions completed!")
|
| 118 |
+
st.dataframe(results_df)
|
| 119 |
+
|
| 120 |
+
# Download button for results
|
| 121 |
+
csv = results_df.to_csv(index=False)
|
| 122 |
+
st.download_button(
|
| 123 |
+
label="Download predictions as CSV",
|
| 124 |
+
data=csv,
|
| 125 |
+
file_name='sales_predictions.csv',
|
| 126 |
+
mime='text/csv'
|
| 127 |
+
)
|
| 128 |
+
else:
|
| 129 |
+
st.error("Unexpected response format from server")
|
| 130 |
+
st.json(results)
|
| 131 |
+
except ValueError:
|
| 132 |
+
st.error("Could not decode JSON response from server")
|
| 133 |
+
st.text(f"Raw response: {response.text}")
|
| 134 |
else:
|
| 135 |
+
st.error(f"Error making predictions (Status {response.status_code})")
|
| 136 |
+
try:
|
| 137 |
+
error_details = response.json()
|
| 138 |
+
st.json(error_details)
|
| 139 |
+
except ValueError:
|
| 140 |
+
st.text(f"Raw response: {response.text}")
|
| 141 |
+
except requests.exceptions.RequestException as e:
|
| 142 |
st.error(f"Connection error: {str(e)}")
|
| 143 |
+
st.info("Please check if the backend server is running and accessible")
|
| 144 |
|
| 145 |
# Add sample data section
|
| 146 |
st.sidebar.markdown("### Sample Data")
|
|
|
|
| 158 |
}
|
| 159 |
|
| 160 |
st.sidebar.json(sample_data)
|
| 161 |
+
st.sidebar.download_button(
|
| 162 |
+
label="Download Sample CSV",
|
| 163 |
+
data=pd.DataFrame([sample_data]).to_csv(index=False),
|
| 164 |
+
file_name='sample_input.csv',
|
| 165 |
+
mime='text/csv'
|
| 166 |
+
)
|