File size: 29,498 Bytes
82f6468
 
0fe4c49
858ce7d
 
 
417f2a1
858ce7d
82f6468
0fe4c49
 
82f6468
0fe4c49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7062933
 
 
 
 
 
 
 
 
 
 
0fe4c49
 
82f6468
0fe4c49
7062933
 
 
82f6468
0fe4c49
7062933
 
 
82f6468
0fe4c49
7062933
 
 
82f6468
0fe4c49
 
 
 
 
82f6468
0fe4c49
 
528ffa9
0fe4c49
 
 
 
82f6468
0fe4c49
 
7062933
 
 
 
 
 
 
 
 
 
 
0fe4c49
82f6468
0fe4c49
82f6468
0fe4c49
 
 
 
 
 
 
 
 
 
 
 
 
 
528ffa9
 
 
 
 
7062933
 
 
 
 
 
 
 
 
 
 
0fe4c49
 
82f6468
0fe4c49
7062933
 
 
82f6468
0fe4c49
7062933
 
 
0fe4c49
 
7062933
 
 
0fe4c49
82f6468
 
 
 
0fe4c49
82f6468
 
 
528ffa9
0fe4c49
 
 
 
 
 
 
7062933
 
 
 
 
 
 
 
 
 
 
0fe4c49
 
 
82f6468
3ce655a
0fe4c49
82f6468
 
0fe4c49
 
 
 
 
82f6468
 
0fe4c49
 
3ce655a
 
 
 
 
 
 
0fe4c49
 
 
 
 
 
 
 
 
 
82f6468
 
0fe4c49
 
82f6468
0fe4c49
 
82f6468
0fe4c49
 
 
 
 
82f6468
0fe4c49
 
 
82f6468
 
0fe4c49
82f6468
0fe4c49
 
 
 
 
 
 
3ce655a
0fe4c49
 
 
 
 
 
 
 
 
 
 
 
7062933
 
 
 
 
3ce655a
 
 
82f6468
0fe4c49
82f6468
0fe4c49
7062933
 
 
 
0fe4c49
 
3ce655a
82f6468
0fe4c49
82f6468
4eb76aa
 
 
 
 
0fe4c49
 
 
82f6468
 
0fe4c49
7062933
 
0fe4c49
7062933
 
 
 
 
0fe4c49
 
 
3ce655a
82f6468
 
0fe4c49
82f6468
3ce655a
82f6468
 
3ce655a
82f6468
f7d536c
 
 
 
 
 
 
 
 
 
 
 
858ce7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82f6468
 
1c5d7df
82f6468
1c5d7df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
528ffa9
 
 
1c5d7df
528ffa9
82f6468
 
 
0fe4c49
 
528ffa9
 
 
 
 
0fe4c49
 
 
 
 
 
 
1c5d7df
82f6468
 
 
1c5d7df
 
82f6468
 
29fa7a0
82f6468
1c5d7df
82f6468
 
1c5d7df
 
82f6468
1c5d7df
29fa7a0
82f6468
1c5d7df
0fe4c49
528ffa9
 
1c5d7df
528ffa9
1c5d7df
3ce655a
1c5d7df
528ffa9
3ce655a
 
 
1c5d7df
0fe4c49
 
 
528ffa9
0fe4c49
 
 
 
3ce655a
 
 
 
 
 
 
 
 
2a05f44
3ce655a
 
 
 
1c5d7df
0fe4c49
528ffa9
 
 
0fe4c49
 
 
1c5d7df
 
 
f7d536c
1c5d7df
 
f7d536c
 
 
1c5d7df
 
 
 
528ffa9
1c5d7df
0fe4c49
 
528ffa9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c5d7df
e7600f8
 
528ffa9
1c5d7df
e7600f8
 
1c5d7df
e7600f8
528ffa9
 
 
82f6468
 
1c5d7df
82f6468
 
 
1c5d7df
82f6468
 
1c5d7df
82f6468
 
 
 
3ce655a
82f6468
1c5d7df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82f6468
 
 
0fe4c49
82f6468
 
 
 
 
0fe4c49
 
 
 
