Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForSeq2SeqLM, BitsAndBytesConfig, AutoTokenizer
|
| 4 |
+
from IndicTransToolkit import IndicProcessor
|
| 5 |
+
import speech_recognition as sr
|
| 6 |
+
|
| 7 |
+
# Constants
|
| 8 |
+
BATCH_SIZE = 4
|
| 9 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
+
quantization = None
|
| 11 |
+
|
| 12 |
+
# ---- IndicTrans2 Model Initialization ----
|
| 13 |
+
def initialize_model_and_tokenizer(ckpt_dir, quantization):
|
| 14 |
+
if quantization == "4-bit":
|
| 15 |
+
qconfig = BitsAndBytesConfig(
|
| 16 |
+
load_in_4bit=True,
|
| 17 |
+
bnb_4bit_use_double_quant=True,
|
| 18 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
| 19 |
+
)
|
| 20 |
+
elif quantization == "8-bit":
|
| 21 |
+
qconfig = BitsAndBytesConfig(
|
| 22 |
+
load_in_8bit=True,
|
| 23 |
+
bnb_8bit_use_double_quant=True,
|
| 24 |
+
bnb_8bit_compute_dtype=torch.bfloat16,
|
| 25 |
+
)
|
| 26 |
+
else:
|
| 27 |
+
qconfig = None
|
| 28 |
+
|
| 29 |
+
tokenizer = AutoTokenizer.from_pretrained(ckpt_dir, trust_remote_code=True)
|
| 30 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(
|
| 31 |
+
ckpt_dir,
|
| 32 |
+
trust_remote_code=True,
|
| 33 |
+
low_cpu_mem_usage=True,
|
| 34 |
+
quantization_config=qconfig,
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
if qconfig is None:
|
| 38 |
+
model = model.to(DEVICE)
|
| 39 |
+
if DEVICE == "cuda":
|
| 40 |
+
model.half()
|
| 41 |
+
|
| 42 |
+
model.eval()
|
| 43 |
+
return tokenizer, model
|
| 44 |
+
|
| 45 |
+
def batch_translate(input_sentences, src_lang, tgt_lang, model, tokenizer, ip):
|
| 46 |
+
translations = []
|
| 47 |
+
for i in range(0, len(input_sentences), BATCH_SIZE):
|
| 48 |
+
batch = input_sentences[i : i + BATCH_SIZE]
|
| 49 |
+
batch = ip.preprocess_batch(batch, src_lang=src_lang, tgt_lang=tgt_lang)
|
| 50 |
+
inputs = tokenizer(
|
| 51 |
+
batch,
|
| 52 |
+
truncation=True,
|
| 53 |
+
padding="longest",
|
| 54 |
+
return_tensors="pt",
|
| 55 |
+
return_attention_mask=True,
|
| 56 |
+
).to(DEVICE)
|
| 57 |
+
|
| 58 |
+
with torch.no_grad():
|
| 59 |
+
generated_tokens = model.generate(
|
| 60 |
+
**inputs,
|
| 61 |
+
use_cache=True,
|
| 62 |
+
min_length=0,
|
| 63 |
+
max_length=256,
|
| 64 |
+
num_beams=5,
|
| 65 |
+
num_return_sequences=1,
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
with tokenizer.as_target_tokenizer():
|
| 69 |
+
generated_tokens = tokenizer.batch_decode(
|
| 70 |
+
generated_tokens.detach().cpu().tolist(),
|
| 71 |
+
skip_special_tokens=True,
|
| 72 |
+
clean_up_tokenization_spaces=True,
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
translations += ip.postprocess_batch(generated_tokens, lang=tgt_lang)
|
| 76 |
+
del inputs
|
| 77 |
+
torch.cuda.empty_cache()
|
| 78 |
+
|
| 79 |
+
return translations
|
| 80 |
+
|
| 81 |
+
# Initialize IndicTrans2
|
| 82 |
+
en_indic_ckpt_dir = "ai4bharat/indictrans2-indic-en-1B"
|
| 83 |
+
en_indic_tokenizer, en_indic_model = initialize_model_and_tokenizer(en_indic_ckpt_dir, quantization)
|
| 84 |
+
ip = IndicProcessor(inference=True)
|
| 85 |
+
|
| 86 |
+
# ---- Gradio Function ----
|
| 87 |
+
def transcribe_and_translate(audio):
|
| 88 |
+
recognizer = sr.Recognizer()
|
| 89 |
+
with sr.AudioFile(audio) as source:
|
| 90 |
+
audio_data = recognizer.record(source)
|
| 91 |
+
try:
|
| 92 |
+
# Malayalam transcription using Google API
|
| 93 |
+
malayalam_text = recognizer.recognize_google(audio_data, language="ml-IN")
|
| 94 |
+
except sr.UnknownValueError:
|
| 95 |
+
return "Could not understand audio", ""
|
| 96 |
+
except sr.RequestError as e:
|
| 97 |
+
return f"Google API Error: {e}", ""
|
| 98 |
+
|
| 99 |
+
# Translation
|
| 100 |
+
en_sents = [malayalam_text]
|
| 101 |
+
src_lang, tgt_lang = "mal_Mlym", "eng_Latn"
|
| 102 |
+
translations = batch_translate(en_sents, src_lang, tgt_lang, en_indic_model, en_indic_tokenizer, ip)
|
| 103 |
+
|
| 104 |
+
return malayalam_text, translations[0]
|
| 105 |
+
|
| 106 |
+
# ---- Gradio Interface ----
|
| 107 |
+
iface = gr.Interface(
|
| 108 |
+
fn=transcribe_and_translate,
|
| 109 |
+
inputs=gr.Audio(sources=["microphone", "upload"], type="filepath"),
|
| 110 |
+
outputs=[
|
| 111 |
+
gr.Textbox(label="Malayalam Transcription"),
|
| 112 |
+
gr.Textbox(label="English Translation")
|
| 113 |
+
],
|
| 114 |
+
title="Malayalam Speech Recognition & Translation",
|
| 115 |
+
description="Speak in Malayalam → Transcribe using Google Speech Recognition → Translate to English using IndicTrans2."
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
iface.launch(debug=True)
|