DD009 commited on
Commit
baea535
·
verified ·
1 Parent(s): 8893428

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +68 -28
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
- response = requests.post("https://D009-SuperKartBackend.hf.space/v1/sales", json=input_data)
 
 
 
 
 
 
 
56
  if response.status_code == 200:
57
- result = response.json()
58
- st.success(f"Predicted Sales Total: ${result['predicted_sales']}")
59
-
60
- # Display features used
61
- st.markdown("**Features Used**")
62
- st.write(", ".join(result['features_used']))
 
 
 
 
63
  else:
64
- st.error(f"Error making prediction: {response.json().get('error', 'Unknown error')}")
65
- except Exception as e:
 
 
 
 
 
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
- files = {'file': uploaded_file}
88
- response = requests.post("https://D009-SuperKartBackend.hf.space/v1/salesbatch", files=files)
 
 
 
 
 
89
 
90
  if response.status_code == 200:
91
- results = response.json()['predictions']
92
- results_df = pd.DataFrame(results)
93
-
94
- st.success("Batch predictions completed!")
95
- st.dataframe(results_df)
96
-
97
- # Download button for results
98
- csv = results_df.to_csv(index=False)
99
- st.download_button(
100
- label="Download predictions as CSV",
101
- data=csv,
102
- file_name='sales_predictions.csv',
103
- mime='text/csv'
104
- )
 
 
 
 
 
 
 
105
  else:
106
- st.error(f"Error making predictions: {response.json().get('error', 'Unknown error')}")
107
- except Exception as e:
 
 
 
 
 
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
+ )