Spaces:
Sleeping
Sleeping
Commit
ยท
7304960
1
Parent(s):
079cf4d
Debugging
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
|
|
@@ -67,7 +68,7 @@ def get_translation_history(direction):
|
|
| 67 |
# --- Translation ---
|
| 68 |
def translate(text, direction):
|
| 69 |
if not text.strip():
|
| 70 |
-
return "Enter some text.",
|
| 71 |
|
| 72 |
if direction == "en_to_ks":
|
| 73 |
src_lang, tgt_lang = "eng_Latn", "kas_Arab"
|
|
@@ -83,12 +84,12 @@ def translate(text, direction):
|
|
| 83 |
output = model.generate(**tokens, max_length=256, num_beams=5)
|
| 84 |
result = tokenizer.batch_decode(output, skip_special_tokens=True)
|
| 85 |
final = ip.postprocess_batch(result, lang=tgt_lang)[0]
|
| 86 |
-
return final,
|
| 87 |
except Exception as e:
|
| 88 |
print("Translation error:", e)
|
| 89 |
-
return "โ ๏ธ Translation failed.",
|
| 90 |
|
| 91 |
-
# --- TTS for English output
|
| 92 |
def synthesize_tts(text):
|
| 93 |
try:
|
| 94 |
tts = gTTS(text=text, lang="en")
|
|
@@ -100,25 +101,31 @@ def synthesize_tts(text):
|
|
| 100 |
return None
|
| 101 |
|
| 102 |
# --- STT only for en_to_ks ---
|
| 103 |
-
def
|
| 104 |
if direction != "en_to_ks":
|
| 105 |
-
return "โ ๏ธ Audio input is only supported for English to Kashmiri.", "",
|
| 106 |
|
| 107 |
try:
|
| 108 |
transcription = asr(audio_path)["text"]
|
| 109 |
except Exception as e:
|
| 110 |
print("STT error:", e)
|
| 111 |
-
return "โ ๏ธ Transcription failed.", "",
|
| 112 |
|
| 113 |
-
translated, _
|
| 114 |
-
return transcription, translated,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
# --- Switch UI direction ---
|
| 117 |
def switch_direction(direction, input_text_val, output_text_val):
|
| 118 |
new_direction = "ks_to_en" if direction == "en_to_ks" else "en_to_ks"
|
| 119 |
input_label = "Kashmiri Text" if new_direction == "ks_to_en" else "English Text"
|
| 120 |
output_label = "English Translation" if new_direction == "ks_to_en" else "Kashmiri Translation"
|
| 121 |
-
return new_direction, gr.update(value=output_text_val, label=input_label), gr.update(value=input_text_val, label=output_label)
|
| 122 |
|
| 123 |
# === Gradio Interface ===
|
| 124 |
with gr.Blocks() as interface:
|
|
@@ -126,8 +133,8 @@ with gr.Blocks() as interface:
|
|
| 126 |
translation_direction = gr.State(value="en_to_ks")
|
| 127 |
|
| 128 |
with gr.Row():
|
| 129 |
-
input_text = gr.Textbox(label="English Text", placeholder="Enter text here...")
|
| 130 |
-
output_text = gr.Textbox(label="Kashmiri Translation", placeholder="Translated text...")
|
| 131 |
|
| 132 |
with gr.Row():
|
| 133 |
translate_button = gr.Button("Translate")
|
|
@@ -139,21 +146,45 @@ with gr.Blocks() as interface:
|
|
| 139 |
|
| 140 |
with gr.Row():
|
| 141 |
audio_input = gr.Audio(type="filepath", label="๐๏ธ Record English audio")
|
| 142 |
-
audio_output = gr.Audio(label="๐ English TTS")
|
| 143 |
|
| 144 |
stt_button = gr.Button("๐ค Transcribe & Translate (EN โ KS Only)")
|
|
|
|
| 145 |
|
| 146 |
# Events
|
| 147 |
-
translate_button.click(
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
if __name__ == "__main__":
|
| 159 |
-
interface.queue().launch()
|
|
|
|
| 1 |
+
#app.py
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
|
|
|
|
| 68 |
# --- Translation ---
|
| 69 |
def translate(text, direction):
|
| 70 |
if not text.strip():
|
| 71 |
+
return "Enter some text.", None
|
| 72 |
|
| 73 |
if direction == "en_to_ks":
|
| 74 |
src_lang, tgt_lang = "eng_Latn", "kas_Arab"
|
|
|
|
| 84 |
output = model.generate(**tokens, max_length=256, num_beams=5)
|
| 85 |
result = tokenizer.batch_decode(output, skip_special_tokens=True)
|
| 86 |
final = ip.postprocess_batch(result, lang=tgt_lang)[0]
|
| 87 |
+
return final, None
|
| 88 |
except Exception as e:
|
| 89 |
print("Translation error:", e)
|
| 90 |
+
return "โ ๏ธ Translation failed.", None
|
| 91 |
|
| 92 |
+
# --- TTS for English output ---
|
| 93 |
def synthesize_tts(text):
|
| 94 |
try:
|
| 95 |
tts = gTTS(text=text, lang="en")
|
|
|
|
| 101 |
return None
|
| 102 |
|
| 103 |
# --- STT only for en_to_ks ---
|
| 104 |
+
def generate_stt_for_input(audio_path, direction):
|
| 105 |
if direction != "en_to_ks":
|
| 106 |
+
return "โ ๏ธ Audio input is only supported for English to Kashmiri.", "", None
|
| 107 |
|
| 108 |
try:
|
| 109 |
transcription = asr(audio_path)["text"]
|
| 110 |
except Exception as e:
|
| 111 |
print("STT error:", e)
|
| 112 |
+
return "โ ๏ธ Transcription failed.", "", None
|
| 113 |
|
| 114 |
+
translated, _ = translate(transcription, direction)
|
| 115 |
+
return transcription, translated, None
|
| 116 |
+
|
| 117 |
+
# --- Generate TTS for English output ---
|
| 118 |
+
def generate_tts_for_output(output_text, direction):
|
| 119 |
+
if direction == "ks_to_en" and output_text.strip():
|
| 120 |
+
return synthesize_tts(output_text)
|
| 121 |
+
return None
|
| 122 |
|
| 123 |
# --- Switch UI direction ---
|
| 124 |
def switch_direction(direction, input_text_val, output_text_val):
|
| 125 |
new_direction = "ks_to_en" if direction == "en_to_ks" else "en_to_ks"
|
| 126 |
input_label = "Kashmiri Text" if new_direction == "ks_to_en" else "English Text"
|
| 127 |
output_label = "English Translation" if new_direction == "ks_to_en" else "Kashmiri Translation"
|
| 128 |
+
return new_direction, gr.update(value=output_text_val, label=input_label), gr.update(value=input_text_val, label=output_label), None
|
| 129 |
|
| 130 |
# === Gradio Interface ===
|
| 131 |
with gr.Blocks() as interface:
|
|
|
|
| 133 |
translation_direction = gr.State(value="en_to_ks")
|
| 134 |
|
| 135 |
with gr.Row():
|
| 136 |
+
input_text = gr.Textbox(label="English Text", placeholder="Enter text here...", lines=2)
|
| 137 |
+
output_text = gr.Textbox(label="Kashmiri Translation", placeholder="Translated text...", lines=2)
|
| 138 |
|
| 139 |
with gr.Row():
|
| 140 |
translate_button = gr.Button("Translate")
|
|
|
|
| 146 |
|
| 147 |
with gr.Row():
|
| 148 |
audio_input = gr.Audio(type="filepath", label="๐๏ธ Record English audio")
|
| 149 |
+
audio_output = gr.Audio(label="๐ English TTS", interactive=False)
|
| 150 |
|
| 151 |
stt_button = gr.Button("๐ค Transcribe & Translate (EN โ KS Only)")
|
| 152 |
+
tts_button = gr.Button("๐ Generate English Speech (KS โ EN Only)")
|
| 153 |
|
| 154 |
# Events
|
| 155 |
+
translate_button.click(
|
| 156 |
+
fn=translate,
|
| 157 |
+
inputs=[input_text, translation_direction],
|
| 158 |
+
outputs=[output_text, audio_output]
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
tts_button.click(
|
| 162 |
+
fn=generate_tts_for_output,
|
| 163 |
+
inputs=[output_text, translation_direction],
|
| 164 |
+
outputs=audio_output
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
save_button.click(
|
| 168 |
+
fn=save_to_supabase,
|
| 169 |
+
inputs=[input_text, output_text, translation_direction],
|
| 170 |
+
outputs=save_status
|
| 171 |
+
).then(
|
| 172 |
+
fn=get_translation_history,
|
| 173 |
+
inputs=translation_direction,
|
| 174 |
+
outputs=history
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
switch_button.click(
|
| 178 |
+
fn=switch_direction,
|
| 179 |
+
inputs=[translation_direction, input_text, output_text],
|
| 180 |
+
outputs=[translation_direction, input_text, output_text, audio_output]
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
stt_button.click(
|
| 184 |
+
fn=generate_stt_for_input,
|
| 185 |
+
inputs=[audio_input, translation_direction],
|
| 186 |
+
outputs=[input_text, output_text, audio_output]
|
| 187 |
+
)
|
| 188 |
|
| 189 |
if __name__ == "__main__":
|
| 190 |
+
interface.queue().launch(share=True)
|