File size: 2,293 Bytes
c7e434a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Background tasks untuk RQ worker
"""

from typing import List, Optional
from app.services.audio_processor import AudioProcessor
from app.core.storage import delete_file


def process_audio_task(
    audio_path: str,
    reference_text: Optional[str] = None,
    topic_id: Optional[str] = None,
    custom_topic: Optional[str] = None,
    custom_keywords: Optional[List[str]] = None,
    analyze_tempo: bool = True,
    analyze_articulation: bool = True,
    analyze_structure: bool = True,
    analyze_keywords: bool = False,
    analyze_profanity: bool = False
):
    """
    Background task untuk process audio
    
    This function will be executed by RQ worker
    
    Args:
        audio_path: Path ke file audio
        reference_text: Teks referensi untuk artikulasi
        topic_id: ID topik dari database (Level 1-2)
        custom_topic: Topik custom dari user (Level 3)
        custom_keywords: List kata kunci dari GPT (Level 3)
        analyze_tempo: Flag tempo analysis
        analyze_articulation: Flag articulation analysis
        analyze_structure: Flag structure analysis
        analyze_keywords: Flag keyword analysis
        analyze_profanity: Flag profanity detection
    """
    try:
        processor = AudioProcessor()
        
        result = processor.process_audio(
            audio_path=audio_path,
            reference_text=reference_text,
            topic_id=topic_id,
            custom_topic=custom_topic,
            custom_keywords=custom_keywords,
            analyze_tempo=analyze_tempo,
            analyze_articulation=analyze_articulation,
            analyze_structure=analyze_structure,
            analyze_keywords=analyze_keywords,
            analyze_profanity=analyze_profanity
        )
        
        # Cleanup file after processing
        try:
            delete_file(audio_path)
        except Exception as e:
            print(f"Warning: Could not delete file {audio_path}: {e}")
        
        return {
            'status': 'completed',
            'result': result
        }
        
    except Exception as e:
        # Cleanup file on error
        try:
            delete_file(audio_path)
        except:
            pass
        
        return {
            'status': 'failed',
            'error': str(e)
        }