Nra commited on
Commit
64bc22a
·
verified ·
1 Parent(s): 3e6c52a

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. Dockerfile +16 -0
  2. README.md +12 -0
  3. app.py +72 -0
  4. requirements.txt +3 -0
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
README.md ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ ---
3
+ title: M7W2-FrontEnd Application
4
+ emoji: 🚀
5
+ colorFrom: red
6
+ colorTo: red
7
+ sdk: docker
8
+ app_port: 8501
9
+ tags:
10
+ - streamlit
11
+ pinned: false
12
+ ---
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import requests
4
+ import os
5
+
6
+
7
+ #Import Space Environment Variables
8
+ HF_ID = os.environ.get("HF_ID")
9
+ HF_REPO_BACKEND = os.environ.get("HF_REPO_BACKEND")
10
+
11
+ # Huggingface Repo if it contains underscore, the Endpoint does not work and you
12
+ # will get 404 as HF internally expects hyphen.
13
+ # So lets explicity convert any underscore to hyphen if any.
14
+ endpoint_root_url = f"https://{HF_ID}-{HF_REPO_BACKEND}".replace("_", "-") + ".hf.space"
15
+
16
+ # Set the title of the Streamlit app
17
+ st.title("Airbnb Rental Price Prediction")
18
+
19
+ # Section for online prediction
20
+ st.subheader("Online Prediction")
21
+
22
+ # Collect user input for property features
23
+ room_type = st.selectbox("Room Type", ["Entire home/apt", "Private room", "Shared room"])
24
+ accommodates = st.number_input("Accommodates (Number of guests)", min_value=1, value=2)
25
+ bathrooms = st.number_input("Bathrooms", min_value=1, step=1, value=2)
26
+ cancellation_policy = st.selectbox("Cancellation Policy (kind of cancellation policy)", ["strict", "flexible", "moderate"])
27
+ cleaning_fee = st.selectbox("Cleaning Fee Charged?", ["True", "False"])
28
+ instant_bookable = st.selectbox("Instantly Bookable?", ["False", "True"])
29
+ review_scores_rating = st.number_input("Review Score Rating", min_value=0.0, max_value=100.0, step=1.0, value=90.0)
30
+ bedrooms = st.number_input("Bedrooms", min_value=0, step=1, value=1)
31
+ beds = st.number_input("Beds", min_value=0, step=1, value=1)
32
+
33
+ # Convert user input into a DataFrame
34
+ input_data = pd.DataFrame([{
35
+ 'room_type': room_type,
36
+ 'accommodates': accommodates,
37
+ 'bathrooms': bathrooms,
38
+ 'cancellation_policy': cancellation_policy,
39
+ 'cleaning_fee': cleaning_fee,
40
+ 'instant_bookable': 'f' if instant_bookable=="False" else "t", # Convert to 't' or 'f'
41
+ 'review_scores_rating': review_scores_rating,
42
+ 'bedrooms': bedrooms,
43
+ 'beds': beds
44
+ }])
45
+
46
+ # Make prediction when the "Predict" button is clicked
47
+ if st.button("Predict"):
48
+ response = requests.post(endpoint_root_url + "/v1/rental",
49
+ json=input_data.to_dict(orient='records')[0]) # Send data to Flask API
50
+ if response.status_code == 200:
51
+ prediction = response.json()['Predicted Price (in dollars)']
52
+ st.success(f"Predicted Rental Price (in dollars): {prediction}")
53
+ else:
54
+ st.error(f"Error Code : {response.status_code}. Root URL : {endpoint_root_url}")
55
+
56
+ # Section for batch prediction
57
+ st.subheader("Batch Prediction")
58
+
59
+ # Allow users to upload a CSV file for batch prediction
60
+ uploaded_file = st.file_uploader("Upload CSV file for batch prediction", type=["csv"])
61
+
62
+ # Make batch prediction when the "Predict Batch" button is clicked
63
+ if uploaded_file is not None:
64
+ if st.button("Predict Batch"):
65
+ response = requests.post(endpoint_root_url + "/v1/rentalbatch",
66
+ files={"file": uploaded_file}) # Send file to Flask API
67
+ if response.status_code == 200:
68
+ predictions = response.json()
69
+ st.success("Batch predictions completed!")
70
+ st.write(predictions) # Display the predictions
71
+ else:
72
+ st.error(f"Error Code : {response.status_code}. Root URL : {endpoint_root_url}")
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ pandas==2.2.2
2
+ requests==2.28.1
3
+ streamlit==1.43.2