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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
YouTube Video Summarizer API - Hugging Face Spaces Edition

Flask backend deployed on Hugging Face Spaces.
Provides multilingual YouTube video summarization using:
- Whisper (speech-to-text)
- NLLB-200 (translation)
- Groq API (summarization)

All ML models are FREE and run locally on HF Spaces infrastructure.
"""

from flask import Flask, request, jsonify
from flask_cors import CORS
from dotenv import load_dotenv
import os
import logging

from services.transcript import TranscriptService
from services.summarizer import SummarizerService
from config import (
    SUPPORTED_LANGUAGES,
    get_language_name,
    is_english,
)

# Load environment variables
load_dotenv()

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = Flask(__name__)

# Enable CORS for all origins (allow frontend from any domain)
CORS(app, resources={
    r"/*": {
        "origins": "*",
        "methods": ["GET", "POST", "OPTIONS"],
        "allow_headers": ["Content-Type", "Authorization"]
    }
})

# Initialize services (lazy-loaded for heavy models)
transcript_service = TranscriptService()
summarizer_service = SummarizerService()

# Translation service is lazy-loaded to avoid loading 2.4GB model on startup
_translation_service = None

def get_translation_service():
    """Lazy-load the translation service."""
    global _translation_service
    if _translation_service is None:
        from services.translation import TranslationService
        _translation_service = TranslationService()
    return _translation_service


# =============================================================================
# ROOT & HEALTH ENDPOINTS
# =============================================================================

@app.route('/', methods=['GET'])
def root():
    """Root endpoint - serves as health check for HF Spaces"""
    return jsonify({
        'status': 'healthy',
        'service': 'YouTube Summarizer API',
        'version': '2.0.0',
        'docs': '/api/health for detailed status'
    }), 200


@app.route('/api/health', methods=['GET'])
def health_check():
    """Detailed health check endpoint"""
    return jsonify({
        'status': 'healthy',
        'message': 'YouTube Summarizer API is running on Hugging Face Spaces',
        'version': '2.0.0',
        'features': ['multilingual', 'whisper', 'translation'],
        'models': {
            'whisper': 'openai/whisper-small',
            'translation': 'facebook/nllb-200-distilled-600M',
            'summarization': 'groq/llama-3.1-8b-instant'
        }
    }), 200


@app.route('/api/languages', methods=['GET'])
def get_languages():
    """Get list of supported languages"""
    return jsonify({
        'success': True,
        'languages': SUPPORTED_LANGUAGES
    }), 200


@app.route('/api/warmup', methods=['POST'])
def warmup_models():
    """
    Pre-load ML models to avoid delay on first request.
    This can take 2-5 minutes on first run (downloading models).
    """
    try:
        results = {}
        data = request.get_json() or {}
        
        if data.get('translation', False):
            logger.info("Warming up translation model...")
            translation_service = get_translation_service()
            translation_service.warmup()
            results['translation'] = 'loaded'
        
        if data.get('whisper', False):
            logger.info("Warming up Whisper model...")
            from services.speech_to_text import SpeechToTextService
            stt = SpeechToTextService()
            stt.warmup()
            results['whisper'] = 'loaded'
        
        return jsonify({
            'success': True,
            'message': 'Models warmed up successfully',
            'models': results
        }), 200
        
    except Exception as e:
        logger.error(f"Warmup failed: {e}")
        return jsonify({
            'error': 'Warmup failed',
            'message': str(e)
        }), 500


# =============================================================================
# AUDIO TRANSCRIPTION ENDPOINTS (for Railway integration)
# =============================================================================

@app.route('/api/transcribe-audio', methods=['POST'])
def transcribe_audio():
    """
    Transcribe audio using Whisper.
    Receives audio as base64 from Railway backend.
    """
    try:
        data = request.get_json()
        
        if not data or 'audio_base64' not in data:
            return jsonify({
                'error': 'Missing audio',
                'message': 'Please provide audio_base64'
            }), 400
        
        import base64
        import tempfile
        
        # Decode audio
        audio_data = base64.b64decode(data['audio_base64'])
        
        # Save to temp file
        with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as f:
            f.write(audio_data)
            audio_path = f.name
        
        try:
            # Transcribe with Whisper
            from services.speech_to_text import SpeechToTextService
            stt = SpeechToTextService()
            result = stt.transcribe_audio(audio_path)
            
            return jsonify({
                'success': True,
                'transcript': result['text'],
                'language': result['language'],
                'word_count': len(result['text'].split())
            }), 200
            
        finally:
            # Cleanup
            import os
            if os.path.exists(audio_path):
                os.remove(audio_path)
        
    except Exception as e:
        logger.error(f"Audio transcription failed: {e}")
        return jsonify({
            'error': 'Transcription failed',
            'message': str(e)
        }), 500


@app.route('/api/process-audio', methods=['POST'])
def process_audio():
    """
    Full pipeline for audio: Whisper transcription → Translation → Summary.
    Receives audio as base64 from Railway backend.
    """
    try:
        data = request.get_json()
        
        if not data or 'audio_base64' not in data:
            return jsonify({
                'error': 'Missing audio',
                'message': 'Please provide audio_base64'
            }), 400
        
        import base64
        import tempfile
        
        video_id = data.get('video_id', 'unknown')
        summary_type = data.get('summary_type', 'general')
        target_language = data.get('target_language', 'eng')
        
        # Decode audio
        audio_data = base64.b64decode(data['audio_base64'])
        
        # Save to temp file
        with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as f:
            f.write(audio_data)
            audio_path = f.name
        
        try:
            # Step 1: Transcribe with Whisper
            logger.info("Transcribing audio with Whisper...")
            from services.speech_to_text import SpeechToTextService
            stt = SpeechToTextService()
            whisper_result = stt.transcribe_audio(audio_path)
            
            transcript = whisper_result['text']
            original_language = whisper_result['language']
            original_word_count = len(transcript.split())
            
            logger.info(f"Transcription complete. Language: {original_language}")
            
            # Step 2: Translate to English if needed
            english_transcript = transcript
            
            if not is_english(original_language):
                logger.info("Translating to English...")
                translation_service = get_translation_service()
                english_transcript = translation_service.translate_to_english(
                    transcript, 
                    original_language
                )
            
            # Step 3: Summarize
            logger.info("Generating summary...")
            summary = summarizer_service.summarize(
                text=english_transcript,
                summary_type=summary_type,
                chunk_size=2500,
                max_tokens=500
            )
            
            # Step 4: Translate summary to target language if needed
            final_summary = summary
            summary_language = "eng"
            
            if not is_english(target_language):
                logger.info(f"Translating summary to {target_language}...")
                translation_service = get_translation_service()
                final_summary = translation_service.translate_from_english(summary, target_language)
                summary_language = target_language
            
            # Calculate statistics
            summary_word_count = len(final_summary.split())
            compression_ratio = (summary_word_count / original_word_count) * 100 if original_word_count > 0 else 0
            
            response = {
                'success': True,
                'video_id': video_id,
                'original_language': original_language,
                'original_language_name': get_language_name(original_language),
                'transcript': transcript,
                'transcript_source': 'whisper',
                'summary': final_summary,
                'summary_language': summary_language,
                'summary_language_name': get_language_name(summary_language),
                'statistics': {
                    'original_word_count': original_word_count,
                    'summary_word_count': summary_word_count,
                    'compression_ratio': round(compression_ratio, 1),
                    'reading_time_minutes': max(1, summary_word_count // 200)
                }
            }
            
            if not is_english(original_language):
                response['english_transcript'] = english_transcript
            if not is_english(target_language):
                response['english_summary'] = summary
            
            logger.info("Audio processing complete!")
            return jsonify(response), 200
            
        finally:
            # Cleanup
            import os
            if os.path.exists(audio_path):
                os.remove(audio_path)
        
    except Exception as e:
        logger.error(f"Audio processing failed: {e}")
        return jsonify({
            'error': 'Processing failed',
            'message': str(e)
        }), 500


# =============================================================================
# TRANSCRIPT ENDPOINTS
# =============================================================================

@app.route('/api/transcript', methods=['POST'])
def get_transcript():
    """
    Extract transcript from YouTube video (multilingual).
    
    Request: { "url": "youtube_url", "use_whisper": true }
    Response: { "success": true, "transcript": "...", "language": "tam", ... }
    """
    try:
        data = request.get_json()
        
        if not data or 'url' not in data:
            return jsonify({
                'error': 'Missing YouTube URL',
                'message': 'Please provide a valid YouTube URL'
            }), 400
        
        url = data['url']
        use_whisper = data.get('use_whisper', True)
        
        video_id = transcript_service.extract_video_id(url)
        result = transcript_service.get_video_transcript(url, use_whisper_fallback=use_whisper)
        
        return jsonify({
            'success': True,
            'video_id': video_id,
            'transcript': result['transcript'],
            'language': result['language'],
            'language_name': get_language_name(result['language']),
            'source': result['source'],
            'word_count': result['word_count']
        }), 200
        
    except ValueError as e:
        return jsonify({'error': 'Invalid URL', 'message': str(e)}), 400
    except Exception as e:
        logger.error(f"Transcript extraction failed: {e}")
        return jsonify({'error': 'Transcript extraction failed', 'message': str(e)}), 500


# =============================================================================
# TRANSLATION ENDPOINTS
# =============================================================================

@app.route('/api/translate', methods=['POST'])
def translate_text():
    """
    Translate text between languages.
    
    Request: { "text": "Hello", "source_lang": "eng", "target_lang": "hin" }
    Response: { "success": true, "translated_text": "नमस्ते", ... }
    """
    try:
        data = request.get_json()
        
        if not data or 'text' not in data:
            return jsonify({
                'error': 'Missing text',
                'message': 'Please provide text to translate'
            }), 400
        
        text = data['text']
        source_lang = data.get('source_lang', 'eng')
        target_lang = data.get('target_lang', 'hin')
        
        translation_service = get_translation_service()
        translated = translation_service.translate(text, source_lang, target_lang)
        
        return jsonify({
            'success': True,
            'translated_text': translated,
            'source_lang': source_lang,
            'source_lang_name': get_language_name(source_lang),
            'target_lang': target_lang,
            'target_lang_name': get_language_name(target_lang)
        }), 200
        
    except ValueError as e:
        return jsonify({'error': 'Invalid language', 'message': str(e)}), 400
    except Exception as e:
        logger.error(f"Translation failed: {e}")
        return jsonify({'error': 'Translation failed', 'message': str(e)}), 500


@app.route('/api/detect-language', methods=['POST'])
def detect_language():
    """Detect the language of given text."""
    try:
        data = request.get_json()
        
        if not data or 'text' not in data:
            return jsonify({
                'error': 'Missing text',
                'message': 'Please provide text for language detection'
            }), 400
        
        translation_service = get_translation_service()
        result = translation_service.detect_language(data['text'])
        
        return jsonify({
            'success': True,
            'language': result['code'],
            'language_name': result['name']
        }), 200
        
    except Exception as e:
        logger.error(f"Language detection failed: {e}")
        return jsonify({'error': 'Language detection failed', 'message': str(e)}), 500


# =============================================================================
# SUMMARIZATION ENDPOINTS
# =============================================================================

@app.route('/api/summarize', methods=['POST'])
def summarize():
    """
    Generate summary from transcript.
    
    Request: { "transcript": "...", "summary_type": "general" }
    Response: { "success": true, "summary": "...", "statistics": {...} }
    """
    try:
        data = request.get_json()
        
        if not data or 'transcript' not in data:
            return jsonify({
                'error': 'Missing transcript',
                'message': 'Please provide transcript text'
            }), 400
        
        transcript = data['transcript']
        summary_type = data.get('summary_type', 'general')
        chunk_size = data.get('chunk_size', 2500)
        max_tokens = data.get('max_tokens', 500)
        
        valid_types = ['general', 'detailed', 'bullet_points', 'key_takeaways']
        if summary_type not in valid_types:
            return jsonify({
                'error': 'Invalid summary type',
                'message': f'Must be one of: {", ".join(valid_types)}'
            }), 400
        
        summary = summarizer_service.summarize(
            text=transcript,
            summary_type=summary_type,
            chunk_size=chunk_size,
            max_tokens=max_tokens
        )
        
        summary_word_count = len(summary.split())
        original_word_count = len(transcript.split())
        compression_ratio = (summary_word_count / original_word_count) * 100 if original_word_count > 0 else 0
        
        return jsonify({
            'success': True,
            'summary': summary,
            'statistics': {
                'original_word_count': original_word_count,
                'summary_word_count': summary_word_count,
                'compression_ratio': round(compression_ratio, 1),
                'reading_time_minutes': max(1, summary_word_count // 200)
            }
        }), 200
        
    except Exception as e:
        logger.error(f"Summarization failed: {e}")
        return jsonify({'error': 'Summarization failed', 'message': str(e)}), 500


# =============================================================================
# FULL PIPELINE ENDPOINT
# =============================================================================

@app.route('/api/process', methods=['POST'])
def process_video():
    """
    Full multilingual pipeline: Transcript → Translation → Summary → Translation
    
    Request: {
        "url": "youtube_url",
        "summary_type": "general",
        "target_language": "hin" (optional)
    }
    """
    try:
        data = request.get_json()
        
        if not data or 'url' not in data:
            return jsonify({
                'error': 'Missing YouTube URL',
                'message': 'Please provide a valid YouTube URL'
            }), 400
        
        url = data['url']
        summary_type = data.get('summary_type', 'general')
        target_language = data.get('target_language', 'eng')
        chunk_size = data.get('chunk_size', 2500)
        max_tokens = data.get('max_tokens', 500)
        
        # Step 1: Extract video ID
        video_id = transcript_service.extract_video_id(url)
        logger.info(f"Processing video: {video_id}")
        
        # Step 2: Get transcript with language
        logger.info("Step 1/4: Extracting transcript...")
        transcript_result = transcript_service.get_video_transcript(url, use_whisper_fallback=True)
        
        original_transcript = transcript_result['transcript']
        original_language = transcript_result['language']
        original_word_count = transcript_result['word_count']
        
        # Step 3: Translate to English if needed
        english_transcript = original_transcript
        
        if not is_english(original_language):
            logger.info("Step 2/4: Translating to English...")
            translation_service = get_translation_service()
            english_transcript = translation_service.translate_to_english(
                original_transcript, 
                original_language
            )
        else:
            logger.info("Step 2/4: Skipped (already English)")
        
        # Step 4: Summarize in English
        logger.info("Step 3/4: Generating summary...")
        summary = summarizer_service.summarize(
            text=english_transcript,
            summary_type=summary_type,
            chunk_size=chunk_size,
            max_tokens=max_tokens
        )
        
        # Step 5: Translate summary to target language
        final_summary = summary
        summary_language = "eng"
        
        if not is_english(target_language):
            logger.info(f"Step 4/4: Translating summary to {target_language}...")
            translation_service = get_translation_service()
            final_summary = translation_service.translate_from_english(summary, target_language)
            summary_language = target_language
        else:
            logger.info("Step 4/4: Skipped (English output)")
        
        # Calculate statistics
        summary_word_count = len(final_summary.split())
        compression_ratio = (summary_word_count / original_word_count) * 100 if original_word_count > 0 else 0
        
        response = {
            'success': True,
            'video_id': video_id,
            'original_language': original_language,
            'original_language_name': get_language_name(original_language),
            'transcript': original_transcript,
            'transcript_source': transcript_result['source'],
            'summary': final_summary,
            'summary_language': summary_language,
            'summary_language_name': get_language_name(summary_language),
            'statistics': {
                'original_word_count': original_word_count,
                'summary_word_count': summary_word_count,
                'compression_ratio': round(compression_ratio, 1),
                'reading_time_minutes': max(1, summary_word_count // 200)
            }
        }
        
        if not is_english(original_language):
            response['english_transcript'] = english_transcript
        if not is_english(target_language):
            response['english_summary'] = summary
        
        logger.info("Processing complete!")
        return jsonify(response), 200
        
    except ValueError as e:
        return jsonify({'error': 'Invalid URL', 'message': str(e)}), 400
    except Exception as e:
        logger.error(f"Processing failed: {e}")
        return jsonify({'error': 'Processing failed', 'message': str(e)}), 500


# =============================================================================
# ERROR HANDLERS
# =============================================================================

@app.errorhandler(404)
def not_found(error):
    return jsonify({
        'error': 'Not found',
        'message': 'The requested endpoint does not exist'
    }), 404


@app.errorhandler(500)
def internal_error(error):
    return jsonify({
        'error': 'Internal server error',
        'message': 'An unexpected error occurred'
    }), 500


# =============================================================================
# MAIN (for local testing only - gunicorn is used in production)
# =============================================================================

if __name__ == '__main__':
    port = int(os.environ.get('PORT', 7860))
    
    if not os.getenv('GROQ_API_KEY'):
        print("⚠️  Warning: GROQ_API_KEY not found")
        print("Set it in HF Spaces Settings → Secrets")
    
    print("🚀 Starting YouTube Summarizer API...")
    print(f"📡 API available at: http://localhost:{port}")
    
    app.run(debug=False, host='0.0.0.0', port=port)