import gradio as gr import pandas as pd import numpy as np import os import random from sklearn.metrics.pairwise import cosine_similarity # --- SAFE IMPORT BLOCK --- # Prevents crash if advanced ML libraries are missing try: import joblib from sentence_transformers import SentenceTransformer from huggingface_hub import hf_hub_download LIBRARIES_AVAILABLE = True except ImportError: LIBRARIES_AVAILABLE = False print("⚠️ Warning: Advanced ML libraries missing. Running in Simulation Mode.") # ========================================== # 1. LOAD MODELS & DATA (FAIL-SAFE) # ========================================== print("🔄 System Startup...") # Global Variables MODE = "SIMULATION" PREDICTION_MODEL = None EMBEDDINGS = None EMBEDDING_MODEL = None DATASET = None # 1.1 Attempt to load real AI models if LIBRARIES_AVAILABLE: try: print("⏳ Attempting to load AI Models...") # Load Embedder EMBEDDING_MODEL = SentenceTransformer('all-MiniLM-L6-v2') # Check and load local files if os.path.exists("play_embeddings.npy") and os.path.exists("playticker_engine.pkl"): EMBEDDINGS = np.load("play_embeddings.npy") PREDICTION_MODEL = joblib.load("playticker_engine.pkl") MODE = "PRODUCTION (AI-POWERED)" print("✅ SUCCESS: Real AI Models Loaded.") else: print("⚠️ Files not found on disk. Switching to Logic Fallback.") except Exception as e: print(f"❌ Error loading models: {e}") print("⚠️ Switching to Logic Fallback.") # 1.2 Attempt to load dataset (CSV) try: if LIBRARIES_AVAILABLE: # Attempt to download from Repo dataset_path = hf_hub_download( repo_id="meirnm13/playticker-nba-momentum", filename="synthetic_nba_momentum_data.csv", repo_type="dataset" ) DATASET = pd.read_csv(dataset_path) print(f"✅ Loaded Dataset: {len(DATASET)} rows.") else: raise Exception("No libraries") except Exception as e: print(f"⚠️ Could not load CSV ({e}). Generating dummy data.") DATASET = pd.DataFrame({ 'play_description': [ "LeBron James throws down a monster dunk", "Stephen Curry hits a deep three pointer", "Timeout called by the Miami Heat", "Turnover by James Harden", "Giannis blocks the shot" ] * 20, 'player_name': ["LeBron James", "Stephen Curry", "Team", "James Harden", "Giannis Antetokounmpo"] * 20, 'team': ["Lakers", "Warriors", "Heat", "Clippers", "Bucks"] * 20, 'momentum_score': [0.85, 0.92, -0.1, -0.6, 0.75] * 20 }) # ========================================== # 2. LOGIC ENGINES (HYBRID) # ========================================== def heuristic_logic(text): text = str(text).lower() score = 0.0 if any(x in text for x in ["dunk", "3-pointer", "win", "clutch", "lead", "steal", "block"]): score = random.uniform(0.6, 0.95) elif any(x in text for x in ["miss", "turnover", "foul", "lose", "brick", "timeout"]): score = random.uniform(-0.7, -0.3) else: score = random.uniform(-0.2, 0.2) return score def predict_momentum(user_input_text): if not user_input_text: return "", 0.0 momentum_score = 0.0 if MODE.startswith("PRODUCTION") and PREDICTION_MODEL is not None: try: embedding = EMBEDDING_MODEL.encode([user_input_text]) momentum_score = PREDICTION_MODEL.predict(embedding.reshape(1, -1))[0] if isinstance(momentum_score, (str, int, np.integer)): mapping = {0: -0.8, 1: 0.0, 2: 0.8} momentum_score = mapping.get(momentum_score, 0.5) except: momentum_score = heuristic_logic(user_input_text) else: momentum_score = heuristic_logic(user_input_text) # Visualization if momentum_score > 0.4: category, color, icon = "HIGH POSITIVE", "#10b981", "🔥" elif momentum_score > 0.1: category, color, icon = "POSITIVE", "#22c55e", "✅" elif momentum_score < -0.4: category, color, icon = "HIGH NEGATIVE", "#ef4444", "🛑" else: category, color, icon = "NEUTRAL", "#6b7280", "⚖️" html_output = f"""
System Status: {MODE}