services package

class services.TranscriptionService(model_size='base')[source]

Bases: object

Service for audio transcription using OpenAI’s Whisper model.

Handles the complete process of transcribing audio files to text, including preprocessing, transcribing, and post-processing of the result.

Variables:
  • model_name – Name of the Whisper model to use

  • model – Instance of the loaded Whisper model

Initializes the transcription service with a Whisper model.

Parameters:

model_size (str) – Size of the Whisper model (“tiny”, “base”, “small”, “medium”, “large”)

__init__(model_size='base')[source]

Initializes the transcription service with a Whisper model.

Parameters:

model_size (str) – Size of the Whisper model (“tiny”, “base”, “small”, “medium”, “large”)

transcribe_audio_file(audio_file, encounter_id, language='it')[source]

Transcribes an audio file and saves the result to the database.

Parameters:
  • audio_file (FileField) – Audio file to transcribe

  • encounter_id (str) – Unique ID of the encounter associated with the transcription

  • language (str) – Language code of the audio content (default: “it”)

Returns:

Transcription object saved in the database

Return type:

AudioTranscript

Raises:

Exception – If an error occurs during transcription

class services.ClinicalExtractionService[source]

Bases: object

Service for extracting structured clinical data from transcripts

Initialize the extraction service

__init__()[source]

Initialize the extraction service

extract_clinical_data(transcript)[source]

Extract structured clinical data from a transcript

Parameters:

transcript (AudioTranscript) – AudioTranscript object to process

Returns:

ClinicalData: Object with extracted clinical data

Return type:

ClinicalData

Raises:

Exception – If extraction fails

Submodules

services.clinical_extraction module

Unified service for extracting clinical entities Handles both LLM (NVIDIA NIM) and NER (Text2NER) extraction

class services.clinical_extraction.ExtractionMethod(value)[source]

Bases: Enum

Listing of available clinical entity extraction methods.

Variables:
  • LLM – Extraction using Large Language Model (NVIDIA NIM)

  • NER – Extraction using Named Entity Recognition

LLM = 'llm'
NER = 'ner'
class services.clinical_extraction.ClinicalExtractionService[source]

Bases: object

Unified service for extracting clinical entities from medical transcriptions.

Allows selection between different extraction methods (LLM and NER) while maintaining a consistent interface for usage.

Variables:
  • llm_service – Service for extraction using LLM

  • ner_service – Service for extraction using NER

  • default_method – Default extraction method

Initializes the clinical extraction service.

Configures the available LLM and NER services, handling any initialization errors and setting the default method.

__init__()[source]

Initializes the clinical extraction service.

Configures the available LLM and NER services, handling any initialization errors and setting the default method.

get_available_methods()[source]

Returns the available extraction methods with their operational status.

Returns:

Dictionary containing information about available methods and their status

Return type:

Dict[str, Dict[str, Any]]

extract_clinical_entities(transcript_text, method=None, usage_mode='')[source]

Extract clinical entities from a medical transcription using the specified method.

Parameters:
  • transcript_text (str) – Text of the medical transcription to analyze

  • method (str) – Extraction method (“llm” or “ner”), if None uses the default

  • usage_mode (str) – Usage mode to customize extraction (e.g. “Checkup”, “Emergency”)

Returns:

Dictionary containing the extracted clinical entities and metadata

Return type:

Dict[str, Any]

Raises:

ValueError – If the specified method is invalid

set_default_method(method)[source]

Setting of default extraction method

Parameters:

method (str) – Method to set as default (“llm” or “ner”)

Returns:

True if the method was set successfully

Return type:

bool

get_method_comparison(transcript_text, usage_mode='')[source]

Execute extraction with both methods for comparison WARNING: This is a costly operation, use only for testing/debugging

Parameters:
  • transcript_text (str) – Text of the medical transcription

  • usage_mode (str) – Usage mode

Returns:

Dictionary with results from both methods

Return type:

Dict[str, Any]

services.extraction module

Service for extracting clinical data using LLM

class services.extraction.ClinicalExtractionService[source]

Bases: object

Service for extracting structured clinical data from transcripts

Initialize the extraction service

__init__()[source]

Initialize the extraction service

extract_clinical_data(transcript)[source]

Extract structured clinical data from a transcript

Parameters:

transcript (AudioTranscript) – AudioTranscript object to process

Returns:

ClinicalData: Object with extracted clinical data

Return type:

ClinicalData

Raises:

Exception – If extraction fails

services.mongodb_service module

Service for MongoDB management of patient and visit data

class services.mongodb_service.MongoDBService[source]

Bases: object

Service for MongoDB management of patient and visit data

Initialize MongoDB connection

__init__()[source]

Initialize MongoDB connection

is_connected()[source]

Check MongoDB connection status

save_patient_visit_transcript_only(encounter_id, patient_id, doctor_id, audio_file_path, transcript_text, triage_code=None, symptoms=None, triage_notes=None)[source]

Save a new patient visit with ONLY transcript (without extracted clinical data)

