Spaces:
Sleeping
Sleeping
Create app. py
Browse files
app. py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import torch
|
| 4 |
+
import whisper
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
import subprocess
|
| 7 |
+
|
| 8 |
+
# =========================
|
| 9 |
+
# 1. LOAD MODELS
|
| 10 |
+
# =========================
|
| 11 |
+
asr_model = whisper.load_model("base")
|
| 12 |
+
|
| 13 |
+
translator = pipeline(
|
| 14 |
+
"translation",
|
| 15 |
+
model="facebook/nllb-200-distilled-600M"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
tts = pipeline(
|
| 19 |
+
"text-to-speech",
|
| 20 |
+
model="microsoft/speecht5_tts"
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# =========================
|
| 24 |
+
# 2. MAIN FUNCTION
|
| 25 |
+
# =========================
|
| 26 |
+
def translate_song(audio_file):
|
| 27 |
+
|
| 28 |
+
# STEP 1: TRANSCRIBE (vocals → text)
|
| 29 |
+
result = asr_model.transcribe(audio_file)
|
| 30 |
+
kinyarwanda_text = result["text"]
|
| 31 |
+
|
| 32 |
+
# STEP 2: TRANSLATE
|
| 33 |
+
translated = translator(
|
| 34 |
+
kinyarwanda_text,
|
| 35 |
+
src_lang="kin_Latn",
|
| 36 |
+
tgt_lang="eng_Latn"
|
| 37 |
+
)[0]["translation_text"]
|
| 38 |
+
|
| 39 |
+
# STEP 3: TEXT TO SPEECH (new voice)
|
| 40 |
+
speech = tts(translated)
|
| 41 |
+
|
| 42 |
+
output_audio = "translated.wav"
|
| 43 |
+
with open(output_audio, "wb") as f:
|
| 44 |
+
f.write(speech["audio"])
|
| 45 |
+
|
| 46 |
+
return output_audio, kinyarwanda_text, translated
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# =========================
|
| 50 |
+
# 3. GRADIO UI
|
| 51 |
+
# =========================
|
| 52 |
+
app = gr.Interface(
|
| 53 |
+
fn=translate_song,
|
| 54 |
+
inputs=gr.Audio(type="filepath"),
|
| 55 |
+
outputs=[
|
| 56 |
+
gr.Audio(),
|
| 57 |
+
gr.Textbox(label="Original Lyrics"),
|
| 58 |
+
gr.Textbox(label="Translated Lyrics")
|
| 59 |
+
],
|
| 60 |
+
title="🎵 AI Song Translator (Kinyarwanda → English)",
|
| 61 |
+
description="Upload a song in Kinyarwanda and get translated version as audio"
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
app.launch()
|