Jimin Park
commited on
Commit
·
7e4083f
1
Parent(s):
cd2e4d0
added model
Browse files- util/app.py +104 -110
util/app.py
CHANGED
@@ -27,63 +27,60 @@ from selenium.webdriver.support.ui import WebDriverWait
|
|
27 |
from selenium.webdriver.support import expected_conditions as EC
|
28 |
from helper import format_summoner_name
|
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 |
training_features = get_user_training_df(player_opgg_url)
|
|
|
|
|
|
|
77 |
|
78 |
-
# Assume `training_features` is the DataFrame provided
|
79 |
-
# Calculate total wins and losses
|
80 |
wins = training_features['result'].sum()
|
81 |
losses = len(training_features) - wins
|
82 |
-
|
83 |
-
# Calculate winrate
|
84 |
winrate = f"{(wins / len(training_features)) * 100:.0f}%"
|
85 |
-
|
86 |
-
# Calculate favorite champions
|
87 |
favorite_champions = (
|
88 |
training_features['champion']
|
89 |
.value_counts()
|
@@ -91,75 +88,72 @@ with gr.Blocks() as demo:
|
|
91 |
.index.tolist()
|
92 |
)
|
93 |
|
94 |
-
# Create the summary dictionary
|
95 |
-
summary_data = {
|
96 |
-
'wins': wins,
|
97 |
-
'losses': losses,
|
98 |
-
'winrate': winrate,
|
99 |
-
'favorite_champions': favorite_champions
|
100 |
-
}
|
101 |
-
|
102 |
stats_html = f"""
|
103 |
<div style='padding: 20px; background: #f5f5f5; border-radius: 10px;'>
|
104 |
-
<h3>Player Stats
|
105 |
-
<p>Wins: {
|
106 |
-
<p>Winrate: {
|
107 |
-
<p>Favorite Champions: {', '.join(
|
108 |
</div>
|
109 |
"""
|
110 |
|
111 |
-
return stats_html
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
|
113 |
-
def predict_champion(player_opgg_url, *champions):
|
114 |
-
"""Make prediction based on selected champions"""
|
115 |
-
if not player_opgg_url or None in champions:
|
116 |
-
return "Please fill in all fields"
|
117 |
-
|
118 |
-
# Prepare features
|
119 |
features = get_user_training_df(player_opgg_url, champions)
|
120 |
-
|
121 |
-
|
|
|
122 |
prediction = model.predict(features)
|
123 |
-
|
124 |
-
# Get predicted champion name
|
125 |
-
predicted_champion = CHAMPIONS[prediction[0]] # Adjust based on your model output
|
126 |
-
|
127 |
return f"Predicted champion: {predicted_champion}"
|
|
|
|
|
128 |
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
# Set up event handlers
|
152 |
-
show_button.click(
|
153 |
-
fn=show_stats,
|
154 |
-
inputs=[player_opgg_url],
|
155 |
-
outputs=[stats_output, recent_matches]
|
156 |
-
)
|
157 |
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
|
164 |
# Add this line at the end
|
165 |
demo.queue() # Enable queuing for better handling of multiple users
|
|
|
27 |
from selenium.webdriver.support import expected_conditions as EC
|
28 |
from helper import format_summoner_name
|
29 |
|
30 |
+
# Define champion list for dropdowns
|
31 |
+
CHAMPIONS = [
|
32 |
+
"Aatrox", "Ahri", "Akali", "Akshan", "Alistar", "Amumu", "Anivia", "Annie", "Aphelios", "Ashe",
|
33 |
+
"Aurelion Sol", "Azir", "Bard", "Bel'Veth", "Blitzcrank", "Brand", "Braum", "Caitlyn", "Camille",
|
34 |
+
"Cassiopeia", "Cho'Gath", "Corki", "Darius", "Diana", "Dr. Mundo", "Draven", "Ekko", "Elise",
|
35 |
+
"Evelynn", "Ezreal", "Fiddlesticks", "Fiora", "Fizz", "Galio", "Gangplank", "Garen", "Gnar",
|
36 |
+
"Gragas", "Graves", "Gwen", "Hecarim", "Heimerdinger", "Illaoi", "Irelia", "Ivern", "Janna",
|
37 |
+
"Jarvan IV", "Jax", "Jayce", "Jhin", "Jinx", "Kai'Sa", "Kalista", "Karma", "Karthus", "Kassadin",
|
38 |
+
"Katarina", "Kayle", "Kayn", "Kennen", "Kha'Zix", "Kindred", "Kled", "Kog'Maw", "KSante", "LeBlanc",
|
39 |
+
"Lee Sin", "Leona", "Lillia", "Lissandra", "Lucian", "Lulu", "Lux", "Malphite", "Malzahar", "Maokai",
|
40 |
+
"Master Yi", "Milio", "Miss Fortune", "Mordekaiser", "Morgana", "Naafiri", "Nami", "Nasus", "Nautilus",
|
41 |
+
"Neeko", "Nidalee", "Nilah", "Nocturne", "Nunu & Willump", "Olaf", "Orianna", "Ornn", "Pantheon",
|
42 |
+
"Poppy", "Pyke", "Qiyana", "Quinn", "Rakan", "Rammus", "Rek'Sai", "Rell", "Renata Glasc", "Renekton",
|
43 |
+
"Rengar", "Riven", "Rumble", "Ryze", "Samira", "Sejuani", "Senna", "Seraphine", "Sett", "Shaco",
|
44 |
+
"Shen", "Shyvana", "Singed", "Sion", "Sivir", "Skarner", "Sona", "Soraka", "Swain", "Sylas",
|
45 |
+
"Syndra", "Tahm Kench", "Taliyah", "Talon", "Taric", "Teemo", "Thresh", "Tristana", "Trundle",
|
46 |
+
"Tryndamere", "Twisted Fate", "Twitch", "Udyr", "Urgot", "Varus", "Vayne", "Veigar", "Vel'Koz",
|
47 |
+
"Vex", "Vi", "Viego", "Viktor", "Vladimir", "Volibear", "Warwick", "Wukong", "Xayah", "Xerath",
|
48 |
+
"Xin Zhao", "Yasuo", "Yone", "Yorick", "Yuumi", "Zac", "Zed", "Zeri", "Ziggs", "Zilean", "Zoe", "Zyra"
|
49 |
+
]
|
50 |
+
|
51 |
+
# Load model
|
52 |
+
try:
|
53 |
+
model_path = hf_hub_download(
|
54 |
+
repo_id="ivwhy/champion-predictor-model",
|
55 |
+
filename="champion_predictor.json"
|
56 |
+
)
|
57 |
+
model = xgb.Booster()
|
58 |
+
model.load_model(model_path)
|
59 |
+
except Exception as e:
|
60 |
+
print(f"Error loading model: {e}")
|
61 |
+
model = None
|
62 |
+
|
63 |
+
def get_user_training_df(player_opgg_url):
|
64 |
+
try:
|
65 |
+
training_df = create_app_user_training_df(player_opgg_url)
|
66 |
+
return training_df
|
67 |
+
except Exception as e:
|
68 |
+
return f"Error getting training data: {e}"
|
69 |
+
|
70 |
+
def show_stats(player_opgg_url):
|
71 |
+
"""Display player statistics and recent matches"""
|
72 |
+
if not player_opgg_url:
|
73 |
+
return "Please enter a player link to OPGG", None
|
74 |
+
|
75 |
+
try:
|
76 |
training_features = get_user_training_df(player_opgg_url)
|
77 |
+
|
78 |
+
if isinstance(training_features, str): # Error message
|
79 |
+
return training_features, None
|
80 |
|
|
|
|
|
81 |
wins = training_features['result'].sum()
|
82 |
losses = len(training_features) - wins
|
|
|
|
|
83 |
winrate = f"{(wins / len(training_features)) * 100:.0f}%"
|
|
|
|
|
84 |
favorite_champions = (
|
85 |
training_features['champion']
|
86 |
.value_counts()
|
|
|
88 |
.index.tolist()
|
89 |
)
|
90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
stats_html = f"""
|
92 |
<div style='padding: 20px; background: #f5f5f5; border-radius: 10px;'>
|
93 |
+
<h3>Player Stats</h3>
|
94 |
+
<p>Wins: {wins} | Losses: {losses}</p>
|
95 |
+
<p>Winrate: {winrate}</p>
|
96 |
+
<p>Favorite Champions: {', '.join(favorite_champions)}</p>
|
97 |
</div>
|
98 |
"""
|
99 |
|
100 |
+
return stats_html, None
|
101 |
+
except Exception as e:
|
102 |
+
return f"Error processing stats: {e}", None
|
103 |
+
|
104 |
+
def predict_champion(player_opgg_url, *champions):
|
105 |
+
"""Make prediction based on selected champions"""
|
106 |
+
if not player_opgg_url or None in champions:
|
107 |
+
return "Please fill in all fields"
|
108 |
+
|
109 |
+
try:
|
110 |
+
if model is None:
|
111 |
+
return "Model not loaded properly"
|
112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
features = get_user_training_df(player_opgg_url, champions)
|
114 |
+
if isinstance(features, str): # Error message
|
115 |
+
return features
|
116 |
+
|
117 |
prediction = model.predict(features)
|
118 |
+
predicted_champion = CHAMPIONS[prediction[0]]
|
|
|
|
|
|
|
119 |
return f"Predicted champion: {predicted_champion}"
|
120 |
+
except Exception as e:
|
121 |
+
return f"Error making prediction: {e}"
|
122 |
|
123 |
+
# Define your interface
|
124 |
+
with gr.Blocks() as demo:
|
125 |
+
gr.Markdown("# League of Legends Champion Prediction")
|
126 |
+
|
127 |
+
with gr.Row():
|
128 |
+
player_opgg_url = gr.Textbox(label="OPGG Player URL")
|
129 |
+
show_button = gr.Button("Show Stats")
|
130 |
+
|
131 |
+
with gr.Row():
|
132 |
+
stats_output = gr.HTML(label="Player Statistics")
|
133 |
+
recent_matches = gr.HTML(label="Recent Matches")
|
134 |
+
|
135 |
+
with gr.Row():
|
136 |
+
champion_dropdowns = [
|
137 |
+
gr.Dropdown(choices=CHAMPIONS, label=f"Champion {i+1}")
|
138 |
+
for i in range(9)
|
139 |
+
]
|
140 |
+
|
141 |
+
with gr.Row():
|
142 |
+
predict_button = gr.Button("Predict")
|
143 |
+
prediction_output = gr.Text(label="Prediction")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
|
145 |
+
# Set up event handlers
|
146 |
+
show_button.click(
|
147 |
+
fn=show_stats,
|
148 |
+
inputs=[player_opgg_url],
|
149 |
+
outputs=[stats_output, recent_matches]
|
150 |
+
)
|
151 |
+
|
152 |
+
predict_button.click(
|
153 |
+
fn=predict_champion,
|
154 |
+
inputs=[player_opgg_url] + champion_dropdowns,
|
155 |
+
outputs=prediction_output
|
156 |
+
)
|
157 |
|
158 |
# Add this line at the end
|
159 |
demo.queue() # Enable queuing for better handling of multiple users
|