nick-leland commited on
Commit
301d4ae
·
1 Parent(s): a7caf70

claude save me

Browse files
Files changed (1) hide show
  1. app.py +114 -65
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
- def fetch_data(user_id, mmr, comf_1, comf_2, comf_3, comf_4, comf_5):
12
- player_id = user_id.split("/")[-1]
13
-
14
- series = {"player_id" : player_id, "mmr" : mmr, "p1" : comf_1, "p2" : comf_2, "p3" : comf_3, "p4" : comf_4, "p5" : comf_5}
15
-
16
- money = pd.read_csv("result_money.csv")
17
-
18
- print()
19
- print(f"Reading player {player_id}. Starting now")
20
-
21
- print(money)
22
- print(money.values)
23
-
24
- for item in money.values:
25
- series.update({item[0] : float(item[1])})
26
-
27
- print("Corrected Series")
28
- print(series)
29
-
30
- d = pd.Series(series)
31
- print(d)
32
-
33
- # This the original section used to add the money section to the series.
34
- # d = d.assign(count=lambda x: money[player_season].loc['count'], mean=lambda x: money[player_season].loc['mean'], std=lambda x: money[player_season].loc['std'], min=lambda x: money[player_season].loc['min'], max=lambda x: money[player_season].loc['max'], sum=lambda x: money[player_season].loc['sum'])
35
-
36
- # We need to generate the inputs for the sheet using hugging face
37
- # We also need to load the money values from the generated csv file
38
- # df_gen(draft, league_money(captains, data_type), data_type)
39
-
40
- print(f"Done reading player {player_id}")
41
- print()
42
-
43
- return player_id
44
-
45
- demo = gr.Interface.load("https://huggingface.co/nick-leland/RD2L_Random_Forest")
46
-
47
- # demo = gr.Interface(
48
- # fn=fetch_data,
49
- # inputs=[
50
- # gr.Textbox(label="Player ID or Link to OpenDota/Dotabuff"),
51
- # gr.Textbox(label="MMR"),
52
- # gr.Slider(1, 5, value=1, step=1, label="Comfort (Pos 1)"),
53
- # gr.Slider(1, 5, value=1, step=1, label="Comfort (Pos 2)"),
54
- # gr.Slider(1, 5, value=1, step=1, label="Comfort (Pos 3)"),
55
- # gr.Slider(1, 5, value=1, step=1, label="Comfort (Pos 4)"),
56
- # gr.Slider(1, 5, value=1, step=1, label="Comfort (Pos 5)")
57
- # ],
58
- # # examples=[
59
- # # [np.asarray(Image.open("examples/1500_maze.jpg")), "Bulge", True, 0.25, 0.5, 0.5, 0.5],
60
- # # [np.asarray(Image.open("examples/2048_maze.jpg")), "Bulge", True, 0.25, 0.5, 0.5, 0.5],
61
- # # [np.asarray(Image.open("examples/2300_fresh.jpg")), "Bulge", True, 0.25, 0.5, 0.5, 0.5],
62
- # # [np.asarray(Image.open("examples/50_fresh.jpg")), "Bulge", True, 0.25, 0.5, 0.5, 0.5]
63
- # # ],
64
- # outputs=[
65
- # "text"
66
- # ],
67
- # title="RD2L Pricing Prediction",
68
- # article="Uhhhhh this is the article",
69
- # description="Uhhhhh this is the description"
70
- # )
71
- demo.launch()
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()