-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgradio_app.py
More file actions
895 lines (740 loc) · 31.9 KB
/
Copy pathgradio_app.py
File metadata and controls
895 lines (740 loc) · 31.9 KB
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
#!/usr/bin/env python3
"""
TraductAL Gradio Web Interface
Multilingual, multimodal translation system
A neuro-symbolic approach combining neural MT with Prolog-based validation
Supports 66+ languages with STT/TTS capabilities
"""
import os
import sys
import gradio as gr
import warnings
warnings.filterwarnings("ignore")
# Run startup check for required models
try:
from startup_check import check_models
print("🔍 Checking required models...")
if not check_models(interactive=False):
print("\n⚠️ Some required models are missing.")
print("💡 Run 'python startup_check.py' to download them interactively.")
# Continue anyway for development/testing
except ImportError:
print("⚠️ startup_check.py not found - skipping model verification")
except Exception as e:
print(f"⚠️ Model check failed: {e}")
# Continue anyway
try:
from unified_translator import UnifiedTranslator
print("✅ Unified translator loaded")
except ImportError as e:
print(f"❌ Error loading translator: {e}")
sys.exit(1)
try:
from tts_engine import TTSEngine
print("✅ TTS engine loaded")
tts_enabled = True
except ImportError as e:
print(f"⚠️ TTS engine not available: {e}")
tts_enabled = False
try:
from whisper_stt import WhisperSTT
print("✅ Whisper STT engine loaded")
whisper_enabled = True
except ImportError as e:
print(f"⚠️ Whisper STT not available: {e}")
whisper_enabled = False
# Initialize translator, TTS, and Whisper globally
translator = UnifiedTranslator()
if tts_enabled:
tts_engine = TTSEngine()
if whisper_enabled:
whisper_stt = WhisperSTT(model_size="base")
# Language options - Expanded for production (50+ languages)
# NLLB-200 supports 200 languages, showing the most commonly used ones
NLLB_LANGUAGES = {
# Core European Languages
"German": "de",
"English": "en",
"French": "fr",
"Italian": "it",
"Spanish": "es",
"Portuguese": "pt",
# Major World Languages
"Russian": "ru",
"Chinese": "zh",
"Hindi": "hi",
"Arabic": "ar",
"Japanese": "ja",
"Korean": "ko",
# Additional European Languages
"Dutch": "nl",
"Polish": "pl",
"Czech": "cs",
"Swedish": "sv",
"Danish": "da",
"Norwegian": "no",
"Finnish": "fi",
"Greek": "el",
"Turkish": "tr",
"Hungarian": "hu",
"Romanian": "ro",
# Major Asian Languages
"Vietnamese": "vi",
"Thai": "th",
"Indonesian": "id",
"Malay": "ms",
"Tamil": "ta",
"Bengali": "bn",
"Urdu": "ur",
"Persian": "fa",
"Hebrew": "he",
# African Languages
"Swahili": "sw",
"Amharic": "am",
"Hausa": "ha",
"Yoruba": "yo",
# Regional European Languages
"Catalan": "ca",
"Galician": "gl",
"Basque": "eu",
# Slavic Languages
"Ukrainian": "uk",
"Bulgarian": "bg",
"Serbian": "sr",
"Croatian": "hr",
"Slovak": "sk",
"Slovenian": "sl",
# Baltic & Other European
"Albanian": "sq",
"Macedonian": "mk",
"Lithuanian": "lt",
"Latvian": "lv",
"Estonian": "et",
"Icelandic": "is"
}
# Apertus-8B Specialist Languages (Swiss & Low-Resource Languages)
APERTUS_LANGUAGES = {
# Romansh Variants (Swiss)
"Romansh Sursilvan": "rm-sursilv",
"Romansh Vallader": "rm-vallader",
"Romansh Puter": "rm-puter",
"Romansh Surmiran": "rm-surmiran",
"Romansh Sutsilvan": "rm-sutsilv",
"Rumantsch Grischun": "rm-rumgr",
# Other Low-Resource Languages supported by Apertus
"Occitan": "oc",
"Breton": "br",
"Welsh": "cy",
"Scottish Gaelic": "gd",
"Irish": "ga",
"Luxembourgish": "lb",
"Friulian": "fur",
"Ladin": "lld",
"Sardinian": "sc"
}
# Keep backward compatibility
ROMANSH_VARIANTS = {k: v for k, v in APERTUS_LANGUAGES.items() if k.startswith("Romansh") or k.startswith("Rumantsch")}
COMMON_LANGUAGES = NLLB_LANGUAGES # For backward compatibility
# Languages with TTS support (MMS-TTS subset)
# Note: Not all languages have MMS-TTS models - only including confirmed ones
TTS_LANGUAGES = {
# Core languages with MMS-TTS support
"English": "en",
"German": "de",
"French": "fr",
"Spanish": "es",
"Portuguese": "pt",
"Russian": "ru",
"Hindi": "hi",
"Arabic": "ar",
"Korean": "ko",
# Additional European languages with TTS
"Dutch": "nl",
"Polish": "pl",
"Czech": "cs",
"Swedish": "sv",
"Finnish": "fi",
"Greek": "el",
"Turkish": "tr",
"Hungarian": "hu",
"Romanian": "ro",
# Additional Asian languages with TTS
"Vietnamese": "vi",
"Thai": "th",
"Indonesian": "id",
"Tamil": "ta",
"Bengali": "bn",
"Urdu": "ur",
"Persian": "fa",
"Hebrew": "he",
# African languages with TTS
"Swahili": "sw"
}
# Combine all languages for full translation support
ALL_LANGUAGES = {**NLLB_LANGUAGES, **APERTUS_LANGUAGES}
# Languages with STT support (Whisper supports 100+ languages)
# Including all NLLB languages + Apertus specialist languages
STT_LANGUAGES = {**NLLB_LANGUAGES, **APERTUS_LANGUAGES}
ENGINE_OPTIONS = {
"Auto (Recommended)": None,
"NLLB-200 (Fast)": "nllb",
"Apertus8B (Swiss Specialist)": "apertus"
}
# NLLB Model Options - user can select specific model
NLLB_MODEL_OPTIONS = {
"Auto (Best Available)": None,
"NLLB-200-3.3B (Highest Quality)": "nllb_200_3.3b",
"NLLB-200-1.3B (Fast)": "nllb_200_1.3b",
"NLLB-200-Distilled-1.3B (Fastest)": "nllb_200_distilled_1.3b"
}
def translate_text(text, src_lang_name, tgt_lang_name, engine_name, nllb_model_name, show_details):
"""Translate text with selected parameters."""
if not text.strip():
return "⚠️ Please enter text to translate", ""
src_code = ALL_LANGUAGES.get(src_lang_name)
tgt_code = ALL_LANGUAGES.get(tgt_lang_name)
engine = ENGINE_OPTIONS.get(engine_name)
model_name = NLLB_MODEL_OPTIONS.get(nllb_model_name) if nllb_model_name else None
if not src_code or not tgt_code:
return "❌ Invalid language selection", ""
# Perform translation
result = translator.translate(text, src_code, tgt_code, engine=engine, model_name=model_name)
if "error" in result:
return f"❌ Translation Error:\n{result['error']}", ""
translation = result.get("translation", "")
# Build details string
details = ""
if show_details:
details = f"""
**Translation Details:**
- **Engine**: {result.get('engine', 'Unknown')}
- **Model**: {result.get('model', 'Unknown')}
- **Time**: {result.get('total_time', result.get('time', 'Unknown'))}
- **Device**: {result.get('device', 'Unknown')}
"""
return translation, details
def batch_translate(file_content, src_lang_name, tgt_lang_name):
"""Batch translate lines from uploaded file."""
if not file_content:
return "⚠️ Please upload a text file"
src_code = ALL_LANGUAGES.get(src_lang_name)
tgt_code = ALL_LANGUAGES.get(tgt_lang_name)
lines = file_content.strip().split('\n')
translations = []
for i, line in enumerate(lines, 1):
if not line.strip():
translations.append("")
continue
result = translator.translate(line.strip(), src_code, tgt_code)
if "error" in result:
translations.append(f"[ERROR: {result['error']}]")
else:
translations.append(result.get("translation", ""))
return "\n".join(translations)
def transcribe_audio_multilang(audio_file, src_lang_name="Romansh Sursilvan"):
"""Transcribe audio to text using Whisper (general languages) or wav2vec2 (Romansh)."""
if audio_file is None:
return "⚠️ Please upload an audio file"
try:
src_code = STT_LANGUAGES.get(src_lang_name)
# Check if Romansh variant - use wav2vec2
if src_code and src_code.startswith('rm'):
print(f"🎤 Using wav2vec2 for Romansh transcription...")
# Import speech recognition libraries
import torch
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
import librosa
# Load wav2vec2 model from Hugging Face
model_name = "sammy786/wav2vec2-xlsr-romansh_sursilvan"
processor = Wav2Vec2Processor.from_pretrained(model_name)
model = Wav2Vec2ForCTC.from_pretrained(model_name)
# Load and preprocess audio
audio, rate = librosa.load(audio_file, sr=16000)
# Process audio
inputs = processor(audio, sampling_rate=16000, return_tensors="pt", padding=True)
with torch.no_grad():
logits = model(inputs.input_values).logits
# Decode
predicted_ids = torch.argmax(logits, dim=-1)
transcription = processor.batch_decode(predicted_ids)[0]
return transcription
# Use Whisper for other languages
elif whisper_enabled:
print(f"🎤 Using Whisper for {src_lang_name} transcription...")
# Get language code for Whisper
lang_code = None
if src_code in ['de', 'en', 'fr', 'it', 'es', 'pt', 'ru', 'zh', 'hi', 'ar', 'ja', 'ko']:
lang_code = src_code
transcription = whisper_stt.transcribe(audio_file, language=lang_code)
return transcription
else:
return "❌ Whisper STT not available. Install with: pip install openai-whisper"
except ImportError as e:
return f"❌ Missing library: {e}\nInstall with: pip install librosa transformers"
except Exception as e:
return f"❌ Transcription error: {str(e)}"
def transcribe_audio(audio_file):
"""Legacy function for backward compatibility - defaults to Romansh."""
return transcribe_audio_multilang(audio_file, "Romansh Sursilvan")
def audio_to_translation(audio_file, src_lang_name, tgt_lang_name):
"""Complete STT + Translation pipeline."""
if audio_file is None:
return "⚠️ Please upload an audio file", ""
# Step 1: Transcribe
transcription = transcribe_audio_multilang(audio_file, src_lang_name)
if transcription.startswith("❌") or transcription.startswith("⚠️"):
return transcription, ""
# Step 2: Translate
src_code = STT_LANGUAGES.get(src_lang_name)
tgt_code = ALL_LANGUAGES.get(tgt_lang_name)
result = translator.translate(transcription, src_code, tgt_code)
if "error" in result:
return transcription, f"❌ Translation failed: {result['error']}"
return transcription, result.get("translation", "")
def text_to_speech_simple(text, language_name):
"""Convert text to speech using TTS engine."""
if not tts_enabled:
return None, "❌ TTS engine not available"
if not text.strip():
return None, "⚠️ Please enter text to synthesize"
try:
audio_path, sample_rate = tts_engine.text_to_speech(text, language_name)
return audio_path, f"✅ Speech synthesized successfully!\n📊 Sample rate: {sample_rate}Hz"
except Exception as e:
return None, f"❌ TTS Error: {str(e)}"
def translate_and_speak(text, src_lang_name, tgt_lang_name):
"""Translate text and convert to speech."""
if not tts_enabled:
return "", None, "❌ TTS engine not available"
if not text.strip():
return "", None, "⚠️ Please enter text to translate"
# Step 1: Translate
src_code = ALL_LANGUAGES.get(src_lang_name)
tgt_code = ALL_LANGUAGES.get(tgt_lang_name)
result = translator.translate(text, src_code, tgt_code)
if "error" in result:
return f"❌ Translation Error:\n{result['error']}", None, ""
translation = result.get("translation", "")
# Step 2: Text-to-Speech
try:
audio_path, sample_rate = tts_engine.text_to_speech(translation, tgt_lang_name)
details = f"✅ Translation and speech synthesis complete!\n📊 Sample rate: {sample_rate}Hz"
return translation, audio_path, details
except Exception as e:
return translation, None, f"⚠️ Translation succeeded but TTS failed: {str(e)}"
def audio_to_audio_pipeline(audio_file, src_lang_name, tgt_lang_name):
"""Complete pipeline: Audio (any language) → Transcription → Translation → TTS."""
if not tts_enabled:
return "", "", None, "❌ TTS engine not available"
if audio_file is None:
return "", "", None, "⚠️ Please upload an audio file"
# Step 1: Transcribe
transcription = transcribe_audio_multilang(audio_file, src_lang_name)
if transcription.startswith("❌") or transcription.startswith("⚠️"):
return transcription, "", None, ""
# Step 2: Translate
src_code = STT_LANGUAGES.get(src_lang_name)
tgt_code = ALL_LANGUAGES.get(tgt_lang_name)
result = translator.translate(transcription, src_code, tgt_code)
if "error" in result:
return transcription, f"❌ Translation failed: {result['error']}", None, ""
translation = result.get("translation", "")
# Step 3: Text-to-Speech
try:
audio_path, sample_rate = tts_engine.text_to_speech(translation, tgt_lang_name)
details = f"✅ Complete pipeline successful!\n📊 Audio sample rate: {sample_rate}Hz"
return transcription, translation, audio_path, details
except Exception as e:
return transcription, translation, None, f"⚠️ TTS failed: {str(e)}"
# Create Gradio interface with professional theme
# Using Base theme for clean, professional look
with gr.Blocks(title="TraductAL - Multilingual, multimodal translation system", theme=gr.themes.Base()) as demo:
tts_status_msg = " + TTS" if tts_enabled else ""
whisper_status_msg = " + STT" if whisper_enabled else ""
# Count languages dynamically
total_languages = len(ALL_LANGUAGES)
nllb_count = len(NLLB_LANGUAGES)
apertus_count = len(APERTUS_LANGUAGES)
# Simplified professional header
gr.Markdown(f"""
## TraductAL — Multilingual, multimodal translation system
*A neuro-symbolic approach combining neural MT with Prolog-based validation*
Offline neural machine translation • {total_languages} languages ({nllb_count} NLLB + {apertus_count} specialist){tts_status_msg}{whisper_status_msg}
""")
with gr.Tabs():
# Tab 1: Text Translation
with gr.TabItem("Text Translation"):
gr.Markdown("**Note**: NLLB-200-3.3B recommended for literary translation (highest quality)")
with gr.Row():
with gr.Column():
src_lang = gr.Dropdown(
choices=sorted(list(ALL_LANGUAGES.keys())),
value="German",
label="Source Language",
filterable=True
)
input_text = gr.Textbox(
lines=6,
placeholder="Enter text to translate...",
label="Input Text"
)
with gr.Column():
tgt_lang = gr.Dropdown(
choices=sorted(list(ALL_LANGUAGES.keys())),
value="Romansh Sursilvan",
label="Target Language",
filterable=True
)
output_text = gr.Textbox(
lines=6,
label="Translation"
)
with gr.Row():
engine_choice = gr.Radio(
choices=list(ENGINE_OPTIONS.keys()),
value="Auto (Recommended)",
label="Translation Engine"
)
nllb_model_choice = gr.Dropdown(
choices=list(NLLB_MODEL_OPTIONS.keys()),
value="NLLB-200-3.3B (Highest Quality)",
label="NLLB Model (when using NLLB engine)",
filterable=False
)
show_details = gr.Checkbox(
value=True,
label="Show translation details"
)
with gr.Row():
translate_btn = gr.Button("Translate", variant="primary", size="lg")
clear_btn = gr.Button("Clear", variant="secondary", size="lg")
details_output = gr.Markdown(label="Details")
# Example translations
gr.Markdown("**Examples:**")
gr.Examples(
examples=[
["Guten Tag! Wie geht es Ihnen?", "German", "Romansh Sursilvan"],
["Willkommen in der Schweiz", "German", "Romansh Sursilvan"],
["Bonjour, comment allez-vous?", "French", "Romansh Vallader"],
["Hello, how are you?", "English", "Rumantsch Grischun"],
["Buongiorno, come sta?", "Italian", "Romansh Puter"],
],
inputs=[input_text, src_lang, tgt_lang]
)
translate_btn.click(
fn=translate_text,
inputs=[input_text, src_lang, tgt_lang, engine_choice, nllb_model_choice, show_details],
outputs=[output_text, details_output]
)
clear_btn.click(
fn=lambda: ("", "", ""),
inputs=[],
outputs=[input_text, output_text, details_output]
)
# Tab 2: Batch Translation
with gr.TabItem("Batch Translation"):
gr.Markdown("**Batch mode:** Translate multiple lines from file or pasted text. One sentence per line.")
with gr.Row():
with gr.Column():
batch_src_lang = gr.Dropdown(
choices=sorted(list(ALL_LANGUAGES.keys())),
value="German",
label="Source Language",
filterable=True
)
batch_file = gr.Textbox(
lines=10,
placeholder="Paste text here (one line per sentence) or upload file below...",
label="Input Text"
)
upload_file = gr.File(
label="Or upload .txt file",
file_types=[".txt"]
)
with gr.Column():
batch_tgt_lang = gr.Dropdown(
choices=sorted(list(ALL_LANGUAGES.keys())),
value="Romansh Sursilvan",
label="Target Language",
filterable=True
)
batch_output = gr.Textbox(
lines=10,
label="Translations"
)
with gr.Row():
batch_btn = gr.Button("Translate All", variant="primary", size="lg")
batch_clear_btn = gr.Button("Clear", variant="secondary", size="lg")
def load_file_content(file):
if file is None:
return ""
with open(file.name, 'r', encoding='utf-8') as f:
return f.read()
upload_file.change(
fn=load_file_content,
inputs=[upload_file],
outputs=[batch_file]
)
batch_btn.click(
fn=batch_translate,
inputs=[batch_file, batch_src_lang, batch_tgt_lang],
outputs=[batch_output]
)
batch_clear_btn.click(
fn=lambda: ("", ""),
inputs=[],
outputs=[batch_file, batch_output]
)
# Tab 3: Speech to Text (STT) - Multi-language with Whisper
with gr.TabItem("Speech to Text"):
gr.Markdown("**Automatic speech recognition:** Transcribe audio to text (100+ languages). Upload file or record directly.")
with gr.Row():
with gr.Column():
stt_src_lang = gr.Dropdown(
choices=sorted(list(STT_LANGUAGES.keys())),
value="English",
label="Audio Language",
filterable=True
)
audio_input = gr.Audio(
sources=["upload", "microphone"],
type="filepath",
label="Audio Input"
)
transcribe_btn = gr.Button("Transcribe", variant="primary")
with gr.Column():
transcription_output = gr.Textbox(
lines=8,
label="Transcription"
)
gr.Markdown("""
**Engines:** Whisper (100+ languages) • wav2vec2 (Romansh Sursilvan). Automatic engine selection based on source language.
""")
transcribe_btn.click(
fn=transcribe_audio_multilang,
inputs=[audio_input, stt_src_lang],
outputs=[transcription_output]
)
# Tab 4: Audio Translation Pipeline - Multi-language
with gr.TabItem("Audio Translation"):
gr.Markdown("**Complete pipeline:** Audio → Transcription → Translation. Upload audio in any language for automatic transcription and translation.")
with gr.Row():
with gr.Column():
audio_src_lang = gr.Dropdown(
choices=sorted(list(STT_LANGUAGES.keys())),
value="Romansh Sursilvan",
label="Audio Language",
filterable=True
)
audio_input_2 = gr.Audio(
sources=["upload", "microphone"],
type="filepath",
label="Audio Input"
)
audio_tgt_lang = gr.Dropdown(
choices=sorted(list(ALL_LANGUAGES.keys())),
value="German",
label="Translate to",
filterable=True
)
audio_translate_btn = gr.Button("Transcribe & Translate", variant="primary")
with gr.Column():
audio_transcription = gr.Textbox(
lines=4,
label="Transcription"
)
audio_translation = gr.Textbox(
lines=4,
label="Translation"
)
audio_translate_btn.click(
fn=audio_to_translation,
inputs=[audio_input_2, audio_src_lang, audio_tgt_lang],
outputs=[audio_transcription, audio_translation]
)
# Tab 5: Text-to-Speech (TTS)
if tts_enabled:
with gr.TabItem("Text-to-Speech"):
gr.Markdown("**Speech synthesis:** Generate audio from text in any supported language.")
with gr.Row():
with gr.Column():
tts_text = gr.Textbox(
lines=6,
placeholder="Enter text to convert to speech...",
label="Input Text"
)
tts_language = gr.Dropdown(
choices=sorted(list(TTS_LANGUAGES.keys())),
value="English",
label="Speech Language",
filterable=True
)
tts_btn = gr.Button("Generate Speech", variant="primary", size="lg")
with gr.Column():
tts_audio_output = gr.Audio(
label="Generated Speech",
type="filepath"
)
tts_status = gr.Textbox(
lines=3,
label="Status"
)
gr.Markdown("**Examples:**")
gr.Examples(
examples=[
["Hello! Welcome to the TraductAL translation system.", "English"],
["Guten Tag! Willkommen im TraductAL Übersetzungssystem.", "German"],
["Bonjour! Bienvenue dans le système de traduction TraductAL.", "French"],
["Buongiorno! Benvenuti nel sistema di traduzione TraductAL.", "Italian"],
["¡Hola! Bienvenido al sistema de traducción TraductAL.", "Spanish"],
],
inputs=[tts_text, tts_language]
)
tts_btn.click(
fn=text_to_speech_simple,
inputs=[tts_text, tts_language],
outputs=[tts_audio_output, tts_status]
)
# Tab 6: Translation + TTS
if tts_enabled:
with gr.TabItem("Translate & Speak"):
gr.Markdown("**Complete pipeline:** Text translation + speech synthesis.")
with gr.Row():
with gr.Column():
translate_tts_src_lang = gr.Dropdown(
choices=sorted(list(ALL_LANGUAGES.keys())),
value="German",
label="Source Language",
filterable=True
)
translate_tts_text = gr.Textbox(
lines=6,
placeholder="Enter text to translate and speak...",
label="Input Text"
)
with gr.Column():
translate_tts_tgt_lang = gr.Dropdown(
choices=sorted(list(TTS_LANGUAGES.keys())),
value="English",
label="Target Language (with speech)",
filterable=True
)
translate_tts_translation = gr.Textbox(
lines=6,
label="Translation"
)
translate_tts_btn = gr.Button("Translate & Speak", variant="primary", size="lg")
translate_tts_audio = gr.Audio(
label="Generated Speech",
type="filepath"
)
translate_tts_status = gr.Textbox(
lines=2,
label="Status"
)
translate_tts_btn.click(
fn=translate_and_speak,
inputs=[translate_tts_text, translate_tts_src_lang, translate_tts_tgt_lang],
outputs=[translate_tts_translation, translate_tts_audio, translate_tts_status]
)
# Tab 7: Complete Audio Pipeline (Audio → Audio) - Multi-language
if tts_enabled:
with gr.TabItem("Audio to Audio"):
gr.Markdown("**Complete audio pipeline:** Audio → Transcription → Translation → Speech. Upload audio in any language for spoken translation.")
with gr.Row():
with gr.Column():
pipeline_src_lang = gr.Dropdown(
choices=sorted(list(STT_LANGUAGES.keys())),
value="Romansh Sursilvan",
label="Audio Language",
filterable=True
)
pipeline_audio_input = gr.Audio(
sources=["upload", "microphone"],
type="filepath",
label="Audio Input"
)
pipeline_tgt_lang = gr.Dropdown(
choices=sorted(list(TTS_LANGUAGES.keys())),
value="German",
label="Target Language (with speech)",
filterable=True
)
pipeline_btn = gr.Button("Complete Pipeline", variant="primary", size="lg")
with gr.Column():
pipeline_transcription = gr.Textbox(
lines=3,
label="Transcription"
)
pipeline_translation = gr.Textbox(
lines=3,
label="Translation"
)
pipeline_audio_output = gr.Audio(
label="Generated Speech",
type="filepath"
)
pipeline_status = gr.Textbox(
lines=2,
label="Status"
)
pipeline_btn.click(
fn=audio_to_audio_pipeline,
inputs=[pipeline_audio_input, pipeline_src_lang, pipeline_tgt_lang],
outputs=[pipeline_transcription, pipeline_translation, pipeline_audio_output, pipeline_status]
)
# Tab 8: About & Info
with gr.TabItem("About"):
gr.Markdown("""
## About TraductAL
**TraductAL** is a multilingual, multimodal translation system using a neuro-symbolic approach that combines:
- **Neural models**: NLLB-200 and Apertus-8B for translation
- **Symbolic validation**: Treallo-Prolog dependency grammar parser for error checking and correction
This hybrid architecture helps identify and correct potential hallucinations from neural models.
### Translation Engines
**NLLB-200** (Meta AI)
- 200 languages, seq2seq architecture
- Models: 3.3B parameters (recommended), 1.3B, distilled-1.3B
- Inference: 0.5-2s per sentence
**Apertus8B** (Swiss AI - ETH Zurich & EPFL)
- 1,811 languages, causal LLM architecture
- 8B parameters, specialized for Swiss languages
- All 6 Romansh variants supported
- Released September 2025
**Treallo-Prolog Parser**
- Symbolic validation layer using dependency grammar
- Parses glossaries with Prolog-based lexicon
- Helps detect and correct neural model errors and hallucinations
### Romansh Variants
Sursilvan (55%), Vallader (20%), Puter (12%), Surmiran (10%), Sutsilvan (3%), Rumantsch Grischun (standard)
### Speech Recognition
**Whisper** (OpenAI): 100+ languages, whisper-base (74M parameters)
**wav2vec2-xlsr-romansh_sursilvan**: Romansh specialist, fine-tuned on Mozilla Common Voice 8.0. WER: 13.82%, CER: 3.02%
### Training Data
swiss-ai/apertus-posttrain-romansh: 46,092 German-Romansh parallel sentences (CC-BY-4.0)
### Privacy & Compliance
All processing performed locally. No external data transmission. GDPR compliant.
### Resources
Apertus8B: [huggingface.co/swiss-ai/Apertus-8B-2509](https://huggingface.co/swiss-ai/Apertus-8B-2509)
NLLB-200: [huggingface.co/facebook/nllb-200-1.3B](https://huggingface.co/facebook/nllb-200-1.3B)
Dataset: [huggingface.co/datasets/swiss-ai/apertus-posttrain-romansh](https://huggingface.co/datasets/swiss-ai/apertus-posttrain-romansh)
Version 1.0.0 • Apache 2.0 License
""")
gr.Markdown("""
---
**Note:** The system automatically selects the best engine — Apertus8B for Romansh translations, NLLB-200 for other language pairs.
""")
if __name__ == "__main__":
print("\n" + "="*60)
print("🌍 TraductAL - Multilingual, multimodal translation system")
print(" A neuro-symbolic approach combining neural MT with Prolog")
print("="*60)
print("🚀 Starting Gradio interface...")
print("📡 Server will be available at: http://localhost:7860")
print("="*60 + "\n")
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False,
show_error=True
)