File size: 11,023 Bytes
074e8ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
661fd83
4dedfa0
661fd83
 
 
 
4dedfa0
 
661fd83
 
 
 
4dedfa0
 
661fd83
 
 
 
4dedfa0
 
661fd83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4dedfa0
 
 
 
074e8ce
 
 
 
 
 
 
 
 
 
 
 
 
 
4ee79f3
661fd83
4ee79f3
661fd83
4ee79f3
661fd83
4ee79f3
661fd83
4ee79f3
 
 
82c1b69
661fd83
074e8ce
661fd83
074e8ce
 
 
 
 
 
 
4ee79f3
074e8ce
 
 
0627d63
074e8ce
 
 
 
 
661fd83
074e8ce
661fd83
074e8ce
 
 
661fd83
074e8ce
 
 
 
4ee79f3
661fd83
074e8ce
 
 
4ee79f3
 
82c1b69
 
 
 
661fd83
82c1b69
 
 
 
 
 
 
 
 
 
661fd83
82c1b69
 
 
 
 
 
661fd83
82c1b69
 
661fd83
82c1b69
074e8ce
 
 
 
 
661fd83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
074e8ce
 
661fd83
 
 
 
 
 
074e8ce
 
661fd83
 
 
 
 
 
 
 
 
074e8ce
661fd83
 
074e8ce
 
 
661fd83
 
 
 
 
 
 
 
074e8ce
 
661fd83
074e8ce
 
 
 
661fd83
074e8ce
 
661fd83
 
 
 
 
 
074e8ce
661fd83
 
82c1b69
 
 
661fd83
9aed552
074e8ce
661fd83
074e8ce
82c1b69
661fd83
074e8ce
 
661fd83
 
 
 
 
4ee79f3
661fd83
 
074e8ce
661fd83
 
074e8ce
661fd83
 
074e8ce
 
661fd83
 
 
 
 
 
074e8ce
 
661fd83
074e8ce
 
661fd83
 
 
 
 
68aa467
661fd83
 
 
68aa467
661fd83
 
 
68aa467
661fd83
074e8ce
 
661fd83
 
 
 
 
 
 
074e8ce
 
661fd83
82c1b69
074e8ce
ef89b24
 
 
82c1b69
074e8ce
661fd83
 
 
 
 
074e8ce
 
82c1b69
 
074e8ce
 
661fd83
074e8ce
 
82c1b69
 
 
 
661fd83
82c1b69
 
 
 
074e8ce
 
 
 
 
 
 
 
 
5420911
 
074e8ce
 
 
 
5420911
9515cfe
074e8ce
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
"""Explainor - AI Agent that explains any topic in fun persona voices.

MCP's 1st Birthday Hackathon Submission
Track: MCP in Action (Creative)
Team: kaiser-data
"""

import os
import tempfile
import gradio as gr
from dotenv import load_dotenv

from src.personas import PERSONAS, get_persona_names, get_persona
from src.agent import run_agent
from src.tts import generate_speech

# Load environment variables
load_dotenv()


# Custom CSS for better styling
CUSTOM_CSS = """
/* Dark mode input fix */
.dark input, .dark textarea {
    background-color: #374151 !important;
    color: #ffffff !important;
}

/* Header styling */
.header-container {
    text-align: center;
    padding: 1rem 0;
}

/* Card-like sections */
.input-section, .output-section {
    border-radius: 12px;
    padding: 1rem;
}

/* Primary button enhancement */
.primary-btn {
    font-size: 1.1rem !important;
    padding: 0.75rem 2rem !important;
}

/* Audio section layout */
.audio-row {
    display: flex;
    align-items: center;
    gap: 1rem;
}

/* Persona cards in examples */
.example-row {
    margin-top: 0.5rem;
}

/* Footer styling */
.footer {
    text-align: center;
    opacity: 0.8;
    font-size: 0.9rem;
}

/* MCP badge */
.mcp-badge {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 0.5rem 1rem;
    border-radius: 8px;
    display: inline-block;
    font-weight: bold;
}
"""


def format_sources(sources: list[dict]) -> str:
    """Format sources as markdown."""
    if not sources:
        return "*No external sources used*"

    md = ""
    for i, src in enumerate(sources, 1):
        if src.get("url"):
            md += f"{i}. [{src['title']}]({src['url']})\n"
        else:
            md += f"{i}. {src['title']} ({src.get('source', 'General')})\n"
    return md