Parameters:
  • encounter_id (str) – ID encounter Django

  • patient_id (str) – ID paziente Django

  • doctor_id (str) – ID medico Django

  • audio_file_path (str) – Path file audio

  • transcript_text (str) – Testo trascrizione

  • triage_code (str) – Codice triage (white, green, yellow, red)

  • symptoms (str) – Sintomi principali

  • triage_notes (str) – Note del triage

Returns:

ID del transcript MongoDB creato

Return type:

Optional[str]

save_patient_visit(encounter_id, patient_id, doctor_id, audio_file_path, transcript_text, clinical_data)[source]

Save a new patient visit with audio and clinical data

Parameters:
  • encounter_id (str) – ID encounter Django

  • patient_id (str) – ID paziente Django

  • doctor_id (str) – ID medico Django

  • audio_file_path (str) – Path file audio

  • transcript_text (str) – Testo trascrizione

  • clinical_data (Dict[str, Any]) – Dati clinici estratti

Returns:

ID del transcript MongoDB creato

Return type:

Optional[str]

get_patient_visits(patient_id)[source]

Retrieve all visits of a patient

Parameters:

patient_id (str) – ID paziente Django

Returns:

Lista delle visite del paziente

Return type:

List[Dict[str, Any]]

get_all_patients_summary()[source]

Retrieve the summary of all patients with their latest visits

Returns:

List of patients with aggregated data

Return type:

List[Dict[str, Any]]

get_visits_today()[source]

Count ALL visits today (created today, both completed and in progress)

Returns:

Number of visits created today

Return type:

int

get_waiting_patients_count()[source]

Count patients waiting for processing (visits not yet completed)

Returns:

Number of patients waiting for processing

Return type:

int

get_completed_visits_today()[source]

Count ALL completed visits (with status ‘extracted’ or ‘validated’)

Returns:

Number of completed visits

Return type:

int

get_unique_patients()[source]

Retrieve a list of unique patients grouped by fiscal code from all interventions

Returns:

List of unique patients with aggregated data

Return type:

List[Dict[str, Any]]

update_patient_data(patient_id, updated_data)[source]

Update the personal data of a patient in the latest visit

Parameters:
  • patient_id (str) – Django patient ID

  • updated_data (Dict[str, Any]) – Updated data fields

Returns:

True if update succeeded, False otherwise

Return type:

bool

generate_report_content(transcript_id)[source]

Generate the content for the PDF report

Parameters:

transcript_id (str) – ID transcript MongoDB

Returns:

Dictionary with structured content for PDF

Return type:

Optional[Dict[str, Any]]

update_transcript_text(transcript_id, new_text)[source]

Update the transcript text

Parameters:
  • transcript_id (str) – ID transcript MongoDB

  • new_text (str) – New transcript text

Returns:

True if update succeeded, False otherwise

Return type:

bool

update_clinical_data(transcript_id, clinical_dict)[source]

Update the clinical data associated with the transcript

Parameters:
  • transcript_id (str) – ID transcript MongoDB

  • clinical_dict (Dict[str, Any]) – Dictionary with clinical data fields to update

Returns:

True if update succeeded, False otherwise

Return type:

bool

get_all_visits_summary()[source]

Retrieve a summary list of all visits/interventions

Returns:

List of dictionaries with summary information

Return type:

List[Dict[str, Any]]

get_visit_data(transcript_id)[source]

Retrieve the complete data of a visit/transcript

Parameters:

transcript_id (str) – ID of the transcript

Returns:

Dictionary with visit data or None if not found

Return type:

Optional[Dict[str, Any]]

delete_visit(transcript_id)[source]

Delete a visit from MongoDB

Parameters:

transcript_id (str) – ID of the transcript to delete

Returns:

True if deletion was successful, False otherwise

Return type:

bool

services.ner_service module

Service for extracting clinical entities with NER model Manages extraction using the Text2NER model from pacovalentino/Text2NER

class services.ner_service.NERService[source]

Bases: object

Service for extracting clinical entities with NER model pacovalentino/Text2NER

Initialize the NER service

__init__()[source]

Initialize the NER service

test_connection()[source]

Test the availability of the NER model

Returns:

Dictionary with model information

Return type:

Dict[str, Any]

extract_clinical_entities(transcript_text, usage_mode='')[source]

Extract clinical entities from the transcribed text using the NER model

Parameters:
  • transcript_text (str) – Transcribed medical text

  • usage_mode (str) – Usage mode (e.g. “Checkup”, “Emergency”)

Returns:

Dictionary with extracted clinical entities

Return type:

Dict[str, Any]

services.nvidia_nim module

Service for integration with NVIDIA NIM API Handles LLM calls for extracting clinical entities

class services.nvidia_nim.NVIDIANIMService[source]

Bases: object

Service for integration with NVIDIA NIM API for clinical entity extraction.

Handles LLM calls for structured extraction of medical information from audio transcriptions, with support for local fallback when the service is not available.

Variables:
  • model – LLM model used for extraction

  • available – Flag indicating if the service is available

  • client – OpenAI client for API calls

