import logging import gradio as gr from assets.i18n.i18n import I18nAuto from tts_service.docs import document_manager from tts_service.tts import run_tts_script from tts_service.utils import html_to_markdown, markdown_to_text from tts_service.voices import voice_manager i18n = I18nAuto() log = logging.getLogger(__name__) async def fetch_document(source: str) -> tuple[str, gr.Dataset]: log.info("Fetching document %s", source) doc = await document_manager.get_doc(source) if doc: overline = doc.get("overline") title = doc.get("title") underline = doc.get("underline") authors = doc.get("authors") content = doc["content"] pieces = [] if overline: pieces.append(f"### {overline}") if title: pieces.append(f"# {title}") if underline: pieces.append(f"### {underline}") if authors: pieces.append(f"#### By {','.join(authors)}") content = html_to_markdown(content) pieces.append(content) content = "\n\n".join(pieces) text = markdown_to_text(content) log.info("Successfully fetched document %s: %s chars", source, len(text)) return content, text return "", "" # TTS tab def workflow_tab(): with gr.Row(): with gr.Column(): source = gr.Textbox( label=i18n("Source"), info=i18n("Enter the document ID or URL."), ) fetch_button = gr.Button(i18n("Fetch")) text = gr.Textbox( label=i18n("Text"), visible=False, ) voice = gr.Dropdown( label=i18n("Voice"), choices=voice_manager.voices.keys(), value=voice_manager.voice_names[0], visible=len(voice_manager.voices) > 1, ) synthesize_button = gr.Button(i18n("Synthesize")) audio = gr.Audio( label=i18n("Generated Audio"), ) status = gr.Markdown( label=i18n("Status"), show_label=True, ) with gr.Column(): markdown = gr.Markdown( label=i18n("Document"), show_label=True, ) fetch_button.click( fn=fetch_document, inputs=[source], outputs=[markdown, text], ) synthesize_button.click( fn=run_tts_script, inputs=[text, voice], outputs=[status, audio], )