import streamlit as st import pandas as pd import numpy as np import tensorflow as tf import json import os # Dummy TensorFlow model for demonstration purposes def create_model(): model = tf.keras.Sequential([ tf.keras.layers.Dense(8, activation='relu', input_shape=(4,)), tf.keras.layers.Dense(4, activation='relu'), tf.keras.layers.Dense(1, activation='sigmoid') ]) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) return model model = create_model() # Function to get user preferences def get_user_preferences(): st.sidebar.write("## User Preferences") username = st.sidebar.text_input("Username", value="Default") preferences = { "username": username, "age": st.sidebar.number_input("Age", min_value=0, max_value=120, value=30), "gender": st.sidebar.selectbox("Gender", options=["Male", "Female", "Other"]), "hobbies": st.sidebar.multiselect("Hobbies", options=["Sports", "Reading", "Travel", "Cooking", "Gaming"]), "occupation": st.sidebar.selectbox("Occupation", options=["Student", "Employed", "Unemployed", "Retired"]) } return preferences # Function to preprocess user preferences for TensorFlow model def preprocess_user_preferences(preferences): # Preprocess the user data as needed for your specific model user_data = np.array([preferences['age'], len(preferences['hobbies']), int(preferences['gender'] == "Male"), int(preferences['occupation'] == "Employed")]) return user_data.reshape(1, -1) # Function to save user preferences to a text file def save_user_preferences(preferences): file_path = f"{preferences['username']}.txt" with open(file_path, 'w') as outfile: json.dump(preferences, outfile) # Function to load user preferences from a text file def load_user_preferences(username): file_path = f"{username}.txt" if os.path.exists(file_path): with open(file_path, 'r') as infile: preferences = json.load(infile) return preferences return None def main(): st.title("AI-driven Personalized Experience") preferences = get_user_preferences() # Load button if st.sidebar.button("Load"): loaded_preferences = load_user_preferences(preferences["username"]) if loaded_preferences: preferences.update(loaded_preferences) st.write("## User Preferences") st.write(preferences) user_data = preprocess_user_preferences(preferences) prediction = model.predict(user_data) st.write("## AI-driven Personalized Content") st.markdown("### Recommendation Score") st.write(f"{prediction[0][0] * 100:.2f}%") st.markdown("### Recommended Activities") activities = pd.DataFrame([ {"Activity": "Outdoor Adventure", "Score": np.random.rand()}, {"Activity": "Book Club", "Score": np.random.rand()}, {"Activity": "Cooking Class", "Score": np.random.rand()}, {"Activity": "Gaming Tournament", "Score": np.random.rand()} ]) # Sort activities by score in descending order and take the top 10 activities = activities.sort_values(by="Score", ascending=False).head(10) activities["Score"] = activities["Score"].apply(lambda x: f"{x * 100:.2f}%") st.table(activities) # Save button if st.sidebar.button("Save"): save_user_preferences(preferences) if __name__ == "__main__": main()