-
Notifications
You must be signed in to change notification settings - Fork 1
/
whisper.py
45 lines (37 loc) · 1.29 KB
/
whisper.py
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
import requests
import json
import os
from io import BytesIO
from tempfile import NamedTemporaryFile
# Environment variables should be used to securely store the API keys
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
def get_audio(audio_file):
with NamedTemporaryFile(suffix=".webm", delete=False) as temp:
temp.write(audio_file.read()) # ファイルの内容を一時ファイルに書き込む
temp_filename = temp.name # ファイル名を保存
# 一時ファイルを閉じてから音声認識を行う
return speech_to_text(temp_filename)
def speech_to_text(file_path):
with open(file_path, 'rb') as f:
payload = {
'model': 'whisper-1',
'temperature': 0
}
headers = {
'Authorization': f'Bearer {OPENAI_API_KEY}'
}
files = {
'file': (os.path.basename(file_path), f)
}
response = requests.post(
"https://api.openai.com/v1/audio/transcriptions",
headers=headers,
data=payload,
files=files,
timeout=30
)
if response.status_code == 200:
return response.json().get('text')
else:
print(f"Failed to transcribe audio: {response.content}")
return None