File size: 4,142 Bytes
ce3240c
 
 
 
88d9353
 
ce3240c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41fe573
 
a7d760c
 
 
 
 
 
 
 
41fe573
ce3240c
41fe573
a7d760c
 
 
 
ce3240c
 
 
 
 
 
 
 
 
88d9353
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce3240c
 
 
 
88d9353
41fe573
 
88d9353
 
1ac72a8
a7d760c
 
41fe573
88d9353
ce3240c
 
 
 
 
7fdf4db
 
88d9353
ce3240c
 
 
 
 
 
 
 
 
 
 
88d9353
 
ce3240c
 
 
41fe573
 
 
 
ce3240c
1ac72a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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")

    if "username" not in st.session_state:
        st.session_state.username = username
    
    st.session_state.age = st.sidebar.number_input("Age", min_value=0, max_value=120, value=st.session_state.get("age", 30))
    st.session_state.gender = st.sidebar.selectbox("Gender", options=["Male", "Female", "Other"], index=["Male", "Female", "Other"].index(st.session_state.get("gender", "Male")))
    st.session_state.hobbies = st.sidebar.multiselect("Hobbies", options=["Sports", "Reading", "Travel", "Cooking", "Gaming"], default=st.session_state.get("hobbies", []))
    st.session_state.occupation = st.sidebar.selectbox("Occupation", options=["Student", "Employed", "Unemployed", "Retired"], index=["Student", "Employed", "Unemployed", "Retired"].index(st.session_state.get("occupation", "Student")))
    
    preferences = {
        "username": username,
        "age": st.session_state.age,
        "gender": st.session_state.gender,
        "hobbies": st.session_state.hobbies,
        "occupation": st.session_state.occupation
    }
    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)
            for key, value in loaded_preferences.items():
                st.session_state[key] = value
    
    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()