Spaces:
Running
on
Zero
Running
on
Zero
chore: add requirements
Browse fileschore: sample audio files
feat: update app
feat: update app
feat: update app
feat: update app
feat: update app
feat: update app
feat: update app
feat: update app
feat: update app
feat: update app
chore: update config
- .gitattributes +1 -0
- README.md +4 -4
- app.py +80 -59
- requirements.txt +7 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
*.wav filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
---
|
| 2 |
-
title: Gemma3n Audio
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.41.1
|
| 8 |
app_file: app.py
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Gemma3n Audio MN
|
| 3 |
+
emoji: 🎤
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.41.1
|
| 8 |
app_file: app.py
|
app.py
CHANGED
|
@@ -1,26 +1,26 @@
|
|
| 1 |
-
import
|
| 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
|
| 14 |
-
model = AutoModelForImageTextToText.from_pretrained(
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
model.eval()
|
| 18 |
|
| 19 |
print("Model loaded successfully!")
|
| 20 |
|
| 21 |
|
| 22 |
-
@spaces.GPU
|
| 23 |
-
|
|
|
|
| 24 |
if audio_file is None:
|
| 25 |
return "Please upload an audio file."
|
| 26 |
|
|
@@ -32,55 +32,44 @@ def process_audio(audio_file, prompt_type):
|
|
| 32 |
|
| 33 |
selected_prompt = prompts[prompt_type]
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
"
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 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 |
-
|
| 67 |
-
generated_tokens = outputs[:, input_length:]
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
clean_up_tokenization_spaces=False,
|
| 73 |
-
)
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
| 84 |
|
| 85 |
|
| 86 |
with gr.Blocks(title="Gemma 3n Audio Transcription & Translation") as demo:
|
|
@@ -113,22 +102,54 @@ with gr.Blocks(title="Gemma 3n Audio Transcription & Translation") as demo:
|
|
| 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 |
-
[
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
],
|
| 131 |
-
inputs=[audio_input, prompt_dropdown],
|
| 132 |
outputs=output_text,
|
| 133 |
fn=process_audio,
|
| 134 |
cache_examples=True,
|
|
|
|
| 1 |
+
from threading import Thread
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import spaces
|
| 5 |
import torch
|
| 6 |
from transformers import AutoModelForImageTextToText, AutoProcessor
|
| 7 |
+
from transformers.generation.streamers import TextIteratorStreamer
|
| 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)
|
| 14 |
+
model = AutoModelForImageTextToText.from_pretrained(
|
| 15 |
+
GEMMA_MODEL_ID, torch_dtype=torch.bfloat16, device_map="auto"
|
| 16 |
+
)
|
|
|
|
| 17 |
|
| 18 |
print("Model loaded successfully!")
|
| 19 |
|
| 20 |
|
| 21 |
+
@spaces.GPU(duration=60)
|
| 22 |
+
@torch.inference_mode()
|
| 23 |
+
def process_audio(audio_file, prompt_type, max_tokens):
|
| 24 |
if audio_file is None:
|
| 25 |
return "Please upload an audio file."
|
| 26 |
|
|
|
|
| 32 |
|
| 33 |
selected_prompt = prompts[prompt_type]
|
| 34 |
|
| 35 |
+
messages = [
|
| 36 |
+
{
|
| 37 |
+
"role": "user",
|
| 38 |
+
"content": [
|
| 39 |
+
{"type": "audio", "audio": audio_file},
|
| 40 |
+
{"type": "text", "text": selected_prompt},
|
| 41 |
+
],
|
| 42 |
+
}
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
input_ids = processor.apply_chat_template(
|
| 46 |
+
messages,
|
| 47 |
+
add_generation_prompt=True,
|
| 48 |
+
tokenize=True,
|
| 49 |
+
return_dict=True,
|
| 50 |
+
return_tensors="pt",
|
| 51 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
input_ids = input_ids.to(model.device, dtype=model.dtype)
|
|
|
|
| 54 |
|
| 55 |
+
streamer = TextIteratorStreamer(
|
| 56 |
+
processor, timeout=30.0, skip_prompt=True, skip_special_tokens=True
|
| 57 |
+
)
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
generate_kwargs = dict(
|
| 60 |
+
input_ids,
|
| 61 |
+
streamer=streamer,
|
| 62 |
+
max_new_tokens=max_tokens,
|
| 63 |
+
disable_compile=True,
|
| 64 |
+
)
|
| 65 |
|
| 66 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 67 |
+
t.start()
|
| 68 |
|
| 69 |
+
output = ""
|
| 70 |
+
for delta in streamer:
|
| 71 |
+
output += delta
|
| 72 |
+
yield output
|
| 73 |
|
| 74 |
|
| 75 |
with gr.Blocks(title="Gemma 3n Audio Transcription & Translation") as demo:
|
|
|
|
| 102 |
max_lines=20,
|
| 103 |
placeholder="Transcribed text will appear here...",
|
| 104 |
show_copy_button=True,
|
| 105 |
+
interactive=False,
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
with gr.Row():
|
| 109 |
+
with gr.Accordion("Additional Settings", open=False):
|
| 110 |
+
max_tokens_slider = gr.Slider(
|
| 111 |
+
minimum=16,
|
| 112 |
+
maximum=512,
|
| 113 |
+
value=128,
|
| 114 |
+
step=16,
|
| 115 |
+
label="Max New Tokens",
|
| 116 |
+
info="Maximum number of tokens to generate",
|
| 117 |
)
|
| 118 |
|
| 119 |
process_btn.click(
|
| 120 |
fn=process_audio,
|
| 121 |
+
inputs=[audio_input, prompt_dropdown, max_tokens_slider],
|
| 122 |
outputs=output_text,
|
| 123 |
)
|
| 124 |
|
| 125 |
gr.Examples(
|
| 126 |
examples=[
|
| 127 |
+
[
|
| 128 |
+
"https://github.com/bilguun0203/gemma3n-audio-mn/raw/refs/heads/main/audio_samples/en1.wav",
|
| 129 |
+
"Transcribe",
|
| 130 |
+
128,
|
| 131 |
+
],
|
| 132 |
+
[
|
| 133 |
+
"https://github.com/bilguun0203/gemma3n-audio-mn/raw/refs/heads/main/audio_samples/en3.wav",
|
| 134 |
+
"Transcribe EN to MN",
|
| 135 |
+
128,
|
| 136 |
+
],
|
| 137 |
+
[
|
| 138 |
+
"https://github.com/bilguun0203/gemma3n-audio-mn/raw/refs/heads/main/audio_samples/mn2.wav",
|
| 139 |
+
"Transcribe",
|
| 140 |
+
128,
|
| 141 |
+
],
|
| 142 |
+
[
|
| 143 |
+
"https://github.com/bilguun0203/gemma3n-audio-mn/raw/refs/heads/main/audio_samples/mn2.wav",
|
| 144 |
+
"Transcribe MN to EN",
|
| 145 |
+
128,
|
| 146 |
+
],
|
| 147 |
+
],
|
| 148 |
+
inputs=[
|
| 149 |
+
audio_input,
|
| 150 |
+
prompt_dropdown,
|
| 151 |
+
max_tokens_slider,
|
| 152 |
],
|
|
|
|
| 153 |
outputs=output_text,
|
| 154 |
fn=process_audio,
|
| 155 |
cache_examples=True,
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
datasets[audio]
|
| 2 |
+
peft
|
| 3 |
+
tensorboard
|
| 4 |
+
timm
|
| 5 |
+
torch<2.7
|
| 6 |
+
transformers<4.55
|
| 7 |
+
trl
|