added docstings and typings
This commit is contained in:
+39
-11
@@ -1,21 +1,35 @@
|
|||||||
"""
|
"""
|
||||||
Gradio App.
|
Gradio App
|
||||||
--------------------------------
|
----------
|
||||||
|
|
||||||
This module provides an interface to transcribe audio files using the
|
This module provides an interface to transcribe audio files using the
|
||||||
Scraibe model. Users can either upload an audio file or record their speech
|
Scraibe model. Users can either upload an audio file or record their speech
|
||||||
live for transcription. The application supports multiple languages and provides
|
live for transcription. The application supports multiple languages and provides
|
||||||
options to specify the number of speakers and the language of the audio.
|
options to specify the number of speakers and the language of the audio. It also
|
||||||
|
enables efficient management of resources by loading and unloading AI models
|
||||||
|
based on usage.
|
||||||
|
|
||||||
Attributes:
|
The configuration is managed via a 'config.yml' file, which allows customization
|
||||||
LANGUAGES (list): A list of supported languages for transcription.
|
of various aspects of the application, including the Gradio interface, queue
|
||||||
|
management, and model parameters.
|
||||||
|
|
||||||
|
Configuration Sections in 'config.yml':
|
||||||
|
- launch: Settings for launching the interface, such as server port, authentication, SSL configuration.
|
||||||
|
- queue: Configuration for managing request handling and concurrency.
|
||||||
|
- layout: Customization options for the interface layout, like headers, footers, and logos.
|
||||||
|
- model: Specifications for different AI models used in transcription.
|
||||||
|
- advanced: Advanced settings, including session timeout duration.
|
||||||
|
|
||||||
|
Note:
|
||||||
|
The .queue function of the Gradio interface is currently experiencing issues
|
||||||
|
and might not work as expected.
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
Run this script to start the Gradio web interface for audio transcription.
|
Run this script to start the Gradio web interface for audio transcription.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
####
|
####
|
||||||
# Gradio Interface
|
# Gradio Interface
|
||||||
####
|
####
|
||||||
@@ -32,17 +46,26 @@ def app(config : str = None, **kwargs):
|
|||||||
"""
|
"""
|
||||||
Launches the Gradio interface for audio transcription.
|
Launches the Gradio interface for audio transcription.
|
||||||
|
|
||||||
|
Initializes the Gradio web interface with settings from a YAML configuration file
|
||||||
|
and/or keyword arguments. The function manages AI models, handling their loading
|
||||||
|
into RAM and unloading after a session or specified timeout.
|
||||||
|
|
||||||
|
The `kwargs` are used to override or supplement values from the `config.yml` file.
|
||||||
|
They should follow the structure of `config.yml`, which includes sections like
|
||||||
|
'launch', 'queue', 'layout', 'model', and 'advanced'.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
interface_params (dict): A dictionary of parameters for the Gradio interface.
|
config (str): Path to the YAML configuration file. Default settings are used
|
||||||
queue_params (dict): A dictionary of parameters for the queue.
|
if not provided.
|
||||||
launch_params (dict): A dictionary of parameters for launching the interface.
|
**kwargs: Keyword arguments corresponding to the configuration sections. Each
|
||||||
|
argument should be a dictionary reflecting the structure of its
|
||||||
|
respective section in `config.yml`.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
None
|
None
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Load the configuration
|
# Load and override configuration from the YAML file with kwargs
|
||||||
|
|
||||||
config = AppConfig.load_config(config, **kwargs)
|
config = AppConfig.load_config(config, **kwargs)
|
||||||
|
|
||||||
@@ -54,19 +77,24 @@ def app(config : str = None, **kwargs):
|
|||||||
gv.LOADED_EVENT,
|
gv.LOADED_EVENT,
|
||||||
gv.RUNNING_EVENT)
|
gv.RUNNING_EVENT)
|
||||||
|
|
||||||
|
# Set the timer thread to manage model loading and unloading
|
||||||
timer = Thread(target=timer_thread, args=(gv.REQUEST_QUEUE,
|
timer = Thread(target=timer_thread, args=(gv.REQUEST_QUEUE,
|
||||||
gv.LAST_ACTIVE_TIME,
|
gv.LAST_ACTIVE_TIME,
|
||||||
gv.LOADED_EVENT,
|
gv.LOADED_EVENT,
|
||||||
gv.RUNNING_EVENT,
|
gv.RUNNING_EVENT,
|
||||||
gv.TIMEOUT), daemon=True)
|
gv.TIMEOUT), daemon=True)
|
||||||
|
|
||||||
|
# Set the layout for the Gradio interface
|
||||||
layout = config.get_layout()
|
layout = config.get_layout()
|
||||||
|
|
||||||
|
# start the timer thread
|
||||||
timer.start()
|
timer.start()
|
||||||
|
|
||||||
print("Starting Gradio Web Interface")
|
print("Starting Gradio Web Interface")
|
||||||
|
|
||||||
|
# Launch the Gradio interface
|
||||||
gradio_Interface(layout).queue(**config.queue).launch(**config.launch)
|
gradio_Interface(layout).queue(**config.queue).launch(**config.launch)
|
||||||
|
|
||||||
|
# Wait for the timer thread to finish
|
||||||
timer.join()
|
timer.join()
|
||||||
gv.MODEL_PROCESS.join()
|
gv.MODEL_PROCESS.join()
|
||||||
@@ -1,18 +1,47 @@
|
|||||||
"""
|
"""Starts the Gradio interface for audio transcription with optional configuration.
|
||||||
This script is used to start the Gradio interface for audio transcription.
|
|
||||||
A configuration file can be passed to the script to configure the interface.
|
This script, app_starter.py, initializes and runs a Gradio interface for audio
|
||||||
If no configuration file is passed, the default configuration is used.
|
transcription tasks. It allows users to provide a configuration file for custom
|
||||||
The main Reason for this script is to allow the use of multiprocessing in the app.
|
settings. If no configuration file is specified, default settings are applied.
|
||||||
|
The script is designed to support multiprocessing for improved performance.
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
args (argparse.Namespace): Parsed command line arguments.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
To run the script with custom server configuration and keyword arguments:
|
||||||
|
$ python app_starter.py --server-config path/to/config.yml --server-kwargs key1=val1 key2=val2
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import multiprocessing
|
import multiprocessing
|
||||||
from argparse import ArgumentParser, Action
|
from argparse import ArgumentParser, Action
|
||||||
|
|
||||||
class ParseKwargs(Action):
|
class ParseKwargs(Action):
|
||||||
"""
|
"""Custom action for argparse to parse keyword arguments for Gradio app configuration.
|
||||||
Custom argparse action to parse keyword arguments. has to bne redifined here because of multiprocessing.
|
|
||||||
|
This action parses a series of keyword arguments and converts them into a
|
||||||
|
dictionary, which is then used to configure the Gradio application. It
|
||||||
|
supports dynamic types by attempting to evaluate the argument values.
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
dest (str): The name of the attribute to be added to the object returned by parse_args().
|
||||||
"""
|
"""
|
||||||
def __call__(self, parser, namespace, values, option_string=None):
|
def __call__(self, parser, namespace, values, option_string=None):
|
||||||
|
"""Parses keyword arguments and updates the namespace with these arguments as a dictionary.
|
||||||
|
|
||||||
|
For each value provided, this method splits the string on the '=' character
|
||||||
|
to separate keys and values, attempting to evaluate the values for Python
|
||||||
|
literals. If evaluation fails, the raw string is used as the value.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
parser (ArgumentParser): The ArgumentParser object that called this method.
|
||||||
|
namespace (Namespace): An argparse.Namespace object that will be returned by parse_args().
|
||||||
|
values (list of str): List of strings, each representing a key-value pair in 'key=value' format.
|
||||||
|
option_string (Optional[str]): The option string that was used to invoke this action.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError: If any string in values does not contain the '=' character, indicating an invalid format.
|
||||||
|
"""
|
||||||
setattr(namespace, self.dest, dict())
|
setattr(namespace, self.dest, dict())
|
||||||
for value in values:
|
for value in values:
|
||||||
key, value = value.split('=')
|
key, value = value.split('=')
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ launch:
|
|||||||
ssl_certfile : null
|
ssl_certfile : null
|
||||||
ssl_keyfile_password : null
|
ssl_keyfile_password : null
|
||||||
ssl_verify : false
|
ssl_verify : false
|
||||||
quiet : false
|
|
||||||
show_api : false
|
show_api : false
|
||||||
allowed_paths : null
|
allowed_paths : null
|
||||||
blocked_paths : null
|
blocked_paths : null
|
||||||
|
|||||||
+26
-13
@@ -1,24 +1,37 @@
|
|||||||
"""
|
"""
|
||||||
Stores global variables for the app.
|
global_var.py
|
||||||
|
|
||||||
|
This module stores global variables for the app.
|
||||||
|
|
||||||
|
Global variables:
|
||||||
|
REQUEST_QUEUE (multiprocessing.Queue): A queue to store audio file paths as strings.
|
||||||
|
RESPONSE_QUEUE (multiprocessing.Queue): A queue to store transcriptions as strings.
|
||||||
|
LAST_ACTIVE_TIME (multiprocessing.Value): A value to store the time of the last activity.
|
||||||
|
LOADED_EVENT (multiprocessing.Event): An event to indicate when the model is loaded.
|
||||||
|
RUNNING_EVENT (multiprocessing.Event): An event to indicate when the model is running.
|
||||||
|
MODEL_PARAMS (Optional[dict]): A dictionary to store the model parameters.
|
||||||
|
MODEL_PROCESS (Optional[multiprocessing.Process]): A process to handle the model globally.
|
||||||
|
LAST_USED (float): A float to track the time of the last user activity.
|
||||||
|
TIMEOUT (Optional[int]): An integer to store the timeout in seconds.
|
||||||
|
DEFAULT_APP_CONIFG_PATH (str): A string to store the default path to the app configuration file.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Global variable to store the model
|
|
||||||
import multiprocessing
|
import multiprocessing
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
REQUEST_QUEUE = multiprocessing.Queue() # audio file path as string
|
REQUEST_QUEUE: multiprocessing.Queue = multiprocessing.Queue() # audio file path as string
|
||||||
RESPONSE_QUEUE = multiprocessing.Queue() # transcription as string
|
RESPONSE_QUEUE: multiprocessing.Queue = multiprocessing.Queue() # transcription as string
|
||||||
LAST_ACTIVE_TIME = multiprocessing.Value('d', time.time()) # time of last activity
|
LAST_ACTIVE_TIME: multiprocessing.Value = multiprocessing.Value('d', time.time()) # time of last activity
|
||||||
LOADED_EVENT = multiprocessing.Event() # model loaded event
|
LOADED_EVENT: multiprocessing.Event = multiprocessing.Event() # model loaded event
|
||||||
RUNNING_EVENT = multiprocessing.Event() # model running event
|
RUNNING_EVENT: multiprocessing.Event = multiprocessing.Event() # model running event
|
||||||
|
|
||||||
MODEL_PARAMS = None # model parameters
|
MODEL_PARAMS: Optional[dict] = None # model parameters
|
||||||
MODEL_PROCESS = None # model process to handle globally
|
MODEL_PROCESS: Optional[multiprocessing.Process] = None # model process to handle globally
|
||||||
|
|
||||||
# Global variable to track user activity
|
# Global variable to track user activity
|
||||||
LAST_USED = time.time()
|
LAST_USED: float = time.time()
|
||||||
TIMEOUT = None #seconds
|
TIMEOUT: Optional[int] = None # seconds
|
||||||
|
|
||||||
DEFAULT_APP_CONIFG_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.yml")
|
|
||||||
|
|
||||||
|
DEFAULT_APP_CONIFG_PATH: str = os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.yml")
|
||||||
@@ -3,8 +3,6 @@ This file contains ervery function that will be called when the user interacts w
|
|||||||
UI like pressing a button or uploading a file.
|
UI like pressing a button or uploading a file.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from re import M
|
|
||||||
import time
|
|
||||||
import gradio as gr
|
import gradio as gr
|
||||||
import scraibe.app.global_var as gv
|
import scraibe.app.global_var as gv
|
||||||
from scraibe import Transcript
|
from scraibe import Transcript
|
||||||
|
|||||||
@@ -1,11 +1,20 @@
|
|||||||
"""
|
"""
|
||||||
This file contains the actual gradio Interface which is used to interact with the user.
|
This module contains the gradio Interface which is used to interact with the user.
|
||||||
|
|
||||||
|
The interface is themed with a soft color scheme, with primary colors of green and orange, and a neutral color of gray.
|
||||||
|
|
||||||
|
A list of languages is also defined in this module, which may be used elsewhere in the application.
|
||||||
|
|
||||||
|
Classes:
|
||||||
|
Soft: A class from the gradio library used to theme the interface.
|
||||||
|
|
||||||
|
Variables:
|
||||||
|
theme (gr.themes.Soft): The theme for the gradio interface.
|
||||||
|
LANGUAGES (list of str): A list of languages supported by the application.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import gradio as gr
|
import gradio as gr
|
||||||
import os
|
|
||||||
|
|
||||||
import scraibe.app.global_var as gv
|
|
||||||
from .interactions import *
|
from .interactions import *
|
||||||
from .stg import *
|
from .stg import *
|
||||||
|
|
||||||
@@ -35,7 +44,20 @@ LANGUAGES = [
|
|||||||
|
|
||||||
|
|
||||||
def gradio_Interface(layout = None,):
|
def gradio_Interface(layout = None,):
|
||||||
|
"""
|
||||||
|
Creates a gradio interface for audio transcription.
|
||||||
|
|
||||||
|
The interface includes options for the user to select the task, number of speakers, translation, language, and input type.
|
||||||
|
It also provides options for the user to upload or record audio/video, or upload files.
|
||||||
|
The output of the transcription is displayed in a textbox, and the JSON output in a JSON viewer.
|
||||||
|
The user can also annotate the output by naming the speakers.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
layout (dict, optional): A dictionary containing layout information. Defaults to None.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
gr.Blocks: A gradio Blocks object representing the interface.
|
||||||
|
"""
|
||||||
with gr.Blocks(theme=theme,title='ScrAIbe: Automatic Audio Transcription') as demo:
|
with gr.Blocks(theme=theme,title='ScrAIbe: Automatic Audio Transcription') as demo:
|
||||||
|
|
||||||
# Define components
|
# Define components
|
||||||
|
|||||||
+80
-23
@@ -1,14 +1,30 @@
|
|||||||
"""
|
"""
|
||||||
This file contains the functions which are related to monitoring the actual app usage.
|
This module contains functions for managing and optimizing the resource usage of the application.
|
||||||
Therefore, the app is to be more efficient in the usage of the resources.
|
|
||||||
By for example, unloading or reloading the model.
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
The functions in this module monitor the application's usage and make adjustments to improve efficiency.
|
||||||
|
This includes managing the loading and unloading of the model based on the application's activity.
|
||||||
|
This dynamic management of resources helps to ensure that the application uses only the resources it needs,
|
||||||
|
improving overall performance and reducing unnecessary resource consumption.
|
||||||
|
|
||||||
|
Functions:
|
||||||
|
clear_queue(queue): Clears all items from the queue.
|
||||||
|
model_worker(model_params, request_queue, last_active_time,
|
||||||
|
response_queue, loaded_event, running_event, *args, **kwargs): Manages the model worker process.
|
||||||
|
|
||||||
|
Modules:
|
||||||
|
time: Provides various time-related functions.
|
||||||
|
gc: Provides an interface to the garbage collector.
|
||||||
|
multiprocessing: Provides support for parallel execution of code.
|
||||||
|
torch: Provides tensor computation and deep learning functionality.
|
||||||
|
gradio: Provides a simple way to create interactive UIs for Python functions.
|
||||||
|
scraibe.autotranscript: Provides automatic transcription functionality.
|
||||||
|
.stg: Contains the GradioTranscriptionInterface class.
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
import time
|
import time
|
||||||
import gc
|
import gc
|
||||||
from typing import Union
|
from typing import Union, Any
|
||||||
import multiprocessing
|
import multiprocessing
|
||||||
import torch
|
import torch
|
||||||
|
|
||||||
@@ -24,12 +40,27 @@ def clear_queue(queue):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
def model_worker(model_params : Union[Scraibe, dict],
|
def model_worker(model_params : Union[Scraibe, dict],
|
||||||
request_queue,
|
request_queue: multiprocessing.Queue,
|
||||||
last_active_time,
|
last_active_time: multiprocessing.Value,
|
||||||
response_queue,
|
response_queue: multiprocessing.Queue,
|
||||||
loaded_event,
|
loaded_event: multiprocessing.Event,
|
||||||
running_event,
|
running_event: multiprocessing.Event,
|
||||||
*args, **kwargs):
|
*args: Any, **kwargs: Any) -> None:
|
||||||
|
"""
|
||||||
|
Manages the model worker process.
|
||||||
|
|
||||||
|
The model worker process is responsible for running the model and returning the results.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
model_params (Union[Scraibe, dict]): The parameters for the Scraibe model.
|
||||||
|
request_queue (multiprocessing.Queue): The queue for incoming requests.
|
||||||
|
last_active_time (multiprocessing.Value): The last time the model was active.
|
||||||
|
response_queue (multiprocessing.Queue): The queue for outgoing responses.
|
||||||
|
loaded_event (multiprocessing.Event): An event that signals when the model is loaded.
|
||||||
|
running_event (multiprocessing.Event): An event that signals when the model is running.
|
||||||
|
*args: Additional arguments.
|
||||||
|
**kwargs: Additional keyword arguments.
|
||||||
|
"""
|
||||||
|
|
||||||
loaded_event.set()
|
loaded_event.set()
|
||||||
|
|
||||||
@@ -68,23 +99,49 @@ def model_worker(model_params : Union[Scraibe, dict],
|
|||||||
clear_queue(response_queue)
|
clear_queue(response_queue)
|
||||||
loaded_event.clear()
|
loaded_event.clear()
|
||||||
|
|
||||||
def start_model_worker(model_params,
|
def start_model_worker(model_params: Union[Scraibe, dict],
|
||||||
request_queue,
|
request_queue: multiprocessing.Queue,
|
||||||
last_active_time,
|
last_active_time: multiprocessing.Value,
|
||||||
response_queue,
|
response_queue: multiprocessing.Queue,
|
||||||
loaded_event,
|
loaded_event: multiprocessing.Event,
|
||||||
running_event,
|
running_event: multiprocessing.Event,
|
||||||
*args, **kwargs):
|
*args: Any, **kwargs: Any) -> multiprocessing.Process:
|
||||||
|
"""
|
||||||
|
Starts the model worker process.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
model_params (Union[Scraibe, dict]): The parameters for the Scraibe model.
|
||||||
|
request_queue (multiprocessing.Queue): The queue for incoming requests.
|
||||||
|
last_active_time (multiprocessing.Value): The last time the model was active.
|
||||||
|
response_queue (multiprocessing.Queue): The queue for outgoing responses.
|
||||||
|
loaded_event (multiprocessing.Event): An event that signals when the model is loaded.
|
||||||
|
running_event (multiprocessing.Event): An event that signals when the model is running.
|
||||||
|
*args: Additional arguments.
|
||||||
|
**kwargs: Additional keyword arguments.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
multiprocessing.Process: The model worker process.
|
||||||
|
"""
|
||||||
context = multiprocessing.get_context('spawn')
|
context = multiprocessing.get_context('spawn')
|
||||||
model_process = context.Process(target=model_worker, args=(model_params, request_queue, last_active_time, response_queue,loaded_event, running_event, *args), kwargs=kwargs)
|
model_process = context.Process(target=model_worker, args=(model_params, request_queue, last_active_time, response_queue,loaded_event, running_event, *args), kwargs=kwargs)
|
||||||
model_process.start()
|
model_process.start()
|
||||||
return model_process
|
return model_process
|
||||||
|
|
||||||
def timer_thread(request_queue,
|
def timer_thread(request_queue: multiprocessing.Queue,
|
||||||
last_active_time,
|
last_active_time: multiprocessing.Value,
|
||||||
loaded_event,
|
loaded_event: multiprocessing.Event,
|
||||||
running_event,
|
running_event: multiprocessing.Event,
|
||||||
timeout):
|
timeout: int) -> None:
|
||||||
|
"""
|
||||||
|
Monitors the model worker process and stops it after a period of inactivity.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
request_queue (multiprocessing.Queue): The queue for incoming requests.
|
||||||
|
last_active_time (multiprocessing.Value): The last time the model was active.
|
||||||
|
loaded_event (multiprocessing.Event): An event that signals when the model is loaded.
|
||||||
|
running_event (multiprocessing.Event): An event that signals when the model is running.
|
||||||
|
timeout (int): The period of inactivity after which the model worker process is stopped.
|
||||||
|
"""
|
||||||
while True:
|
while True:
|
||||||
time.sleep(timeout)
|
time.sleep(timeout)
|
||||||
|
|
||||||
|
|||||||
+72
-26
@@ -1,41 +1,65 @@
|
|||||||
"""
|
"""
|
||||||
stg - scraibe to gradio interface
|
stg - Scraibe to Gradio Interface
|
||||||
|
|
||||||
This file contains the code for the scraibe to gradio interface.
|
This module provides an interface between the Scraibe transcription system and the Gradio user interface.
|
||||||
It makes adds gradio interactions to the scraibe class in the back.
|
It defines a class, GradioTranscriptionInterface, that wraps the Scraibe model and provides methods for performing transcription tasks through the Gradio UI.
|
||||||
|
|
||||||
|
Modules:
|
||||||
|
json: Used for encoding and decoding JSON data.
|
||||||
|
gradio as gr: Used for creating the Gradio UI.
|
||||||
|
tqdm: Used for displaying progress bars.
|
||||||
|
scraibe.app.global_var as gv: Contains global variables for the Scraibe app.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import gradio as gr
|
import gradio as gr
|
||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
|
from typing import Any, Dict, Union, Tuple, List
|
||||||
|
|
||||||
|
|
||||||
import scraibe.app.global_var as gv
|
|
||||||
|
|
||||||
|
|
||||||
class GradioTranscriptionInterface:
|
class GradioTranscriptionInterface:
|
||||||
"""
|
"""
|
||||||
Interface handling the interaction between Gradio UI and the Audio Transcription system.
|
A class that provides an interface between the Gradio UI and the Scraibe transcription system.
|
||||||
|
|
||||||
|
This class wraps the Scraibe model and provides methods for performing transcription tasks through the Gradio UI.
|
||||||
|
These tasks include auto transcription, transcription, and diarisation.
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
model (Scraibe): The Scraibe model for performing transcription tasks.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, model):
|
def __init__(self, model) -> None:
|
||||||
"""
|
"""
|
||||||
Initializes the GradioTranscriptionInterface with a transcription model.
|
Initializes the GradioTranscriptionInterface with a Scraibe model.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
model (Scraibe): Model responsible for audio transcription tasks.
|
model (Scraibe): The Scraibe model for performing transcription tasks.
|
||||||
|
*args (Any): Additional positional arguments.
|
||||||
|
**kwargs (Dict[str, Any]): Additional keyword arguments.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self.model = model
|
self.model = model
|
||||||
|
|
||||||
def autotranscribe(self, source,
|
def autotranscribe(self, source: Union[str, List[str]],
|
||||||
num_speakers: int,
|
num_speakers: int,
|
||||||
translate: bool,
|
translate: bool,
|
||||||
language : str,*args ,**kwargs):
|
language: str,
|
||||||
|
*args: Any, **kwargs: Dict[str, Any]) -> Tuple[str, Union[str, dict]]:
|
||||||
"""
|
"""
|
||||||
Shortcut method for the Scraibe task.
|
Performs auto transcription on the given source.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
source (Union[str, List[str]]): The source to transcribe. This can be a string representing a single source,
|
||||||
|
or a list of strings representing multiple sources.
|
||||||
|
num_speakers (int): The number of speakers in the source.
|
||||||
|
translate (bool): Whether to translate the transcription.
|
||||||
|
language (str): The language of the source.
|
||||||
|
*args (Any): Additional positional arguments.
|
||||||
|
**kwargs (Dict[str, Any]): Additional keyword arguments.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
tuple: Transcribed text (str), JSON output (dict)
|
Tuple[str, Union[str, dict]]: A tuple containing the transcribed text (str) and the JSON output (str or dict).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
_kwargs = {
|
_kwargs = {
|
||||||
@@ -82,12 +106,23 @@ class GradioTranscriptionInterface:
|
|||||||
raise gr.Error("Please provide a valid audio file.")
|
raise gr.Error("Please provide a valid audio file.")
|
||||||
|
|
||||||
|
|
||||||
def transcribe(self, source, translate, language,*args ,**kwargs):
|
def transcribe(self, source: Union[str, List[str]],
|
||||||
|
translate: bool,
|
||||||
|
language: str,
|
||||||
|
*args: Any, **kwargs: Dict[str, Any]) -> str:
|
||||||
"""
|
"""
|
||||||
Shortcut method for the Transcribe task.
|
Performs transcription on the given source.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
source (Union[str, List[str]]): The source to transcribe.
|
||||||
|
This can be a string representing a single source, or a list of strings representing multiple sources.
|
||||||
|
translate (bool): Whether to translate the transcription.
|
||||||
|
language (str): The language of the source.
|
||||||
|
*args (Any): Additional positional arguments.
|
||||||
|
**kwargs (Dict[str, Any]): Additional keyword arguments.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
str: Transcribed text.
|
str: The transcribed text.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
_kwargs = {
|
_kwargs = {
|
||||||
@@ -118,14 +153,25 @@ class GradioTranscriptionInterface:
|
|||||||
else:
|
else:
|
||||||
raise gr.Error("Please provide a valid audio file.")
|
raise gr.Error("Please provide a valid audio file.")
|
||||||
|
|
||||||
def diarisation(self, source, num_speakers, *args ,**kwargs):
|
def diarisation(self, source: Union[str, List[str]],
|
||||||
|
num_speakers: int,
|
||||||
|
*args: Any, **kwargs: Dict[str, Any]) -> str:
|
||||||
"""
|
"""
|
||||||
Shortcut method for the Diarisation task.
|
Performs diarisation on the given source.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
source (Union[str, List[str]]): The source to perform diarisation on.
|
||||||
|
This can be a string representing a single source,
|
||||||
|
or a list of strings representing multiple sources.
|
||||||
|
num_speakers (int): The number of speakers in the source.
|
||||||
|
*args (Any): Additional positional arguments.
|
||||||
|
**kwargs (Dict[str, Any]): Additional keyword arguments.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
str: JSON output of diarisation result.
|
str: The JSON output of the diarisation result.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
_kwargs = {
|
_kwargs = {
|
||||||
"num_speakers": num_speakers if num_speakers != 0 else None,
|
"num_speakers": num_speakers if num_speakers != 0 else None,
|
||||||
}
|
}
|
||||||
@@ -160,15 +206,15 @@ class GradioTranscriptionInterface:
|
|||||||
else:
|
else:
|
||||||
gr.Error("Please provide a valid audio file.")
|
gr.Error("Please provide a valid audio file.")
|
||||||
|
|
||||||
def get_task_from_str(self, task):
|
def get_task_from_str(self, task: str) -> callable:
|
||||||
"""
|
"""
|
||||||
Returns the coresponing task function based on the task string.
|
Returns the corresponding task function based on the given task string.
|
||||||
|
|
||||||
params:
|
Args:
|
||||||
task (str): Task string. Can be one of the following:
|
task (str): The task string. This can be one of the following: 'Auto Transcribe', 'Transcribe', 'Diarisation'.
|
||||||
- 'Auto Transcribe'
|
|
||||||
- 'Transcribe'
|
Returns:
|
||||||
- 'Diarisation'
|
callable: The corresponding task function.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if task == 'Auto Transcribe':
|
if task == 'Auto Transcribe':
|
||||||
|
|||||||
+113
-31
@@ -1,22 +1,51 @@
|
|||||||
|
"""
|
||||||
|
utils.py
|
||||||
|
|
||||||
|
This module contains two classes, ConfigLoader and AppConfig, which are used to manage application-specific configuration settings.
|
||||||
|
|
||||||
|
The ConfigLoader class provides methods for loading a configuration file, applying overrides, and restoring default values for specified keys. It also includes methods for recursively updating nested keys and getting the default configuration.
|
||||||
|
|
||||||
|
The AppConfig class extends ConfigLoader and provides additional methods for setting global variables, launch options, and layout options from the configuration. It also includes methods for checking and setting file paths, and getting layout options.
|
||||||
|
|
||||||
|
Classes:
|
||||||
|
ConfigLoader: Manages application-specific configuration settings.
|
||||||
|
AppConfig: Extends ConfigLoader to provide additional methods for managing application-specific configuration settings.
|
||||||
|
"""
|
||||||
import os
|
import os
|
||||||
import warnings
|
import warnings
|
||||||
import yaml
|
import yaml
|
||||||
|
from typing import Any, Dict, Optional
|
||||||
|
|
||||||
import scraibe.app.global_var as gv
|
import scraibe.app.global_var as gv
|
||||||
|
|
||||||
CURRENT_PATH = os.path.dirname(os.path.realpath(__file__))
|
CURRENT_PATH = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
|
||||||
class ConfigLoader:
|
class ConfigLoader:
|
||||||
def __init__(self, config):
|
"""A class that extends ConfigLoader to manage application-specific configuration settings.
|
||||||
|
|
||||||
self.config = config
|
This class provides methods for setting global variables, launch options, and layout options from the configuration.
|
||||||
|
|
||||||
def restore_defaults_for_keys(self, *args):
|
Attributes:
|
||||||
|
config (Dict[str, Any]): The current configuration settings.
|
||||||
|
launch (Dict[str, Any]): The launch configuration settings.
|
||||||
|
model (Dict[str, Any]): The model configuration settings.
|
||||||
|
advanced (Dict[str, Any]): The advanced configuration settings.
|
||||||
|
queue (Dict[str, Any]): The queue configuration settings.
|
||||||
|
layout (Dict[str, Any]): The layout configuration settings.
|
||||||
"""
|
"""
|
||||||
Restores specified keys to their default values, including nested keys.
|
def __init__(self, config: Dict[str, Any]):
|
||||||
|
"""Initializes a new instance of the ConfigLoader class.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
keys (list): A list of keys or paths to keys (for nested dictionaries) to restore to default values.
|
config (dict): The configuration dictionary.
|
||||||
|
"""
|
||||||
|
self.config = config
|
||||||
|
|
||||||
|
def restore_defaults_for_keys(self, *args: str):
|
||||||
|
"""Restores specified keys to their default values, including nested keys.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
*args (str): A list of keys or paths to keys (for nested dictionaries) to restore to default values.
|
||||||
Each key or path should be a list of keys leading to the desired key.
|
Each key or path should be a list of keys leading to the desired key.
|
||||||
"""
|
"""
|
||||||
default_config = self.get_default_config()
|
default_config = self.get_default_config()
|
||||||
@@ -27,16 +56,15 @@ class ConfigLoader:
|
|||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def load_config(cls, yaml_path = None, **kwargs):
|
def load_config(cls, yaml_path: Optional[str] = None, **kwargs: Any) -> 'ConfigLoader':
|
||||||
"""
|
"""Load the configuration file and apply overrides.
|
||||||
Load the configuration file and apply overrides.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
yaml_path (str): Path to the YAML file containing overrides.
|
yaml_path (str, optional): Path to the YAML file containing overrides.
|
||||||
**kwargs: Additional overrides as keyword arguments.
|
**kwargs: Additional overrides as keyword arguments.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Config: A Config object with the loaded configuration.
|
ConfigLoader: A ConfigLoader object with the loaded configuration.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Load the original configuration
|
# Load the original configuration
|
||||||
@@ -54,8 +82,14 @@ class ConfigLoader:
|
|||||||
return cls(config)
|
return cls(config)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def apply_overrides(orig_dict, override_dict, specific=None):
|
def apply_overrides(orig_dict: Dict[str, Any], override_dict: Dict[str, Any], specific: Optional[str] = None):
|
||||||
""" Recursively apply overrides to the configuration, only for specific keys. """
|
"""Recursively apply overrides to the configuration, only for specific keys.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
orig_dict (Dict[str, Any]): The original dictionary.
|
||||||
|
override_dict (Dict[str, Any]): The override dictionary.
|
||||||
|
specific (str, optional): The specific key to override.
|
||||||
|
"""
|
||||||
for key, value in override_dict.items():
|
for key, value in override_dict.items():
|
||||||
|
|
||||||
if isinstance(value, dict):
|
if isinstance(value, dict):
|
||||||
@@ -80,7 +114,16 @@ class ConfigLoader:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def update_nested_key(d, key, value):
|
def update_nested_key(d, key, value):
|
||||||
""" Recursively search and update the key in nested dictionary. """
|
"""Recursively search and update the key in nested dictionary.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
d (Dict[str, Any]): The dictionary.
|
||||||
|
key (str): The key to update.
|
||||||
|
value (Any): The new value.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if the key was found and updated, False otherwise.
|
||||||
|
"""
|
||||||
|
|
||||||
if key in d:
|
if key in d:
|
||||||
d[key] = value
|
d[key] = value
|
||||||
@@ -92,16 +135,35 @@ class ConfigLoader:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_default_config():
|
def get_default_config():
|
||||||
""" Return the default configuration. """
|
"""Return the default configuration.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Dict[str, Any]: The default configuration.
|
||||||
|
"""
|
||||||
with open(gv.DEFAULT_APP_CONIFG_PATH , 'r') as file:
|
with open(gv.DEFAULT_APP_CONIFG_PATH , 'r') as file:
|
||||||
config = yaml.safe_load(file)
|
config = yaml.safe_load(file)
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
class AppConfig(ConfigLoader):
|
class AppConfig(ConfigLoader):
|
||||||
|
"""A class that extends ConfigLoader to manage application-specific configuration settings.
|
||||||
|
|
||||||
def __init__(self, config):
|
This class provides methods for setting global variables, launch options, and layout options from the configuration.
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
config (dict): The current configuration settings.
|
||||||
|
launch (dict): The launch configuration settings.
|
||||||
|
model (dict): The model configuration settings.
|
||||||
|
advanced (dict): The advanced configuration settings.
|
||||||
|
queue (dict): The queue configuration settings.
|
||||||
|
layout (dict): The layout configuration settings.
|
||||||
|
"""
|
||||||
|
def __init__(self, config : Dict[str, Any]):
|
||||||
|
"""Initializes a new instance of the AppConfig class.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
config (dict): The configuration dictionary.
|
||||||
|
"""
|
||||||
self.config = config
|
self.config = config
|
||||||
|
|
||||||
self.set_global_vars_from_config()
|
self.set_global_vars_from_config()
|
||||||
@@ -114,23 +176,28 @@ class AppConfig(ConfigLoader):
|
|||||||
self.queue = self.config.get("queue")
|
self.queue = self.config.get("queue")
|
||||||
self.layout = self.config.get("layout")
|
self.layout = self.config.get("layout")
|
||||||
|
|
||||||
def set_global_vars_from_config(self):
|
def set_global_vars_from_config(self) -> None:
|
||||||
"""
|
"""Sets the global variables from a configuration dictionary.
|
||||||
Sets the global variables from a configuration dictionary.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
config (dict): A dictionary containing the parameters for the model. Modify the default parameters in the config.yml file.
|
config (dict): A dictionary containing the parameters for the model. Modify the default parameters in the config.yml file.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
None
|
None
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
gv.MODEL_PARAMS = self.config.get('model')
|
gv.MODEL_PARAMS = self.config.get('model')
|
||||||
gv.TIMEOUT = self.config.get("advanced").get('timeout')
|
gv.TIMEOUT = self.config.get("advanced").get('timeout')
|
||||||
|
|
||||||
def set_launch_options(self):
|
def set_launch_options(self) -> None:
|
||||||
|
"""Sets the launch options from a configuration dictionary.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
None
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
None
|
||||||
|
"""
|
||||||
launch_options = self.config.get("launch")
|
launch_options = self.config.get("launch")
|
||||||
|
|
||||||
if launch_options.get('auth').pop('auth_enabled'):
|
if launch_options.get('auth').pop('auth_enabled'):
|
||||||
@@ -139,13 +206,28 @@ class AppConfig(ConfigLoader):
|
|||||||
else:
|
else:
|
||||||
self.config['launch']['auth'] = None
|
self.config['launch']['auth'] = None
|
||||||
|
|
||||||
def set_layout_options(self):
|
def set_layout_options(self) -> None:
|
||||||
|
"""Sets the layout options from a configuration dictionary.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
None
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
None
|
||||||
|
"""
|
||||||
self.config['layout']['header'] = self.check_and_set_path(self.config['layout'], 'header')
|
self.config['layout']['header'] = self.check_and_set_path(self.config['layout'], 'header')
|
||||||
self.config['layout']['footer'] = self.check_and_set_path(self.config['layout'], 'footer')
|
self.config['layout']['footer'] = self.check_and_set_path(self.config['layout'], 'footer')
|
||||||
self.config['layout']['logo'] = self.check_and_set_path(self.config['layout'], 'logo')
|
self.config['layout']['logo'] = self.check_and_set_path(self.config['layout'], 'logo')
|
||||||
|
|
||||||
def get_layout(self):
|
def get_layout(self) -> Dict[str, str]:
|
||||||
|
"""Gets the layout options from a configuration dictionary.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
None
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
dict: A dictionary containing the header and footer layout options.
|
||||||
|
"""
|
||||||
if not os.path.exists(self.config['layout']['header']) and \
|
if not os.path.exists(self.config['layout']['header']) and \
|
||||||
self.config['layout']['header'] == "scraibe/app/header.html":
|
self.config['layout']['header'] == "scraibe/app/header.html":
|
||||||
|
|
||||||
@@ -189,10 +271,16 @@ class AppConfig(ConfigLoader):
|
|||||||
'footer' : footer}
|
'footer' : footer}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def check_and_set_path(config_item, key):
|
def check_and_set_path(config_item: dict, key: str) -> Optional[str]:
|
||||||
"""
|
"""Check if the file exists at the given path. If not, try with CURRENT_PATH.
|
||||||
Check if the file exists at the given path. If not, try with CURRENT_PATH.
|
|
||||||
Raise FileNotFoundError if the file still doesn't exist.
|
Raise FileNotFoundError if the file still doesn't exist.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
config_item (dict): The configuration item.
|
||||||
|
key (str): The key to check in the configuration item.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: The path to the file if it exists, None otherwise.
|
||||||
"""
|
"""
|
||||||
_current_path = os.path.dirname(os.path.realpath(__file__)) # Define your CURRENT_PATH
|
_current_path = os.path.dirname(os.path.realpath(__file__)) # Define your CURRENT_PATH
|
||||||
|
|
||||||
@@ -208,9 +296,3 @@ class AppConfig(ConfigLoader):
|
|||||||
config_item[key] = new_path
|
config_item[key] = new_path
|
||||||
|
|
||||||
return config_item[key]
|
return config_item[key]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user