Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -162,6 +162,46 @@ def get_batting_avg_column(df):
|
|
| 162 |
if col in df.columns:
|
| 163 |
return col
|
| 164 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
# ---- Main Content ----
|
| 167 |
if option == "Team Info" and team_info:
|
|
|
|
| 162 |
if col in df.columns:
|
| 163 |
return col
|
| 164 |
return None
|
| 165 |
+
# Sidebar Query Agent (LLM-based Stats Assistant)
|
| 166 |
+
with st.sidebar:
|
| 167 |
+
st.markdown("---")
|
| 168 |
+
st.markdown("### 🤖 Ask CricketStatBot")
|
| 169 |
+
|
| 170 |
+
show_input = st.button("Start Query")
|
| 171 |
+
|
| 172 |
+
if show_input:
|
| 173 |
+
user_query = st.text_input("Ask a question about batting or bowling stats:", key="agent_query")
|
| 174 |
+
|
| 175 |
+
if user_query:
|
| 176 |
+
# Combine all dataframes into a context string
|
| 177 |
+
def df_to_text(df, name, max_rows=100):
|
| 178 |
+
return f"{name} Data:\n" + df.head(max_rows).to_csv(index=False)
|
| 179 |
+
|
| 180 |
+
context = (
|
| 181 |
+
df_to_text(batting_df, "Batting") + "\n" +
|
| 182 |
+
df_to_text(bowling_df, "Bowling") + "\n" +
|
| 183 |
+
df_to_text(odi_df, "ODI") + "\n" +
|
| 184 |
+
df_to_text(t20_df, "T20") + "\n" +
|
| 185 |
+
df_to_text(test_df, "Test") + "\n" +
|
| 186 |
+
df_to_text(odi_teams_df, "ODI Teams") + "\n" +
|
| 187 |
+
df_to_text(t20_teams_df, "T20 Teams") + "\n" +
|
| 188 |
+
df_to_text(test_teams_df, "Test Teams")
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
# Agent Prompt
|
| 192 |
+
agent_prompt = ChatPromptTemplate.from_messages([
|
| 193 |
+
("system",
|
| 194 |
+
"You are a cricket analytics assistant. Use the below data to answer cricket-related questions in a detailed and insightful manner:\n\n{context}"),
|
| 195 |
+
("human", "{question}")
|
| 196 |
+
])
|
| 197 |
+
agent_chain = agent_prompt | model | out_par
|
| 198 |
+
|
| 199 |
+
with st.spinner("Analyzing your question..."):
|
| 200 |
+
agent_response = agent_chain.invoke({"context": context, "question": user_query})
|
| 201 |
+
|
| 202 |
+
st.markdown("#### 🧠 CricketStatBot Answer")
|
| 203 |
+
st.markdown(f"<div class='card'>{agent_response}</div>", unsafe_allow_html=True)
|
| 204 |
+
|
| 205 |
|
| 206 |
# ---- Main Content ----
|
| 207 |
if option == "Team Info" and team_info:
|