feat: add voice transcription
This commit is contained in:
@@ -20,8 +20,36 @@ async def generate_voice(messages: list):
|
||||
try:
|
||||
response = client.chat.completions.create(
|
||||
model=OPENAI_AUDIO_MODEL,
|
||||
response_format="mp3",
|
||||
messages=messages,
|
||||
max_tokens=1000,
|
||||
temperature=0.7,
|
||||
stream=False
|
||||
)
|
||||
|
||||
if not response.choices or not response.choices[0].message.content:
|
||||
return "No response content from the model"
|
||||
|
||||
return response.choices[0].message.content
|
||||
|
||||
except OpenAIError as e:
|
||||
error_msg = f"OpenAI API Error: {str(e)}"
|
||||
print(error_msg)
|
||||
raise Exception(error_msg) from e
|
||||
except Exception as e:
|
||||
error_msg = f"Unexpected error: {str(e)}"
|
||||
print(error_msg)
|
||||
raise Exception(error_msg) from e
|
||||
|
||||
async def generate_transcription(audio_file_path: str) -> str:
|
||||
if not audio_file_path:
|
||||
raise ValueError("Audio file path cannot be empty")
|
||||
|
||||
try:
|
||||
response = client.audio.transcriptions.create(
|
||||
model=OPENAI_AUDIO_MODEL,
|
||||
file=audio_file_path,
|
||||
response_format="text",
|
||||
language="id"
|
||||
)
|
||||
|
||||
if not response.choices or not response.choices[0].message.content:
|
||||
Reference in New Issue
Block a user