Spaces:
Runtime error
Runtime error
ApnaCricketTeam
commited on
Commit
•
d10d4a1
1
Parent(s):
c35b73f
Update app.py
Browse files
app.py
CHANGED
@@ -22,10 +22,9 @@ def make_msg_chunk_ai(history,team_dropdown,all_teams_df,batting_team,batting_sc
|
|
22 |
req_params = {'flag':os.environ.get("FLAG")}
|
23 |
url = os.environ["FLASK_AI_PROMPT"]
|
24 |
response = requests.post(url, json=req_params)
|
25 |
-
print(response)
|
26 |
-
print(response.json())
|
27 |
full_data = response.json()
|
28 |
system_message=full_data['ai_data']
|
|
|
29 |
temp_df = all_teams_df[all_teams_df["team_id"]==team_dropdown]
|
30 |
temp_df = temp_df.drop(["team_id","player_code"],axis=1)
|
31 |
cur_tem_selection = temp_df[["player_name","role_type","team"]]
|
@@ -45,8 +44,9 @@ def user(user_message, history):
|
|
45 |
return "", history + [[user_message, None]]
|
46 |
|
47 |
def bot(history,team_dropdown,all_teams_df,batting_team,batting_score,chase):
|
48 |
-
print(history,"====This is history")
|
49 |
bot_templete = make_msg_chunk_ai(history,team_dropdown,all_teams_df,batting_team,batting_score,chase)
|
|
|
50 |
stream = client.chat.completions.create(
|
51 |
model="gpt-4-1106-preview",
|
52 |
messages=bot_templete,
|
@@ -55,6 +55,8 @@ def bot(history,team_dropdown,all_teams_df,batting_team,batting_score,chase):
|
|
55 |
for chunk in stream:
|
56 |
if chunk.choices[0].delta.content is not None:
|
57 |
history[-1][1]+=chunk.choices[0].delta.content
|
|
|
|
|
58 |
yield history
|
59 |
|
60 |
|
@@ -80,9 +82,46 @@ def activate_full_builder_and_load_data(email):
|
|
80 |
|
81 |
|
82 |
team_name_and_team_comp_dict={}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
def get_teams_data(batting_team,batting_score,chase):
|
84 |
# print(batting_team,"bat team",batting_score,"nan score",chase)
|
85 |
if batting_team and batting_score and chase and len(batting_team)>0 and len(batting_score)>0 and len(chase)>0:
|
|
|
86 |
req_params = {'batting_team':batting_team,
|
87 |
'batting_score':batting_score,
|
88 |
'chase':chase,
|
@@ -90,7 +129,6 @@ def get_teams_data(batting_team,batting_score,chase):
|
|
90 |
url = os.environ["FLASK_TEAM_BUILDER"]
|
91 |
gr.Warning("Making Teams")
|
92 |
response = requests.post(url, json=req_params)
|
93 |
-
|
94 |
full_data = response.json()
|
95 |
|
96 |
full_data=full_data['teams_generated']
|
@@ -99,14 +137,20 @@ def get_teams_data(batting_team,batting_score,chase):
|
|
99 |
for idx,team in enumerate(full_data):
|
100 |
team_name_and_team_comp_dict[f"Team {idx+1}"]=team
|
101 |
team = pd.DataFrame(team)
|
|
|
102 |
team["team_id"]=f"Team {idx+1}"
|
|
|
103 |
all_teams_df.append(team)
|
104 |
all_teams_df = pd.concat(all_teams_df)
|
|
|
105 |
all_team_name = list(team_name_and_team_comp_dict.keys())
|
106 |
first_df = pd.DataFrame(team_name_and_team_comp_dict[all_team_name[0]])
|
107 |
-
|
|
|
|
|
|
|
108 |
|
109 |
-
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
|
110 |
else:
|
111 |
gr.Warning("Please Verify Inputs")
|
112 |
# return gr.update(visible=False,interactive=True),gr.update(visible=False),gr.update(visible=False),gr.update(visible=False),pd.DataFrame()
|
@@ -153,10 +197,13 @@ def display_selected_df(team_index,all_teams_df):
|
|
153 |
temp_df2["hit"]=temp_df2["hit"].apply(fix_hit)
|
154 |
temp_df2.columns=["Player Name","Role","Team","HIT%"]
|
155 |
# html_header = f"<h2>{team_index}</h2>"
|
156 |
-
|
|
|
157 |
print("changing data")
|
158 |
|
159 |
return req_html,disp_df
|
|
|
|
|
160 |
|
161 |
|
162 |
intro_html='''<h3> Welcome to AI-Saathi </h3>
|
@@ -173,12 +220,14 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="amber", secondary_hue="pink"
|
|
173 |
activation_btn=gr.Button("Load Team Data")
|
174 |
|
175 |
with gr.Row():
|
176 |
-
batting_team = gr.
|
177 |
-
batting_score = gr.
|
178 |
-
chase = gr.
|
179 |
all_teams_df = gr.DataFrame(visible=False)
|
180 |
with gr.Row():
|
181 |
submit = gr.Button("Submit",visible=False)
|
|
|
|
|
182 |
with gr.Row():
|
183 |
team_dropdown = gr.Dropdown(label="Select Team",allow_custom_value=False,multiselect=False,visible=False)
|
184 |
with gr.Row():
|
@@ -197,11 +246,11 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="amber", secondary_hue="pink"
|
|
197 |
clear_btn = gr.ClearButton(chat_bot,visible=False)
|
198 |
|
199 |
activation_btn.click(fn=activate_full_builder_and_load_data,inputs=[email_input,],outputs=[batting_team,batting_score,chase,submit,email_input,activation_btn])
|
200 |
-
submit.click(fn=get_teams_data,inputs=[batting_team,batting_score,chase],outputs=[team_dropdown,html_data,basic_status,btn,all_teams_df])
|
201 |
team_dropdown.change(fn=display_selected_df, inputs=[team_dropdown,all_teams_df], outputs=[html_data,basic_status])
|
202 |
|
203 |
|
204 |
-
html_data.change(fn=put_df2use,inputs=html_data,outputs=curr_team_data)
|
205 |
|
206 |
btn.click(fn=activate_chat,inputs=[chat_bot,user_msg,clear_btn],outputs=[chat_bot,user_msg,clear_btn])
|
207 |
|
@@ -213,8 +262,8 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="amber", secondary_hue="pink"
|
|
213 |
team_dropdown.change(lambda: None, None,thread_id)
|
214 |
chat_bot.like(fn=save_like_data)
|
215 |
|
216 |
-
|
217 |
-
demo.launch(share=
|
218 |
|
219 |
|
220 |
|
|
|
22 |
req_params = {'flag':os.environ.get("FLAG")}
|
23 |
url = os.environ["FLASK_AI_PROMPT"]
|
24 |
response = requests.post(url, json=req_params)
|
|
|
|
|
25 |
full_data = response.json()
|
26 |
system_message=full_data['ai_data']
|
27 |
+
print("Got Ai Data : ",system_message,"\n====SYSTEM MESSAGE")
|
28 |
temp_df = all_teams_df[all_teams_df["team_id"]==team_dropdown]
|
29 |
temp_df = temp_df.drop(["team_id","player_code"],axis=1)
|
30 |
cur_tem_selection = temp_df[["player_name","role_type","team"]]
|
|
|
44 |
return "", history + [[user_message, None]]
|
45 |
|
46 |
def bot(history,team_dropdown,all_teams_df,batting_team,batting_score,chase):
|
47 |
+
print(len(history),"====This is history")
|
48 |
bot_templete = make_msg_chunk_ai(history,team_dropdown,all_teams_df,batting_team,batting_score,chase)
|
49 |
+
print("Made bot templete")
|
50 |
stream = client.chat.completions.create(
|
51 |
model="gpt-4-1106-preview",
|
52 |
messages=bot_templete,
|
|
|
55 |
for chunk in stream:
|
56 |
if chunk.choices[0].delta.content is not None:
|
57 |
history[-1][1]+=chunk.choices[0].delta.content
|
58 |
+
time.sleep(0.09)
|
59 |
+
print(history)
|
60 |
yield history
|
61 |
|
62 |
|
|
|
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:
|
124 |
+
batting_score=batting_score.split("(")[0].strip()
|
125 |
req_params = {'batting_team':batting_team,
|
126 |
'batting_score':batting_score,
|
127 |
'chase':chase,
|
|
|
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']
|
|
|
137 |
for idx,team in enumerate(full_data):
|
138 |
team_name_and_team_comp_dict[f"Team {idx+1}"]=team
|
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 |
|
253 |
+
# html_data.change(fn=put_df2use,inputs=html_data,outputs=curr_team_data)
|
254 |
|
255 |
btn.click(fn=activate_chat,inputs=[chat_bot,user_msg,clear_btn],outputs=[chat_bot,user_msg,clear_btn])
|
256 |
|
|
|
262 |
team_dropdown.change(lambda: None, None,thread_id)
|
263 |
chat_bot.like(fn=save_like_data)
|
264 |
|
265 |
+
demo.queue(default_concurrency_limit=3)
|
266 |
+
demo.launch(share=True)
|
267 |
|
268 |
|
269 |
|