Ashexperiments commited on
Commit
b656290
·
verified ·
1 Parent(s): 205ca7f

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +9 -10
  2. app.py +60 -22
  3. requirements.txt +2 -2
Dockerfile CHANGED
@@ -1,17 +1,16 @@
1
- # Use an official Python runtime as a parent image
2
  FROM python:3.9-slim
3
 
4
- # Set the working directory in the container
5
  WORKDIR /app
6
 
7
- # Copy the current directory contents into the container at /app
8
- COPY . /app
9
 
10
- # Install any needed packages specified in requirements.txt
11
- RUN pip install --no-cache-dir -r requirements.txt
12
 
13
- # Make port 5000 available to the world outside this container
14
- EXPOSE 5000
15
 
16
- # Run app.py when the container launches
17
- CMD ["python", "app.py"]
 
1
+ # Use a minimal base image with Python 3.9 installed
2
  FROM python:3.9-slim
3
 
4
+ # Set the working directory inside the container to /app
5
  WORKDIR /app
6
 
7
+ # Copy all files from the current directory on the host to the container's /app directory
8
+ COPY . .
9
 
10
+ # Install Python dependencies listed in requirements.txt
11
+ RUN pip3 install -r requirements.txt
12
 
13
+ # Define the command to run the Streamlit app on port 8501 and make it accessible externally
14
+ CMD ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0", "--server.enableXsrfProtection=false"]
15
 
16
+ # NOTE: Disable XSRF protection for easier external access in order to make batch predictions
 
app.py CHANGED
@@ -1,33 +1,71 @@
1
 
2
- import flask
3
- import joblib
4
- import pandas as pd
5
 
6
- # Load the trained model
7
- model = joblib.load('tuned_random_forest_model.pkl') # Replace with the actual model filename
8
 
9
- app = flask.Flask(__name__)
10
 
11
- @app.route('/')
12
- def home():
13
- return "Lead Conversion Prediction Backend"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- @app.route('/predict', methods=['POST'])
16
- def predict():
17
  try:
18
- # Get the data from the request
19
- data = flask.request.get_json(force=True)
20
- df_predict = pd.DataFrame(data)
 
 
 
21
 
22
- # Make predictions
23
- predictions = model.predict(df_predict)
 
 
24
 
25
- # Return the predictions as JSON
26
- return flask.jsonify(predictions.tolist())
 
27
 
28
  except Exception as e:
29
- return flask.jsonify({'error': str(e)})
30
 
31
- if __name__ == '__main__':
32
- # Run the Flask app
33
- app.run(host='0.0.0.0', port=5000)
 
1
 
2
+ import streamlit as st
3
+ import requests
4
+ import json
5
 
6
+ st.title("Lead Conversion Prediction")
 
7
 
8
+ st.write("Enter the lead details to predict conversion likelihood.")
9
 
10
+ # Input fields for lead features (replace with your actual features)
11
+ age = st.number_input("Age", min_value=0)
12
+ current_occupation = st.selectbox("Current Occupation", ['Professional', 'Unemployed', 'Student'])
13
+ first_interaction = st.selectbox("First Interaction", ['Website', 'Mobile App'])
14
+ profile_completed = st.selectbox("Profile Completed", ['Low', 'Medium', 'High'])
15
+ website_visits = st.number_input("Website Visits", min_value=0)
16
+ time_spent_on_website = st.number_input("Time Spent on Website (seconds)", min_value=0)
17
+ page_views_per_visit = st.number_input("Page Views per Visit", min_value=0.0)
18
+ last_activity = st.selectbox("Last Activity", ['Email Activity', 'Website Activity', 'Phone Activity'])
19
+ print_media_type1 = st.selectbox("Print Media Type 1", ['Yes', 'No'])
20
+ print_media_type2 = st.selectbox("Print Media Type 2", ['Yes', 'No'])
21
+ digital_media = st.selectbox("Digital Media", ['Yes', 'No'])
22
+ educational_channels = st.selectbox("Educational Channels", ['Yes', 'No'])
23
+ referral = st.selectbox("Referral", ['Yes', 'No'])
24
+
25
+
26
+ # Create a dictionary with the input data
27
+ input_data = {
28
+ 'age': [age],
29
+ 'current_occupation': [current_occupation],
30
+ 'first_interaction': [first_interaction],
31
+ 'profile_completed': [profile_completed],
32
+ 'website_visits': [website_visits],
33
+ 'time_spent_on_website': [time_spent_on_website],
34
+ 'page_views_per_visit': [page_views_per_visit],
35
+ 'last_activity': [last_activity],
36
+ 'print_media_type1': [print_media_type1],
37
+ 'print_media_type2': [print_media_type2],
38
+ 'digital_media': [digital_media],
39
+ 'educational_channels': [educational_channels],
40
+ 'referral': [referral]
41
+ }
42
+
43
+ # Convert input data to a list of dictionaries (required by the backend)
44
+ input_data_list = [dict(zip(input_data, t)) for t in zip(*input_data.values())]
45
+
46
+
47
+ # Button to trigger prediction
48
+ if st.button("Predict Conversion"):
49
+ # Replace with the URL of your deployed backend API
50
+ backend_url = "YOUR_BACKEND_API_URL/predict" # e.g., "https://your-username-your-backend-space.hf.space/predict"
51
 
 
 
52
  try:
53
+ # Send a POST request to the backend API
54
+ response = requests.post(backend_url, json=input_data_list)
55
+
56
+ if response.status_code == 200:
57
+ predictions = response.json()
58
+ prediction = predictions[0] # Assuming the backend returns a list with a single prediction
59
 
60
+ if prediction == 1:
61
+ st.success("This lead is likely to convert!")
62
+ else:
63
+ st.warning("This lead is less likely to convert.")
64
 
65
+ else:
66
+ st.error(f"Error from backend: {response.status_code}")
67
+ st.error(response.text)
68
 
69
  except Exception as e:
70
+ st.error(f"An error occurred: {e}")
71
 
 
 
 
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- flask==3.0.3
2
- joblib==1.4.2
3
  pandas==2.2.2
4
  scikit-learn==1.6.1
 
1
+ streamlit==1.36.0
2
+ requests==2.32.3
3
  pandas==2.2.2
4
  scikit-learn==1.6.1