File size: 2,366 Bytes
2c01ee6
 
b3385db
 
 
 
 
 
2c01ee6
b3385db
 
 
2c01ee6
 
b3385db
 
2c01ee6
b3385db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c01ee6
b3385db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c01ee6
 
 
 
 
 
b3385db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c01ee6
b3385db
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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"),
                interactive=True,
            )

            voice = gr.Dropdown(
                label=i18n("Voice"),
                choices=voice_manager.voices.keys(),
                value=voice_manager.voice_names[0],
            )

            synthesize_button = gr.Button(i18n("Synthesize"))

            status = gr.Textbox(visible=False)

            audio = gr.Audio(label=i18n("Export Audio"))

        with gr.Column():
            markdown = gr.Markdown(
                label=i18n("Document"),
            )

    fetch_button.click(
        fn=fetch_document,
        inputs=[source],
        outputs=[markdown, text],
    )

    synthesize_button.click(
        fn=run_tts_script,
        inputs=[text, voice],
        outputs=[status, audio],
    )