tonic
Initial dataset commit
9902c89
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'")