def format_mcp_tools(tools: list[dict]) -> str:
    """Format tools used as markdown table."""
    if not tools:
        return "*Waiting for explanation...*"

    md = "| Tool | Description |\n|------|-------------|\n"
    for tool in tools:
        md += f"| {tool['icon']} `{tool['name']}` | {tool['desc']} |\n"
    return md


def explain_topic(topic: str, persona_name: str, audience: str = "", progress=gr.Progress()):
    """Main function to explain a topic in a persona's voice."""
    if not topic.strip():
        return "Please enter a topic to explain!", "", "", ""

    if not persona_name:
        persona_name = "5-Year-Old"

    steps_log = []
    explanation = ""
    sources = []
    mcp_tools = []

    progress(0, desc="Starting...")

    for update in run_agent(topic, persona_name, audience):
        if update["type"] == "step":
            step_text = f"**{update['title']}**\n{update['content']}"
            steps_log.append(step_text)

            if update["step"] == "research":
                progress(0.2, desc="πŸ” Researching...")
            elif update["step"] == "research_done":
                progress(0.4, desc="πŸ“š Research complete")
                if "sources" in update:
                    sources = update["sources"]
            elif update["step"] == "generating":
                progress(0.6, desc="🎭 Generating explanation...")

        elif update["type"] == "result":
            explanation = update["explanation"]
            sources = update.get("sources", sources)
            mcp_tools = update.get("mcp_tools", [])
            progress(1.0, desc="βœ… Done!")

    steps_md = "\n\n---\n\n".join(steps_log)
    sources_md = format_sources(sources)
    mcp_md = format_mcp_tools(mcp_tools)

    return explanation, sources_md, steps_md, mcp_md


def generate_audio(explanation: str, persona_name: str, progress=gr.Progress()):
    """Generate audio from the explanation text."""
    if not explanation or not explanation.strip():
        return None

    if not persona_name:
        persona_name = "5-Year-Old"

    persona = get_persona(persona_name)
    voice_id = persona["voice_id"]
    voice_settings = persona.get("voice_settings")

    progress(0.3, desc="πŸ”Š Generating audio...")

    try:
        audio_bytes = generate_speech(explanation, voice_id, voice_settings)
        with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as f:
            f.write(audio_bytes)
            audio_path = f.name
        progress(1.0, desc="βœ… Audio ready!")
        return audio_path
    except Exception as e:
        progress(1.0, desc="❌ Audio failed")
        raise gr.Error(f"Audio generation failed: {str(e)}")


