File size: 4,385 Bytes
3c67330 73e1103 3c67330 73e1103 3c67330 73e1103 3c67330 73e1103 3c67330 73e1103 3c67330 73e1103 3c67330 73e1103 3c67330 73e1103 3c67330 |
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 |
# app.py
import gradio as gr
import pandas as pd
import requests
import xgboost as xgb
from huggingface_hub import hf_hub_download
# Download the model from Hugging Face Hub
model_path = hf_hub_download(
repo_id="YOUR_USERNAME/YOUR_MODEL_REPO", # Replace with your model repo
filename="model.json" # Replace with your model filename
)
model = xgb.Booster()
model.load_model(model_path)
# Rest of your code remains the same as before, but remove demo.launch()
# Define your interface
with gr.Blocks() as demo:
# Assuming you have these helper functions implemented
def get_player_stats(player_name):
"""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_name):
"""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_name, champions):
"""Prepare features for model prediction"""
# Placeholder - implement actual feature engineering
features = [] # Transform champions into model features
return pd.DataFrame([features])
# Load the model from Hugging Face
model = xgb.Booster() # Initialize model
# model.load_model("path_to_your_model") # Load your model
# Define champion list for dropdowns
CHAMPIONS = [
"Aatrox", "Ahri", "Akali", "Alistar", "Amumu",
# Add more champions...
]
def show_stats(player_name):
"""Display player statistics and recent matches"""
if not player_name:
return "Please enter a player name", None
stats = get_player_stats(player_name)
recent = get_recent_matches(player_name)
stats_html = f"""
<div style='padding: 20px; background: #f5f5f5; border-radius: 10px;'>
<h3>Player Stats: {player_name}</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, recent.to_html(index=False)
def predict_champion(player_name, *champions):
"""Make prediction based on selected champions"""
if not player_name or None in champions:
return "Please fill in all fields"
# Prepare features
features = prepare_features(player_name, 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_name = gr.Textbox(label="Player Name")
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_name],
outputs=[stats_output, recent_matches]
)
predict_button.click(
fn=predict_champion,
inputs=[player_name] + champion_dropdowns,
outputs=prediction_output
)
# Add this line at the end
demo.queue() # Enable queuing for better handling of multiple users
# .gitignore
"""
__pycache__/
*.py[cod]
*$py.class
.env
.venv
env/
venv/
.DS_Store
""" |