File size: 834 Bytes
cc4ea58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import io
import logging

from openai import OpenAI

from api.config import settings

client = OpenAI(api_key=settings.OPENAI_API_KEY)

if settings.ENVIRONMENT == "development":
    logging.basicConfig(level=logging.DEBUG)
else:
    logging.basicConfig(level=logging.WARNING)


def transcribe_with_whisper(
    filename: str, file_like: io.BytesIO, content_type: str
) -> str:
    """Helper function to transcribe audio using OpenAI Whisper."""
    logging.info("Transcribing with whisper")

    # Prepare the file data as a tuple
    file_data = (filename, file_like.read(), content_type)

    # Call the OpenAI API to transcribe the audio file
    transcription = client.audio.transcriptions.create(
        model="whisper-1", file=file_data
    )

    logging.debug(f"Transcription: {transcription.text}")
    return transcription