tifischer commited on
Commit
cad6845
·
verified ·
1 Parent(s): 15f4c43

Upload frontend_files/app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. frontend_files/app.py +70 -0
frontend_files/app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import requests
4
+ import os
5
+
6
+ # Set the title of the Streamlit app
7
+ st.title('Airbnb Rental Price Prediction')
8
+
9
+ # Get the backend API URL from environment variables (set during Container Apps deployment)
10
+ # Format: https://<app-name>.<region>.azurecontainerapps.io
11
+ backend_api_url = os.getenv('BACKEND_API_URL', 'https://rental-price-prediction-api.azurecontainerapps.io')
12
+
13
+ # Section for online prediction
14
+ st.subheader('Online Prediction')
15
+
16
+ # Collect user input for property features
17
+ room_type = st.selectbox('Room Type', ['Entire home/apt', 'Private room', 'Shared room'])
18
+ accommodates = st.number_input('Accommodates (Number of guests)', min_value=1, value=2)
19
+ bathrooms = st.number_input('Bathrooms', min_value=1, step=1, value=2)
20
+ cancellation_policy = st.selectbox('Cancellation Policy (kind of cancellation policy)', ['strict', 'flexible', 'moderate'])
21
+ cleaning_fee = st.selectbox('Cleaning Fee Charged?', ['True', 'False'])
22
+ instant_bookable = st.selectbox('Instantly Bookable?', ['False', 'True'])
23
+ review_scores_rating = st.number_input('Review Score Rating', min_value=0.0, max_value=100.0, step=1.0, value=90.0)
24
+ bedrooms = st.number_input('Bedrooms', min_value=0, step=1, value=1)
25
+ beds = st.number_input('Beds', min_value=0, step=1, value=1)
26
+
27
+ # Convert user input into a DataFrame
28
+ input_data = pd.DataFrame([{
29
+ 'room_type': room_type,
30
+ 'accommodates': accommodates,
31
+ 'bathrooms': bathrooms,
32
+ 'cancellation_policy': cancellation_policy,
33
+ 'cleaning_fee': cleaning_fee,
34
+ 'instant_bookable': 'f' if instant_bookable=='False' else 't', # Convert to 't' or 'f'
35
+ 'review_scores_rating': review_scores_rating,
36
+ 'bedrooms': bedrooms,
37
+ 'beds': beds
38
+ }])
39
+
40
+ # Make prediction when the 'Predict' button is clicked
41
+ if st.button('Predict'):
42
+ try:
43
+ response = requests.post(f'{backend_api_url}/v1/rental', json=input_data.to_dict(orient='records')[0])
44
+ if response.status_code == 200:
45
+ prediction = response.json()['Predicted Price (in dollars)']
46
+ st.success(f'Predicted Rental Price (in dollars): {prediction}')
47
+ else:
48
+ st.error(f'Error making prediction. Status code: {response.status_code}')
49
+ except Exception as e:
50
+ st.error(f'Error connecting to backend: {str(e)}')
51
+
52
+ # Section for batch prediction
53
+ st.subheader('Batch Prediction')
54
+
55
+ # Allow users to upload a CSV file for batch prediction
56
+ uploaded_file = st.file_uploader('Upload CSV file for batch prediction', type=['csv'])
57
+
58
+ # Make batch prediction when the 'Predict Batch' button is clicked
59
+ if uploaded_file is not None:
60
+ if st.button('Predict Batch'):
61
+ try:
62
+ response = requests.post(f'{backend_api_url}/v1/rentalbatch', files={'file': uploaded_file})
63
+ if response.status_code == 200:
64
+ predictions = response.json()
65
+ st.success('Batch predictions completed!')
66
+ st.write(predictions) # Display the predictions
67
+ else:
68
+ st.error(f'Error making batch prediction. Status code: {response.status_code}')
69
+ except Exception as e:
70
+ st.error(f'Error connecting to backend: {str(e)}')