File size: 12,139 Bytes
9834088 91a5d9a 9834088 91a5d9a 9834088 32ea10c 9834088 4798043 9834088 dcc06f1 32ea10c dcc06f1 32ea10c dcc06f1 32ea10c 9834088 91a5d9a 9834088 32ea10c 9834088 32ea10c 91a5d9a 9834088 41f55d6 9834088 91a5d9a 9834088 91a5d9a 9834088 2edc457 9834088 2edc457 25ccb9c 9834088 2edc457 32ea10c 9834088 41f55d6 | 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 | """
Sunbird AI Pipeline — Gradio frontend
Entry point: app.py
"""
import os
import tempfile
import traceback
import gradio as gr
from dotenv import load_dotenv
load_dotenv()
from backend.pipeline import run_pipeline
from backend.sunbird_client import TTS_SPEAKER_IDS
LANGUAGES = list(TTS_SPEAKER_IDS.keys()) # ["Acholi", "Ateso", "Runyankole", "Lugbara", "Luganda"]
# ── CSS theming ──────────────────────────────────────────────────────────────
CUSTOM_CSS = """
@import url('https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;500;600&family=Sora:wght@600;700&display=swap');
* { box-sizing: border-box; }
body, .gradio-container {
font-family: 'DM Sans', sans-serif !important;
background: #f5f0eb !important;
}
#header-box {
background: linear-gradient(135deg, #DC7828 0%, #b85e1a 100%);
border-radius: 14px;
padding: 28px 32px;
margin-bottom: 20px;
color: white;
box-shadow: 0 4px 24px rgba(220, 120, 40, 0.25);
}
#header-box h1 {
margin: 0 0 6px 0;
font-family: 'Sora', sans-serif;
font-size: 1.9rem;
letter-spacing: -0.02em;
text-align: center;
}
#header-box p {
margin: 0;
opacity: 0.88;
font-size: 0.95rem;
font-weight: 400;
}
.section-heading {
font-family: 'Sora', sans-serif;
font-size: 0.8rem;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.1em;
color: #8a6a50;
margin-bottom: 10px;
padding-bottom: 6px;
border-bottom: 1.5px solid #e0d4c8;
}
label.svelte-1b6s6s, .gradio-container label {
font-size: 0.82rem !important;
font-weight: 500 !important;
color: #5c4030 !important;
text-transform: uppercase !important;
letter-spacing: 0.06em !important;
}
textarea, input[type="text"] {
background: #fffaf6 !important;
border: 1.5px solid #e0d0c0 !important;
border-radius: 8px !important;
font-family: 'DM Sans', sans-serif !important;
font-size: 0.93rem !important;
color: #2d1a0a !important;
transition: border-color 0.15s ease;
}
textarea:focus, input[type="text"]:focus {
border-color: #DC7828 !important;
outline: none !important;
box-shadow: 0 0 0 3px rgba(220, 120, 40, 0.12) !important;
}
#run-btn {
background: #DC7828 !important;
color: white !important;
font-family: 'Sora', sans-serif !important;
font-size: 0.9rem !important;
font-weight: 600 !important;
letter-spacing: 0.04em !important;
border-radius: 8px !important;
border: none !important;
padding: 12px 24px !important;
transition: background 0.15s ease, transform 0.1s ease, box-shadow 0.15s ease !important;
box-shadow: 0 2px 12px rgba(220, 120, 40, 0.3) !important;
}
#run-btn:hover {
background: #b85e1a !important;
transform: translateY(-1px) !important;
box-shadow: 0 4px 18px rgba(220, 120, 40, 0.4) !important;
}
#run-btn:active {
transform: translateY(0) !important;
}
.gr-panel, .gr-box, .gr-form, [class*="block"] {
background: #fffaf6 !important;
border: 1.5px solid #e8ddd4 !important;
border-radius: 12px !important;
}
select, .gr-dropdown {
background: #fffaf6 !important;
border: 1.5px solid #e0d0c0 !important;
border-radius: 8px !important;
font-family: 'DM Sans', sans-serif !important;
color: #2d1a0a !important;
}
#error-box {
color: #c0392b;
background: #fff5f5;
border: 1.5px solid #c0392b;
border-radius: 8px;
padding: 10px 14px;
font-size: 0.9rem;
font-family: 'DM Sans', sans-serif;
margin-top: 8px;
}
.step-badge {
display: inline-block;
background: #DC7828;
color: white;
font-family: 'Sora', sans-serif;
font-size: 0.65rem;
font-weight: 700;
padding: 2px 8px;
border-radius: 20px;
letter-spacing: 0.08em;
margin-right: 6px;
vertical-align: middle;
}
.gr-examples {
background: transparent !important;
border: none !important;
}
.divider {
border: none;
border-top: 1.5px solid #e0d4c8;
margin: 16px 0;
}
"""
# ── Friendly error messages ─────────────────────────────────────────────────
def _friendly_error(e: Exception) -> str:
"""Convert raw technical exceptions into user-friendly messages."""
import requests as req
msg = str(e)
if isinstance(e, (req.exceptions.ConnectionError, ConnectionError, OSError)):
if "NameResolutionError" in msg or "getaddrinfo" in msg or "Failed to resolve" in msg:
return "Could not reach the server — please check your internet connection and try again."
return "A network error occurred. Please check your connection and try again."
if isinstance(e, req.exceptions.Timeout):
return "The request timed out. The server may be busy — please try again in a moment."
if isinstance(e, req.exceptions.HTTPError):
if "401" in msg or "403" in msg:
return "Authentication failed. Please check that your API token is correct."
if "429" in msg:
return "Too many requests — please wait a moment and try again."
if "5" in msg[:3]:
return "The Sunbird server returned an error. Please try again later."
return "The server returned an unexpected error. Please try again."
if isinstance(e, (ValueError, RuntimeError)):
return msg
return "Something went wrong. Please try again or contact support if the issue persists."
# ── Pipeline runner (called by Gradio) ──────────────────────────────────────
def process(input_mode, text_input, audio_input, target_language):
"""
Called when the user clicks Generate.
Validation raises gr.Error immediately (shows as toast, no generator quirk).
Pipeline errors are also raised as gr.Error from within the generator.
"""
print("TOKEN SET:", bool(os.environ.get("SUNBIRD_API_TOKEN")))
print(f"\n=== PROCESS CALLED ===")
print(f"Input mode: {input_mode}")
print(f"Text input: {text_input[:50] if text_input else 'None'}")
print(f"Audio input: {audio_input}")
print(f"Target language: {target_language}")
use_audio = (input_mode == "Audio Upload")
# ── Validation: raise gr.Error immediately, before any yield ──────────
if use_audio and not audio_input:
raise gr.Error("Please upload an audio file before running the pipeline.")
if not use_audio and not (text_input and text_input.strip()):
raise gr.Error("Please enter some text before running the pipeline.")
# ── Clear previous results ─────────────────────────────────────────────
yield ("", "", "", None, gr.update(value="", visible=False))
try:
audio_path = audio_input if use_audio else None
user_text = None if use_audio else text_input
print("Calling run_pipeline...")
transcript, summary, translation, audio_bytes = run_pipeline(
input_text=user_text,
audio_path=audio_path,
target_language=target_language,
)
print("Pipeline completed successfully")
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
tmp.write(audio_bytes)
tmp.flush()
tmp.close()
print(f"Audio saved to: {tmp.name}")
yield (
transcript,
summary,
translation,
tmp.name,
gr.update(value="", visible=False),
)
except gr.Error:
raise # let Gradio handle its own errors
except Exception as e:
print(f"\n=== EXCEPTION CAUGHT ===")
print(f"Exception type: {type(e).__name__}")
print(f"Exception message: {str(e)}")
traceback.print_exc()
raise gr.Error(_friendly_error(e))
# ── Build Gradio UI ──────────────────────────────────────────────────────────
def build_ui():
with gr.Blocks(title="GenAI", css=CUSTOM_CSS) as demo:
# Header
gr.HTML("""
<div id="header-box">
<h1>GenAI</h1>
</div>
""")
with gr.Row():
# ── Left column: inputs ──────────────────────────────────────────
with gr.Column(scale=1):
gr.HTML('<p class="section-heading">Input</p>')
input_mode = gr.Radio(
choices=["Text Input", "Audio Upload"],
value="Text Input",
label="Input type",
interactive=True,
)
text_input = gr.Textbox(
label="Text (ENG OR LUG)",
placeholder="Type or paste your text here…",
lines=6,
visible=True,
)
audio_input = gr.Audio(
label="Audio file (MP3, WAV, OGG, M4A, AAC — max 5 min)",
type="filepath",
sources=["upload"],
visible=False,
)
target_language = gr.Dropdown(
choices=LANGUAGES,
value="Luganda",
label="Target language for translation and speech",
)
run_btn = gr.Button("Generate", elem_id="run-btn", variant="primary")
# ── Right column: outputs ────────────────────────────────────────
with gr.Column(scale=1):
gr.HTML('<p class="section-heading">Results</p>')
transcript_out = gr.Textbox(
label="Transcript / Source text",
lines=4,
interactive=False,
)
summary_out = gr.Textbox(
label="English summary",
lines=4,
interactive=False,
)
translation_out = gr.Textbox(
label="Translated summary",
lines=4,
interactive=False,
)
audio_out = gr.Audio(
label="Synthesised speech",
type="filepath",
interactive=False,
)
# ── Toggle text / audio visibility ────────────────────────────────
def toggle_inputs(mode):
return (
gr.update(visible=(mode == "Text Input"), value=""),
gr.update(visible=(mode == "Audio Upload"), value=None),
gr.update(value=""),
gr.update(value=""),
gr.update(value=""),
gr.update(value=None),
)
input_mode.change(
fn=toggle_inputs,
inputs=[input_mode],
outputs=[text_input, audio_input, transcript_out, summary_out, translation_out, audio_out],
show_progress="hidden",
)
# ── Wire the Run button ────────────────────────────────────────────
run_btn.click(
fn=process,
inputs=[input_mode, text_input, audio_input, target_language],
outputs=[transcript_out, summary_out, translation_out, audio_out],
show_progress="full",
)
return demo
if __name__ == "__main__":
demo = build_ui()
demo.queue()
demo.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False) |