Skip to content

Commit

Permalink
Breaking up ds_tool to make it more manageable/readable (#16)
Browse files Browse the repository at this point in the history
* breaking up ds_tool to make it more manageable/readable

* move tts and chat client to their respective files
  • Loading branch information
farzadab authored Dec 11, 2024
1 parent d145193 commit c5b8fe8
Show file tree
Hide file tree
Showing 5 changed files with 387 additions and 349 deletions.
39 changes: 39 additions & 0 deletions ultravox/tools/ds_tool/ds_commons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import json
from typing import Any, Dict, Optional, Set

import jinja2

from ultravox.data import text_proc


def apply_jinja_template(
template: str, sample: Dict[str, Any], exclude_fields: Optional[Set[str]] = None
):
"""
Apply a Jinja template to a sample, rendering it into text.
Jinja template allows for added flexibility as template can include variables and functions.
Args:
template: The Jinja template to apply. It can include variables, functions, and control structures.
Example:
{{ text }}
{{ text_proc.format_asr_text(text) }}
sample: The sample to apply the template to.
exclude_fields: Fields to exclude from the sample before rendering the template, to avoid loading large fields into memory.
"""
if exclude_fields:
# Filter out big fields like audio before the sample is passed into the jinja template
# otherwise it can lead to unnecessary memory usage.
sample = {k: sample[k] for k in sample.keys() if k not in exclude_fields}

try:
return jinja2.Template(template, undefined=jinja2.StrictUndefined).render(
**sample, json_dump=json.dumps, text_proc=text_proc
)
except jinja2.TemplateError as e:
print(f"Error rendering template: {e}")
print(f"template: {template}")
print(f"sample keys: {list(sample.keys())}, excluded keys: {exclude_fields}")
raise ValueError(
f"Template rendering failed. Make sure all keys in the template exist in the sample."
) from e
Loading

0 comments on commit c5b8fe8

Please sign in to comment.