Spaces:
Running
on
Zero
Running
on
Zero
feat: gradio app
Browse files
app.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gc
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import spaces
|
| 5 |
+
import torch
|
| 6 |
+
from transformers import AutoModelForImageTextToText, AutoProcessor
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
BASE_GEMMA_MODEL_ID = "google/gemma-3n-E2B-it"
|
| 10 |
+
GEMMA_MODEL_ID = "bilguun/gemma-3n-E2B-it-audio-en-mn"
|
| 11 |
+
|
| 12 |
+
print("Loading processor and model...")
|
| 13 |
+
processor = AutoProcessor.from_pretrained(BASE_GEMMA_MODEL_ID, device_map="cuda")
|
| 14 |
+
model = AutoModelForImageTextToText.from_pretrained(GEMMA_MODEL_ID, device_map="cuda")
|
| 15 |
+
|
| 16 |
+
if hasattr(model, "eval"):
|
| 17 |
+
model.eval()
|
| 18 |
+
|
| 19 |
+
print("Model loaded successfully!")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
@spaces.GPU
|
| 23 |
+
def process_audio(audio_file, prompt_type):
|
| 24 |
+
if audio_file is None:
|
| 25 |
+
return "Please upload an audio file."
|
| 26 |
+
|
| 27 |
+
prompts = {
|
| 28 |
+
"Transcribe": "Transcribe this audio.",
|
| 29 |
+
"Transcribe EN to MN": "Transcribe this audio into English and translate into Mongolian.",
|
| 30 |
+
"Transcribe MN to EN": "Transcribe this audio into Mongolian and translate into English.",
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
selected_prompt = prompts[prompt_type]
|
| 34 |
+
|
| 35 |
+
try:
|
| 36 |
+
messages = [
|
| 37 |
+
{
|
| 38 |
+
"role": "user",
|
| 39 |
+
"content": [
|
| 40 |
+
{"type": "audio", "audio": audio_file},
|
| 41 |
+
{"type": "text", "text": selected_prompt},
|
| 42 |
+
],
|
| 43 |
+
}
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
with torch.no_grad():
|
| 47 |
+
input_ids = processor.apply_chat_template(
|
| 48 |
+
messages,
|
| 49 |
+
add_generation_prompt=True,
|
| 50 |
+
tokenize=True,
|
| 51 |
+
return_dict=True,
|
| 52 |
+
return_tensors="pt",
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
input_ids = {
|
| 56 |
+
k: v.to(model.device, dtype=torch.long if "input_ids" in k else v.dtype)
|
| 57 |
+
for k, v in input_ids.items()
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
outputs = model.generate(
|
| 61 |
+
**input_ids,
|
| 62 |
+
max_new_tokens=128,
|
| 63 |
+
pad_token_id=processor.tokenizer.eos_token_id,
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
input_length = input_ids["input_ids"].shape[1]
|
| 67 |
+
generated_tokens = outputs[:, input_length:]
|
| 68 |
+
|
| 69 |
+
text = processor.batch_decode(
|
| 70 |
+
generated_tokens,
|
| 71 |
+
skip_special_tokens=True,
|
| 72 |
+
clean_up_tokenization_spaces=False,
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
if torch.cuda.is_available():
|
| 76 |
+
torch.cuda.empty_cache()
|
| 77 |
+
del input_ids, outputs, generated_tokens
|
| 78 |
+
gc.collect()
|
| 79 |
+
|
| 80 |
+
return text[0]
|
| 81 |
+
|
| 82 |
+
except Exception as e:
|
| 83 |
+
return f"Error processing audio: {str(e)}"
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
with gr.Blocks(title="Gemma 3n Audio Transcription & Translation") as demo:
|
| 87 |
+
gr.Markdown("# Gemma 3n E2B - English-Mongolian Audio Transcription & Translation")
|
| 88 |
+
gr.Markdown(
|
| 89 |
+
"Upload an audio file and select the processing type to get transcription and/or translation."
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
with gr.Row():
|
| 93 |
+
with gr.Column():
|
| 94 |
+
audio_input = gr.Audio(
|
| 95 |
+
label="Audio",
|
| 96 |
+
type="filepath",
|
| 97 |
+
sources=["upload", "microphone"],
|
| 98 |
+
max_length=300,
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
prompt_dropdown = gr.Dropdown(
|
| 102 |
+
choices=["Transcribe", "Transcribe EN to MN", "Transcribe MN to EN"],
|
| 103 |
+
value="Transcribe",
|
| 104 |
+
label="Prompt Type",
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
process_btn = gr.Button("Process Audio", variant="primary")
|
| 108 |
+
|
| 109 |
+
with gr.Column():
|
| 110 |
+
output_text = gr.Textbox(
|
| 111 |
+
label="Generated Output",
|
| 112 |
+
lines=10,
|
| 113 |
+
max_lines=20,
|
| 114 |
+
placeholder="Transcribed text will appear here...",
|
| 115 |
+
show_copy_button=True,
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
process_btn.click(
|
| 119 |
+
fn=process_audio,
|
| 120 |
+
inputs=[audio_input, prompt_dropdown],
|
| 121 |
+
outputs=output_text,
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
gr.Examples(
|
| 125 |
+
examples=[
|
| 126 |
+
["./audio_samples/en1.wav", "Transcribe"],
|
| 127 |
+
["./audio_samples/en3.wav", "Transcribe EN to MN"],
|
| 128 |
+
["./audio_samples/mn2.wav", "Transcribe"],
|
| 129 |
+
["./audio_samples/mn2.wav", "Transcribe MN to EN"],
|
| 130 |
+
],
|
| 131 |
+
inputs=[audio_input, prompt_dropdown],
|
| 132 |
+
outputs=output_text,
|
| 133 |
+
fn=process_audio,
|
| 134 |
+
cache_examples=True,
|
| 135 |
+
cache_mode="eager", # Cache examples eagerly for model warmup
|
| 136 |
+
label="Example Audio Files",
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
if __name__ == "__main__":
|
| 140 |
+
demo.launch()
|