Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
|
|
| 1 |
|
| 2 |
-
# app.py: Workshop-in-a-Box Space with OpenTTS & Live Slide Previews
|
| 3 |
import os
|
| 4 |
import json
|
| 5 |
import tempfile
|
|
@@ -9,23 +9,26 @@ import gradio as gr
|
|
| 9 |
from agents import Agent, AgentRunner, handoff
|
| 10 |
from agents.extensions.handoff_prompt import RECOMMENDED_PROMPT_PREFIX
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
from
|
| 14 |
|
| 15 |
-
# Initialize
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def generate_tts_audio(script: str) -> str:
|
| 19 |
"""
|
| 20 |
-
Synthesize speech using
|
| 21 |
Returns the local file path to the audio.
|
| 22 |
"""
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
with open(out_path, "wb") as f:
|
| 28 |
-
f.write(audio_bytes)
|
| 29 |
return out_path
|
| 30 |
|
| 31 |
# --- Multi-Agent Scaffold ---
|
|
@@ -70,7 +73,7 @@ voice_agent = Agent(
|
|
| 70 |
name="Voiceover Agent",
|
| 71 |
instructions=(
|
| 72 |
f"{RECOMMENDED_PROMPT_PREFIX}\n"
|
| 73 |
-
"Create a 1-2 minute voiceover script. Return JSON with
|
| 74 |
)
|
| 75 |
)
|
| 76 |
|
|
@@ -118,11 +121,11 @@ def build_workshop_bundle(topic: str, audience: str):
|
|
| 118 |
jf.write(json.dumps(results, indent=2))
|
| 119 |
zipf.write(out_json, 'workshop_outputs.json')
|
| 120 |
|
| 121 |
-
# Slides
|
| 122 |
-
|
| 123 |
-
with open(
|
| 124 |
sf.write(json.dumps(slides_json, indent=2))
|
| 125 |
-
zipf.write(
|
| 126 |
|
| 127 |
slide_html_file = os.path.join(tmpdir, 'slides.html')
|
| 128 |
with open(slide_html_file, 'w') as hf:
|
|
@@ -143,7 +146,8 @@ def build_workshop_bundle(topic: str, audience: str):
|
|
| 143 |
|
| 144 |
# --- Gradio UI ---
|
| 145 |
def run_app(topic, audience):
|
| 146 |
-
|
|
|
|
| 147 |
|
| 148 |
with gr.Blocks(title='🚀 Workshop in a Box') as demo:
|
| 149 |
gr.Markdown('# Workshop in a Box')
|
|
|
|
| 1 |
+
## 1. `app.py`
|
| 2 |
|
|
|
|
| 3 |
import os
|
| 4 |
import json
|
| 5 |
import tempfile
|
|
|
|
| 9 |
from agents import Agent, AgentRunner, handoff
|
| 10 |
from agents.extensions.handoff_prompt import RECOMMENDED_PROMPT_PREFIX
|
| 11 |
|
| 12 |
+
# === Coqui TTS Integration ===
|
| 13 |
+
from TTS.api import TTS
|
| 14 |
|
| 15 |
+
# Initialize Coqui TTS (choose a model)
|
| 16 |
+
# Using Tacotron2-DDC model from LJSpeech
|
| 17 |
+
tts = TTS(
|
| 18 |
+
model_name="tts_models/en/ljspeech/tacotron2-DDC",
|
| 19 |
+
progress_bar=False,
|
| 20 |
+
gpu=False
|
| 21 |
+
)
|
| 22 |
|
| 23 |
def generate_tts_audio(script: str) -> str:
|
| 24 |
"""
|
| 25 |
+
Synthesize speech using Coqui TTS and save to a WAV file.
|
| 26 |
Returns the local file path to the audio.
|
| 27 |
"""
|
| 28 |
+
# Determine output path in temp directory
|
| 29 |
+
out_path = os.path.join(tempfile.gettempdir(), "voiceover.wav")
|
| 30 |
+
# Render the speech to the file
|
| 31 |
+
tts.tts_to_file(text=script, file_path=out_path)
|
|
|
|
|
|
|
| 32 |
return out_path
|
| 33 |
|
| 34 |
# --- Multi-Agent Scaffold ---
|
|
|
|
| 73 |
name="Voiceover Agent",
|
| 74 |
instructions=(
|
| 75 |
f"{RECOMMENDED_PROMPT_PREFIX}\n"
|
| 76 |
+
"Create a 1-2 minute voiceover script. Return JSON with key 'script'."
|
| 77 |
)
|
| 78 |
)
|
| 79 |
|
|
|
|
| 121 |
jf.write(json.dumps(results, indent=2))
|
| 122 |
zipf.write(out_json, 'workshop_outputs.json')
|
| 123 |
|
| 124 |
+
# Slides JSON & HTML
|
| 125 |
+
slide_json_file = os.path.join(tmpdir, 'slides.json')
|
| 126 |
+
with open(slide_json_file, 'w') as sf:
|
| 127 |
sf.write(json.dumps(slides_json, indent=2))
|
| 128 |
+
zipf.write(slide_json_file, 'slides.json')
|
| 129 |
|
| 130 |
slide_html_file = os.path.join(tmpdir, 'slides.html')
|
| 131 |
with open(slide_html_file, 'w') as hf:
|
|
|
|
| 146 |
|
| 147 |
# --- Gradio UI ---
|
| 148 |
def run_app(topic, audience):
|
| 149 |
+
slide_html, audio_path, zip_path = build_workshop_bundle(topic, audience)
|
| 150 |
+
return slide_html, audio_path, zip_path
|
| 151 |
|
| 152 |
with gr.Blocks(title='🚀 Workshop in a Box') as demo:
|
| 153 |
gr.Markdown('# Workshop in a Box')
|