528ffa9
3ce655a
 
 
82f6468
 
f7d536c
 
 
 
 
 
 
528ffa9
f7d536c
 
1c5d7df
528ffa9
 
 
 
 
 
 
 
 
 
 
 
 
1c5d7df
 
 
 
 
 
 
 
82f6468
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
import gradio as gr
import pandas as pd
import os
from athletic_performance import (
    analyze_youtube_video, analyze_video_file, get_performance_insights, 
    get_ai_sports_coaching_analysis, test_gemini_api_connection,
    generate_annotated_video_from_youtube, generate_annotated_video_from_file
)

def analyze_jump_from_youtube(youtube_url, user_height_cm, user_weight_kg, progress=gr.Progress()):
    """Main analysis function for Gradio interface."""
    
    # Create progress callback for the athletic_performance module
    def progress_callback(prog, desc):
        progress(prog, desc=desc)
    
    # Call the core analysis function
    result = analyze_youtube_video(youtube_url, user_height_cm, user_weight_kg, progress_callback)
    
    # Handle errors
    if "error" in result:
        return f"❌ {result['error']}", None, None
    
    if result is None:
        return "⚠️ Could not analyze jump. Make sure the video shows a person clearly performing a vertical jump.", None, None
    
    # Format results for display
    # Handle potential None values safely
    jump_height = result.get('jump_height_cm', 0) or 0
    flight_time = result.get('flight_time_s', 0) or 0
    normalized_rise = result.get('normalized_rise', 0) or 0
    peak_power = result.get('peak_power_watts', 0) or 0
    peak_force = result.get('peak_force_n', 0) or 0
    impulse = result.get('impulse_ns', 0) or 0
    rfd = result.get('rate_of_force_development', 0) or 0
    takeoff_duration = result.get('takeoff_phase_duration_s', 0) or 0
    ground_contact = result.get('ground_contact_time_s', 0) or 0
    
    results_text = f"""
## πŸŽ‰ Comprehensive Jump Analysis Results

### πŸ“Š Core Performance Metrics
- **Jump Height**: {jump_height:.2f} cm
- **Flight Time**: {flight_time:.3f} seconds  
- **Normalized Rise**: {normalized_rise:.3f} ({normalized_rise*100:.1f}%)

### ⚑ Power & Force Metrics
- **Peak Power Output**: {peak_power:.0f} watts
- **Peak Force**: {peak_force:.0f} N
- **Impulse**: {impulse:.2f} Nβ‹…s

### πŸš€ Explosiveness Metrics
- **Rate of Force Development**: {rfd:.2f}
- **Takeoff Phase Duration**: {takeoff_duration:.3f} seconds
- **Ground Contact Time**: {ground_contact:.3f} seconds

### πŸ“Ή Video Information
- **Total Frames**: {result['frames']}
- **Frame Rate**: {result['fps']:.2f} FPS
- **Video File**: {result['video']}
- **Subject Weight**: {result.get('user_weight_kg', 'N/A')} kg

### πŸ“ˆ Performance Insights
"""
    
    # Add performance insights using the new function
    insights = get_performance_insights(result)
    for insight in insights:
        results_text += f"{insight}\n"
    
    # Create a comprehensive results dataframe for the table
    results_df = pd.DataFrame([
        ["Jump Height", f"{jump_height:.2f} cm"],
        ["Flight Time", f"{flight_time:.3f} seconds"],
        ["Peak Power", f"{peak_power:.0f} watts"],
        ["Peak Force", f"{peak_force:.0f} N"],
        ["Rate of Force Development", f"{rfd:.2f}"],
        ["Ground Contact Time", f"{ground_contact:.3f} seconds"],
        ["Impulse", f"{impulse:.2f} Nβ‹…s"],
        ["Takeoff Duration", f"{takeoff_duration:.3f} seconds"],
        ["Normalized Rise", f"{normalized_rise*100:.1f}%"],
        ["Video Frames", f"{result.get('frames', 0)}"],
        ["Frame Rate", f"{result.get('fps', 0):.2f} FPS"],
    ], columns=["Metric", "Value"])
    
    return results_text, results_df, "βœ… Analysis completed successfully!"

