Spaces:
Sleeping
Sleeping
Commit
·
301d4ae
1
Parent(s):
a7caf70
claude save me
Browse files
app.py
CHANGED
@@ -1,72 +1,121 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
|
|
|
|
3 |
import sys
|
|
|
4 |
sys.path.append("rd2l_pred")
|
5 |
from training_data_prep import list_format, modification, league_money, df_gen
|
6 |
from feature_engineering import heroes, hero_information
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import onnxruntime as ort
|
5 |
import sys
|
6 |
+
from pathlib import Path
|
7 |
sys.path.append("rd2l_pred")
|
8 |
from training_data_prep import list_format, modification, league_money, df_gen
|
9 |
from feature_engineering import heroes, hero_information
|
10 |
|
11 |
+
# Load the ONNX model
|
12 |
+
model_path = Path("model/rd2l_predictor.onnx")
|
13 |
+
session = ort.InferenceSession(model_path)
|
14 |
+
|
15 |
+
def process_player_data(player_id, mmr, comf_1, comf_2, comf_3, comf_4, comf_5):
|
16 |
+
"""Process player data similar to training pipeline"""
|
17 |
+
try:
|
18 |
+
# Clean player ID from URL if needed
|
19 |
+
if "/" in player_id:
|
20 |
+
player_id = player_id.split("/")[-1]
|
21 |
+
|
22 |
+
# Create initial player series
|
23 |
+
player_data = {
|
24 |
+
"player_id": player_id,
|
25 |
+
"mmr": float(mmr),
|
26 |
+
"p1": int(comf_1),
|
27 |
+
"p2": int(comf_2),
|
28 |
+
"p3": int(comf_3),
|
29 |
+
"p4": int(comf_4),
|
30 |
+
"p5": int(comf_5)
|
31 |
+
}
|
32 |
+
|
33 |
+
# Get hero statistics using OpenDota API
|
34 |
+
try:
|
35 |
+
hero_stats = hero_information(player_id)
|
36 |
+
# Merge hero stats with player data
|
37 |
+
player_data.update(hero_stats.to_dict())
|
38 |
+
except Exception as e:
|
39 |
+
return f"Error fetching hero data: {str(e)}"
|
40 |
+
|
41 |
+
# Convert to DataFrame for consistency with training
|
42 |
+
df = pd.DataFrame([player_data])
|
43 |
+
|
44 |
+
# Ensure columns match training data
|
45 |
+
required_columns = [col.name for col in session.get_inputs()[0].type.tensor_type.shape.dim]
|
46 |
+
for col in required_columns:
|
47 |
+
if col not in df.columns:
|
48 |
+
df[col] = 0
|
49 |
+
|
50 |
+
# Reorder columns to match model input
|
51 |
+
df = df[required_columns]
|
52 |
+
|
53 |
+
return df
|
54 |
+
except Exception as e:
|
55 |
+
return f"Error processing player data: {str(e)}"
|
56 |
+
|
57 |
+
def predict_cost(user_id, mmr, comf_1, comf_2, comf_3, comf_4, comf_5):
|
58 |
+
"""Main prediction function for Gradio interface"""
|
59 |
+
try:
|
60 |
+
# Process input data
|
61 |
+
processed_data = process_player_data(user_id, mmr, comf_1, comf_2, comf_3, comf_4, comf_5)
|
62 |
+
|
63 |
+
if isinstance(processed_data, str): # Error occurred
|
64 |
+
return processed_data
|
65 |
+
|
66 |
+
# Make prediction
|
67 |
+
input_name = session.get_inputs()[0].name
|
68 |
+
prediction = session.run(None, {input_name: processed_data.values.astype(np.float32)})[0]
|
69 |
+
|
70 |
+
# Format prediction
|
71 |
+
predicted_cost = round(float(prediction[0]), 2)
|
72 |
+
|
73 |
+
return f"""Predicted Cost: {predicted_cost}
|
74 |
+
|
75 |
+
Player Details:
|
76 |
+
- MMR: {mmr}
|
77 |
+
- Position Comfort:
|
78 |
+
* Pos 1: {comf_1}
|
79 |
+
* Pos 2: {comf_2}
|
80 |
+
* Pos 3: {comf_3}
|
81 |
+
* Pos 4: {comf_4}
|
82 |
+
* Pos 5: {comf_5}
|
83 |
+
|
84 |
+
Note: This prediction is based on historical data and player statistics from OpenDota."""
|
85 |
+
|
86 |
+
except Exception as e:
|
87 |
+
return f"Error making prediction: {str(e)}"
|
88 |
+
|
89 |
+
# Create Gradio interface
|
90 |
+
demo = gr.Interface(
|
91 |
+
fn=predict_cost,
|
92 |
+
inputs=[
|
93 |
+
gr.Textbox(label="Player ID or Link to OpenDota/Dotabuff",
|
94 |
+
placeholder="Enter player ID or full profile URL"),
|
95 |
+
gr.Number(label="MMR", value=3000),
|
96 |
+
gr.Slider(1, 5, value=3, step=1, label="Comfort (Pos 1)"),
|
97 |
+
gr.Slider(1, 5, value=3, step=1, label="Comfort (Pos 2)"),
|
98 |
+
gr.Slider(1, 5, value=3, step=1, label="Comfort (Pos 3)"),
|
99 |
+
gr.Slider(1, 5, value=3, step=1, label="Comfort (Pos 4)"),
|
100 |
+
gr.Slider(1, 5, value=3, step=1, label="Comfort (Pos 5)")
|
101 |
+
],
|
102 |
+
outputs=gr.Textbox(label="Prediction Results"),
|
103 |
+
title="RD2L Player Cost Predictor",
|
104 |
+
description="""This tool predicts the auction cost for RD2L players based on their MMR,
|
105 |
+
position comfort levels, and historical performance data from OpenDota.
|
106 |
+
Enter a player's OpenDota ID or profile URL along with their current stats
|
107 |
+
to get a predicted cost.""",
|
108 |
+
article="""### How it works
|
109 |
+
- The predictor uses machine learning trained on historical RD2L draft data
|
110 |
+
- Player statistics are fetched from OpenDota API
|
111 |
+
- Position comfort levels range from 1 (least comfortable) to 5 (most comfortable)
|
112 |
+
- Predictions are based on both current stats and historical performance
|
113 |
+
|
114 |
+
### Notes
|
115 |
+
- MMR should be the player's current solo MMR
|
116 |
+
- Position comfort should reflect actual role experience
|
117 |
+
- Predictions are estimates and may vary from actual draft results"""
|
118 |
+
)
|
119 |
+
|
120 |
+
if __name__ == "__main__":
|
121 |
+
demo.launch()
|