def create_app():
    """Create and configure the Gradio app."""

    # Build persona choices
    persona_choices = [
        f"{PERSONAS[name]['emoji']} {name}"
        for name in get_persona_names()
    ]

    # Audience choices
    audience_choices = [
        "πŸ‘€ Just me",
        "πŸ‘΅ Confused grandmother",
        "πŸ€– Skeptical robot",
        "πŸ‘½ Alien visitor",
        "🧟 Zombie",
        "πŸ‘” Stressed CEO",
    ]

    with gr.Blocks(title="Explainor", fill_width=True) as app:

        # ===== HEADER =====
        gr.Markdown(
            """
            <div style="text-align: center; padding: 1rem 0;">
                <h1>🎭 Explainor</h1>
                <p style="font-size: 1.2rem; opacity: 0.9;">Learn anything through the voice of your favorite characters!</p>
            </div>
            """,
            elem_classes=["header-container"]
        )

        # ===== INPUT SECTION =====
        with gr.Group():
            # Topic input - full width, prominent
            topic_input = gr.Textbox(
                label="What do you want to learn about?",
                placeholder="Try: Quantum Computing, Blockchain, Black Holes, Climate Change...",
                lines=1,
                scale=2,
            )

            # Persona and Audience in one row
            with gr.Row():
                persona_dropdown = gr.Dropdown(
                    choices=persona_choices,
                    value=persona_choices[0],
                    label="🎭 Explainer",
                    scale=1,
                )
                audience_dropdown = gr.Dropdown(
                    choices=audience_choices,
                    value=audience_choices[0],
                    label="πŸ‘€ Audience",
                    scale=1,
                )

        # ===== ACTION BUTTON =====
        explain_btn = gr.Button(
            "✨ Explain it to me!",
            variant="primary",
            size="lg",
            elem_classes=["primary-btn"],
        )

        # ===== OUTPUT SECTION =====
        with gr.Group():
            explanation_output = gr.Textbox(
                label="πŸ“– Explanation",
                lines=6,
            )

            # Audio controls in a row
            with gr.Row():
                read_aloud_btn = gr.Button(
                    "πŸ”Š Read Aloud",
                    variant="secondary",
                    scale=1,
                )
                audio_output = gr.Audio(
                    label="Listen",
                    type="filepath",
                    autoplay=True,
                    scale=3,
                )

        # ===== DETAILS SECTION (Tabs) =====
        with gr.Accordion("πŸ“Š Details", open=False):
            with gr.Tabs():
                with gr.TabItem("πŸ”§ Agent Tools"):
                    mcp_output = gr.Markdown("*Run an explanation to see tool calls*")

                with gr.TabItem("πŸ“š Sources"):
                    sources_output = gr.Markdown("*Sources will appear here*")

                with gr.TabItem("πŸ” Trace"):
                    steps_output = gr.Markdown("*Execution trace will appear here*")

        # ===== EXAMPLES =====
        gr.Markdown("### πŸ’‘ Try these examples")
        gr.Examples(
            examples=[
                ["Quantum Computing", "πŸ‘Ά 5-Year-Old"],
                ["Blockchain", "πŸ‘¨β€πŸ³ Gordon Ramsay"],
                ["Black Holes", "πŸ΄β€β˜ οΈ Pirate"],
                ["Machine Learning", "🎭 Shakespeare"],
                ["Climate Change", "πŸ„ Surfer Dude"],
                ["The Force", "πŸ§™ Yoda"],
            ],
            inputs=[topic_input, persona_dropdown],
            label="",
        )

        # ===== MCP INFO =====
        with gr.Accordion("πŸ”Œ MCP Server", open=False):
            gr.Markdown(
                """
                This app is an **MCP Server**! Connect it to Claude Desktop or any MCP client:

                ```
                https://kaiser-data-mcp-1st-birthday-explainor.hf.space/gradio_api/mcp/sse
                ```

                **Available Tools:** `explain_topic`, `generate_audio`
                """
            )

        # ===== FOOTER =====
        gr.Markdown(
            """
            <div style="text-align: center; padding: 1rem 0; opacity: 0.7; font-size: 0.85rem;">
                <strong>MCP's 1st Birthday Hackathon</strong> Β· Track: MCP in Action (Creative)<br/>
                Powered by <a href="https://nebius.com">Nebius AI</a> + <a href="https://elevenlabs.io">ElevenLabs</a> Β·
                Made with ❀️ by <strong>kaiser-data</strong>
            </div>
            """,
            elem_classes=["footer"]
        )

        # ===== EVENT HANDLERS =====
        def process_and_explain(topic, persona_with_emoji, audience_with_emoji):
            persona_name = persona_with_emoji.split(" ", 1)[1] if " " in persona_with_emoji else persona_with_emoji
            audience = ""
            if audience_with_emoji and "Just me" not in audience_with_emoji:
                audience = audience_with_emoji.split(" ", 1)[1] if " " in audience_with_emoji else audience_with_emoji
            return explain_topic(topic, persona_name, audience)

        def process_audio(explanation, persona_with_emoji):
            persona_name = persona_with_emoji.split(" ", 1)[1] if " " in persona_with_emoji else persona_with_emoji
            return generate_audio(explanation, persona_name)

        # Explain button click
        explain_btn.click(
            fn=process_and_explain,
            inputs=[topic_input, persona_dropdown, audience_dropdown],
            outputs=[explanation_output, sources_output, steps_output, mcp_output],
        )

        # Enter key in topic input
        topic_input.submit(
            fn=process_and_explain,
            inputs=[topic_input, persona_dropdown, audience_dropdown],
            outputs=[explanation_output, sources_output, steps_output, mcp_output],
        )

        # Read aloud button
        read_aloud_btn.click(
            fn=process_audio,
            inputs=[explanation_output, persona_dropdown],
            outputs=[audio_output],
        )

    return app


# Create the app
app = create_app()

if __name__ == "__main__":
    enable_mcp = os.getenv("ENABLE_MCP_SERVER", "true").lower() == "true"

    app.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False,
        mcp_server=enable_mcp,
        css=CUSTOM_CSS,
    )