def analyze_jump_from_file(video_file, user_height_cm, user_weight_kg, progress=gr.Progress()):
    """Analysis function for uploaded video files."""
    
    # Create progress callback for the athletic_performance module
    def progress_callback(prog, desc):
        progress(prog, desc=desc)
    
    # Call the core analysis function
    video_path = video_file.name if video_file else None
    result = analyze_video_file(video_path, user_height_cm, user_weight_kg, progress_callback)
    
    # Handle errors
    if "error" in result:
        return f"❌ {result['error']}", None, None
    
    if result is None:
        return "⚠️ Could not analyze jump. Make sure the video shows a person clearly performing a vertical jump.", None, None
    
    # Format results (same as YouTube function)
    # Handle potential None values safely
    jump_height = result.get('jump_height_cm', 0) or 0
    flight_time = result.get('flight_time_s', 0) or 0
    normalized_rise = result.get('normalized_rise', 0) or 0
    peak_power = result.get('peak_power_watts', 0) or 0
    peak_force = result.get('peak_force_n', 0) or 0
    impulse = result.get('impulse_ns', 0) or 0
    rfd = result.get('rate_of_force_development', 0) or 0
    takeoff_duration = result.get('takeoff_phase_duration_s', 0) or 0
    ground_contact = result.get('ground_contact_time_s', 0) or 0
    
    results_text = f"""
## πŸŽ‰ Comprehensive Jump Analysis Results

### πŸ“Š Core Performance Metrics
- **Jump Height**: {jump_height:.2f} cm
- **Flight Time**: {flight_time:.3f} seconds  
- **Normalized Rise**: {normalized_rise:.3f} ({normalized_rise*100:.1f}%)

### ⚑ Power & Force Metrics
- **Peak Power Output**: {peak_power:.0f} watts
- **Peak Force**: {peak_force:.0f} N
- **Impulse**: {impulse:.2f} Nβ‹…s

### πŸš€ Explosiveness Metrics
- **Rate of Force Development**: {rfd:.2f}
- **Takeoff Phase Duration**: {takeoff_duration:.3f} seconds
- **Ground Contact Time**: {ground_contact:.3f} seconds

### πŸ“Ή Video Information
- **Total Frames**: {result['frames']}
- **Frame Rate**: {result['fps']:.2f} FPS
- **Video File**: {result['video']}
- **Subject Weight**: {result.get('user_weight_kg', 'N/A')} kg

### πŸ“ˆ Performance Insights
"""
    
    # Add performance insights using the new function
    insights = get_performance_insights(result)
    for insight in insights:
        results_text += f"{insight}\n"
    
    # Create a comprehensive results dataframe for the table
    results_df = pd.DataFrame([
        ["Jump Height", f"{jump_height:.2f} cm"],
        ["Flight Time", f"{flight_time:.3f} seconds"],
        ["Peak Power", f"{peak_power:.0f} watts"],
        ["Peak Force", f"{peak_force:.0f} N"],
        ["Rate of Force Development", f"{rfd:.2f}"],
        ["Ground Contact Time", f"{ground_contact:.3f} seconds"],
        ["Impulse", f"{impulse:.2f} Nβ‹…s"],
        ["Takeoff Duration", f"{takeoff_duration:.3f} seconds"],
        ["Normalized Rise", f"{normalized_rise*100:.1f}%"],
        ["Video Frames", f"{result.get('frames', 0)}"],
        ["Frame Rate", f"{result.get('fps', 0):.2f} FPS"],
    ], columns=["Metric", "Value"])
    
    return results_text, results_df, "βœ… Analysis completed successfully!"

