File size: 7,855 Bytes
abcb943 |
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# app.py
import gradio as gr
import pandas as pd
import requests
import xgboost as xgb
from huggingface_hub import hf_hub_download
from Recent_match_scrapper import get_multiple_matches_stats
from Meta_scrapper import get_meta_stats
from Leaderboard_scrapper import scrape_leaderboards
from connection_check import check_connection
from helper import merge_stats, filter_leaderboard, get_player_list
from Player_scrapper import get_multiple_player_stats, get_player_stats
from feature_eng import create_champion_features
from Weekly_meta_scrapper import get_weekly_meta
from app_training_df_getter import create_app_user_training_df
from sklearn.metrics import top_k_accuracy_score
import os
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd
from helper import format_summoner_name
# Download the model from Hugging Face Hub
model_path = hf_hub_download(
repo_id="ivwhy/champion-predictor-model", # Replace with your model repo
filename="champion_predictor.json" # Replace with your model filename
)
model = xgb.Booster()
model.load_model(model_path)
# Define your interface
with gr.Blocks() as demo:
# Assuming you have these helper functions implemented
def get_user_training_df(player_opgg_url):
training_df = create_app_user_training_df(player_opgg_url)
return training_df
# Load the model from Hugging Face
model = xgb.Booster() # Initialize model
model.load_model("ivwhy/champion-predictor-model") # Load your model
# Define champion list for dropdowns
CHAMPIONS = [
"Aatrox", "Ahri", "Akali", "Akshan", "Alistar", "Amumu", "Anivia", "Annie", "Aphelios", "Ashe",
"Aurelion Sol", "Azir", "Bard", "Bel'Veth", "Blitzcrank", "Brand", "Braum", "Caitlyn", "Camille",
"Cassiopeia", "Cho'Gath", "Corki", "Darius", "Diana", "Dr. Mundo", "Draven", "Ekko", "Elise",
"Evelynn", "Ezreal", "Fiddlesticks", "Fiora", "Fizz", "Galio", "Gangplank", "Garen", "Gnar",
"Gragas", "Graves", "Gwen", "Hecarim", "Heimerdinger", "Illaoi", "Irelia", "Ivern", "Janna",
"Jarvan IV", "Jax", "Jayce", "Jhin", "Jinx", "Kai'Sa", "Kalista", "Karma", "Karthus", "Kassadin",
"Katarina", "Kayle", "Kayn", "Kennen", "Kha'Zix", "Kindred", "Kled", "Kog'Maw", "KSante", "LeBlanc",
"Lee Sin", "Leona", "Lillia", "Lissandra", "Lucian", "Lulu", "Lux", "Malphite", "Malzahar", "Maokai",
"Master Yi", "Milio", "Miss Fortune", "Mordekaiser", "Morgana", "Naafiri", "Nami", "Nasus", "Nautilus",
"Neeko", "Nidalee", "Nilah", "Nocturne", "Nunu & Willump", "Olaf", "Orianna", "Ornn", "Pantheon",
"Poppy", "Pyke", "Qiyana", "Quinn", "Rakan", "Rammus", "Rek'Sai", "Rell", "Renata Glasc", "Renekton",
"Rengar", "Riven", "Rumble", "Ryze", "Samira", "Sejuani", "Senna", "Seraphine", "Sett", "Shaco",
"Shen", "Shyvana", "Singed", "Sion", "Sivir", "Skarner", "Sona", "Soraka", "Swain", "Sylas",
"Syndra", "Tahm Kench", "Taliyah", "Talon", "Taric", "Teemo", "Thresh", "Tristana", "Trundle",
"Tryndamere", "Twisted Fate", "Twitch", "Udyr", "Urgot", "Varus", "Vayne", "Veigar", "Vel'Koz",
"Vex", "Vi", "Viego", "Viktor", "Vladimir", "Volibear", "Warwick", "Wukong", "Xayah", "Xerath",
"Xin Zhao", "Yasuo", "Yone", "Yorick", "Yuumi", "Zac", "Zed", "Zeri", "Ziggs", "Zilean", "Zoe", "Zyra"
]
def show_stats(player_opgg_url):
"""Display player statistics and recent matches"""
if not player_opgg_url:
return "Please enter a player link to OPGG", None
training_features = get_user_training_df(player_opgg_url)
# Assume `training_features` is the DataFrame provided
# Calculate total wins and losses
wins = training_features['result'].sum()
losses = len(training_features) - wins
# Calculate winrate
winrate = f"{(wins / len(training_features)) * 100:.0f}%"
# Calculate favorite champions
favorite_champions = (
training_features['champion']
.value_counts()
.head(3)
.index.tolist()
)
# Create the summary dictionary
summary_data = {
'wins': wins,
'losses': losses,
'winrate': winrate,
'favorite_champions': favorite_champions
}
stats_html = f"""
<div style='padding: 20px; background: #f5f5f5; border-radius: 10px;'>
<h3>Player Stats: {player_opgg_url}</h3>
<p>Wins: {stats['wins']} | Losses: {stats['losses']}</p>
<p>Winrate: {stats['winrate']}</p>
<p>Favorite Champions: {', '.join(stats['favorite_champions'])}</p>
</div>
"""
return stats_html
def predict_champion(player_opgg_url, *champions):
"""Make prediction based on selected champions"""
if not player_opgg_url or None in champions:
return "Please fill in all fields"
# Prepare features
features = get_user_training_df(player_opgg_url, champions)
# Make prediction
prediction = model.predict(features)
# Get predicted champion name
predicted_champion = CHAMPIONS[prediction[0]] # Adjust based on your model output
return f"Predicted champion: {predicted_champion}"
# Create Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# League of Legends Champion Prediction")
with gr.Row():
player_opgg_url = gr.Textbox(label="OPGG Player URL")
show_button = gr.Button("Show Stats")
with gr.Row():
stats_output = gr.HTML(label="Player Statistics")
recent_matches = gr.HTML(label="Recent Matches")
with gr.Row():
champion_dropdowns = [
gr.Dropdown(choices=CHAMPIONS, label=f"Champion {i+1}")
for i in range(9)
]
with gr.Row():
predict_button = gr.Button("Predict")
prediction_output = gr.Text(label="Prediction")
# Set up event handlers
show_button.click(
fn=show_stats,
inputs=[player_opgg_url],
outputs=[stats_output, recent_matches]
)
predict_button.click(
fn=predict_champion,
inputs=[player_opgg_url] + champion_dropdowns,
outputs=prediction_output
)
# Add this line at the end
demo.queue() # Enable queuing for better handling of multiple users
''' code graveyard
def get_player_stats(player_opgg_url):
"""Get player statistics from API"""
# Placeholder - implement actual API call
return {
'wins': 120,
'losses': 80,
'winrate': '60%',
'favorite_champions': ['Ahri', 'Zed', 'Yasuo']
}
def get_recent_matches(player_opgg_url):
"""Get recent match history"""
# Placeholder - implement actual API call
return pd.DataFrame({
'champion': ['Ahri', 'Zed', 'Yasuo'],
'result': ['Win', 'Loss', 'Win'],
'kda': ['8/2/10', '4/5/3', '12/3/7']
})
def prepare_features(player_opgg_url, champions):
"""Prepare features for model prediction"""
# Placeholder - implement actual feature engineering
features = [] # Transform champions into model features
return pd.DataFrame([features])
''' |