Spaces:
Sleeping
Sleeping
Download src/streamlit_app.py from ajaykumar0712/Streamlit-Testing: direct link, hf CLI and curl.
- Browser
- Download file 9.79 kB
-
https://huggingface.co/spaces/ajaykumar0712/Streamlit-Testing/resolve/main/src/streamlit_app.py
- Command line
-
hf download hf://spaces/ajaykumar0712/Streamlit-Testing/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/ajaykumar0712/Streamlit-Testing/resolve/main/src/streamlit_app.py
9.79 kB
| import streamlit as st | |
| import pandas as pd | |
| import plotly.graph_objects as go | |
| import plotly.express as px | |
| import time | |
| from agents import run_agents, detect_intent | |
| from probability_engine import elo_probability | |
| from data_loader import teams, team_map | |
| # ========================= | |
| # LOAD DATA | |
| # ========================= | |
| batters = pd.read_csv("src/data/batters.csv") | |
| bowlers = pd.read_csv("src/data/bowlers.csv") | |
| teams = pd.read_csv("src/data/teams.csv") | |
| venues = pd.read_csv("src/data/venue.csv") | |
| # ========================= | |
| # PAGE CONFIG | |
| # ========================= | |
| st.set_page_config(layout="wide") | |
| st.title("π Cricket World Cup AI Panel") | |
| # ========================= | |
| # MAIN LAYOUT | |
| # ========================= | |
| left, right = st.columns([4,6]) | |
| # ========================= | |
| # LEFT SIDE CHAT | |
| # ========================= | |
| with left: | |
| st.header("Ask the AI Panel") | |
| # Initialize variables | |
| stats_output = "" | |
| strategist_output = "" | |
| chaos_output = "" | |
| compere_output = "" | |
| agent_outputs = None | |
| teamA = None | |
| teamB = None | |
| probA = None | |
| probB = None | |
| query = st.text_input("Example: IND vs AUS") | |
| if query: | |
| # words = query.upper().split() | |
| # teamA = words[0] | |
| # teamB = words[2] | |
| intent_data = detect_intent(query) | |
| st.write("DEBUG:", intent_data) | |
| intent = intent_data.get("intent") | |
| teamA = intent_data.get("teamA") | |
| teamB = intent_data.get("teamB") | |
| group = intent_data.get("group") | |
| venue = intent_data.get("venue") | |
| # Create impression of thinking and a spinning icon | |
| # with st.spinner("Consulting the AI panel..."): | |
| # Safety net to check rate limit error | |
| max_retries = 3 | |
| for attempt in range(max_retries): | |
| try: | |
| if intent == "match_prediction": | |
| if not teamA or not teamB: | |
| st.error("β Could not detect both teams. Try: India vs Australia") | |
| st.stop() | |
| agent_outputs = run_agents("match", teamA, teamB) | |
| probs = elo_probability(teamA, teamB) | |
| probA = probs["probA"] | |
| probB = probs["probB"] | |
| elif intent == "group_progression": | |
| if not group: | |
| st.error("Please specify a group (e.g. Group D)") | |
| break | |
| agent_outputs = run_agents("group", group = group) | |
| # no probability here | |
| probA = None | |
| probB = None | |
| elif intent == "tournament_prediction": | |
| agent_outputs = run_agents("tournament", intent_data.get("tournament_team")) | |
| probA = None | |
| probB = None | |
| else: | |
| st.error("Could not understand query") | |
| break | |
| break | |
| except Exception as e: | |
| if "RateLimit" in str(e): | |
| wait = 10 + attempt * 5 | |
| st.warning(f"Rate limit hit. Waiting {wait} seconds...") | |
| time.sleep(wait) | |
| else: | |
| raise e | |
| # stats_output = agent_outputs["stats"] | |
| # strategist_output = agent_outputs["strategist"] | |
| # chaos_output = agent_outputs["chaos"] | |
| # compere_output = agent_outputs["compere"] | |
| if isinstance(agent_outputs, dict): | |
| stats_output = agent_outputs.get("stats", "") | |
| strategist_output = agent_outputs.get("strategist", "") | |
| chaos_output = agent_outputs.get("chaos", "") | |
| compere_output = agent_outputs.get("compere", "") | |
| elif isinstance(agent_outputs, str): | |
| # fallback if agents return raw string | |
| stats_output = "" | |
| strategist_output = "" | |
| chaos_output = "" | |
| compere_output = agent_outputs | |
| else: | |
| stats_output = "" | |
| strategist_output = "" | |
| chaos_output = "" | |
| compere_output = "" | |
| # Run probabilities | |
| probs = elo_probability(teamA, teamB) | |
| probA = probs["probA"] | |
| probB = probs["probB"] | |
| # st.write("### Panel Verdict") | |
| # st.write(compere_output) | |
| # ========================= | |
| # RIGHT SIDE TABS | |
| # ========================= | |
| with right: | |
| tab1, tab2 = st.tabs(["Agent Panel", "Insights"]) | |
| # ========================= | |
| # TAB 1 : AGENT PANEL | |
| # ========================= | |
| with tab1: | |
| st.subheader("Expert Debate Panel") | |
| # explanation box | |
| st.info("π§ Our AI expert panel debates the match before announcing the final prediction.") | |
| col1, col2 = st.columns(2) | |
| col3, col4 = st.columns(2) | |
| stat_placeholder = col1.empty() | |
| strat_placeholder = col2.empty() | |
| chaos_placeholder = col3.empty() | |
| compere_placeholder = col4.empty() | |
| # Statistician | |
| with stat_placeholder.container(): | |
| with st.spinner("Statistician analyzing numbers..."): | |
| time.sleep(1.5) | |
| st.markdown("### π Statistician") | |
| st.write(stats_output) | |
| # Strategist | |
| with strat_placeholder.container(): | |
| with st.spinner("Strategist planning tactics..."): | |
| time.sleep(1.5) | |
| st.markdown("### π§ Strategist") | |
| st.write(strategist_output) | |
| # Chaos agent | |
| with chaos_placeholder.container(): | |
| with st.spinner("Chaos agent considering unpredictable factors..."): | |
| time.sleep(1.5) | |
| st.markdown("### π² Chaos Agent") | |
| st.write(chaos_output) | |
| # Compere | |
| with compere_placeholder.container(): | |
| with st.spinner("Compere summarizing the debate..."): | |
| time.sleep(1.5) | |
| st.markdown("### π€ Compere") | |
| st.write(compere_output) | |
| # ========================= | |
| # TAB 2 : INSIGHTS | |
| # ========================= | |
| with tab2: | |
| if query and intent == "match_prediction": | |
| st.subheader("Win Probability") | |
| # SPEEDOMETER | |
| fig = go.Figure(go.Indicator( | |
| mode="gauge+number", | |
| value=probA*100, | |
| title={'text': f"{teamA} Win Probability"}, | |
| gauge={ | |
| 'axis': {'range': [0,100]}, | |
| 'bar': {'color': "darkblue"}, | |
| 'steps': [ | |
| {'range': [0,40], 'color': "red"}, | |
| {'range': [40,60], 'color': "orange"}, | |
| {'range': [60,100], 'color': "green"} | |
| ] | |
| } | |
| )) | |
| st.plotly_chart(fig, use_container_width=True) | |
| # ========================= | |
| # BATTER COMPARISON | |
| # ========================= | |
| st.subheader("Batting Comparison") | |
| teamA_bat = team_map.get(teamA.upper(), teamA.upper()) if teamA else None | |
| teamB_bat = team_map.get(teamB.upper(), teamB.upper()) if teamB else None | |
| team_batters = batters[ | |
| batters["team"].str.upper().isin([teamA_bat.upper(), teamB_bat.upper()]) | |
| ] | |
| fig2 = px.bar( | |
| team_batters, | |
| x="player", | |
| y="strike_rate", | |
| color="team", | |
| title="Strike Rate Comparison" | |
| ) | |
| st.plotly_chart(fig2, use_container_width=True) | |
| # ========================= | |
| # BOWLING COMPARISON | |
| # ========================= | |
| st.subheader("Bowling Comparison") | |
| teamA_bowl = team_map.get(teamA.upper(), teamA.upper()) | |
| teamB_bowl = team_map.get(teamB.upper(), teamB.upper()) | |
| team_bowlers = bowlers[ | |
| bowlers["team"].str.upper().isin([teamA_bowl.upper(), teamB_bowl.upper()]) | |
| ] | |
| fig3 = px.bar( | |
| team_bowlers, | |
| x="player", | |
| y="economy", | |
| color="team", | |
| title="Economy Rate Comparison" | |
| ) | |
| st.plotly_chart(fig3, use_container_width=True) | |
| # ########################################################################################## | |
| # ########################################################################################## | |
| # import streamlit as st | |
| # st.set_page_config(page_title="Hello Chat App", layout="wide") | |
| # # --- Sidebar --- | |
| # st.sidebar.title("Controls") | |
| # theme = st.sidebar.selectbox("Theme (demo)", ["Light", "Dark"]) | |
| # st.sidebar.info("This is the sidebar area.") | |
| # # --- Main layout panels --- | |
| # st.title("Streamlit Chat Input Demo") | |
| # left, right = st.columns([2, 1]) | |
| # with left: | |
| # st.subheader("Chat Panel") | |
| # # session state to store messages | |
| # if "messages" not in st.session_state: | |
| # st.session_state.messages = [ | |
| # {"role": "assistant", "content": "Hi! Type anything β I always say hello."} | |
| # ] | |
| # # display chat history | |
| # for msg in st.session_state.messages: | |
| # with st.chat_message(msg["role"]): | |
| # st.write(msg["content"]) | |
| # # chat input | |
| # user_text = st.chat_input("Enter free text here...") | |
| # if user_text: | |
| # st.session_state.messages.append({"role": "user", "content": user_text}) | |
| # st.session_state.messages.append({"role": "assistant", "content": "hello"}) | |
| # # re-render | |
| # st.rerun() | |
| # with right: | |
| # st.subheader("Info Panel") | |
| # st.write("Sidebar selection:", theme) | |
| # st.write("Messages so far:", len(st.session_state.messages)) | |
| # st.success("Bot response is always: hello β ") |