rasmodev commited on
Commit
5af8a88
·
1 Parent(s): da85428

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -11
app.py CHANGED
@@ -6,8 +6,6 @@ import catboost
6
  from sklearn.impute import SimpleImputer
7
  import requests
8
 
9
-
10
-
11
  # Load the saved model and unique values:
12
  with open("model_and_key_components.pkl", "rb") as f:
13
  components = pickle.load(f)
@@ -116,25 +114,31 @@ if st.button("Predict"):
116
  "importance_of_record": float(importance_of_record)
117
  }
118
 
119
- # Specify the port that the FastAPI API is running on
 
 
120
  api_port = 7860
 
 
 
 
 
 
 
121
 
122
- # Send a POST request to the FastAPI server
123
- response = requests.post("https://rasmodev-income-prediction-fastapi.hf.space/predict/", json=user_input, port=api_port)
124
-
125
  # Check if the request was successful
126
- if response.status_code == 200:
127
- prediction_data = response.json()
128
-
129
  # Display prediction result to the user
130
  st.subheader("Prediction Result")
131
-
132
  # Determine income prediction and format message
133
  if prediction_data['income_prediction'] == "Income over $50K":
134
  st.success("This individual is predicted to have an income of over $50K.")
135
  else:
136
  st.error("This individual is predicted to have an income of under $50K")
137
-
138
  # Display prediction probability
139
  st.subheader("Prediction Probability")
140
  probability = prediction_data['prediction_probability']
 
6
  from sklearn.impute import SimpleImputer
7
  import requests
8
 
 
 
9
  # Load the saved model and unique values:
10
  with open("model_and_key_components.pkl", "rb") as f:
11
  components = pickle.load(f)
 
114
  "importance_of_record": float(importance_of_record)
115
  }
116
 
117
+ # Send a POST request to the FastAPI server using urllib.request.urlopen()
118
+ import urllib.request
119
+
120
  api_port = 7860
121
+ url = urllib.request.URLopener()
122
+ url.base_url = f"https://rasmodev-income-prediction-fastapi.hf.space/predict/"
123
+ url.addheaders = [("Content-Type", "application/json")]
124
+ url.addheaders.append(("Accept", "application/json"))
125
+ request = urllib.request.Request(url)
126
+ request.data = json.dumps(user_input).encode("utf-8")
127
+ response = url.open(request)
128
 
 
 
 
129
  # Check if the request was successful
130
+ if response.status == 200:
131
+ prediction_data = json.loads(response.read().decode("utf-8"))
132
+
133
  # Display prediction result to the user
134
  st.subheader("Prediction Result")
135
+
136
  # Determine income prediction and format message
137
  if prediction_data['income_prediction'] == "Income over $50K":
138
  st.success("This individual is predicted to have an income of over $50K.")
139
  else:
140
  st.error("This individual is predicted to have an income of under $50K")
141
+
142
  # Display prediction probability
143
  st.subheader("Prediction Probability")
144
  probability = prediction_data['prediction_probability']