forked from MrHiraiwa/LineBotForGPTPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
whisper.py
60 lines (48 loc) · 1.64 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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')
CHANNEL_ACCESS_TOKEN = os.getenv('CHANNEL_ACCESS_TOKEN')
def get_audio(message_id):
url = f'https://api-data.line.me/v2/bot/message/{message_id}/content'
headers = {
'Authorization': f'Bearer {CHANNEL_ACCESS_TOKEN}',
}
response = requests.get(url, headers=headers, timeout=30)
if response.status_code == 200:
# Save the audio file temporarily
with NamedTemporaryFile(suffix=".m4a", delete=False) as temp:
temp.write(response.content)
temp.flush()
# Call the speech_to_text function with the temporary file
return speech_to_text(temp.name)
else:
print(f"Failed to fetch audio: {response.content}")
return None
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