Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
import os
|
|
|
|
|
2 |
|
3 |
import gradio as gr
|
4 |
import numpy as np
|
@@ -105,6 +107,47 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
105 |
generator = load_csm_1b(device=device)
|
106 |
|
107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
@spaces.GPU(duration=gpu_timeout)
|
109 |
def infer(
|
110 |
text_prompt_speaker_a,
|
@@ -203,6 +246,20 @@ def create_speaker_prompt_ui(speaker_name: str):
|
|
203 |
return speaker_dropdown, text_prompt_speaker, audio_prompt_speaker
|
204 |
|
205 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
with gr.Blocks() as app:
|
207 |
gr.Markdown(SPACE_INTRO_TEXT)
|
208 |
gr.Markdown("## Voices")
|
@@ -236,8 +293,35 @@ with gr.Blocks() as app:
|
|
236 |
speaker_b_dropdown.change(fn=update_text, inputs=[speaker_b_dropdown], outputs=[text_prompt_speaker_b])
|
237 |
|
238 |
gr.Markdown(CONVO_INTRO_TEXT)
|
239 |
-
|
240 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
241 |
generate_btn = gr.Button("Generate conversation", variant="primary")
|
242 |
gr.Markdown("GPU time limited to 3 minutes, for longer usage duplicate the space.")
|
243 |
audio_output = gr.Audio(label="Synthesized audio")
|
@@ -254,4 +338,4 @@ with gr.Blocks() as app:
|
|
254 |
outputs=[audio_output],
|
255 |
)
|
256 |
|
257 |
-
app.launch(ssr_mode=True)
|
|
|
1 |
import os
|
2 |
+
import subprocess
|
3 |
+
import tempfile
|
4 |
|
5 |
import gradio as gr
|
6 |
import numpy as np
|
|
|
107 |
generator = load_csm_1b(device=device)
|
108 |
|
109 |
|
110 |
+
def convert_ebook_to_txt(ebook_path):
|
111 |
+
"""Convert an ebook file to text using Calibre's ebook-convert."""
|
112 |
+
if not ebook_path:
|
113 |
+
return None
|
114 |
+
|
115 |
+
# Create a temporary file for the output
|
116 |
+
with tempfile.NamedTemporaryFile(suffix='.txt', delete=False) as temp_txt:
|
117 |
+
txt_path = temp_txt.name
|
118 |
+
|
119 |
+
try:
|
120 |
+
# Run ebook-convert from Calibre
|
121 |
+
subprocess.run(
|
122 |
+
["ebook-convert", ebook_path, txt_path],
|
123 |
+
check=True,
|
124 |
+
stdout=subprocess.PIPE,
|
125 |
+
stderr=subprocess.PIPE
|
126 |
+
)
|
127 |
+
|
128 |
+
# Read the converted text
|
129 |
+
with open(txt_path, 'r', encoding='utf-8') as f:
|
130 |
+
text_content = f.read()
|
131 |
+
|
132 |
+
# Clean up
|
133 |
+
os.unlink(txt_path)
|
134 |
+
|
135 |
+
# Format the text into alternating lines for conversation
|
136 |
+
lines = [line.strip() for line in text_content.split('.') if line.strip()]
|
137 |
+
formatted_lines = []
|
138 |
+
|
139 |
+
# Take up to 20 sentences to avoid extremely long conversations
|
140 |
+
for i, line in enumerate(lines[:20]):
|
141 |
+
formatted_lines.append(line + ".")
|
142 |
+
|
143 |
+
return "\n".join(formatted_lines)
|
144 |
+
|
145 |
+
except Exception as e:
|
146 |
+
if os.path.exists(txt_path):
|
147 |
+
os.unlink(txt_path)
|
148 |
+
raise gr.Error(f"Error converting ebook: {str(e)}")
|
149 |
+
|
150 |
+
|
151 |
@spaces.GPU(duration=gpu_timeout)
|
152 |
def infer(
|
153 |
text_prompt_speaker_a,
|
|
|
246 |
return speaker_dropdown, text_prompt_speaker, audio_prompt_speaker
|
247 |
|
248 |
|
249 |
+
def process_ebook(ebook_file):
|
250 |
+
if ebook_file is None:
|
251 |
+
return None
|
252 |
+
text_content = convert_ebook_to_txt(ebook_file)
|
253 |
+
return text_content
|
254 |
+
|
255 |
+
|
256 |
+
def update_input_method(choice):
|
257 |
+
if choice == "text_input":
|
258 |
+
return gr.update(visible=True), gr.update(visible=False), None
|
259 |
+
else:
|
260 |
+
return gr.update(visible=False), gr.update(visible=True), None
|
261 |
+
|
262 |
+
|
263 |
with gr.Blocks() as app:
|
264 |
gr.Markdown(SPACE_INTRO_TEXT)
|
265 |
gr.Markdown("## Voices")
|
|
|
293 |
speaker_b_dropdown.change(fn=update_text, inputs=[speaker_b_dropdown], outputs=[text_prompt_speaker_b])
|
294 |
|
295 |
gr.Markdown(CONVO_INTRO_TEXT)
|
296 |
+
|
297 |
+
# Radio button for selecting input method
|
298 |
+
input_method = gr.Radio(
|
299 |
+
["Direct text input", "Upload ebook file"],
|
300 |
+
label="Choose input method",
|
301 |
+
value="Direct text input"
|
302 |
+
)
|
303 |
+
|
304 |
+
# Container for text input method
|
305 |
+
with gr.Group(visible=True) as text_input_group:
|
306 |
+
gen_conversation_input = gr.TextArea(label="Conversation", lines=20, value=DEFAULT_CONVERSATION)
|
307 |
+
|
308 |
+
# Container for ebook upload method
|
309 |
+
with gr.Group(visible=False) as ebook_input_group:
|
310 |
+
ebook_file = gr.File(label="Upload ebook file (will be converted using Calibre)", file_types=[".epub", ".mobi", ".azw", ".azw3", ".fb2", ".pdf"])
|
311 |
+
process_ebook_btn = gr.Button("Process Ebook")
|
312 |
+
|
313 |
+
input_method.change(
|
314 |
+
fn=lambda choice: update_input_method("text_input" if choice == "Direct text input" else "ebook"),
|
315 |
+
inputs=[input_method],
|
316 |
+
outputs=[text_input_group, ebook_input_group, gen_conversation_input]
|
317 |
+
)
|
318 |
+
|
319 |
+
process_ebook_btn.click(
|
320 |
+
fn=process_ebook,
|
321 |
+
inputs=[ebook_file],
|
322 |
+
outputs=[gen_conversation_input]
|
323 |
+
)
|
324 |
+
|
325 |
generate_btn = gr.Button("Generate conversation", variant="primary")
|
326 |
gr.Markdown("GPU time limited to 3 minutes, for longer usage duplicate the space.")
|
327 |
audio_output = gr.Audio(label="Synthesized audio")
|
|
|
338 |
outputs=[audio_output],
|
339 |
)
|
340 |
|
341 |
+
app.launch(ssr_mode=True)
|