def get_ai_coaching_recommendations(youtube_url, video_file, user_height_cm, user_weight_kg, gender, favorite_sports, gemini_api_key, progress=gr.Progress()):
    """Get AI-powered sports coaching recommendations."""
    
    # Validate inputs
    if not gemini_api_key or not gemini_api_key.strip():
        return "❌ Please provide your Gemini API key", None, None
    
    if not gender:
        return "❌ Please select your gender", None, None
    
    if not user_height_cm or user_height_cm <= 0:
        return "❌ Please provide a valid height", None, None
    
    # Validate favorite sports
    if not favorite_sports or len(favorite_sports) == 0:
        return "❌ Please select at least one favorite sport", None, None
    
    if len(favorite_sports) > 5:
        return "❌ Please select maximum 5 favorite sports", None, None
    
    # Determine which video source to use
    video_source = None
    if youtube_url and youtube_url.strip():
        video_source = "youtube"
        progress(0.1, desc="Analyzing YouTube video...")
    elif video_file:
        video_source = "file"
        progress(0.1, desc="Analyzing uploaded video...")
    else:
        return "❌ Please provide either a YouTube URL or upload a video file", None, None
    
    try:
        # First, get the jump analysis
        progress(0.2, desc="Performing biomechanical analysis...")
        
        def progress_callback(prog, desc):
            progress(0.2 + (prog * 0.5), desc=desc)
        
        if video_source == "youtube":
            result = analyze_youtube_video(youtube_url, user_height_cm, user_weight_kg, progress_callback)
        else:
            video_path = video_file.name if video_file else None
            result = analyze_video_file(video_path, user_height_cm, user_weight_kg, progress_callback)
        
        # Handle analysis errors
        if "error" in result:
            return f"❌ Video analysis failed: {result['error']}", None, None
        
        if result is None:
            return "❌ Could not analyze jump. Please ensure the video shows a clear vertical jump.", None, None
        
        progress(0.7, desc="Getting AI coaching analysis...")
        
        # Get AI coaching analysis
        ai_result = get_ai_sports_coaching_analysis(
            jump_height_cm=result['jump_height_cm'],
            user_height_cm=user_height_cm,
            gender=gender,
            favorite_sports=favorite_sports,
            peak_power_watts=result.get('peak_power_watts'),
            flight_time_s=result.get('flight_time_s'),
            rfd=result.get('rate_of_force_development'),
            api_key=gemini_api_key.strip()
        )
        
        progress(0.9, desc="Formatting results...")
        
        if "error" in ai_result:
            return f"❌ AI analysis failed: {ai_result['error']}", None, None
        
        # Format the comprehensive results
        # Handle potential None values safely
        jump_height = result.get('jump_height_cm', 0) or 0
        flight_time = result.get('flight_time_s', 0) or 0
        peak_power = result.get('peak_power_watts', 0) or 0
        
        # Format favorite sports list for display
        sports_display = ", ".join(favorite_sports)
        
        results_text = f"""
# πŸ€– AI Sports Coaching Analysis

## πŸ“Š Performance Summary
- **Jump Height**: {jump_height:.2f} cm
- **Relative Jump**: {(jump_height/user_height_cm*100):.1f}% of body height
- **Flight Time**: {flight_time:.3f} seconds
- **Peak Power**: {peak_power:.0f} watts
- **Gender**: {gender}
- **Height**: {user_height_cm} cm
- **Favorite Sports**: {sports_display}

## πŸ† AI Expert Coaching Analysis

**πŸ“Š Performance Percentiles:**
{chr(10).join([f"- **{sport}**: {percentile}th percentile" for sport, percentile in ai_result.get('analysis', {}).get('sports', {}).items()])}

**πŸ’‘ Improvement Tips:**
{chr(10).join([f"{i+1}. {tip}" for i, tip in enumerate(ai_result.get('analysis', {}).get('tips', []))])}

---
*Analysis powered by Google Gemini AI*
"""
        
        # Create summary dataframe
        rfd = result.get('rate_of_force_development', 0) or 0
        
        summary_df = pd.DataFrame([
            ["Jump Height", f"{jump_height:.2f} cm"],
            ["Relative Jump Height", f"{(jump_height/user_height_cm*100):.1f}%"],
            ["Flight Time", f"{flight_time:.3f} seconds"],
            ["Peak Power", f"{peak_power:.0f} watts"],
            ["Rate of Force Development", f"{rfd:.2f}"],
            ["Gender", gender],
            ["Height", f"{user_height_cm} cm"],
            ["Weight", f"{user_weight_kg} kg"],
            ["Favorite Sports", sports_display],
        ], columns=["Metric", "Value"])
        
        progress(1.0, desc="AI coaching analysis complete!")
        
        return results_text, summary_df, "βœ… AI coaching analysis completed!"
        
    except Exception as e:
        return f"❌ Unexpected error: {str(e)}", None, None

