ApnaCricketTeam commited on
Commit
ceb4776
1 Parent(s): dc2c23f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -9
app.py CHANGED
@@ -82,6 +82,42 @@ def activate_full_builder_and_load_data(email):
82
 
83
 
84
  team_name_and_team_comp_dict={}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  def get_teams_data(batting_team,batting_score,chase):
86
  # print(batting_team,"bat team",batting_score,"nan score",chase)
87
  if batting_team and batting_score and chase and len(batting_team)>0 and len(batting_score)>0 and len(chase)>0:
@@ -93,7 +129,6 @@ def get_teams_data(batting_team,batting_score,chase):
93
  url = os.environ["FLASK_TEAM_BUILDER"]
94
  gr.Warning("Making Teams")
95
  response = requests.post(url, json=req_params)
96
-
97
  full_data = response.json()
98
 
99
  full_data=full_data['teams_generated']
@@ -104,13 +139,18 @@ def get_teams_data(batting_team,batting_score,chase):
104
  team = pd.DataFrame(team)
105
  team = team.drop_duplicates(["player_code"],ignore_index=True)
106
  team["team_id"]=f"Team {idx+1}"
 
107
  all_teams_df.append(team)
108
  all_teams_df = pd.concat(all_teams_df)
 
109
  all_team_name = list(team_name_and_team_comp_dict.keys())
110
  first_df = pd.DataFrame(team_name_and_team_comp_dict[all_team_name[0]])
111
- first_html = first_df.sample(len(first_df)).to_html(classes='ui celled table', index=False)
 
 
 
112
 
113
- return gr.update(choices=all_team_name,value=all_team_name[0],visible=True,interactive=True),gr.update(value=first_html,visible=True),gr.update(visible=True),gr.update(visible=True),all_teams_df
114
  else:
115
  gr.Warning("Please Verify Inputs")
116
  # return gr.update(visible=False,interactive=True),gr.update(visible=False),gr.update(visible=False),gr.update(visible=False),pd.DataFrame()
@@ -157,10 +197,13 @@ def display_selected_df(team_index,all_teams_df):
157
  temp_df2["hit"]=temp_df2["hit"].apply(fix_hit)
158
  temp_df2.columns=["Player Name","Role","Team","HIT%"]
159
  # html_header = f"<h2>{team_index}</h2>"
160
- req_html= temp_df2.sample(len(temp_df2)).to_html(classes='ui celled table', index=False)
 
161
  print("changing data")
162
 
163
  return req_html,disp_df
 
 
164
 
165
 
