Spaces:
Running
Running
| import pandas as pd | |
| import gradio as gr | |
| catalog = pd.read_csv('cleaned_streaming_catalog.csv') | |
| users = pd.read_csv('synthetic_users_enriched.csv') | |
| watch_history = pd.read_csv('final_streamsmart_dataset.csv') | |
| def recommend_for_user(user_id, top_n=5): | |
| user = users[users['user_id'] == user_id].iloc[0] | |
| watched = set(watch_history.loc[watch_history['user_id'] == user_id, 'title_id']) | |
| recs = catalog[~catalog['title_id'].isin(watched)].copy() | |
| recs['match_score'] = 0 | |
| recs.loc[recs['genre'].str.contains(user['favorite_genre_1'], case=False, na=False), 'match_score'] += 2 | |
| recs.loc[recs['genre'].str.contains(user['favorite_genre_2'], case=False, na=False), 'match_score'] += 1 | |
| recs.loc[recs['duration_bucket'].astype(str) == user['preferred_length'], 'match_score'] += 1 | |
| recs['final_score'] = recs['match_score'] * 20 + recs['quality_score'] * 0.5 + recs['platform_popularity'] * 0.3 | |
| return recs.sort_values(['match_score', 'final_score'], ascending=False).head(top_n) | |
| def app_recommend(user_id): | |
| recs = recommend_for_user(user_id) | |
| user = users[users['user_id'] == user_id].iloc[0] | |
| summary = f"Top picks for {user['first_name']} ({user_id}) based on {user['favorite_genre_1']} / {user['favorite_genre_2']} preferences." | |
| return summary, recs[['title', 'type', 'genre', 'release_year', 'final_score']] | |
| with gr.Blocks(title='StreamSmart Recommender') as demo: | |
| gr.Markdown('# StreamSmart Recommender') | |
| gr.Markdown('Demo interface for the final project.') | |
| user_id = gr.Dropdown(choices=users['user_id'].tolist(), label='Choose a synthetic user', value=users['user_id'].iloc[0]) | |
| btn = gr.Button('Generate Recommendations') | |
| summary = gr.Textbox(label='Recommendation summary') | |
| table = gr.Dataframe(label='Top recommendations') | |
| btn.click(app_recommend, inputs=user_id, outputs=[summary, table]) | |
| if __name__ == '__main__': | |
| demo.launch(share=True) |