def test_api_key(api_key):
    """Test the API key connection."""
    if not api_key or not api_key.strip():
        return "❌ Please provide an API key to test"
    
    result = test_gemini_api_connection(api_key.strip())
    
    if result["success"]:
        return f"βœ… API Key is working! Status: {result['status_code']}\n\nResponse preview: {result['response_text'][:100]}..."
    else:
        return f"❌ API Key test failed!\n\nStatus Code: {result['status_code']}\nError: {result['error']}\n\nResponse: {result['response_text']}"


def generate_video_from_youtube(youtube_url, user_height_cm, user_weight_kg, gender, progress=gr.Progress()):
    """Generate annotated video from YouTube URL."""
    
    # Create progress callback
    def progress_callback(prog, desc):
        progress(prog, desc=desc)
    
    # Call the video generation function
    result = generate_annotated_video_from_youtube(
        youtube_url, user_height_cm, user_weight_kg, gender, progress_callback
    )
    
    # Handle errors
    if "error" in result:
        return f"❌ Video generation failed: {result['error']}", None, None
    
    if result is None:
        return "❌ Could not generate video. Please ensure the video shows a clear vertical jump.", None, None
    
    # Format results
    video_path = result.get("output_video_path", "")
    jump_metrics = result.get("jump_metrics", {})
    
    results_text = f"""
# 🎬 Annotated Video Generated!

## πŸ“Š Jump Analysis Summary
- **Jump Height**: {jump_metrics.get('jump_height_cm', 0):.2f} cm
- **Flight Time**: {jump_metrics.get('flight_time_s', 0):.3f} seconds
- **Peak Power**: {jump_metrics.get('peak_power_watts', 0):.0f} watts
- **Frames Processed**: {result.get('total_frames_processed', 0)}

## πŸŽ₯ Video Features Added
- βœ… **Pose Tracking**: Real-time skeleton overlay
- βœ… **Jump Reference Lines**: Average vs Professional heights
- βœ… **Knee Strain Detection**: Red markers for poor form
- βœ… **Performance Metrics**: Live jump height tracking

## πŸ“₯ Download
Your annotated video is ready for download!
"""
    
    # Create summary dataframe
    summary_df = pd.DataFrame([
        ["Jump Height", f"{jump_metrics.get('jump_height_cm', 0):.2f} cm"],
        ["Flight Time", f"{jump_metrics.get('flight_time_s', 0):.3f} seconds"],
        ["Peak Power", f"{jump_metrics.get('peak_power_watts', 0):.0f} watts"],
        ["Video Features", "Pose + References + Strain Detection"],
        ["Output Format", "MP4 Video"],
        ["Status", "βœ… Ready for Download"],
    ], columns=["Metric", "Value"])
    
    return results_text, summary_df, video_path


