nashjiwani commited on
Commit
9b72362
ยท
verified ยท
1 Parent(s): b7f9bc8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +372 -0
app.py ADDED
@@ -0,0 +1,372 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import pipeline
4
+ import numpy as np
5
+ import random
6
+
7
+ # Initialize models with verified Zero GPU compatible models
8
+ def init_models():
9
+ global text_generator, stats_analyzer
10
+ try:
11
+ text_generator = pipeline(
12
+ "text-generation",
13
+ model="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
14
+ device=0 if torch.cuda.is_available() else -1
15
+ )
16
+
17
+ stats_analyzer = pipeline(
18
+ "text-classification",
19
+ model="distilbert-base-uncased",
20
+ device=0 if torch.cuda.is_available() else -1
21
+ )
22
+ return True
23
+ except Exception as e:
24
+ print(f"Error initializing models: {str(e)}")
25
+ return False
26
+
27
+ SPORTS_CATEGORIES = {
28
+ "Football/Soccer": {
29
+ "positions": ["Forward", "Midfielder", "Defender", "Goalkeeper"],
30
+ "stats": ["Goals", "Assists", "Passes", "Tackles", "Clean Sheets"],
31
+ "legends": ["Pelรฉ", "Maradona", "Cruyff", "Beckenbauer", "Yashin"],
32
+ "emoji": "โšฝ",
33
+ "achievements": {
34
+ "Pelรฉ": ["3x World Cup", "1279 Goals", "Santos Legend"],
35
+ "Maradona": ["World Cup 1986", "Napoli Hero", "Golden Ball"],
36
+ "Cruyff": ["3x Ballon d'Or", "Total Football", "Barcelona Legend"],
37
+ "Beckenbauer": ["2x World Cup", "Der Kaiser", "Bayern Legend"],
38
+ "Yashin": ["Only GK Ballon d'Or", "Black Spider", "Clean Sheet King"]
39
+ }
40
+ },
41
+ "Basketball": {
42
+ "positions": ["Point Guard", "Shooting Guard", "Small Forward", "Power Forward", "Center"],
43
+ "stats": ["Points", "Rebounds", "Assists", "Blocks", "Steals"],
44
+ "legends": ["Jordan", "Magic", "Bird", "Russell", "Kareem"],
45
+ "emoji": "๐Ÿ€",
46
+ "achievements": {
47
+ "Jordan": ["6x NBA Champion", "6x Finals MVP", "5x MVP"],
48
+ "Magic": ["5x NBA Champion", "3x MVP", "Showtime Lakers"],
49
+ "Bird": ["3x NBA Champion", "3x MVP", "Celtics Legend"],
50
+ "Russell": ["11x NBA Champion", "5x MVP", "Defense Master"],
51
+ "Kareem": ["6x NBA Champion", "6x MVP", "All-Time Scorer"]
52
+ }
53
+ },
54
+ "Tennis": {
55
+ "styles": ["Baseline", "Serve and Volley", "All-Court", "Aggressive Baseliner"],
56
+ "stats": ["Grand Slams", "Win Rate", "Aces", "Break Points", "Rankings"],
57
+ "legends": ["Federer", "Nadal", "Sampras", "Graf", "Court"],
58
+ "emoji": "๐ŸŽพ",
59
+ "achievements": {
60
+ "Federer": ["20 Grand Slams", "310 Weeks at #1", "8x Wimbledon"],
61
+ "Nadal": ["22 Grand Slams", "14x French Open", "Golden Slam"],
62
+ "Sampras": ["14 Grand Slams", "7x Wimbledon", "Year-End #1"],
63
+ "Graf": ["22 Grand Slams", "Golden Slam", "377 Weeks at #1"],
64
+ "Court": ["24 Grand Slams", "11x Australian Open", "Career Slam"]
65
+ }
66
+ }
67
+ }
68
+
69
+ ERAS = ["Classic (1950-1970)", "Golden (1970-1990)", "Modern (1990-2010)", "Contemporary (2010-Present)"]
70
+ MATCH_TYPES = ["Regular Season", "Playoffs", "Championship", "All-Star Game", "Dream Match"]
71
+ SIMULATION_MODES = ["Historical", "Peak Performance", "Cross-Era", "What-If Scenario"]
72
+
73
+ css = """
74
+ .gradio-container {
75
+ background: linear-gradient(135deg, #FF6B6B, #4ECDC4, #45B7D1, #96E6A1);
76
+ background-size: 300% 300%;
77
+ animation: gradient 15s ease infinite;
78
+ }
79
+
80
+ @keyframes gradient {
81
+ 0% { background-position: 0% 50%; }
82
+ 50% { background-position: 100% 50%; }
83
+ 100% { background-position: 0% 50%; }
84
+ }
85
+
86
+ .gr-button {
87
+ background: linear-gradient(45deg, #667eea, #764ba2);
88
+ border: none !important;
89
+ color: white !important;
90
+ transition: all 0.3s ease !important;
91
+ }
92
+
93
+ .gr-button:hover {
94
+ transform: translateY(-2px);
95
+ box-shadow: 0 5px 15px rgba(0,0,0,0.2);
96
+ }
97
+
98
+ .sports-card {
99
+ background: rgba(255, 255, 255, 0.9);
100
+ border-radius: 15px;
101
+ padding: 20px;
102
+ margin: 10px 0;
103
+ box-shadow: 0 4px 15px rgba(0,0,0,0.1);
104
+ transition: all 0.3s ease;
105
+ }
106
+
107
+ .sports-card:hover {
108
+ transform: translateY(-5px);
109
+ box-shadow: 0 8px 25px rgba(0,0,0,0.2);
110
+ }
111
+
112
+ .stats-box {
113
+ background: rgba(102, 126, 234, 0.1);
114
+ border-radius: 10px;
115
+ padding: 15px;
116
+ margin: 8px 0;
117
+ transition: all 0.3s ease;
118
+ }
119
+
120
+ .stats-box:hover {
121
+ background: rgba(102, 126, 234, 0.2);
122
+ }
123
+
124
+ .player-comparison {
125
+ display: flex;
126
+ justify-content: space-between;
127
+ background: rgba(255, 255, 255, 0.8);
128
+ border-radius: 10px;
129
+ padding: 15px;
130
+ margin: 10px 0;
131
+ }
132
+
133
+ .achievement-card {
134
+ background: linear-gradient(45deg, rgba(102, 126, 234, 0.1), rgba(118, 75, 162, 0.1));
135
+ border-radius: 8px;
136
+ padding: 12px;
137
+ margin: 5px 0;
138
+ transition: all 0.3s ease;
139
+ }
140
+
141
+ .achievement-card:hover {
142
+ background: linear-gradient(45deg, rgba(102, 126, 234, 0.2), rgba(118, 75, 162, 0.2));
143
+ }
144
+ def generate_matchup(scenario, sport, era, match_type, simulation_mode):
145
+ try:
146
+ if not scenario or len(scenario.strip()) < 10:
147
+ return "Please describe your fantasy matchup in more detail! ๐Ÿ†"
148
+
149
+ selected_sport = SPORTS_CATEGORIES[sport]
150
+ legends = selected_sport["legends"]
151
+ selected_legends = random.sample(legends, 2)
152
+
153
+ # Generate stats with realistic variations based on era and match type
154
+ stats = selected_sport["stats"]
155
+ stats_comparison = {}
156
+ for stat in stats:
157
+ base_value1 = random.randint(85, 99) # Legend 1 base stats
158
+ base_value2 = random.randint(85, 99) # Legend 2 base stats
159
+
160
+ # Adjust stats based on era
161
+ if "Classic" in era:
162
+ era_modifier = random.uniform(0.9, 1.1)
163
+ elif "Golden" in era:
164
+ era_modifier = random.uniform(0.95, 1.15)
165
+ else:
166
+ era_modifier = random.uniform(1.0, 1.2)
167
+
168
+ stats_comparison[stat] = (
169
+ int(base_value1 * era_modifier),
170
+ int(base_value2 * era_modifier)
171
+ )
172
+
173
+ # Generate narrative using text_generator
174
+ narrative_prompt = f"""
175
+ A historic {match_type} match between {selected_legends[0]} and {selected_legends[1]} in {era}.
176
+ {selected_legends[0]}, known for {selected_sport['achievements'][selected_legends[0]][0]},
177
+ faces {selected_legends[1]}, famous for {selected_sport['achievements'][selected_legends[1]][0]}.
178
+ """
179
+
180
+ narrative = text_generator(narrative_prompt, max_length=200, num_return_sequences=1)[0]['generated_text']
181
+
182
+ return create_matchup_html(
183
+ scenario,
184
+ selected_legends,
185
+ stats_comparison,
186
+ narrative,
187
+ sport,
188
+ era,
189
+ match_type,
190
+ simulation_mode,
191
+ selected_sport
192
+ )
193
+
194
+ except Exception as e:
195
+ print(f"Error: {str(e)}")
196
+ return "Something went wrong. Please try again! ๐Ÿ”„"
197
+
198
+ def create_matchup_html(scenario, legends, stats, narrative, sport, era, match_type, simulation_mode, sport_data):
199
+ # Create stats comparison HTML
200
+ stats_html = "\n".join([f"""
201
+ <div class="stats-box">
202
+ <h4>{stat}</h4>
203
+ <div style="display: flex; justify-content: space-between;">
204
+ <span>{legends[0]}: {values[0]}</span>
205
+ <span>{legends[1]}: {values[1]}</span>
206
+ </div>
207
+ </div>
208
+ """ for stat, values in stats.items()])
209
+
210
+ # Create achievements HTML
211
+ achievements_html = "\n".join([f"""
212
+ <div class="achievement-card">
213
+ <h4>{legend}</h4>
214
+ <ul>
215
+ {"".join([f"<li>{achievement}</li>" for achievement in sport_data['achievements'][legend]])}
216
+ </ul>
217
+ </div>
218
+ """ for legend in legends])
219
+
220
+ return f"""
221
+ <div class="sports-card">
222
+ <h2>{sport_data['emoji']} Fantasy Sports Matchup</h2>
223
+
224
+ <div style="margin: 20px 0; padding: 15px; background: rgba(102, 126, 234, 0.1); border-radius: 10px;">
225
+ <h3>๐ŸŽฏ Matchup Overview</h3>
226
+ <p><strong>Sport:</strong> {sport}</p>
227
+ <p><strong>Era:</strong> {era}</p>
228
+ <p><strong>Match Type:</strong> {match_type}</p>
229
+ <p><strong>Simulation Mode:</strong> {simulation_mode}</p>
230
+ <p><strong>Scenario:</strong> {scenario}</p>
231
+ </div>
232
+
233
+ <div style="margin: 20px 0;">
234
+ <h3>โšก Legend vs Legend</h3>
235
+ <div class="player-comparison">
236
+ <div style="text-align: center;">
237
+ <h4>{legends[0]}</h4>
238
+ <p>Legend of {sport}</p>
239
+ </div>
240
+ <div style="text-align: center; font-size: 24px;">
241
+ VS
242
+ </div>
243
+ <div style="text-align: center;">
244
+ <h4>{legends[1]}</h4>
245
+ <p>Legend of {sport}</p>
246
+ </div>
247
+ </div>
248
+ </div>
249
+
250
+ <div style="margin: 20px 0;">
251
+ <h3>๐Ÿ“Š Stats Comparison</h3>
252
+ {stats_html}
253
+ </div>
254
+
255
+ <div style="margin: 20px 0;">
256
+ <h3>๐Ÿ† Career Achievements</h3>
257
+ {achievements_html}
258
+ </div>
259
+
260
+ <div style="margin: 20px 0; padding: 15px; background: rgba(102, 126, 234, 0.1); border-radius: 10px;">
261
+ <h3>๐ŸŽญ Match Narrative</h3>
262
+ <p>{narrative}</p>
263
+ </div>
264
+ </div>
265
+ """
266
+
267
+ # Create Gradio interface
268
+ with gr.Blocks(css=css, title="Sports Fantasy Lab") as demo:
269
+ gr.Markdown("""
270
+ # โœจ Sports Fantasy Lab
271
+ ### Built & Powered by Nash ๐Ÿš€
272
+ *"Where sports legends meet imagination!"*
273
+ """)
274
+
275
+ with gr.Row():
276
+ with gr.Column(scale=1):
277
+ scenario = gr.Textbox(
278
+ label="๐Ÿ† Describe Your Dream Matchup",
279
+ placeholder="Example: Prime Michael Jordan vs Current LeBron James...",
280
+ lines=3
281
+ )
282
+
283
+ sport = gr.Dropdown(
284
+ choices=list(SPORTS_CATEGORIES.keys()),
285
+ label="๐ŸŽฏ Select Sport",
286
+ value="Basketball"
287
+ )
288
+
289
+ era = gr.Dropdown(
290
+ choices=ERAS,
291
+ label="๐Ÿ“… Choose Era",
292
+ value="Contemporary (2010-Present)"
293
+ )
294
+
295
+ match_type = gr.Dropdown(
296
+ choices=MATCH_TYPES,
297
+ label="๐ŸŽฎ Match Type",
298
+ value="Championship"
299
+ )
300
+
301
+ simulation_mode = gr.Dropdown(
302
+ choices=SIMULATION_MODES,
303
+ label="๐ŸŽฒ Simulation Mode",
304
+ value="Peak Performance"
305
+ )
306
+
307
+ generate_btn = gr.Button("โšก Generate Fantasy Matchup!", variant="primary", size="lg")
308
+
309
+ with gr.Column(scale=2):
310
+ output = gr.HTML()
311
+
312
+ # Example matchups
313
+ with gr.Row():
314
+ gr.Markdown("### ๐ŸŒŸ Try These Epic Matchups:")
315
+ example_btn1 = gr.Button("๐Ÿ€ Jordan vs LeBron")
316
+ example_btn2 = gr.Button("โšฝ Pelรฉ vs Messi")
317
+ example_btn3 = gr.Button("๐ŸŽพ Federer vs Sampras")
318
+
319
+ # Event handlers
320
+ generate_btn.click(
321
+ generate_matchup,
322
+ inputs=[scenario, sport, era, match_type, simulation_mode],
323
+ outputs=output
324
+ )
325
+
326
+ # Example matchup handlers
327
+ def load_example_matchup(matchup_type):
328
+ if matchup_type == "basketball":
329
+ return (
330
+ "Michael Jordan in his 1996 championship form vs LeBron James from the 2016 Finals",
331
+ "Basketball",
332
+ "Modern (1990-2010)",
333
+ "Championship",
334
+ "Peak Performance"
335
+ )
336
+ elif matchup_type == "soccer":
337
+ return (
338
+ "Pelรฉ from the 1970 World Cup vs Messi from the 2022 World Cup Final",
339
+ "Football/Soccer",
340
+ "Classic (1950-1970)",
341
+ "Championship",
342
+ "Cross-Era"
343
+ )
344
+ else: # tennis
345
+ return (
346
+ "Roger Federer from Wimbledon 2007 vs Pete Sampras from Wimbledon 1999",
347
+ "Tennis",
348
+ "Modern (1990-2010)",
349
+ "Championship",
350
+ "What-If Scenario"
351
+ )
352
+
353
+ example_btn1.click(
354
+ lambda: load_example_matchup("basketball"),
355
+ outputs=[scenario, sport, era, match_type, simulation_mode]
356
+ )
357
+
358
+ example_btn2.click(
359
+ lambda: load_example_matchup("soccer"),
360
+ outputs=[scenario, sport, era, match_type, simulation_mode]
361
+ )
362
+
363
+ example_btn3.click(
364
+ lambda: load_example_matchup("tennis"),
365
+ outputs=[scenario, sport, era, match_type, simulation_mode]
366
+ )
367
+
368
+ # Initialize and launch
369
+ if init_models():
370
+ demo.launch()
371
+ else:
372
+ print("Failed to initialize models. Please check the error logs.")