166
  intro_html='''<h3> Welcome to AI-Saathi </h3>
@@ -177,12 +220,14 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="amber", secondary_hue="pink"
177
  activation_btn=gr.Button("Load Team Data")
178
 
179
  with gr.Row():
180
- batting_team = gr.Dropdown(choices=[], label="Batting Team",visible=False,interactive=True)
181
- batting_score = gr.Dropdown(choices=["Low (1st innings < 145)", "Average (1st innings 145-170)", "High (1st innings >170)"],label="Batting Score 1st Innings",visible=False,interactive=True)
182
- chase = gr.Dropdown(choices=["Yes", "No"], label="2nd Innings Chased",visible=False,interactive=True)
183
  all_teams_df = gr.DataFrame(visible=False)
184
  with gr.Row():
185
  submit = gr.Button("Submit",visible=False)
 
 
186
  with gr.Row():
187
  team_dropdown = gr.Dropdown(label="Select Team",allow_custom_value=False,multiselect=False,visible=False)
188
  with gr.Row():
@@ -201,7 +246,7 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="amber", secondary_hue="pink"
201
  clear_btn = gr.ClearButton(chat_bot,visible=False)
202
 
203
  activation_btn.click(fn=activate_full_builder_and_load_data,inputs=[email_input,],outputs=[batting_team,batting_score,chase,submit,email_input,activation_btn])
204
- submit.click(fn=get_teams_data,inputs=[batting_team,batting_score,chase],outputs=[team_dropdown,html_data,basic_status,btn,all_teams_df])
205
  team_dropdown.change(fn=display_selected_df, inputs=[team_dropdown,all_teams_df], outputs=[html_data,basic_status])
206
 
207
 
@@ -218,7 +263,7 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="amber", secondary_hue="pink"
218
  chat_bot.like(fn=save_like_data)
219
 
220
  demo.queue(default_concurrency_limit=3)
221
- demo.launch(share=False)
222
 
223
 
224
 
 
82
 
83
 
84
  team_name_and_team_comp_dict={}
85
+
86
+ def display_generated_teams_df(df):
87
+ # Create a pivot table counting the number of players by team and team_id
88
+ pivot_df = pd.pivot_table(df, index='team_id', columns='team', aggfunc='size', fill_value=0)
89
+
90
+ # Reset index to make 'team_id' a column again and ensure a consistent DataFrame structure
91
+ pivot_df = pivot_df.reset_index()
92
+
93
+ # Renaming the columns if needed, for instance, to map internal team names to display names
94
+ # This can be skipped or adjusted based on your specific naming requirements
95
+ # Example: pivot_df.columns = ['Team No', 'PKBS', 'RCB']
96
+
97
+ # Adjust 'team_id' column name to 'Team No'
98
+ pivot_df.rename(columns={'team_id': 'Team No'}, inplace=True)
99
+ pivot_df["sorter"]=pivot_df['Team No'].apply(lambda x: eval(x.lower().replace("team","").strip()))
100
+ pivot_df = pivot_df.sort_values(["sorter"])
101
+ pivot_df.drop(["sorter"],inplace=True,axis=1)
102
+
103
+ return pivot_df
104
+
105
+ def sort_dataframe_by_role(df):
106
+ """
107
+ Sorts the DataFrame by the 'Role' column according to a custom order.
108
+
109
+ Parameters:
110
+ - df: The DataFrame to be sorted.
111
+ - role_order: A list specifying the order in which roles should be sorted.
112
+
113
+ Returns:
114
+ - A DataFrame sorted by the 'Role' column according to the specified order.
115
+ """
116
+ role_order=["Wicket-keeper", "Batsman", "All-rounder", "Bowler"]
117
+ df['Role'] = pd.Categorical(df['Role'], categories=role_order, ordered=True)
118
+ sorted_df = df.sort_values('Role')
119
+ return sorted_df
120
+
121
  def get_teams_data(batting_team,batting_score,chase):
122
  # print(batting_team,"bat team",batting_score,"nan score",chase)
123
  if batting_team and batting_score and chase and len(batting_team)>0 and len(batting_score)>0 and len(chase)>0:
 
129
  url = os.environ["FLASK_TEAM_BUILDER"]
130
  gr.Warning("Making Teams")
131
  response = requests.post(url, json=req_params)
 
132
  full_data = response.json()
133
 
134
  full_data=full_data['teams_generated']
 
139
  team = pd.DataFrame(team)
140
  team = team.drop_duplicates(["player_code"],ignore_index=True)
141
  team["team_id"]=f"Team {idx+1}"
142
+ team["hit"]=team["hit"].apply(lambda x: round(float(x),1))
143
  all_teams_df.append(team)
144
  all_teams_df = pd.concat(all_teams_df)
145
+ display_teams_df = display_generated_teams_df(all_teams_df)
146
  all_team_name = list(team_name_and_team_comp_dict.keys())
147
  first_df = pd.DataFrame(team_name_and_team_comp_dict[all_team_name[0]])
148
+ first_df=first_df[["player_name","role_type","team","hit"]]
149
+ first_df.columns = ["Player Name","Role","Team","HIT%"]
150
+ first_df = sort_dataframe_by_role(first_df)
151
+ first_html = first_df.to_html(classes='ui celled table', index=False)
152
 
153
+ return gr.update(choices=all_team_name,value=all_team_name[0],visible=True,interactive=True),gr.update(value=first_html,visible=True),gr.update(visible=True),gr.update(visible=True),all_teams_df,gr.update(value=display_teams_df,visible=True)
154
  else:
155
  gr.Warning("Please Verify Inputs")
156
  # return gr.update(visible=False,interactive=True),gr.update(visible=False),gr.update(visible=False),gr.update(visible=False),pd.DataFrame()
 
197
  temp_df2["hit"]=temp_df2["hit"].apply(fix_hit)
198
  temp_df2.columns=["Player Name","Role","Team","HIT%"]
199
  # html_header = f"<h2>{team_index}</h2>"
200
+ temp_df2 = sort_dataframe_by_role(temp_df2)
201
+ req_html= temp_df2.to_html(classes='ui celled table', index=False)
202
  print("changing data")
203
 
204
  return req_html,disp_df
205
+
206
+
207
 
208
 
209
  intro_html='''<h3> Welcome to AI-Saathi </h3>
 
220
  activation_btn=gr.Button("Load Team Data")
221
 
222
  with gr.Row():
223
+ batting_team = gr.Radio(choices=[], label="Which team will bat 1st?",visible=False,interactive=True)
224
+ batting_score = gr.Radio(choices=["Low (1st innings < 145)", "Average (1st innings 145-180)", "High (1st innings >180)"],label="What will be 1st innings score?",visible=False,interactive=True)
225
+ chase = gr.Radio(choices=["Yes", "No"], label="Will it be chased?",visible=False,interactive=True)
226
  all_teams_df = gr.DataFrame(visible=False)
227
  with gr.Row():
228
  submit = gr.Button("Submit",visible=False)
229
+ with gr.Row():
230
+ teams_info_disp=gr.DataFrame(visible=False,height=300)
231
  with gr.Row():
232
  team_dropdown = gr.Dropdown(label="Select Team",allow_custom_value=False,multiselect=False,visible=False)
233
  with gr.Row():
 
246
  clear_btn = gr.ClearButton(chat_bot,visible=False)
247
 
248
  activation_btn.click(fn=activate_full_builder_and_load_data,inputs=[email_input,],outputs=[batting_team,batting_score,chase,submit,email_input,activation_btn])
249
+ submit.click(fn=get_teams_data,inputs=[batting_team,batting_score,chase],outputs=[team_dropdown,html_data,basic_status,btn,all_teams_df,teams_info_disp])
250
  team_dropdown.change(fn=display_selected_df, inputs=[team_dropdown,all_teams_df], outputs=[html_data,basic_status])
251
 
252
 
 
263
  chat_bot.like(fn=save_like_data)
264
 
265
  demo.queue(default_concurrency_limit=3)
266
+ demo.launch(share=True)
267
 
268
 
269