MainiSandeep1987 commited on
Commit
0edfce7
·
verified ·
1 Parent(s): 2f6720d

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +22 -18
app.py CHANGED
@@ -22,7 +22,7 @@ TotalCharges = st.number_input("Total Charges", min_value=0.0, value=600.0)
22
  # Convert categorical inputs to match model training
23
  customer_data = {
24
  'SeniorCitizen': 1 if SeniorCitizen == "Yes" else 0,
25
- 'Partner':Partner,
26
  'Dependents': Dependents,
27
  'tenure': tenure,
28
  'PhoneService': PhoneService,
@@ -33,9 +33,13 @@ customer_data = {
33
  'TotalCharges': TotalCharges
34
  }
35
 
36
-
37
  if st.button("Predict", type='primary'):
38
- response = requests.post("https://MainiSandeep1987-BackEndFlaskAPITelecomChurnPrediction.hf.space/v1/customer", json=customer_data) # enter user name and space name before running the cell
 
 
 
 
39
  if response.status_code == 200:
40
  result = response.json()
41
  churn_prediction = result["Prediction"] # Extract only the value
@@ -43,32 +47,32 @@ if st.button("Predict", type='primary'):
43
  else:
44
  st.error("Error in API request")
45
 
46
- # Batch Prediction
47
  st.subheader("Batch Prediction")
48
-
49
  file = st.file_uploader("Upload CSV file", type=["csv"])
50
 
51
- # batch_input = {
52
- # 'file': batch_churn_dataset.to_csv(header=True, index=False).encode('utf-8')
53
- # }
54
-
55
  if file is not None:
56
  if st.button("Predict for Batch", type='primary'):
57
- headers = {
 
58
  # "Authorization": "Bearer YOUR_API_KEY", # Replace with actual API key if needed
59
  "Content-Type": "multipart/form-data"
60
  }
61
- file_data = {"file": ("batch.csv", file.getvalue(), "text/csv")} # Properly format file
62
- response = requests.post(
 
63
  "https://MainiSandeep1987-BackEndFlaskAPITelecomChurnPrediction.hf.space/v1/customerbatch",
64
  files=file_data,
65
  headers=headers
66
- )
67
-
68
- ##response = requests.post("https://MainiSandeep1987-BackEndFlaskAPITelecomChurnPrediction.hf.space/v1/customerbatch", files={"file": file.getvalue()}) # enter user name and space name before running the cell
69
- if response.status_code == 200:
70
  result = response.json()
71
  st.header("Batch Prediction Results")
72
  st.write(result)
73
- else:
74
- st.error("Error in API request")
 
 
 
 
22
  # Convert categorical inputs to match model training
23
  customer_data = {
24
  'SeniorCitizen': 1 if SeniorCitizen == "Yes" else 0,
25
+ 'Partner': Partner,
26
  'Dependents': Dependents,
27
  'tenure': tenure,
28
  'PhoneService': PhoneService,
 
33
  'TotalCharges': TotalCharges
34
  }
35
 
36
+ # Single Prediction
37
  if st.button("Predict", type='primary'):
38
+ response = requests.post(
39
+ "https://MainiSandeep1987-BackEndFlaskAPITelecomChurnPrediction.hf.space/v1/customer",
40
+ json=customer_data
41
+ ) # Send data to Flask API
42
+
43
  if response.status_code == 200:
44
  result = response.json()
45
  churn_prediction = result["Prediction"] # Extract only the value
 
47
  else:
48
  st.error("Error in API request")
49
 
50
+ # Batch Prediction Section
51
  st.subheader("Batch Prediction")
 
52
  file = st.file_uploader("Upload CSV file", type=["csv"])
53
 
 
 
 
 
54
  if file is not None:
55
  if st.button("Predict for Batch", type='primary'):
56
+ try:
57
+ headers = {
58
  # "Authorization": "Bearer YOUR_API_KEY", # Replace with actual API key if needed
59
  "Content-Type": "multipart/form-data"
60
  }
61
+ file_data = {"file": ("batch.csv", file.getvalue(), "text/csv")} # Properly format file
62
+
63
+ response = requests.post(
64
  "https://MainiSandeep1987-BackEndFlaskAPITelecomChurnPrediction.hf.space/v1/customerbatch",
65
  files=file_data,
66
  headers=headers
67
+ )
68
+
69
+ response.raise_for_status() # Raise error if request fails
70
+
71
  result = response.json()
72
  st.header("Batch Prediction Results")
73
  st.write(result)
74
+
75
+ except requests.exceptions.RequestException as e:
76
+ st.error(f"Error in API request: {e}")
77
+ except Exception as e:
78
+ st.error(f"Unexpected error: {e}")