File size: 4,878 Bytes
8035662
 
 
 
096bc17
 
 
 
8035662
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import gradio as gr
import requests
from bs4 import BeautifulSoup
from kokoro_tts import generate_audio
import logging


logging.basicConfig(level=logging.INFO)


class Voices:
    flags = {
        "a": "๐Ÿ‡บ๐Ÿ‡ธ",
        "b": "๐Ÿ‡ฌ๐Ÿ‡ง",
        "e": "๐Ÿ‡ช๐Ÿ‡ธ",
        "f": "๐Ÿ‡ซ๐Ÿ‡ท",
        "h": "๐Ÿ‡ฎ๐Ÿ‡ณ",
        "i": "๐Ÿ‡ฎ๐Ÿ‡น",
        "j": "๐Ÿ‡ฏ๐Ÿ‡ต",
        "p": "๐Ÿ‡ง๐Ÿ‡ท",
        "z": "๐Ÿ‡จ๐Ÿ‡ณ",
    }

    flags_win = {
        "a": "american",
        "b": "british",
        "e": "spanish",
        "f": "french",
        "h": "hindi",
        "i": "italian",
        "j": "japanese",
        "p": "portuguese",
        "z": "chinese",
    }

    voices = {
        "a": [
            "af_alloy",
            "af_aoede",
            "af_bella",
            "af_heart",
            "af_jessica",
            "af_kore",
            "af_nicole",
            "af_nova",
            "af_river",
            "af_sarah",
            "af_sky",
            "am_adam",
            "am_echo",
            "am_eric",
            "am_fenrir",
            "am_liam",
            "am_michael",
            "am_onyx",
            "am_puck",
            "am_santa",
        ],
        "b": [
            "bf_alice",
            "bf_emma",
            "bf_isabella",
            "bf_lily",
            "bm_daniel",
            "bm_fable",
            "bm_george",
            "bm_lewis",
        ],
        "e": ["ef_dora", "em_alex", "em_santa"],
        "f": ["ff_siwis"],
        "h": ["hf_alpha", "hf_beta", "hm_omega", "hm_psi"],
        "i": ["if_sara", "im_nicola"],
        "j": ["jf_alpha", "jf_gongitsune", "jf_nezumi", "jf_tebukuro", "jm_kumo"],
        "p": ["pf_dora", "pm_alex", "pm_santa"],
        "z": [
            "zf_xiaobei",
            "zf_xiaoni",
            "zf_xiaoxiao",
            "zf_xiaoyi",
            "zm_yunjian",
            "zm_yunxi",
            "zm_yunxia",
            "zm_yunyang",
        ],
    }


def extract_text_from_url(url):
    try:
        # Download the webpage content
        response = requests.get(url)
        response.raise_for_status()  # Raise an exception for bad status codes

        # Parse the HTML content
        soup = BeautifulSoup(response.text, "html.parser")

        # Remove script and style elements
        for script in soup(["script", "style"]):
            script.decompose()

        # Get text and clean it up
        text = soup.get_text(separator="\n", strip=True)

        # Remove excessive newlines and whitespace
        lines = (line.strip() for line in text.splitlines())
        text = "\n".join(line for line in lines if line)

        return text
    except Exception as e:
        return f"Error: {str(e)}"


def get_language_choices():
    return [
        (f"{Voices.flags[code]} {Voices.flags_win[code].title()}", code)
        for code in Voices.voices.keys()
    ]


def get_voice_choices(lang_code):
    if lang_code in Voices.voices:
        return Voices.voices[lang_code]
    return []


def text_to_audio(text, lang_code, voice, progress=gr.Progress()):
    try:
        audio_data = generate_audio(
            text, lang_code=lang_code, voice=voice, progress=progress
        )
        return (24000, audio_data)  # Return tuple of (sample_rate, audio_data)
    except Exception as e:
        print(f"Error generating audio: {e}")
        return None


# Create Gradio interface
with gr.Blocks(title="Web Page Text Extractor & Audio Generator") as demo:
    gr.Markdown("# Web Page Text Extractor & Audio Generator")
    gr.Markdown(
        "Scrape a website and generate text using [hexgrad/Kokoro-82M](https://huggingface.co/hexgrad/Kokoro-82M)"
    )
    with gr.Row():
        url_input = gr.Textbox(
            label="Enter URL", value="https://paulgraham.com/words.html"
        )
        extract_btn = gr.Button("Extract Text")

    text_output = gr.Textbox(label="Extracted Text", lines=10, interactive=True)

    with gr.Row():
        lang_dropdown = gr.Dropdown(
            choices=get_language_choices(),
            label="Language",
            value="a",  # Default to English
        )
        voice_dropdown = gr.Dropdown(
            choices=Voices.voices["a"],  # Default to English voices
            label="Voice",
            value="am_onyx",  # Default voice
        )

    generate_btn = gr.Button("Generate Audio")
    audio_output = gr.Audio(label="Generated Audio")

    def update_voices(lang_code):
        return gr.Dropdown(choices=get_voice_choices(lang_code))

    extract_btn.click(fn=extract_text_from_url, inputs=url_input, outputs=text_output)
    lang_dropdown.change(fn=update_voices, inputs=lang_dropdown, outputs=voice_dropdown)
    generate_btn.click(
        fn=text_to_audio,
        inputs=[text_output, lang_dropdown, voice_dropdown],
        outputs=audio_output,
    )

if __name__ == "__main__":
    demo.launch()