def generate_video_from_file(video_file, user_height_cm, user_weight_kg, gender, progress=gr.Progress()):
    """Generate annotated video from uploaded file."""
    
    # Create progress callback
    def progress_callback(prog, desc):
        progress(prog, desc=desc)
    
    # Call the video generation function
    video_path = video_file.name if video_file else None
    result = generate_annotated_video_from_file(
        video_path, user_height_cm, user_weight_kg, gender, progress_callback
    )
    
    # Handle errors
    if "error" in result:
        return f"❌ Video generation failed: {result['error']}", None, None
    
    if result is None:
        return "❌ Could not generate video. Please ensure the video shows a clear vertical jump.", None, None
    
    # Format results (same as YouTube function)
    video_path = result.get("output_video_path", "")
    jump_metrics = result.get("jump_metrics", {})
    
    results_text = f"""
# 🎬 Annotated Video Generated!

## πŸ“Š Jump Analysis Summary
- **Jump Height**: {jump_metrics.get('jump_height_cm', 0):.2f} cm
- **Flight Time**: {jump_metrics.get('flight_time_s', 0):.3f} seconds
- **Peak Power**: {jump_metrics.get('peak_power_watts', 0):.0f} watts
- **Frames Processed**: {result.get('total_frames_processed', 0)}

## πŸŽ₯ Video Features Added
- βœ… **Pose Tracking**: Real-time skeleton overlay
- βœ… **Jump Reference Lines**: Average vs Professional heights
- βœ… **Knee Strain Detection**: Red markers for poor form
- βœ… **Performance Metrics**: Live jump height tracking

## πŸ“₯ Download
Your annotated video is ready for download!
"""
    
    # Create summary dataframe
    summary_df = pd.DataFrame([
        ["Jump Height", f"{jump_metrics.get('jump_height_cm', 0):.2f} cm"],
        ["Flight Time", f"{jump_metrics.get('flight_time_s', 0):.3f} seconds"],
        ["Peak Power", f"{jump_metrics.get('peak_power_watts', 0):.0f} watts"],
        ["Video Features", "Pose + References + Strain Detection"],
        ["Output Format", "MP4 Video"],
        ["Status", "βœ… Ready for Download"],
    ], columns=["Metric", "Value"])
    
    return results_text, summary_df, video_path


