Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| import streamlit as st | |
| # Define or import label_encoders, scaler, and model | |
| # Example: | |
| from sklearn.preprocessing import LabelEncoder, StandardScaler | |
| from sklearn.ensemble import RandomForestClassifier | |
| # Assume label_encoders, scaler, and model are defined and fitted elsewhere | |
| # Initialize label_encoders, scaler, and model | |
| label_encoders = {} | |
| scaler = StandardScaler() | |
| model = RandomForestClassifier(n_estimators=100, random_state=42) | |
| # Function to preprocess input data and predict disease risk | |
| def predict_disease_risk(input_data): | |
| input_df = pd.DataFrame({ | |
| 'Age': [input_data['Age']], | |
| 'Gender': [input_data['Gender']], | |
| 'How many meals do you have a day?': [input_data['How many meals do you have a day?']], | |
| 'What would best describe your diet': [input_data['What would best describe your diet']], | |
| 'Choose all that apply: [I skip meals]': [input_data['Choose all that apply: [I skip meals]']], | |
| 'Choose all that apply: [I experience feelings of hunger during the day]': [input_data['Choose all that apply: [I experience feelings of hunger during the day]']], | |
| 'Choose all that apply: [I consult a nutritionist/dietician]': [input_data['Choose all that apply: [I consult a nutritionist/dietician]']], | |
| 'Choose all that apply: [I cook my own meals]': [input_data['Choose all that apply: [I cook my own meals]']], | |
| 'What would you consider to be the main meal of YOUR day?': [input_data['What would you consider to be the main meal of YOUR day?']], | |
| 'What does your diet mostly consist of and how is it prepared?': [input_data['What does your diet mostly consist of and how is it prepared?']], | |
| }) | |
| # Apply label encoding | |
| for column, le in label_encoders.items(): | |
| if column in input_df.columns: | |
| input_df[column] = le.transform(input_df[column]) | |
| # Scale the input features | |
| input_scaled = scaler.transform(input_df) | |
| # Make prediction | |
| prediction = model.predict(input_scaled) | |
| return prediction[0] | |
| # Streamlit GUI | |
| st.title('Health Risk Prediction Based on Diet') | |
| # User inputs | |
| age = st.number_input('Age', min_value=1, max_value=120, value=30, step=1) | |
| gender = st.selectbox('Gender', ['Male', 'Female']) | |
| meals_per_day = st.number_input('Number of meals per day', min_value=1, max_value=10) | |
| diet = st.selectbox('Diet', ['Pollotarian', 'Vegetarian', 'Pescatarian', 'Non-Vegetarian', 'Eggetarian']) | |
| skip_meals = st.selectbox('Do you skip meals?', ['Never', 'Rarely', 'Sometimes', 'Often', 'Very frequently']) | |
| hunger = st.selectbox('Do you experience feelings of hunger during the day?', ['Never', 'Rarely', 'Sometimes', 'Often', 'Very frequently']) | |
| nutritionist = st.selectbox('Do you consult a nutritionist?', ['Never', 'Rarely', 'Sometimes', 'Often', 'Very frequently']) | |
| cook_meals = st.selectbox('Do you cook your own meals?', ['Never', 'Rarely', 'Sometimes', 'Often', 'Very frequently']) | |
| main_meal = st.selectbox('Main meal of the day', ['Breakfast', 'Lunch', 'Dinner', 'All']) | |
| diet_description = st.selectbox('Diet description', ['Freshly home-cooked produce', 'Restaurant meals']) | |
| # Collect all inputs in a dictionary | |
| input_data = { | |
| 'Age': age, | |
| 'Gender': gender, | |
| 'How many meals do you have a day?': meals_per_day, | |
| 'What would best describe your diet': diet, | |
| 'Choose all that apply: [I skip meals]': skip_meals, | |
| 'Choose all that apply: [I experience feelings of hunger during the day]': hunger, | |
| 'Choose all that apply: [I consult a nutritionist/dietician]': nutritionist, | |
| 'Choose all that apply: [I cook my own meals]': cook_meals, | |
| 'What would you consider to be the main meal of YOUR day?': main_meal, | |
| 'What does your diet mostly consist of and how is it prepared?': diet_description, | |
| } | |
| # Prediction button | |
| if st.button('Predict Disease Risk'): | |
| prediction = predict_disease_risk(input_data) | |
| st.write(f'Predicted Disease Risk: {prediction}') | |