Initializes the NVIDIA NIM service.

Configures the OpenAI client with credentials and base URL from Django settings. If credentials are not available, enables fallback mode.

__init__()[source]

Initializes the NVIDIA NIM service.

Configures the OpenAI client with credentials and base URL from Django settings. If credentials are not available, enables fallback mode.

test_connection()[source]

Tests the connection with NVIDIA NIM API.

Returns:

Dictionary containing information about the connection status

Return type:

Dict[str, Any]

Raises:

Exception – If an error occurs during connection testing

extract_clinical_entities(transcript_text, usage_mode='')[source]

Extract structured clinical entities from a medical transcription using NVIDIA NIM.

Parameters:
  • transcript_text (str) – Text of the medical transcription to analyze

  • usage_mode (str) – Service usage mode (e.g. “Checkup”, “Emergency”)

Returns:

Dictionary containing the extracted structured clinical entities

Return type:

Dict[str, Any]

Raises:

Exception – If an error occurs during extraction

services.nvidia_nim.get_nvidia_nim_service()[source]

Factory function per ottenere l’istanza del servizio NVIDIA NIM. Utilizzata per evitare problemi di importazione durante la generazione della documentazione.

Returns:

Istanza del servizio NVIDIA NIM

Return type:

NVIDIANIMService

services.pdf_report module

Enhanced service for generating emergency medical PDF reports

class services.pdf_report.PDFReportService[source]

Bases: object

Enhanced service for generating emergency medical PDF reports - Professional layout - Dynamic (adapts text space) - More readable and well-spaced

Initialize PDF report service with styles and settings

__init__()[source]

Initialize PDF report service with styles and settings

generate_medical_report(report_data, output_path)[source]

Generate a professional medical report PDF

get_report_path(encounter_id, report_type='medical', patient_name='', visit_date='')[source]

Generate a safe file path for the report PDF

Parameters:
  • encounter_id (str) – Unique encounter identifier

  • report_type (str) – Type of report (default “medical”)

  • patient_name (str) – Patient’s name for filename (optional)

  • visit_date (str) – Visit date for filename (optional)

Returns:

Full file path for the report PDF

Return type:

str

services.pdf_report.get_pdf_report_service()[source]

Factory function per ottenere l’istanza del servizio PDF Report. Utilizzata per evitare problemi di importazione durante la generazione della documentazione.

Returns:

Istanza del servizio PDF Report

Return type:

PDFReportService

services.transcription module

Service for audio transcription using Whisper

services.transcription.check_dependencies()[source]

Verify that all necessary dependencies for transcription are available.

Check for the presence of numpy, torch, whisper, and librosa in the system.

Returns:

True if all dependencies are available

Return type:

bool

Raises:

ImportError – If one or more dependencies are missing

class services.transcription.TranscriptionService(model_size='base')[source]

Bases: object

Service for audio transcription using OpenAI’s Whisper model.

Handles the complete process of transcribing audio files to text, including preprocessing, transcribing, and post-processing of the result.

Variables:
  • model_name – Name of the Whisper model to use

  • model – Instance of the loaded Whisper model

Initializes the transcription service with a Whisper model.

Parameters:

model_size (str) – Size of the Whisper model (“tiny”, “base”, “small”, “medium”, “large”)

__init__(model_size='base')[source]

Initializes the transcription service with a Whisper model.

Parameters:

model_size (str) – Size of the Whisper model (“tiny”, “base”, “small”, “medium”, “large”)

transcribe_audio_file(audio_file, encounter_id, language='it')[source]

Transcribes an audio file and saves the result to the database.

Parameters:
  • audio_file (FileField) – Audio file to transcribe

  • encounter_id (str) – Unique ID of the encounter associated with the transcription

  • language (str) – Language code of the audio content (default: “it”)

Returns:

Transcription object saved in the database

Return type:

AudioTranscript

Raises:

Exception – If an error occurs during transcription

services.whisper_realtime module

services.whisper_service module

Service for audio transcription with Whisper medium Stable version for production

class services.whisper_service.WhisperService[source]

Bases: object

Service for audio transcription with Whisper medium

__init__()[source]
transcribe_audio_file(audio_file_path, language='it')[source]

Transcription of audio file with Whisper

Parameters:
  • audio_file_path (str) – Path to the audio file

  • language (str) – Language for transcription (default: Italian)

Returns:

Dictionary with transcription results

Return type:

Dict[str, Any]

transcribe_audio_blob(audio_blob, format='wav', language='it')[source]

Transcription of audio blob with Whisper

Parameters:
  • audio_blob (bytes) – Audio data in bytes

  • format (str) – Audio format (wav, mp3, etc.)

  • language (str) – Language for transcription (default: Italian)

Returns:

Dictionary with transcription results

Return type:

Dict[str, Any]

test_transcription()[source]

Test the transcription service

Returns:

Dictionary with test results

Return type:

Dict[str, Any]

get_supported_formats()[source]

Supported audio formats

Returns:

List of supported audio formats

Return type:

list