Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
## 1. `app.py`
|
| 2 |
+
```python
|
| 3 |
+
# app.py: Workshop-in-a-Box Hugging Face Space with TTS and Slide Previews
|
| 4 |
+
import os
|
| 5 |
+
import json
|
| 6 |
+
import tempfile
|
| 7 |
+
from zipfile import ZipFile
|
| 8 |
+
|
| 9 |
+
import gradio as gr
|
| 10 |
+
from agents import Agent, AgentRunner, handoff
|
| 11 |
+
from agents.extensions.handoff_prompt import RECOMMENDED_PROMPT_PREFIX
|
| 12 |
+
|
| 13 |
+
# Import your TTS/voice API module
|
| 14 |
+
from your_tts_module import generate_tts_audio # implement to return path to .mp3/.wav
|
| 15 |
+
|
| 16 |
+
# --- Multi-Agent Scaffold ---
|
| 17 |
+
# 1. Topic Agent
|
| 18 |
+
topic_agent = Agent(
|
| 19 |
+
name="Topic Agent",
|
| 20 |
+
instructions=(
|
| 21 |
+
f"{RECOMMENDED_PROMPT_PREFIX}\n"
|
| 22 |
+
"You are given a workshop topic and audience. Draft a structured learning path: goals, 4 modules, and a hands-on exercise for each."
|
| 23 |
+
)
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# 2. Content Agent
|
| 27 |
+
content_agent = Agent(
|
| 28 |
+
name="Content Agent",
|
| 29 |
+
instructions=(
|
| 30 |
+
f"{RECOMMENDED_PROMPT_PREFIX}\n"
|
| 31 |
+
"Convert the outline into detailed module scripts, speaker notes, and 3 quiz questions per module."
|
| 32 |
+
)
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# 3. Slide Agent
|
| 36 |
+
slide_agent = Agent(
|
| 37 |
+
name="Slide Agent",
|
| 38 |
+
instructions=(
|
| 39 |
+
f"{RECOMMENDED_PROMPT_PREFIX}\n"
|
| 40 |
+
"Given module content, produce slide JSON with title, bullet points, and design hints."
|
| 41 |
+
)
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# 4. Code Agent
|
| 45 |
+
code_agent = Agent(
|
| 46 |
+
name="Code Agent",
|
| 47 |
+
instructions=(
|
| 48 |
+
f"{RECOMMENDED_PROMPT_PREFIX}\n"
|
| 49 |
+
"Generate runnable Python code snippets or a Colab notebook for hands-on labs in each module."
|
| 50 |
+
)
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# 5. Voiceover Agent (Optional)
|
| 54 |
+
voice_agent = Agent(
|
| 55 |
+
name="Voiceover Agent",
|
| 56 |
+
instructions=(
|
| 57 |
+
f"{RECOMMENDED_PROMPT_PREFIX}\n"
|
| 58 |
+
"Create a 1-2 minute voiceover script. Return JSON with keys 'script' and optionally 'audio_url'."
|
| 59 |
+
)
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# Orchestrator: sequences agents
|
| 63 |
+
document_orchestrator = Agent(
|
| 64 |
+
name="Workshop Orchestrator",
|
| 65 |
+
instructions=(
|
| 66 |
+
"Invoke sub-agents: topic_agent, content_agent, slide_agent, code_agent, voice_agent (optional); collect outputs."
|
| 67 |
+
),
|
| 68 |
+
handoffs=[
|
| 69 |
+
handoff(topic_agent, name="outline"),
|
| 70 |
+
handoff(content_agent, name="content"),
|
| 71 |
+
handoff(slide_agent, name="slides"),
|
| 72 |
+
handoff(code_agent, name="code_labs"),
|
| 73 |
+
handoff(voice_agent, name="voiceover", optional=True),
|
| 74 |
+
]
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
runner = AgentRunner()
|
| 78 |
+
|
| 79 |
+
# --- Helper: Run pipeline and create outputs ---
|
| 80 |
+
def build_workshop_bundle(topic: str, audience: str):
|
| 81 |
+
prompt = f"Create a {topic} workshop for {audience}."
|
| 82 |
+
results = runner.run(document_orchestrator, prompt).outputs
|
| 83 |
+
|
| 84 |
+
# TTS audio
|
| 85 |
+
voice_info = results.get('voiceover', {})
|
| 86 |
+
audio_path = None
|
| 87 |
+
if 'script' in voice_info:
|
| 88 |
+
audio_path = generate_tts_audio(voice_info['script'])
|
| 89 |
+
|
| 90 |
+
# Render slides to HTML
|
| 91 |
+
slides_json = results.get('slides', {})
|
| 92 |
+
with open('static/slides_template.html') as f:
|
| 93 |
+
template = f.read()
|
| 94 |
+
slide_html = template.replace('{{SLIDES_JSON}}', json.dumps(slides_json))
|
| 95 |
+
|
| 96 |
+
# Create ZIP
|
| 97 |
+
tmp = tempfile.mkdtemp()
|
| 98 |
+
zipf_path = os.path.join(tmp, 'workshop_bundle.zip')
|
| 99 |
+
with ZipFile(zipf_path, 'w') as z:
|
| 100 |
+
for name, data in [
|
| 101 |
+
('workshop_outputs.json', json.dumps(results, indent=2)),
|
| 102 |
+
('slides.json', json.dumps(slides_json, indent=2)),
|
| 103 |
+
('slides.html', slide_html),
|
| 104 |
+
('code_labs.py', results.get('code_labs', ''))
|
| 105 |
+
]:
|
| 106 |
+
p = os.path.join(tmp, name)
|
| 107 |
+
with open(p, 'w') as f: f.write(data)
|
| 108 |
+
z.write(p, arcname=name)
|
| 109 |
+
if audio_path and os.path.exists(audio_path):
|
| 110 |
+
z.write(audio_path, arcname=os.path.basename(audio_path))
|
| 111 |
+
return slide_html, audio_path, zipf_path
|
| 112 |
+
|
| 113 |
+
# --- Gradio UI ---
|
| 114 |
+
def run_app(topic, audience):
|
| 115 |
+
slide_html, audio_path, zip_path = build_workshop_bundle(topic, audience)
|
| 116 |
+
return slide_html, audio_path, zip_path
|
| 117 |
+
|
| 118 |
+
with gr.Blocks(title='Workshop in a Box') as demo:
|
| 119 |
+
gr.Markdown('# 🚀 Workshop in a Box')
|
| 120 |
+
topic = gr.Textbox(label='Workshop Topic')
|
| 121 |
+
audience = gr.Textbox(label='Audience')
|
| 122 |
+
btn = gr.Button('Generate Workshop')
|
| 123 |
+
slide_preview = gr.HTML()
|
| 124 |
+
audio_player = gr.Audio(interactive=True)
|
| 125 |
+
download = gr.File()
|
| 126 |
+
btn.click(run_app, [topic, audience], [slide_preview, audio_player, download])
|
| 127 |
+
|
| 128 |
+
if __name__ == '__main__':
|
| 129 |
+
demo.launch()
|