Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| import os | |
| from component import * | |
| from GameRecommender import * | |
| import gc | |
| from sklearn.model_selection import train_test_split | |
| from huggingface_hub import snapshot_download | |
| from sklearn.preprocessing import MultiLabelBinarizer,LabelEncoder,MinMaxScaler | |
| DATA_BASE_PATH = 'data' | |
| # MODEL_BASE_PATH = 'models' | |
| MODEL_BASE_PATH = snapshot_download( | |
| repo_id="VJyzCELERY/SteamGameRecommender", | |
| repo_type="model", | |
| allow_patterns=["GameRecommender/*"] | |
| ) | |
| SEED = 42 | |
| RAW_GAMES_DATAPATH = os.path.join(DATA_BASE_PATH,'converted.csv') | |
| GAMES_DATAPATH = os.path.join(DATA_BASE_PATH,'Cleaned_games.csv') | |
| REVIEWS_DATAPATH = os.path.join(DATA_BASE_PATH,'MergedFragmentData_SAMPLE.csv') | |
| TRIMMED_REVIEW_DATAPATH = os.path.join(DATA_BASE_PATH,'Trimmed_Dataset.csv') | |
| USER_PREFERENCE_DATAPATH = os.path.join(DATA_BASE_PATH,'UserPreferenceDF.csv') | |
| MODEL_PATH = os.path.join(MODEL_BASE_PATH,'GameRecommender') | |
| from datasets import load_dataset | |
| GAMES_DS = load_dataset("VJyzCELERY/Cleaned_games") | |
| # load dataset | |
| model = GameRecommendationEnsemble.load(MODEL_PATH) | |
| vectorizer=model.text_based_recommender.vectorizer | |
| review_app_id_encoder=model.text_based_recommender.app_id_encoder | |
| genres = model.game_content_recommeder.genre_encoder.classes_.tolist() | |
| genres = [genre for genre in genres if genre != 'Unknown'] | |
| categories = model.game_content_recommeder.category_encoder.classes_.tolist() | |
| categories = [cat for cat in categories if cat != 'Unknown'] | |
| price_ranges = model.game_content_recommeder.price_range_encoder.classes_.tolist() | |
| selectable_app_ids = list(model.collaborative_recommender.item_to_index.keys()) | |
| # df_games = pd.read_csv(GAMES_DATAPATH,index_col=False) | |
| df_games = GAMES_DS['train'].to_pandas() | |
| available_names = df_games[df_games['app_id'].astype(str).isin(selectable_app_ids)]['Name'].tolist() | |
| def recommend_game(description=None, app_name=None, price_range=None, year_release=None, | |
| excpected_playtime=None, game_score=None, dlc_count=None, | |
| genres=None, categories=None, top_n=5,weight_text=1.0, weight_collab=1.0, weight_content=1.0): | |
| if app_name: | |
| if isinstance(app_name, (str)): | |
| app_name = [app_name] | |
| app_ids = df_games[df_games['Name'].isin(app_name)]['app_id'].astype(str).tolist() | |
| else: | |
| app_ids = None | |
| prediction = model.predict(description=description,app_ids=app_ids,price_range=price_range,year_release=year_release,average_playtime=excpected_playtime,game_score=game_score, | |
| dlc_count=dlc_count,genres=genres,categories=categories,top_n=top_n,weight_text=weight_text,weight_collab=weight_collab,weight_content=weight_content) | |
| app_ids = prediction['app_id'].tolist() | |
| output = df_games.loc[df_games['app_id'].astype(str).isin(app_ids)].reset_index() | |
| return gr.DataFrame(value=output) | |
| # Load external CSS file | |
| with open('style.css', 'r') as f: | |
| custom_css = f.read() | |
| """ | |
| MAIN DEMO | |
| """ | |
| with gr.Blocks(css = custom_css) as demo: | |
| # container | |
| with gr.Row(elem_classes="container"): | |
| with gr.Column(elem_id="system", elem_classes='content-section', visible=True) as system_section: | |
| # special for this section | |
| gr.HTML('<h1 class="header-title">Game Recommendation System</h1>', elem_id='system') | |
| with gr.Row(): | |
| with gr.Column(min_width=500, elem_classes='input-column'): | |
| app_name = input_choice( | |
| Label='Select games that you liked', | |
| Choices=available_names, | |
| Multiselect=True | |
| ) | |
| year = input_number( | |
| Label='Year Release', | |
| Precision=0, | |
| minimum=0 | |
| ) | |
| expected_playtime = input_number( | |
| Label='Expected Playtime (Hours)', | |
| Precision=2, | |
| minimum=0 | |
| ) | |
| expected_score = input_number( | |
| Label='Expected Score (% Positive)', | |
| Precision=2, | |
| minimum=0 | |
| ) | |
| dlc_count = input_number( | |
| Label='DLC Count', | |
| Precision=0, | |
| minimum=0 | |
| ) | |
| description = input_paragaph_textbox('Description', 'Describe the game (max 1200 characters)...') | |
| genre = input_choice( | |
| Label="Select Your Genre (Multiple Choice)", | |
| Choices=genres, | |
| Multiselect=True | |
| ) | |
| categories = input_choice( | |
| Label="Select Your Categories (Multiple Choice)", | |
| Choices=categories, | |
| Multiselect=True | |
| ) | |
| # single selection (multiselect=False) | |
| price_range = input_choice( | |
| Label="Select Your Price Range (Only Single Choice)", | |
| Choices=price_ranges, | |
| Multiselect=False | |
| ) | |
| top_n= input_number( | |
| Label='Output amount', | |
| Precision=0, | |
| minimum=0, | |
| value=10 | |
| ) | |
| weight_text = input_number( | |
| Label='Weight Text', | |
| Precision=2, | |
| minimum=0, | |
| maximum=1, | |
| value=0.5, | |
| step=0.01 | |
| ) | |
| weight_collab = input_number( | |
| Label='Weight Of Collaborative Model', | |
| Precision=2, | |
| minimum=0, | |
| maximum=1, | |
| value=0.5, | |
| step=0.01 | |
| ) | |
| weight_content = input_number( | |
| Label='Weight Of Content Based Model', | |
| Precision=2, | |
| minimum=0, | |
| maximum=1, | |
| value=0.5, | |
| step=0.01 | |
| ) | |
| submit_btn = gr.Button("Get Recommendations", variant="primary", elem_id="submit-btn") | |
| # Results column | |
| with gr.Column(min_width=500, elem_classes='results-column'): | |
| h2('Result') | |
| with gr.Column(elem_id='Output'): | |
| # Results column using the modular component | |
| h2('Recommended Game') | |
| recommended_game = gr.DataFrame() | |
| # click button logic | |
| submit_btn.click( | |
| fn=recommend_game, | |
| inputs=[description,app_name,price_range,year,expected_playtime,expected_score,dlc_count, genre, categories,top_n,weight_text,weight_collab,weight_content], | |
| outputs=recommended_game | |
| ) | |
| demo.launch() |