File size: 2,009 Bytes
5c15933
 
3bb5f02
80d0076
5c15933
68ce9a1
 
 
 
5c15933
80d0076
 
 
68ce9a1
 
80d0076
5c15933
68ce9a1
3bb5f02
 
5c15933
3bb5f02
 
5c15933
3bb5f02
 
5c15933
80d0076
3bb5f02
 
 
 
 
 
 
 
 
 
68ce9a1
 
 
 
80d0076
 
68ce9a1
 
3bb5f02
80d0076
 
5c15933
3bb5f02
 
 
 
5c15933
80d0076
 
 
 
 
 
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
import librosa
import torch
from tqdm import tqdm
import transformers

# Check for GPU availability
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")

# Load the model pipeline
pipe = transformers.pipeline(
    model='sarvamai/shuka_v1', 
    trust_remote_code=True, 
    device=device,
    torch_dtype=torch.float16 if device.type == 'cuda' else torch.float32
)

def process_audio_batched(audio_file, system_prompt, user_prompt, batch_size=4, segment_length=10):
    # Load audio
    audio, sr = librosa.load(audio_file, sr=16000)
    
    # Calculate number of samples per segment
    samples_per_segment = segment_length * sr
    
    # Split audio into segments
    segments = [audio[i:i+samples_per_segment] for i in range(0, len(audio), samples_per_segment)]
    
    full_result = []
    
    # Process segments in batches
    for i in tqdm(range(0, len(segments), batch_size)):
        batch = segments[i:i+batch_size]
        
        turns = [
            {'role': 'system', 'content': system_prompt},
            {'role': 'user', 'content': f'<|audio|>{user_prompt}'}
        ]
        
        # Move batch to GPU if available
        batch_gpu = [torch.tensor(seg, device=device) for seg in batch]
        
        batch_results = pipe([{'audio': seg, 'turns': turns, 'sampling_rate': sr} for seg in batch_gpu], max_new_tokens=512)
        full_result.extend([result[0]['generated_text'] for result in batch_results])
        
        # Clear GPU memory
        torch.cuda.empty_cache()
    
    # Combine results
    return ' '.join(full_result)

# Example usage
audio_file = "path/to/your/audio/file.wav"
system_prompt = "Transcribe the audio accurately."
user_prompt = "What is being said in this audio?"

try:
    full_result = process_audio_batched(audio_file, system_prompt, user_prompt)
    print(full_result)
except Exception as e:
    print(f"An error occurred: {str(e)}")
    # Additional error handling and logging can be added here