Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -29,6 +29,12 @@ SUPPORTED_TTS_LANGUAGES = {
|
|
29 |
"pt": "p", # Brazilian Portuguese
|
30 |
}
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
### 1️⃣ Fetch and Extract Content (Runs Immediately)
|
33 |
def fetch_and_display_content(url):
|
34 |
"""Fetch and extract text from a given URL (HTML or PDF)."""
|
@@ -40,7 +46,11 @@ def fetch_and_display_content(url):
|
|
40 |
text = extract(downloaded, output_format="markdown", with_metadata=True, include_tables=False, include_links=False, include_formatting=True, include_comments=False) #without metadata extraction
|
41 |
metadata, cleaned_text = extract_and_clean_text(text)
|
42 |
detected_lang = detect_language(cleaned_text)
|
43 |
-
|
|
|
|
|
|
|
|
|
44 |
|
45 |
### 2️⃣ Cleaning Function
|
46 |
def extract_and_clean_text(data):
|
@@ -92,9 +102,10 @@ def extract_and_clean_text(data):
|
|
92 |
|
93 |
return text
|
94 |
|
95 |
-
cleaned_text = clean_text(data)
|
96 |
|
97 |
-
return metadata_dict, cleaned_text
|
|
|
98 |
|
99 |
### 3️⃣ Language Detection
|
100 |
def detect_language(text):
|
@@ -111,7 +122,8 @@ def generate_audio_kokoro(text, lang):
|
|
111 |
"""Generate speech using KokoroTTS for supported languages."""
|
112 |
global kokoro_tts # Access the preloaded model
|
113 |
lang_code = SUPPORTED_TTS_LANGUAGES.get(lang, "a") # Default to English
|
114 |
-
generator = kokoro_tts(text, voice="af_bella", speed=1, split_pattern=r'\n+')
|
|
|
115 |
|
116 |
# Generate and collect audio data
|
117 |
audio_data_list = [audio for _, _, audio in generator]
|
@@ -149,24 +161,35 @@ with gr.Blocks() as demo:
|
|
149 |
url_input = gr.Textbox(label="Enter URL", placeholder="https://example.com/article")
|
150 |
#process_button = gr.Button("Generate Audio")
|
151 |
|
|
|
|
|
152 |
process_text_button = gr.Button("Fetch Text & Detect Language")
|
153 |
process_audio_button = gr.Button("Generate Audio", visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
|
155 |
-
extracted_text = gr.Markdown(label="Extracted Content")
|
156 |
-
detected_language = gr.Textbox(label="Detected Language")
|
157 |
-
full_audio_output = gr.Audio(label="Generated Audio")
|
|
|
158 |
|
159 |
# Step 1: Fetch Text & Detect Language First
|
160 |
process_text_button.click(
|
161 |
fetch_and_display_content,
|
162 |
inputs=[url_input],
|
163 |
-
outputs=[extracted_text, detected_language, process_audio_button, extracted_text]
|
|
|
164 |
)
|
165 |
-
|
166 |
# Step 2: Generate Audio After Text & Language Are Displayed
|
167 |
process_audio_button.click(
|
168 |
generate_audio_kokoro,
|
169 |
-
inputs=[extracted_text, detected_language],
|
|
|
170 |
outputs=[full_audio_output]
|
171 |
)
|
172 |
|
|
|
29 |
"pt": "p", # Brazilian Portuguese
|
30 |
}
|
31 |
|
32 |
+
# Available voices in KokoroTTS
|
33 |
+
AVAILABLE_VOICES = [
|
34 |
+
'af_bella', 'af_sarah', 'am_adam', 'am_michael', 'bf_emma',
|
35 |
+
'bf_isabella', 'bm_george', 'bm_lewis', 'af_nicole', 'af_sky'
|
36 |
+
]
|
37 |
+
|
38 |
### 1️⃣ Fetch and Extract Content (Runs Immediately)
|
39 |
def fetch_and_display_content(url):
|
40 |
"""Fetch and extract text from a given URL (HTML or PDF)."""
|
|
|
46 |
text = extract(downloaded, output_format="markdown", with_metadata=True, include_tables=False, include_links=False, include_formatting=True, include_comments=False) #without metadata extraction
|
47 |
metadata, cleaned_text = extract_and_clean_text(text)
|
48 |
detected_lang = detect_language(cleaned_text)
|
49 |
+
|
50 |
+
# Add detected language to metadata
|
51 |
+
metadata["Detected Language"] = detected_lang.upper()
|
52 |
+
#return cleaned_text, detected_lang, gr.update(visible=True), gr.update(visible=True)
|
53 |
+
return cleaned_text, metadata, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
|
54 |
|
55 |
### 2️⃣ Cleaning Function
|
56 |
def extract_and_clean_text(data):
|
|
|
102 |
|
103 |
return text
|
104 |
|
105 |
+
#cleaned_text = clean_text(data)
|
106 |
|
107 |
+
#return metadata_dict, cleaned_text
|
108 |
+
return metadata_dict, clean_text(data)
|
109 |
|
110 |
### 3️⃣ Language Detection
|
111 |
def detect_language(text):
|
|
|
122 |
"""Generate speech using KokoroTTS for supported languages."""
|
123 |
global kokoro_tts # Access the preloaded model
|
124 |
lang_code = SUPPORTED_TTS_LANGUAGES.get(lang, "a") # Default to English
|
125 |
+
#generator = kokoro_tts(text, voice="af_bella", speed=1, split_pattern=r'\n+')
|
126 |
+
generator = kokoro_tts(text, voice=selected_voice, speed=1, split_pattern=r'\n+')
|
127 |
|
128 |
# Generate and collect audio data
|
129 |
audio_data_list = [audio for _, _, audio in generator]
|
|
|
161 |
url_input = gr.Textbox(label="Enter URL", placeholder="https://example.com/article")
|
162 |
#process_button = gr.Button("Generate Audio")
|
163 |
|
164 |
+
voice_selection = gr.Dropdown(AVAILABLE_VOICES, label="Select Voice", value="af_bella")
|
165 |
+
|
166 |
process_text_button = gr.Button("Fetch Text & Detect Language")
|
167 |
process_audio_button = gr.Button("Generate Audio", visible=False)
|
168 |
+
|
169 |
+
# Layout: Two adjacent columns (Text and Metadata)
|
170 |
+
with gr.Row():
|
171 |
+
extracted_text = gr.Textbox(label="Extracted Content", visible=False, interactive=False, lines=15)
|
172 |
+
metadata_output = gr.JSON(label="Article Metadata", visible=False) # Displays metadata
|
173 |
+
|
174 |
|
175 |
+
#extracted_text = gr.Markdown(label="Extracted Content")
|
176 |
+
#detected_language = gr.Textbox(label="Detected Language")
|
177 |
+
#full_audio_output = gr.Audio(label="Generated Audio")
|
178 |
+
full_audio_output = gr.Audio(label="Generated Audio", visible=False)
|
179 |
|
180 |
# Step 1: Fetch Text & Detect Language First
|
181 |
process_text_button.click(
|
182 |
fetch_and_display_content,
|
183 |
inputs=[url_input],
|
184 |
+
#outputs=[extracted_text, detected_language, process_audio_button, extracted_text]
|
185 |
+
outputs=[extracted_text, metadata_output, process_audio_button, extracted_text, metadata_output]
|
186 |
)
|
187 |
+
|
188 |
# Step 2: Generate Audio After Text & Language Are Displayed
|
189 |
process_audio_button.click(
|
190 |
generate_audio_kokoro,
|
191 |
+
#inputs=[extracted_text, detected_language],
|
192 |
+
inputs=[extracted_text, metadata_output, voice_selection],
|
193 |
outputs=[full_audio_output]
|
194 |
)
|
195 |
|