I am facing an issue where I need to use a microcontroller with Whisper, but due to the slow operation of the microcontroller, I am looking to use an API instead of a local model. Unfortunately, the SDK I am currently using only supports communication via sockets. How can I solve this problem?
## Socket version ############################################################
import requests
import json
import os
import base64
#### API key
with open('OPENAI_API_KEY', 'rb') as f:
OPENAI_API_KEY = json.load(f)['key'][0]
# API URL
url = "https://api.openai.com//v1/audio/translations"
# Proxy
proxy = os.environ.get("HTTPS_PROXY")
# Audio file
with open("sample.mp3", "rb") as f:
audio = f.read()
audio_base64 = base64.b64encode(audio).decode("utf-8")
# Request
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {OPENAI_API_KEY}"
}
data = {
"model": "whisper-1",
"audio": audio_base64,
"type": "transcription",
}
response = requests.post(
url = url,
headers=headers,
json=data,
proxies={'https': proxy}
)
# Output the result
result = response.json()
print(json.dumps(result, indent=2, ensure_ascii=False))
{
"error": {
"message": "1 validation error for Request\nbody -> file\n field required (type=value_error.missing)",
"type": "invalid_request_error",
"param": null,
"code": null
}
}
I have successfully tested the API with GPT-3.5, but when I try to use it with Whisper, it does not work.
import requests
import json
import os
# API Key
with open('OPENAI_API_KEY', 'rb') as f:
OPENAI_API_KEY = json.load(f)['key'][0]
# API URL
url = "https://api.openai.com/v1/chat/completions"
# Proxy
proxy = os.environ.get("HTTPS_PROXY")
# Request
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {OPENAI_API_KEY}'
}
data = {
"model": "gpt-3.5-turbo",
"messages": [
{"role": "user", "content": "Say this is a test!"},
],
"temperature": 0.7
}
response = requests.post(
url=url,
headers=headers,
json=data,
proxies={'https': proxy}
)
# Output the result
result = response.json()
print(json.dumps(result, indent=2, ensure_ascii=False))
~~~
"choices": [
{
"message": {
"role": "assistant",
"content": "This is a test!"
},
"finish_reason": "stop",
"index": 0
}
]
~~~