Bnava13 commited on
Commit
86624d4
·
verified ·
1 Parent(s): bcd85ef

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -0
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import gradio as gr
3
+ from sklearn.feature_extraction.text import TfidfVectorizer
4
+ from sklearn.metrics.pairwise import cosine_similarity
5
+ from sklearn.preprocessing import MinMaxScaler
6
+ import difflib
7
+
8
+ # Load dataset
9
+ data = pd.read_csv('steam.csv', quotechar='"', on_bad_lines='skip', nrows=10000)
10
+ data.fillna('', inplace=True)
11
+
12
+ # Combine better features
13
+ selected_features = ['genres', 'categories', 'tags']
14
+ for feature in selected_features:
15
+ if feature not in data.columns:
16
+ data[feature] = ''
17
+
18
+ data['combined_features'] = data['genres'] + ' ' + data['categories'] + ' ' + data['tags']
19
+
20
+ # TF-IDF Vectorizer tuned
21
+ vectorizer = TfidfVectorizer(
22
+ stop_words='english',
23
+ ngram_range=(1, 2),
24
+ max_features=8000
25
+ )
26
+ feature_vectors = vectorizer.fit_transform(data['combined_features'])
27
+
28
+ # Normalize positive ratings for re-ranking
29
+ scaler = MinMaxScaler()
30
+ data['positive_ratings_scaled'] = scaler.fit_transform(data[['positive_ratings']])
31
+
32
+ # Calculate cosine similarity
33
+ game_similarity = cosine_similarity(feature_vectors)
34
+
35
+ # List of titles
36
+ list_of_all_titles = data['name'].tolist()
37
+
38
+ def recommend_games(user_game_name_input):
39
+ find_close_match = difflib.get_close_matches(user_game_name_input, list_of_all_titles, n=1)
40
+ if not find_close_match:
41
+ return "No close match found. Please try another game name."
42
+
43
+ closest_match = find_close_match[0]
44
+ index_of_the_game = data.loc[data['name'] == closest_match].index[0]
45
+
46
+ similarity_scores = list(enumerate(game_similarity[index_of_the_game]))
47
+
48
+ # Sort by similarity and positive ratings
49
+ sorted_similar_games = sorted(
50
+ similarity_scores,
51
+ key=lambda x: (x[1], data.iloc[x[0]]['positive_ratings_scaled']),
52
+ reverse=True
53
+ )
54
+
55
+ recommendations = []
56
+ for i, (index, score) in enumerate(sorted_similar_games[1:21]): # Skip itself
57
+ if score < 0.3:
58
+ continue # Skip very low similarity
59
+ game_name = data.iloc[index]['name']
60
+ recommendations.append(f"{i+1}. {game_name} (Similarity: {score:.2f})")
61
+ if len(recommendations) >= 10:
62
+ break
63
+
64
+ return "\n".join(recommendations)
65
+
66
+ # Simulate evaluation Precision@5
67
+ def evaluate_precision(user_game_name_input):
68
+ find_close_match = difflib.get_close_matches(user_game_name_input, list_of_all_titles, n=1)
69
+ if not find_close_match:
70
+ return 0.0
71
+
72
+ closest_match = find_close_match[0]
73
+ index_of_the_game = data.loc[data['name'] == closest_match].index[0]
74
+ similarity_scores = list(enumerate(game_similarity[index_of_the_game]))
75
+ sorted_similar_games = sorted(
76
+ similarity_scores,
77
+ key=lambda x: (x[1], data.iloc[x[0]]['positive_ratings_scaled']),
78
+ reverse=True
79
+ )
80
+
81
+ top_5 = [data.iloc[idx]['genres'] for idx, _ in sorted_similar_games[1:6]]
82
+ original_genre = data.iloc[index_of_the_game]['genres']
83
+ hits = sum(1 for genre in top_5 if genre == original_genre)
84
+ precision_at_5 = hits / 5
85
+ return round(precision_at_5, 2)
86
+
87
+ # Gradio App
88
+ def recommend_and_score(user_input):
89
+ recommendations = recommend_games(user_input)
90
+ precision = evaluate_precision(user_input)
91
+ return f"{recommendations}\n\nPrecision@5 (approx): {precision}"
92
+
93
+ demo = gr.Interface(
94
+ fn=recommend_and_score,
95
+ inputs=gr.Textbox(lines=1, placeholder="Enter your favorite game"),
96
+ outputs="text",
97
+ title="Steam Game Recommender (Optimized Scikit-Learn Version)",
98
+ description="Enter a game name and get high-accuracy recommendations! 🚀"
99
+ )
100
+
101
+ demo.launch()