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
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import random
|
5 |
+
import time
|
6 |
+
import os
|
7 |
+
from openai import OpenAI
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
import requests
|
10 |
+
from PIL import Image
|
11 |
+
|
12 |
+
load_dotenv("saathi.env")
|
13 |
+
|
14 |
+
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
|
15 |
+
|
16 |
+
def put_df2use(html):
|
17 |
+
df = pd.read_html(html)[0]
|
18 |
+
return df
|
19 |
+
|
20 |
+
def make_msg_chunk_ai(history,cur_tem_selection,batting_team,batting_score,chase):
|
21 |
+
#hit flask here for system message with flag
|
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 |
+
updated_system_message = system_message + f"\n Here is the senario by user \n First Batting team ->{batting_team} \n Batting Score ->{batting_score} \n Chase by second Team -> {chase} \n\n *Fantasy Team Selected By User : \n\n {cur_tem_selection.to_string(index=False)}\n\n--"
|
28 |
+
ai_msg = [{"role": "system", "content": updated_system_message}]
|
29 |
+
for chat in history:
|
30 |
+
user_m,bot_m=chat
|
31 |
+
if user_m:
|
32 |
+
val = {"role": "user", "content": user_m}
|
33 |
+
ai_msg.append(val)
|
34 |
+
if bot_m:
|
35 |
+
val = {"role": "assistant", "content": bot_m}
|
36 |
+
ai_msg.append(val)
|
37 |
+
return ai_msg
|
38 |
+
|
39 |
+
def user(user_message, history):
|
40 |
+
return "", history + [[user_message, None]]
|
41 |
+
|
42 |
+
def bot(history,cur_tem_selection,batting_team,batting_score,chase):
|
43 |
+
print(history,"====This is history")
|
44 |
+
bot_templete = make_msg_chunk_ai(history,cur_tem_selection,batting_team,batting_score,chase)
|
45 |
+
stream = client.chat.completions.create(
|
46 |
+
model="gpt-4-1106-preview",
|
47 |
+
messages=bot_templete,
|
48 |
+
stream=True)
|
49 |
+
history[-1][1] = ""
|
50 |
+
for chunk in stream:
|
51 |
+
if chunk.choices[0].delta.content is not None:
|
52 |
+
history[-1][1]+=chunk.choices[0].delta.content
|
53 |
+
yield history
|
54 |
+
|
55 |
+
|
56 |
+
def save_like_data(like_data: gr.LikeData):
|
57 |
+
print(like_data.liked,like_data.value,like_data.index)
|
58 |
+
|
59 |
+
def activate_chat(chat,user_btn,clear_btn):
|
60 |
+
return gr.update(visible=True),gr.update(visible=True),gr.update(visible=True)
|
61 |
+
|
62 |
+
def activate_full_builder_and_load_data(email):
|
63 |
+
#batting_score,chase,submit
|
64 |
+
if len(str(email))<5:
|
65 |
+
gr.Warning("Please Enter Valid Email")
|
66 |
+
return gr.update(visible=False),gr.update(visible=False),gr.update(visible=False),gr.update(visible=False),gr.update(visible=True),gr.update(visible=True)
|
67 |
+
request_params = {"email":email,"flag":os.environ.get("FLAG")}
|
68 |
+
url = os.environ["FLASK_API_ACCESS"]
|
69 |
+
|
70 |
+
response = requests.post(url, json=request_params)
|
71 |
+
batting_choices = response.json().get("teams")
|
72 |
+
print(batting_choices,"response from flask")
|
73 |
+
return gr.update(choices=batting_choices,visible=True),gr.update(visible=True),gr.update(visible=True),gr.update(visible=True),gr.update(visible=False),gr.update(visible=False)
|
74 |
+
|
75 |
+
|
76 |
+
|
77 |
+
team_name_and_team_comp_dict={}
|
78 |
+
def get_teams_data(batting_team,batting_score,chase):
|
79 |
+
# print(batting_team,"bat team",batting_score,"nan score",chase)
|
80 |
+
if batting_team and batting_score and chase and len(batting_team)>0 and len(batting_score)>0 and len(chase)>0:
|
81 |
+
req_params = {'batting_team':batting_team,
|
82 |
+
'batting_score':batting_score,
|
83 |
+
'chase':chase,
|
84 |
+
'flag':os.environ.get("FLAG")}
|
85 |
+
url = os.environ["FLASK_TEAM_BUILDER"]
|
86 |
+
gr.Warning("Making Teams")
|
87 |
+
response = requests.post(url, json=req_params)
|
88 |
+
|
89 |
+
full_data = response.json()
|
90 |
+
|
91 |
+
full_data=full_data['teams_generated']
|
92 |
+
team_name_and_team_comp_dict={}
|
93 |
+
all_teams_df =[]
|
94 |
+
for idx,team in enumerate(full_data):
|
95 |
+
team_name_and_team_comp_dict[f"Team {idx+1}"]=team
|
96 |
+
team = pd.DataFrame(team)
|
97 |
+
team["team_id"]=f"Team {idx+1}"
|
98 |
+
all_teams_df.append(team)
|
99 |
+
all_teams_df = pd.concat(all_teams_df)
|
100 |
+
all_team_name = list(team_name_and_team_comp_dict.keys())
|
101 |
+
first_df = pd.DataFrame(team_name_and_team_comp_dict[all_team_name[0]])
|
102 |
+
first_html = first_df.sample(len(first_df)).to_html(classes='ui celled table', index=False)
|
103 |
+
|
104 |
+
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
|
105 |
+
else:
|
106 |
+
gr.Warning("Please Verify Inputs")
|
107 |
+
# return gr.update(visible=False,interactive=True),gr.update(visible=False),gr.update(visible=False),gr.update(visible=False),pd.DataFrame()
|
108 |
+
return None
|
109 |
+
|
110 |
+
def pivot_role_team_with_team_names(df):
|
111 |
+
# Copying the original dataframe to not alter it
|
112 |
+
df_copy = df.copy()
|
113 |
+
|
114 |
+
# Defining the order for the role_type
|
115 |
+
role_order = ["Wicket-keeper", "Batsman", "All-rounder", "Bowler"]
|
116 |
+
|
117 |
+
# Pivoting the dataframe
|
118 |
+
pivot_df = df_copy.pivot_table(index='role_type', columns='team', aggfunc='size', fill_value=0)
|
119 |
+
|
120 |
+
# Reindex the pivot table to ensure the order of role_types
|
121 |
+
pivot_df = pivot_df.reindex(role_order)
|
122 |
+
|
123 |
+
# Adding a total row
|
124 |
+
pivot_df.loc['Total', :] = pivot_df.sum()
|
125 |
+
|
126 |
+
# Resetting index to make 'role_type' a column instead of an index and renaming it for clarity
|
127 |
+
pivot_df.reset_index(inplace=True)
|
128 |
+
pivot_df.rename(columns={'role_type': 'Role'}, inplace=True)
|
129 |
+
pivot_df.rename_axis(None, axis=1, inplace=True)
|
130 |
+
|
131 |
+
return pivot_df
|
132 |
+
|
133 |
+
def fix_hit(hit):
|
134 |
+
try:
|
135 |
+
hit = float(hit)
|
136 |
+
hit = round(hit,1)
|
137 |
+
except Exception as e:
|
138 |
+
hit = ""
|
139 |
+
return hit
|
140 |
+
|
141 |
+
|
142 |
+
def display_selected_df(team_index,all_teams_df):
|
143 |
+
temp_df = all_teams_df[all_teams_df["team_id"]==team_index]
|
144 |
+
temp_df = temp_df.drop(["team_id","player_code"],axis=1)
|
145 |
+
temp_df2 = temp_df[["player_name","role_type","team","hit"]]
|
146 |
+
temp_df = temp_df[["player_name","role_type","team"]]
|
147 |
+
disp_df = pivot_role_team_with_team_names(temp_df)
|
148 |
+
temp_df2["hit"]=temp_df2["hit"].apply(fix_hit)
|
149 |
+
temp_df2.columns=["Player Name","Role","Team","HIT%"]
|
150 |
+
# html_header = f"<h2>{team_index}</h2>"
|
151 |
+
req_html= temp_df2.sample(len(temp_df2)).to_html(classes='ui celled table', index=False)
|
152 |
+
print("changing data")
|
153 |
+
|
154 |
+
return req_html,disp_df
|
155 |
+
|
156 |
+
|
157 |
+
intro_html='''<h3> Welcome to AI-Saathi </h3>
|
158 |
+
<p>Saare fantasy problems ka solution</p>
|
159 |
+
'''
|
160 |
+
|
161 |
+
with gr.Blocks(theme=gr.themes.Default(primary_hue="amber", secondary_hue="pink")) as demo:
|
162 |
+
# gr.HTML(intro_html)
|
163 |
+
logo = Image.open("assets/IMG_0453.jpg")
|
164 |
+
gr.Image(logo)
|
165 |
+
with gr.Row():
|
166 |
+
email_input = gr.Textbox(label="Email ID",placeholder="Enter Email for access. Querries : www.apnacricketteam.com",lines=1)
|
167 |
+
with gr.Row():
|
168 |
+
activation_btn=gr.Button("Load Team Data")
|
169 |
+
|
170 |
+
with gr.Row():
|
171 |
+
batting_team = gr.Dropdown(choices=[], label="Batting Team",visible=False,interactive=True)
|
172 |
+
batting_score = gr.Dropdown(choices=["Low", "Average", "High"], label="Batting Score",visible=False,interactive=True)
|
173 |
+
chase = gr.Dropdown(choices=["Yes", "No"], label="Chased",visible=False,interactive=True)
|
174 |
+
all_teams_df = gr.DataFrame(visible=False)
|
175 |
+
with gr.Row():
|
176 |
+
submit = gr.Button("Submit",visible=False)
|
177 |
+
with gr.Row():
|
178 |
+
team_dropdown = gr.Dropdown(label="Select Team",allow_custom_value=False,multiselect=False,visible=False)
|
179 |
+
with gr.Row():
|
180 |
+
html_data = gr.HTML(visible=False)
|
181 |
+
with gr.Row():
|
182 |
+
basic_status = gr.DataFrame(label="Team Stats",visible=False)
|
183 |
+
with gr.Row():
|
184 |
+
btn = gr.Button("Team improvement with AI-Saathi",visible=False)
|
185 |
+
curr_team_data=gr.DataFrame(visible=False,interactive=False)
|
186 |
+
with gr.Row():
|
187 |
+
chat_bot = gr.Chatbot(visible=False)
|
188 |
+
thread_id = gr.Textbox(value="THIS IS THE THREAD-ID",visible=False)
|
189 |
+
with gr.Row():
|
190 |
+
user_msg = gr.Textbox(placeholder="Team ko change ya improve karne ke liye suggestion le",visible=False)
|
191 |
+
with gr.Row():
|
192 |
+
clear_btn = gr.ClearButton(chat_bot,visible=False)
|
193 |
+
|
194 |
+
activation_btn.click(fn=activate_full_builder_and_load_data,inputs=[email_input,],outputs=[batting_team,batting_score,chase,submit,email_input,activation_btn])
|
195 |
+
submit.click(fn=get_teams_data,inputs=[batting_team,batting_score,chase],outputs=[team_dropdown,html_data,basic_status,btn,all_teams_df])
|
196 |
+
team_dropdown.change(fn=display_selected_df, inputs=[team_dropdown,all_teams_df], outputs=[html_data,basic_status])
|
197 |
+
|
198 |
+
|
199 |
+
html_data.change(fn=put_df2use,inputs=html_data,outputs=curr_team_data)
|
200 |
+
|
201 |
+
btn.click(fn=activate_chat,inputs=[chat_bot,user_msg,clear_btn],outputs=[chat_bot,user_msg,clear_btn])
|
202 |
+
|
203 |
+
user_msg.submit(user, [user_msg, chat_bot], [user_msg, chat_bot], queue=True).then(
|
204 |
+
bot, [chat_bot,curr_team_data,batting_team,batting_score,chase], chat_bot)
|
205 |
+
|
206 |
+
clear_btn.click(lambda: None, None, chat_bot, queue=False)
|
207 |
+
team_dropdown.change(lambda: None, None,chat_bot)
|
208 |
+
team_dropdown.change(lambda: None, None,thread_id)
|
209 |
+
chat_bot.like(fn=save_like_data)
|
210 |
+
|
211 |
+
|
212 |
+
demo.launch(share=False)
|
213 |
+
|
214 |
+
|
215 |
+
|
assets/IMG_0453.jpg
ADDED
requirements.txt
ADDED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
aiofiles==23.2.1
|
2 |
+
altair==5.2.0
|
3 |
+
annotated-types==0.6.0
|
4 |
+
anyio==4.3.0
|
5 |
+
appnope==0.1.4
|
6 |
+
asttokens==2.4.1
|
7 |
+
attrs==23.2.0
|
8 |
+
cachetools==5.3.2
|
9 |
+
certifi==2024.2.2
|
10 |
+
charset-normalizer==3.3.2
|
11 |
+
click==8.1.7
|
12 |
+
colorama==0.4.6
|
13 |
+
comm==0.2.1
|
14 |
+
contourpy==1.2.0
|
15 |
+
cycler==0.12.1
|
16 |
+
debugpy==1.8.1
|
17 |
+
decorator==5.1.1
|
18 |
+
distro==1.9.0
|
19 |
+
exceptiongroup==1.2.0
|
20 |
+
executing==2.0.1
|
21 |
+
fastapi==0.110.0
|
22 |
+
ffmpy==0.3.2
|
23 |
+
filelock==3.13.1
|
24 |
+
fonttools==4.49.0
|
25 |
+
fsspec==2024.2.0
|
26 |
+
google-api-core==2.17.1
|
27 |
+
google-api-python-client==2.119.0
|
28 |
+
google-auth==2.28.1
|
29 |
+
google-auth-httplib2==0.2.0
|
30 |
+
google-auth-oauthlib==1.2.0
|
31 |
+
googleapis-common-protos==1.62.0
|
32 |
+
gradio==4.19.2
|
33 |
+
gradio_client==0.10.1
|
34 |
+
h11==0.14.0
|
35 |
+
httpcore==1.0.4
|
36 |
+
httplib2==0.22.0
|
37 |
+
httpx==0.27.0
|
38 |
+
huggingface-hub==0.20.3
|
39 |
+
idna==3.6
|
40 |
+
importlib-metadata==7.0.1
|
41 |
+
importlib-resources==6.1.1
|
42 |
+
ipykernel==6.29.2
|
43 |
+
ipython==8.18.1
|
44 |
+
jedi==0.19.1
|
45 |
+
Jinja2==3.1.3
|
46 |
+
jsonschema==4.21.1
|
47 |
+
jsonschema-specifications==2023.12.1
|
48 |
+
jupyter_client==8.6.0
|
49 |
+
jupyter_core==5.7.1
|
50 |
+
kiwisolver==1.4.5
|
51 |
+
lxml==5.1.0
|
52 |
+
markdown-it-py==3.0.0
|
53 |
+
MarkupSafe==2.1.5
|
54 |
+
matplotlib==3.8.3
|
55 |
+
matplotlib-inline==0.1.6
|
56 |
+
mdurl==0.1.2
|
57 |
+
nest-asyncio==1.6.0
|
58 |
+
numpy==1.26.4
|
59 |
+
oauthlib==3.2.2
|
60 |
+
openai==1.13.3
|
61 |
+
orjson==3.9.15
|
62 |
+
packaging==23.2
|
63 |
+
pandas==2.2.1
|
64 |
+
parso==0.8.3
|
65 |
+
pexpect==4.9.0
|
66 |
+
pillow==10.2.0
|
67 |
+
platformdirs==4.2.0
|
68 |
+
prompt-toolkit==3.0.43
|
69 |
+
protobuf==4.25.3
|
70 |
+
psutil==5.9.8
|
71 |
+
ptyprocess==0.7.0
|
72 |
+
pure-eval==0.2.2
|
73 |
+
pyasn1==0.5.1
|
74 |
+
pyasn1-modules==0.3.0
|
75 |
+
pydantic==2.6.2
|
76 |
+
pydantic_core==2.16.3
|
77 |
+
pydub==0.25.1
|
78 |
+
Pygments==2.17.2
|
79 |
+
pygsheets==2.0.6
|
80 |
+
pyparsing==3.1.1
|
81 |
+
python-dateutil==2.8.2
|
82 |
+
python-dotenv==1.0.1
|
83 |
+
python-multipart==0.0.9
|
84 |
+
pytz==2024.1
|
85 |
+
PyYAML==6.0.1
|
86 |
+
pyzmq==25.1.2
|
87 |
+
referencing==0.33.0
|
88 |
+
requests==2.31.0
|
89 |
+
requests-oauthlib==1.3.1
|
90 |
+
rich==13.7.0
|
91 |
+
rpds-py==0.18.0
|
92 |
+
rsa==4.9
|
93 |
+
ruff==0.2.2
|
94 |
+
semantic-version==2.10.0
|
95 |
+
shellingham==1.5.4
|
96 |
+
six==1.16.0
|
97 |
+
sniffio==1.3.0
|
98 |
+
stack-data==0.6.3
|
99 |
+
starlette==0.36.3
|
100 |
+
tomlkit==0.12.0
|
101 |
+
toolz==0.12.1
|
102 |
+
tornado==6.4
|
103 |
+
tqdm==4.66.2
|
104 |
+
traitlets==5.14.1
|
105 |
+
typer==0.9.0
|
106 |
+
typing_extensions==4.9.0
|
107 |
+
tzdata==2024.1
|
108 |
+
uritemplate==4.1.1
|
109 |
+
urllib3==2.2.1
|
110 |
+
uvicorn==0.27.1
|
111 |
+
wcwidth==0.2.13
|
112 |
+
websockets==11.0.3
|
113 |
+
zipp==3.17.0
|