Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
from xgboost import Booster, DMatrix
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
# Define the mapping of card names to IDs (placeholder example)
|
7 |
+
card_numbers = {
|
8 |
+
"Card 1": 1,
|
9 |
+
"Card 2": 2,
|
10 |
+
"Card 3": 3,
|
11 |
+
"Card 4": 4,
|
12 |
+
"Card 5": 5,
|
13 |
+
"Card 6": 6,
|
14 |
+
"Card 7": 7,
|
15 |
+
"Card 8": 8,
|
16 |
+
# Add all 181 cards here...
|
17 |
+
}
|
18 |
+
|
19 |
+
MODEL_PATH = "clash_royale_model/model.json"
|
20 |
+
|
21 |
+
def deck_to_ids(deck, mapping):
|
22 |
+
"""Convert card names to IDs based on the mapping."""
|
23 |
+
return [mapping.get(card, 0) - 1 for card in deck] # Zero-based indices
|
24 |
+
|
25 |
+
def preprocess_deck(deck):
|
26 |
+
"""Prepare the selected deck for the model."""
|
27 |
+
# Convert cards to IDs
|
28 |
+
deck_ids = deck_to_ids(deck, card_numbers)
|
29 |
+
|
30 |
+
# Perform one-hot encoding
|
31 |
+
num_choices = 181 # Total number of cards
|
32 |
+
one_hot = np.zeros(num_choices, dtype=int)
|
33 |
+
one_hot[np.array(deck_ids)] = 1 # Set 1 for selected cards
|
34 |
+
|
35 |
+
# Add additional features (placeholder for now)
|
36 |
+
trophy_difference = 0 # Placeholder for trophy difference
|
37 |
+
elixir_leaked = 0 # Placeholder for leaked elixir
|
38 |
+
|
39 |
+
# Combine features
|
40 |
+
features = np.concatenate(([trophy_difference, elixir_leaked], one_hot))
|
41 |
+
return pd.DataFrame([features])
|
42 |
+
|
43 |
+
def load_model(model_path):
|
44 |
+
"""Load the saved XGBoost model."""
|
45 |
+
model = Booster()
|
46 |
+
model.load_model(model_path)
|
47 |
+
return model
|
48 |
+
|
49 |
+
# Load the model at startup
|
50 |
+
model = load_model(MODEL_PATH)
|
51 |
+
|
52 |
+
def predict_outcome(opponent_deck):
|
53 |
+
"""Make a prediction based on the opponent's deck."""
|
54 |
+
# Prepare the opponent deck data
|
55 |
+
deck_data = preprocess_deck(opponent_deck)
|
56 |
+
|
57 |
+
# Make the prediction
|
58 |
+
dmatrix = DMatrix(deck_data) # Convert data to DMatrix format
|
59 |
+
prediction = model.predict(dmatrix)
|
60 |
+
|
61 |
+
# Interpret the prediction
|
62 |
+
result = f"Probability of Winning: {prediction[0] * 100:.2f}%"
|
63 |
+
return result
|
64 |
+
|
65 |
+
# List of cards for selection
|
66 |
+
card_list = list(card_numbers.keys())
|
67 |
+
|
68 |
+
# Create the Gradio interface
|
69 |
+
with gr.Blocks() as interface:
|
70 |
+
gr.Markdown("## Clash Royale Prediction")
|
71 |
+
gr.Markdown("Select the 8 cards from the opponent's deck to predict the probability of winning!")
|
72 |
+
|
73 |
+
opponent_deck = gr.CheckboxGroup(
|
74 |
+
choices=card_list,
|
75 |
+
label="Select 8 cards from the opponent's deck:",
|
76 |
+
info="Select exactly 8 cards."
|
77 |
+
)
|
78 |
+
|
79 |
+
result = gr.Textbox(label="Prediction Result:", interactive=False)
|
80 |
+
|
81 |
+
def validate_and_predict(deck):
|
82 |
+
"""Validate the number of selected cards and make a prediction."""
|
83 |
+
if len(deck) != 8:
|
84 |
+
return "Error: Select exactly 8 cards."
|
85 |
+
return predict_outcome(deck)
|
86 |
+
|
87 |
+
predict_button = gr.Button("Make Prediction")
|
88 |
+
predict_button.click(validate_and_predict, inputs=[opponent_deck], outputs=[result])
|
89 |
+
|
90 |
+
# Launch the interface
|
91 |
+
interface.launch()
|