Mihkelmj commited on
Commit
675bb54
·
1 Parent(s): 8497557

added a preliminary app.py and updated requirements.txt

Browse files
Files changed (2) hide show
  1. app.py +67 -2
  2. requirements.txt +5 -1
app.py CHANGED
@@ -1,4 +1,69 @@
1
  import streamlit as st
 
 
 
 
2
 
3
- x = st.slider('Select a value')
4
- st.write(x, 'squared is', x * x)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ from sklearn.linear_model import LinearRegression
5
+ import joblib
6
 
7
+ # App Title
8
+ st.title("Utrecht Pollution Prediction")
9
+
10
+ # Load the trained model
11
+ @st.cache(allow_output_mutation=True)
12
+ def load_model():
13
+ try:
14
+ # Try loading a pre-trained model
15
+ model = joblib.load("path_to_your_model/linear_regression_model.pkl")
16
+ except:
17
+ # If the model is not available, train a simple Linear Regression model as a fallback
18
+ st.write("No pre-trained model found. Training a new Linear Regression model...")
19
+
20
+ # Fallback - Generate some random training data for demonstration purposes
21
+ # In reality, replace this with your actual data
22
+ np.random.seed(0)
23
+ X_train = np.random.rand(100, 3) # 100 samples, 3 features
24
+ y_train = 3*X_train[:, 0] + 2*X_train[:, 1] + X_train[:, 2] # Example: linear relationship
25
+
26
+ # Train a linear regression model
27
+ model = LinearRegression()
28
+ model.fit(X_train, y_train)
29
+
30
+ # Optionally, save the trained model to use later
31
+ joblib.dump(model, "linear_regression_model.pkl")
32
+
33
+ return model
34
+
35
+ model = load_model()
36
+
37
+ # Explain the app
38
+ st.write("""
39
+ ### Predict Pollution Levels in Utrecht
40
+ This app allows you to input environmental features to predict pollution levels using a simple Linear Regression model.
41
+ """)
42
+
43
+ # Input features needed for your model
44
+ def get_user_input():
45
+ feature_1 = st.number_input('Temperature (°C)', min_value=-10.0, max_value=40.0, value=20.0)
46
+ feature_2 = st.number_input('Wind Speed (km/h)', min_value=0.0, max_value=100.0, value=10.0)
47
+ feature_3 = st.number_input('Humidity (%)', min_value=0.0, max_value=100.0, value=50.0)
48
+
49
+ # Create a DataFrame with user inputs
50
+ input_data = {'Temperature': feature_1,
51
+ 'Wind Speed': feature_2,
52
+ 'Humidity': feature_3}
53
+
54
+ features = pd.DataFrame([input_data])
55
+ return features
56
+
57
+ # Get user input
58
+ input_df = get_user_input()
59
+
60
+ # Display user input
61
+ st.subheader('User Input:')
62
+ st.write(input_df)
63
+
64
+ # Make predictions using the linear regression model
65
+ prediction = model.predict(input_df)
66
+
67
+ # Display the prediction
68
+ st.subheader('Prediction:')
69
+ st.write(f'Predicted Pollution Level: {prediction[0]:.2f}')
requirements.txt CHANGED
@@ -1 +1,5 @@
1
- streamlit
 
 
 
 
 
1
+ streamlit
2
+ pandas
3
+ numpy
4
+ joblib # or pickle if you're using that to load the model
5
+ scikit-learn # for mock model