import gradio as gr import torch from transformers import pipeline import numpy as np import random # Initialize models with verified Zero GPU compatible models def init_models(): global text_generator, stats_analyzer try: text_generator = pipeline( "text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0", device=0 if torch.cuda.is_available() else -1 ) stats_analyzer = pipeline( "text-classification", model="distilbert-base-uncased", device=0 if torch.cuda.is_available() else -1 ) return True except Exception as e: print(f"Error initializing models: {str(e)}") return False SPORTS_CATEGORIES = { "Football/Soccer": { "positions": ["Forward", "Midfielder", "Defender", "Goalkeeper"], "stats": ["Goals", "Assists", "Passes", "Tackles", "Clean Sheets"], "legends": ["Pelรฉ", "Maradona", "Cruyff", "Beckenbauer", "Yashin"], "emoji": "โฝ", "achievements": { "Pelรฉ": ["3x World Cup", "1279 Goals", "Santos Legend"], "Maradona": ["World Cup 1986", "Napoli Hero", "Golden Ball"], "Cruyff": ["3x Ballon d'Or", "Total Football", "Barcelona Legend"], "Beckenbauer": ["2x World Cup", "Der Kaiser", "Bayern Legend"], "Yashin": ["Only GK Ballon d'Or", "Black Spider", "Clean Sheet King"] } }, "Basketball": { "positions": ["Point Guard", "Shooting Guard", "Small Forward", "Power Forward", "Center"], "stats": ["Points", "Rebounds", "Assists", "Blocks", "Steals"], "legends": ["Jordan", "Magic", "Bird", "Russell", "Kareem"], "emoji": "๐", "achievements": { "Jordan": ["6x NBA Champion", "6x Finals MVP", "5x MVP"], "Magic": ["5x NBA Champion", "3x MVP", "Showtime Lakers"], "Bird": ["3x NBA Champion", "3x MVP", "Celtics Legend"], "Russell": ["11x NBA Champion", "5x MVP", "Defense Master"], "Kareem": ["6x NBA Champion", "6x MVP", "All-Time Scorer"] } }, "Tennis": { "styles": ["Baseline", "Serve and Volley", "All-Court", "Aggressive Baseliner"], "stats": ["Grand Slams", "Win Rate", "Aces", "Break Points", "Rankings"], "legends": ["Federer", "Nadal", "Sampras", "Graf", "Court"], "emoji": "๐พ", "achievements": { "Federer": ["20 Grand Slams", "310 Weeks at #1", "8x Wimbledon"], "Nadal": ["22 Grand Slams", "14x French Open", "Golden Slam"], "Sampras": ["14 Grand Slams", "7x Wimbledon", "Year-End #1"], "Graf": ["22 Grand Slams", "Golden Slam", "377 Weeks at #1"], "Court": ["24 Grand Slams", "11x Australian Open", "Career Slam"] } } } ERAS = ["Classic (1950-1970)", "Golden (1970-1990)", "Modern (1990-2010)", "Contemporary (2010-Present)"] MATCH_TYPES = ["Regular Season", "Playoffs", "Championship", "All-Star Game", "Dream Match"] SIMULATION_MODES = ["Historical", "Peak Performance", "Cross-Era", "What-If Scenario"] css = """ .gradio-container { background: linear-gradient(135deg, #FF6B6B, #4ECDC4, #45B7D1, #96E6A1); background-size: 300% 300%; animation: gradient 15s ease infinite; } @keyframes gradient { 0% { background-position: 0% 50%; } 50% { background-position: 100% 50%; } 100% { background-position: 0% 50%; } } .gr-button { background: linear-gradient(45deg, #667eea, #764ba2); border: none !important; color: white !important; transition: all 0.3s ease !important; } .gr-button:hover { transform: translateY(-2px); box-shadow: 0 5px 15px rgba(0,0,0,0.2); } .sports-card { background: rgba(255, 255, 255, 0.9); border-radius: 15px; padding: 20px; margin: 10px 0; box-shadow: 0 4px 15px rgba(0,0,0,0.1); transition: all 0.3s ease; } .sports-card:hover { transform: translateY(-5px); box-shadow: 0 8px 25px rgba(0,0,0,0.2); } .stats-box { background: rgba(102, 126, 234, 0.1); border-radius: 10px; padding: 15px; margin: 8px 0; transition: all 0.3s ease; } .stats-box:hover { background: rgba(102, 126, 234, 0.2); } .player-comparison { display: flex; justify-content: space-between; background: rgba(255, 255, 255, 0.8); border-radius: 10px; padding: 15px; margin: 10px 0; } .achievement-card { background: linear-gradient(45deg, rgba(102, 126, 234, 0.1), rgba(118, 75, 162, 0.1)); border-radius: 8px; padding: 12px; margin: 5px 0; transition: all 0.3s ease; } .achievement-card:hover { background: linear-gradient(45deg, rgba(102, 126, 234, 0.2), rgba(118, 75, 162, 0.2)); } def generate_matchup(scenario, sport, era, match_type, simulation_mode): try: if not scenario or len(scenario.strip()) < 10: return "Please describe your fantasy matchup in more detail! ๐" selected_sport = SPORTS_CATEGORIES[sport] legends = selected_sport["legends"] selected_legends = random.sample(legends, 2) # Generate stats with realistic variations based on era and match type stats = selected_sport["stats"] stats_comparison = {} for stat in stats: base_value1 = random.randint(85, 99) # Legend 1 base stats base_value2 = random.randint(85, 99) # Legend 2 base stats # Adjust stats based on era if "Classic" in era: era_modifier = random.uniform(0.9, 1.1) elif "Golden" in era: era_modifier = random.uniform(0.95, 1.15) else: era_modifier = random.uniform(1.0, 1.2) stats_comparison[stat] = ( int(base_value1 * era_modifier), int(base_value2 * era_modifier) ) # Generate narrative using text_generator narrative_prompt = f""" A historic {match_type} match between {selected_legends[0]} and {selected_legends[1]} in {era}. {selected_legends[0]}, known for {selected_sport['achievements'][selected_legends[0]][0]}, faces {selected_legends[1]}, famous for {selected_sport['achievements'][selected_legends[1]][0]}. """ narrative = text_generator(narrative_prompt, max_length=200, num_return_sequences=1)[0]['generated_text'] return create_matchup_html( scenario, selected_legends, stats_comparison, narrative, sport, era, match_type, simulation_mode, selected_sport ) except Exception as e: print(f"Error: {str(e)}") return "Something went wrong. Please try again! ๐" def create_matchup_html(scenario, legends, stats, narrative, sport, era, match_type, simulation_mode, sport_data): # Create stats comparison HTML stats_html = "\n".join([f"""
Sport: {sport}
Era: {era}
Match Type: {match_type}
Simulation Mode: {simulation_mode}
Scenario: {scenario}
Legend of {sport}
Legend of {sport}
{narrative}