File size: 3,460 Bytes
9efb7f0
 
 
 
7c1f2a8
 
9efb7f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa6665b
 
 
9efb7f0
aa6665b
9efb7f0
 
 
 
 
 
 
 
 
 
 
 
 
7c1f2a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9efb7f0
 
 
 
7c1f2a8
aa6665b
 
7c1f2a8
 
 
aa6665b
7c1f2a8
9efb7f0
 
 
 
 
 
7c1f2a8
9efb7f0
 
 
 
 
 
 
 
 
 
 
 
7c1f2a8
 
9efb7f0
 
 
aa6665b
 
 
 
9efb7f0
7c1f2a8
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
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()