# Create Gradio interface
def create_interface():
    with gr.Blocks(title="πŸƒβ€β™‚οΈ Athletic Ability Analysis") as app:
        gr.Markdown("""
        # πŸƒβ€β™‚οΈ Athletic Ability Analysis & AI Sports Coach
        
        Analyze jumping performance from videos using computer vision and get AI-powered sports coaching recommendations.
        Upload a video or provide a YouTube URL to get detailed metrics and personalized coaching insights.
        
        ## πŸš€ Features
        - **πŸ“Š Biomechanical Analysis**: Comprehensive jump metrics (height, power, force, RFD)
        - **πŸ€– AI Sports Coach**: Personalized sport recommendations and technique improvements
        - **🎬 Annotated Videos**: Generate training videos with pose tracking and performance overlays
        - **⚠️ Technique Analysis**: Real-time knee strain detection and form corrections
        - **🎯 Performance Insights**: Professional-grade analysis and training suggestions
        
        ## πŸ“‹ Instructions
        1. Enter your height in centimeters and weight in kilograms
        2. Choose your analysis type: 
           - **πŸ“Š Standard Analysis**: Get detailed biomechanical metrics
           - **πŸ€– AI Sports Coach**: Personalized recommendations and sport suggestions  
           - **🎬 Video Generation**: Create annotated training videos with visual overlays
        3. Provide a video (YouTube URL or file upload)
        4. Get comprehensive results, actionable insights, or downloadable training videos
        """)
        
        with gr.Row():
            with gr.Column():
                user_height = gr.Number(
                    label="Your Height (cm)",
                    value=175,
                    minimum=100,
                    maximum=250
                )
            with gr.Column():
                user_weight = gr.Number(
                    label="Your Weight (kg)",
                    value=75,
                    minimum=30,
                    maximum=200
                )
        gr.Markdown("πŸ’‘ *Enter your height and weight for accurate biomechanical calculations*")
        
        with gr.Tabs():
            # YouTube URL Tab
            with gr.TabItem("πŸŽ₯ YouTube Video"):
                gr.Markdown("πŸ“Ί *Paste a YouTube URL containing a video of someone jumping*")
                youtube_url = gr.Textbox(
                    label="YouTube URL",
                    placeholder="https://youtube.com/watch?v=..."
                )
                youtube_btn = gr.Button("πŸš€ Analyze YouTube Video", variant="primary")
            
            # File Upload Tab
            with gr.TabItem("πŸ“ Upload Video"):
                gr.Markdown("πŸ“ *Upload a video file showing someone performing a jump*")
                video_file = gr.File(
                    label="Upload Video File",
                    file_types=[".mp4", ".avi", ".mov", ".mkv", ".webm"]
                )
                file_btn = gr.Button("πŸš€ Analyze Uploaded Video", variant="primary")
            
            # AI Coaching Tab
            with gr.TabItem("πŸ€– AI Sports Coach"):
                gr.Markdown("""
                ## πŸ€– AI-Powered Sports Coaching Analysis
                
                Get personalized performance analysis for your favorite sports and targeted improvement suggestions from our AI sports coach powered by Google Gemini.
                
                **What you'll get:**
                - πŸ“Š **Percentile Rankings** across your favorite sports based on your performance
                - 🎯 **Combined Performance Improvement** recommendations (3-4 key pointers)
                - πŸ“ˆ **Sport-Specific Analysis** tailored to your athletic interests
                """)
                
                with gr.Row():
                    with gr.Column():
                        ai_gender = gr.Radio(
                            choices=["Male", "Female"],
                            label="Gender",
                            value="Male"
                        )
                        
                        favorite_sports = gr.CheckboxGroup(
                            choices=[
                                "Basketball", "Volleyball", "Track and Field", "Football", "Soccer",
                                "Tennis", "Badminton", "Swimming", "Gymnastics", "Boxing",
                                "Wrestling", "Baseball", "Hockey", "Rugby", "Cricket",
                                "Martial Arts", "Rock Climbing", "Parkour", "Dancing", "CrossFit"
                            ],
                            label="Favorite Sports (Select 1-5)",
                            value=["Basketball"]
                        )
                        
                        gr.Markdown("πŸ’‘ *Select your favorite sports to get percentile rankings showing how your jump performance compares to typical athletes in each sport*")
                        
                        # Check if API key is available in environment
                        default_api_key = os.getenv("GEMINI_API_KEY", "")
                        ai_gemini_key = gr.Textbox(
                            label="Gemini API Key",
                            placeholder="Enter your Google Gemini API key" if not default_api_key else "API key loaded from environment",
                            type="password",
                            value=default_api_key
                        )
                        with gr.Row():
                            test_api_btn = gr.Button("πŸ§ͺ Test API Key", size="sm")
                        
                        api_test_result = gr.Textbox(
                            label="API Test Result",
                            lines=3,
                            interactive=False,
                            visible=False
                        )
                        
                        gr.Markdown("""
                        πŸ’‘ **Get your free API key**: [Google AI Studio](https://aistudio.google.com/app/apikey)
                        
                        πŸ“± **Privacy**: Your API key is only used for this analysis and not stored.
                        """)
                    
                    with gr.Column():
                        ai_youtube_url = gr.Textbox(
                            label="YouTube URL (Optional)",
                            placeholder="https://youtube.com/watch?v=..."
                        )
                        ai_video_file = gr.File(
                            label="Upload Video File (Optional)",
                            file_types=[".mp4", ".avi", ".mov", ".mkv", ".webm"]
                        )
                        gr.Markdown("*Provide either a YouTube URL or upload a video file*")
                
                ai_coaching_btn = gr.Button("πŸ€– Get AI Coaching Analysis", variant="primary", size="lg")
            
            # Video Generation Tab
            with gr.TabItem("🎬 Annotated Video"):
                gr.Markdown("""
                ## 🎬 Generate Annotated Training Video
                
                Create a professional training video with visual overlays including:
                - **🦴 Pose Tracking**: Real-time skeleton visualization
                - **πŸ“ Performance Lines**: Average vs Professional jump heights
                - **⚠️ Knee Strain Detection**: Red warnings for poor form
                - **πŸ“Š Live Metrics**: Frame-by-frame jump analysis
                
                Perfect for coaches, athletes, and performance analysis!
                """)
                
                with gr.Row():
                    with gr.Column():
                        video_gender = gr.Radio(
                            choices=["Male", "Female"],
                            label="Gender (for performance references)",
                            value="Male"
                        )
                        gr.Markdown("*Used to set appropriate average/pro jump height lines*")
                    
                    with gr.Column():
                        gr.Markdown("### Video Input Options")
                        video_youtube_url = gr.Textbox(
                            label="YouTube URL (Option 1)",
                            placeholder="https://youtube.com/watch?v=..."
                        )
                        video_file_upload = gr.File(
                            label="Upload Video File (Option 2)",
                            file_types=[".mp4", ".avi", ".mov", ".mkv", ".webm"]
                        )
                        gr.Markdown("*Provide either a YouTube URL or upload a video file*")
                
                with gr.Row():
                    video_youtube_btn = gr.Button("🎬 Generate from YouTube", variant="primary", size="lg")
                    video_file_btn = gr.Button("🎬 Generate from Upload", variant="primary", size="lg")
        
        # Results section
        gr.Markdown("## πŸ“Š Analysis Results")
        
        with gr.Row():
            with gr.Column(scale=2):
                results_text = gr.Markdown(label="Results")
            with gr.Column(scale=1):
                results_table = gr.Dataframe(
                    label="Metrics Summary",
                    headers=["Metric", "Value"],
                    datatype=["str", "str"]
                )
        
        status_message = gr.Textbox(label="Status", interactive=False)
        
        # Video requirements
        gr.Markdown("""
        ## πŸ“ Video Requirements
        
        For best results, ensure your videos meet these criteria:
        
        - **Full body visible**: The person should be completely visible in the frame
        - **Clear movement**: Good lighting and minimal background clutter  
        - **Vertical jumps**: Works best with straight vertical jumps
        - **Duration**: 3-30 seconds is optimal
        - **Quality**: Higher quality videos produce better results
        - **Public videos**: For YouTube, ensure the video is not private
        
        ## πŸ”¬ How it Works
        
        1. **Pose Detection**: Uses Google's MediaPipe to detect human pose landmarks
        2. **Hip Tracking**: Tracks the midpoint between left and right hip joints
        3. **Biomechanical Analysis**: Calculates comprehensive metrics based on hip trajectory:
           - **Jump Height**: Relative to your body size
           - **Flight Time**: Duration in the air
           - **Peak Power Output**: Maximum power generated during takeoff
           - **Rate of Force Development (RFD)**: Speed of force generation
           - **Ground Contact Time**: Efficiency in stretch-shortening cycle
           - **Impulse & Peak Force**: Force characteristics during takeoff
           - **Takeoff Phase Duration**: Time from crouch to launch
        """)
        
        # Event handlers
        youtube_btn.click(
            fn=analyze_jump_from_youtube,
            inputs=[youtube_url, user_height, user_weight],
            outputs=[results_text, results_table, status_message]
        )
        
        file_btn.click(
            fn=analyze_jump_from_file,
            inputs=[video_file, user_height, user_weight],
            outputs=[results_text, results_table, status_message]
        )
        
        ai_coaching_btn.click(
            fn=get_ai_coaching_recommendations,
            inputs=[ai_youtube_url, ai_video_file, user_height, user_weight, ai_gender, favorite_sports, ai_gemini_key],
            outputs=[results_text, results_table, status_message]
        )
        
        # API key test handler
        def test_and_show_result(api_key):
            result = test_api_key(api_key)
            return gr.update(value=result, visible=True)
        
        test_api_btn.click(
            fn=test_and_show_result,
            inputs=[ai_gemini_key],
            outputs=[api_test_result]
        )
        
        # Video generation event handlers
        video_youtube_btn.click(
            fn=generate_video_from_youtube,
            inputs=[video_youtube_url, user_height, user_weight, video_gender],
            outputs=[results_text, results_table, gr.File(label="Download Video")]
        )
        
        video_file_btn.click(
            fn=generate_video_from_file,
            inputs=[video_file_upload, user_height, user_weight, video_gender],
            outputs=[results_text, results_table, gr.File(label="Download Video")]
        )
        
        # Example section
        gr.Examples(
            examples=[
                ["https://www.youtube.com/watch?v=dQw4w9WgXcQ", 175, 75],  # This is just a placeholder
            ],
            inputs=[youtube_url, user_height, user_weight],
            label="πŸ“š Example (Replace with actual jump video URLs)"
        )
    
    return app

if __name__ == "__main__":
    app = create_interface()
    app.launch(debug=True, share=True)