import os import pandas as pd import streamlit as st # AI & Database Imports from langchain_core.documents import Document from langchain_community.vectorstores import FAISS from langchain_huggingface import HuggingFaceEndpointEmbeddings # ------------------------------- # 0. CONFIG & STYLING # ------------------------------- st.set_page_config( page_title="IPL 2026 Insight AI", page_icon="🏏", layout="wide", initial_sidebar_state="collapsed" ) # --- IPL 2026 THEME FONTS & CSS --- st.markdown(""" """, unsafe_allow_html=True) # ------------------------------- # 1. DATA LOADING & PROCESSING # ------------------------------- @st.cache_data(ttl=3600) def load_data(): csv_files = [ "batting_stats.csv", "bowling_stats.csv", "deliveries.csv", "fielding_stats.csv", "matches.csv", "points_table.csv", "squads.csv", "venues.csv" ] dataframes = {} with st.spinner('Loading Match Data...'): for file_name in csv_files: if os.path.exists(file_name): key_name = file_name.replace(".csv", "") try: dataframes[key_name] = pd.read_csv(file_name) except Exception as e: print(f"Error: {e}") return dataframes def process_all_tables_into_sentences(all_tables): sentences = [] if "matches" in all_tables: df = all_tables["matches"] for _, row in df.iterrows(): winner = row.get('match_winner', 'No result') venue = row.get('venue', 'Unknown stadium') m_id = row.get('match_id', 'N/A') text = f"In IPL 2026 Match {m_id} at {venue}, {winner} won the match" if 'wb_runs' in row and row['wb_runs'] > 0: text += f" by {row['wb_runs']} runs." elif 'wb_wickets' in row and row['wb_wickets'] > 0: text += f" by {row['wb_wickets']} wickets." else: text += "." sentences.append(text) if "batting_stats" in all_tables: df = all_tables["batting_stats"] for _, row in df.iterrows(): sentences.append( f"Player {row.get('player_name')} scored {row.get('runs')} runs in a match, " f"with a strike rate of {row.get('strike_rate')} and hit {row.get('fours', 0)} fours and {row.get('sixes', 0)} sixes." ) if "bowling_stats" in all_tables: df = all_tables["bowling_stats"] for _, row in df.iterrows(): sentences.append( f"Bowler {row.get('player_name')} took {row.get('wickets')} wickets while conceding {row.get('runs_conceded')} runs, " f"with an economy rate of {row.get('economy')}." ) if "points_table" in all_tables: df = all_tables["points_table"] for _, row in df.iterrows(): sentences.append( f"In the IPL 2026 points table, team {row.get('team_name')} is ranked at position {row.get('position')} " f"with {row.get('points')} points, having won {row.get('won')} matches and lost {row.get('lost')}." ) if "squads" in all_tables: df = all_tables["squads"] for _, row in df.iterrows(): sentences.append( f"Player {row.get('player_name')} plays for team {row.get('team_name')} as a {row.get('role', 'player')} in IPL 2026." ) if "venues" in all_tables: df = all_tables["venues"] for _, row in df.iterrows(): sentences.append( f"The stadium {row.get('venue')} is located in the city of {row.get('city', 'India')}." ) if "deliveries" in all_tables: df = all_tables["deliveries"].head(500) for _, row in df.iterrows(): sentences.append( f"In Match {row.get('match_id')}, over {row.get('over_num')}, ball {row.get('ball_num')}: " f"Bowler {row.get('bowler')} bowled to batsman {row.get('batsman')}, resulting in {row.get('total_runs')} runs." ) if "fielding_stats" in all_tables: df = all_tables["fielding_stats"] for _, row in df.iterrows(): sentences.append( f"Fielder {row.get('player_name')} took {row.get('catches', 0)} catches and made {row.get('stumpings', 0)} stumpings." ) return sentences # ------------------------------- # 2. VECTOR DATABASE BUILDER # ------------------------------- @st.cache_resource def get_vector_db(): all_tables = load_data() if not all_tables: return None sentences = process_all_tables_into_sentences(all_tables) if not sentences: return None docs = [Document(page_content=s) for s in sentences] hf_token = os.getenv("HF_TOKEN") if not hf_token: st.error("Missing HF_TOKEN") return None with st.spinner('Building Vector Index...'): embeddings = HuggingFaceEndpointEmbeddings( model="sentence-transformers/all-MiniLM-L6-v2", task="feature-extraction", huggingfacehub_api_token=hf_token ) vector_db = FAISS.from_documents(docs, embeddings) return vector_db # ------------------------------- # 3. MAIN APPLICATION # ------------------------------- # Header st.markdown("""
Official Smart Insight Assistant