server module

This software is released under the AGPL-3.0 license Copyright (c) 2023-2024 Braedon Hendy

Further updates and packaging added in 2024 through the ClinicianFOCUS initiative, a collaboration with Dr. Braedon Hendy and Conestoga College Institute of Applied Learning and Technology as part of the CNERG+ applied research project, Unburdening Primary Healthcare: An Open-Source AI Clinician Partner Platform”. Prof. Michael Yingbull (PI), Dr. Braedon Hendy (Partner), and Research Students - Software Developer Alex Simko, Pemba Sherpa (F24), and Naitik Patel.

server.faster_whisper_transcribe(audio, **kwargs)[source]

Transcribe audio using the Faster Whisper model.

Parameters:

audio – Audio data to transcribe.

Returns:

Transcribed text or error message if transcription fails.

Return type:

str

Raises:

Exception – Any error during transcription is caught and returned as an error message.

async server.health_check(request: Request)[source]

Health check endpoint to verify the service is running.

Returns:

A simple JSON response indicating the service is running

Return type:

JSONResponse

server.normalize_audio(file_content: bytes, file_type: str) tuple[bytes, str][source]
async server.rate_limit_middleware(request, call_next)[source]

This function adds middleware to an application for rate limiting HTTP requests. It processes incoming requests and either forwards them for handling or returns a 429 Too Many Requests response if the rate limit is exceeded.

Example:

@app.middleware("http")
async def rate_limit_middleware(request, call_next):
    try:
        response = await call_next(request)
        return response
    except RateLimitExceeded as e:
        logging.warning(f"Rate limit exceeded: {e}")
        return PlainTextResponse("Rate limit exceeded. Try again later.", status_code=429)

Parameters:

  • request : The incoming HTTP request.

  • call_next : Callable to pass the request to the next handler.

Returns:

  • response : The HTTP response from the next handler in the middleware chain.

  • If the rate limit is exceeded, returns a PlainTextResponse with a 429 status code and a “Rate limit exceeded” message.

Raises:

  • RateLimitExceeded : Raised when the request exceeds the allowed rate limit.

async server.startup_event()[source]

Asynchronous function that runs during the application startup.

This function performs several initialization tasks: 1. Parses the command-line arguments to retrieve configuration settings. 2. Loads the Whisper model using the specified model name. 3. Checks and retrieves the API key for authentication. 4. Prints the API key for reference.

The function uses the global variable MODEL to store the loaded Whisper model.

The function uses the global variable SESSION_API_KEY to store the API key.

Returns: - None

async server.transcribe_audio(request: Request, audio: UploadFile = File(PydanticUndefined), api_key: str = Security(get_api_key))[source]

Transcribes an uploaded audio file using the Whisper model.

This endpoint accepts an audio file (MP3 or WAV) and an API key for authentication. It validates the file type, saves the file temporarily, transcribes it using the Whisper model, and returns the transcribed text.

Parameters:
  • request (fastapi.Request) – The request object containing headers and client information.

  • file (fastapi.UploadFile) – The audio file to be transcribed. Must be an MP3 or WAV file.

  • api_key (str) – The API key for authentication. Retrieved using the get_api_key function.

Returns:

JSON response containing the transcribed text.

Return type:

dict

Raises:

HTTPException

  • 400: If the uploaded file is not an MP3 or WAV file.

  • 500: If there is an error processing the audio file.

Example:

POST /whisperaudio
Content-Type: multipart/form-data
Authorization: Bearer <api_key>

{
    "audio": <audio_file>
}

Response:

{
    "text": "Transcribed text from the audio file."
}