File size: 1,864 Bytes
9902c89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import json

def transcribe_audio(file_path, api_key, model='whisper-1', language=None, prompt=None, response_format='json', temperature=0):
    url = "https://api.openai.com/v1/audio/transcriptions"

    headers = {
        "Authorization": f"Bearer {api_key}"
    }

    with open(file_path, 'rb') as f:
        files = {
            'file': (file_path, f, 'audio/wav')
        }

        data = {
            'model': (None, model),
            'language': (None, language),
            'prompt': (None, prompt),
            'response_format': (None, response_format),
            'temperature': (None, str(temperature))
        }

        response = requests.post(url, headers=headers, files=files, data=data)
        return response.json()

def save_transcription(results, json_filename, text_filename):
    # Save as JSON
    with open(json_filename, 'w') as json_file:
        json.dump(results, json_file, indent=4)

    # Extract and save as text
    transcripts = [result.get('choices')[0].get('text') if result.get('choices') else "" for result in results]
    with open(text_filename, 'w') as text_file:
        for transcript in transcripts:
            text_file.write(transcript + "\n\n")

# Replace 'your_api_key_here' with your actual OpenAI API key
api_key = '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'

# Generate list of audio files
audio_files = [f'part_{i}.wav' for i in range(1, 101)]


# Process each file and collect the results
results = []
for file_path in audio_files:
    try:
        result = transcribe_audio(file_path, api_key)
        results.append(result)
    except Exception as e:
        print(f"Error processing {file_path}: {e}")

# Save the results
save_transcription(results, 'transcription.json', 'transcription.txt')

print("Transcription saved to 'transcription.json' and 'transcription.txt'")