File size: 3,086 Bytes
c8ec91c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b5f7fb
 
 
 
 
c8ec91c
 
 
 
6b5f7fb
 
 
 
 
 
 
c8ec91c
 
 
6b5f7fb
c8ec91c
 
 
 
 
 
 
6b5f7fb
c8ec91c
6b5f7fb
c8ec91c
6b5f7fb
 
 
 
 
c8ec91c
6b5f7fb
c8ec91c
 
 
 
6b5f7fb
c8ec91c
 
 
 
 
 
 
 
 
6b5f7fb
c8ec91c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import os
import streamlit as st
import pandas as pd
from src.data_sources import list_live_matches, scorecard, extract_simple_context, striker_form_last_overs
from src.features import candidates_from_scorecard, PHASES
from src.strategy_agent import StrategyContext, suggest_strategy

st.set_page_config(page_title="Cricket Strategy Agent (MVP)", page_icon="🏏", layout="wide")
st.title("🏏 Agentic Cricket Strategy (Real-Time MVP)")

with st.sidebar:
    st.markdown("### Settings")
    st.markdown("**Requires**: `GROQ_API_KEY` (sidebar → Space secrets).")
    phase = st.selectbox("Phase", PHASES, index=1)

st.subheader("Live Matches")

# --- FIX: always convert to DataFrame ---
matches = list_live_matches()
df = pd.DataFrame(matches)

if df.empty:
    st.info("No live/preview matches found right now, or API unreachable. Try later.")
    st.stop()

# Select a match
match_row = st.selectbox(
    "Select a match",
    df.to_dict("records"),
    format_func=lambda r: f"{r.get('title', 'Unknown')} ({r.get('state', 'N/A')})"
)
mid = str(match_row.get("match_id"))

with st.spinner("Fetching scorecard..."):
    sc = scorecard(mid)

ctx = extract_simple_context(sc)
if not ctx:
    st.error("Could not parse scorecard for this match.")
    st.stop()

col1, col2, col3 = st.columns(3)
with col1:
    batting_team = st.selectbox("Batting", [ctx.get("batting_team", "Unknown")])
with col2:
    bowling_team = st.selectbox("Bowling", [ctx.get("bowling_team", "Unknown")])
with col3:
    st.metric(
        "Score / Wkts (Overs)",
        f"{ctx.get('score', 0)}-{ctx.get('wickets', 0)} ({ctx.get('overs', 0)})",
        help=f"RR: {ctx.get('rr', 0)} | CRR: {ctx.get('crr', 0)}"
    )

batsmen = ctx.get("players", {}).get("batsmen", [])
if not batsmen:
    st.warning("Batsmen list unavailable. Proceeding with defaults.")
striker = st.selectbox("Striker", batsmen if batsmen else ["Unknown"])

bowlers = candidates_from_scorecard(ctx.get("players", {}))
cand_bowlers = st.multiselect("Candidate Bowlers (next over)", options=bowlers, default=bowlers)

# Simple ‘recent form’ snapshot for striker
recent = striker_form_last_overs(sc, striker)

# Assemble context and call agent
run = st.button("🧠 Generate Strategy", type="primary")
if run:
    sctx = StrategyContext(
        match_title=match_row.get("title", "Unknown Match"),
        phase=phase,
        striker=striker,
        non_striker=(batsmen[1] if len(batsmen) > 1 and batsmen[0] == striker else (batsmen[0] if batsmen else "")),
        batting_team=batting_team,
        bowling_team=bowling_team,
        candidate_bowlers=cand_bowlers,
        striker_recent=recent,
        innings_state={
            "score": ctx.get("score"),
            "wickets": ctx.get("wickets"),
            "overs": ctx.get("overs"),
            "rr": ctx.get("rr"),
            "crr": ctx.get("crr")
        }
    )
    with st.spinner("Thinking with LLaMA-3 (Groq)…"):
        plan = suggest_strategy(sctx)
    st.markdown("### 📋 Strategy")
    st.write(plan)
    st.caption("Model: Groq • LLaMA-3 (fast inference)")