| import gradio as gr |
| import torch |
| from transformers import pipeline |
| import numpy as np |
| import random |
|
|
| |
| 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""" |
| <div class="stats-box"> |
| <h4>{stat}</h4> |
| <div style="display: flex; justify-content: space-between;"> |
| <span>{legends[0]}: {values[0]}</span> |
| <span>{legends[1]}: {values[1]}</span> |
| </div> |
| </div> |
| """ for stat, values in stats.items()]) |
| |
| # Create achievements HTML |
| achievements_html = "\n".join([f""" |
| <div class="achievement-card"> |
| <h4>{legend}</h4> |
| <ul> |
| {"".join([f"<li>{achievement}</li>" for achievement in sport_data['achievements'][legend]])} |
| </ul> |
| </div> |
| """ for legend in legends]) |
| |
| return f""" |
| <div class="sports-card"> |
| <h2>{sport_data['emoji']} Fantasy Sports Matchup</h2> |
| |
| <div style="margin: 20px 0; padding: 15px; background: rgba(102, 126, 234, 0.1); border-radius: 10px;"> |
| <h3>🎯 Matchup Overview</h3> |
| <p><strong>Sport:</strong> {sport}</p> |
| <p><strong>Era:</strong> {era}</p> |
| <p><strong>Match Type:</strong> {match_type}</p> |
| <p><strong>Simulation Mode:</strong> {simulation_mode}</p> |
| <p><strong>Scenario:</strong> {scenario}</p> |
| </div> |
| |
| <div style="margin: 20px 0;"> |
| <h3>⚡ Legend vs Legend</h3> |
| <div class="player-comparison"> |
| <div style="text-align: center;"> |
| <h4>{legends[0]}</h4> |
| <p>Legend of {sport}</p> |
| </div> |
| <div style="text-align: center; font-size: 24px;"> |
| VS |
| </div> |
| <div style="text-align: center;"> |
| <h4>{legends[1]}</h4> |
| <p>Legend of {sport}</p> |
| </div> |
| </div> |
| </div> |
| |
| <div style="margin: 20px 0;"> |
| <h3>📊 Stats Comparison</h3> |
| {stats_html} |
| </div> |
| |
| <div style="margin: 20px 0;"> |
| <h3>🏆 Career Achievements</h3> |
| {achievements_html} |
| </div> |
| |
| <div style="margin: 20px 0; padding: 15px; background: rgba(102, 126, 234, 0.1); border-radius: 10px;"> |
| <h3>🎭 Match Narrative</h3> |
| <p>{narrative}</p> |
| </div> |
| </div> |
| """ |
| |
| # Create Gradio interface |
| with gr.Blocks(css=css, title="Sports Fantasy Lab") as demo: |
| gr.Markdown(""" |
| |
| |
| *"Where sports legends meet imagination!"* |
| """) |
| |
| with gr.Row(): |
| with gr.Column(scale=1): |
| scenario = gr.Textbox( |
| label="🏆 Describe Your Dream Matchup", |
| placeholder="Example: Prime Michael Jordan vs Current LeBron James...", |
| lines=3 |
| ) |
| |
| sport = gr.Dropdown( |
| choices=list(SPORTS_CATEGORIES.keys()), |
| label="🎯 Select Sport", |
| value="Basketball" |
| ) |
| |
| era = gr.Dropdown( |
| choices=ERAS, |
| label="📅 Choose Era", |
| value="Contemporary (2010-Present)" |
| ) |
| |
| match_type = gr.Dropdown( |
| choices=MATCH_TYPES, |
| label="🎮 Match Type", |
| value="Championship" |
| ) |
| |
| simulation_mode = gr.Dropdown( |
| choices=SIMULATION_MODES, |
| label="🎲 Simulation Mode", |
| value="Peak Performance" |
| ) |
| |
| generate_btn = gr.Button("⚡ Generate Fantasy Matchup!", variant="primary", size="lg") |
| |
| with gr.Column(scale=2): |
| output = gr.HTML() |
| |
| # Example matchups |
| with gr.Row(): |
| gr.Markdown("### 🌟 Try These Epic Matchups:") |
| example_btn1 = gr.Button("🏀 Jordan vs LeBron") |
| example_btn2 = gr.Button("⚽ Pelé vs Messi") |
| example_btn3 = gr.Button("🎾 Federer vs Sampras") |
| |
| # Event handlers |
| generate_btn.click( |
| generate_matchup, |
| inputs=[scenario, sport, era, match_type, simulation_mode], |
| outputs=output |
| ) |
| |
| # Example matchup handlers |
| def load_example_matchup(matchup_type): |
| if matchup_type == "basketball": |
| return ( |
| "Michael Jordan in his 1996 championship form vs LeBron James from the 2016 Finals", |
| "Basketball", |
| "Modern (1990-2010)", |
| "Championship", |
| "Peak Performance" |
| ) |
| elif matchup_type == "soccer": |
| return ( |
| "Pelé from the 1970 World Cup vs Messi from the 2022 World Cup Final", |
| "Football/Soccer", |
| "Classic (1950-1970)", |
| "Championship", |
| "Cross-Era" |
| ) |
| else: # tennis |
| return ( |
| "Roger Federer from Wimbledon 2007 vs Pete Sampras from Wimbledon 1999", |
| "Tennis", |
| "Modern (1990-2010)", |
| "Championship", |
| "What-If Scenario" |
| ) |
| |
| example_btn1.click( |
| lambda: load_example_matchup("basketball"), |
| outputs=[scenario, sport, era, match_type, simulation_mode] |
| ) |
| |
| example_btn2.click( |
| lambda: load_example_matchup("soccer"), |
| outputs=[scenario, sport, era, match_type, simulation_mode] |
| ) |
| |
| example_btn3.click( |
| lambda: load_example_matchup("tennis"), |
| outputs=[scenario, sport, era, match_type, simulation_mode] |
| ) |
| |
| # Initialize and launch |
| if init_models(): |
| demo.launch() |
| else: |
| print("Failed to initialize models. Please check the error logs.") |