Spaces:
Runtime error
Runtime error
Download app.py from techsalomon/godai-mms-api: direct link, hf CLI and curl.
- Browser
- Download file 3.91 kB
-
https://huggingface.co/spaces/techsalomon/godai-mms-api/resolve/main/app.py
- Command line
-
hf download hf://spaces/techsalomon/godai-mms-api/app.py
-
curl -L -o app.py https://huggingface.co/spaces/techsalomon/godai-mms-api/resolve/main/app.py
3.91 kB
| import gradio as gr | |
| import torch | |
| import torch.nn as nn | |
| import numpy as np | |
| import os | |
| from transformers import ( | |
| Wav2Vec2ForCTC, | |
| Wav2Vec2Processor, | |
| Wav2Vec2CTCTokenizer, | |
| Wav2Vec2FeatureExtractor | |
| ) | |
| from huggingface_hub import hf_hub_download, login | |
| # ββ Login ββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| HF_TOKEN = os.environ.get("HF_TOKEN") | |
| if HF_TOKEN: | |
| login(token=HF_TOKEN) | |
| REPO_ID = "techsalomon/godai-african-asr" | |
| VOCAB_SIZE = 75 | |
| print("Downloading Mina adapter...") | |
| adapter_path = hf_hub_download( | |
| repo_id=REPO_ID, | |
| filename="adapter_mina_latest.pt", | |
| token=HF_TOKEN | |
| ) | |
| vocab_path = hf_hub_download( | |
| repo_id=REPO_ID, | |
| filename="vocab.json", | |
| token=HF_TOKEN | |
| ) | |
| print("β Files downloaded") | |
| # ββ Processor ββββββββββββββββββββββββββββββββββββββββββββββββ | |
| tokenizer = Wav2Vec2CTCTokenizer( | |
| vocab_path, | |
| unk_token="[UNK]", | |
| pad_token="[PAD]", | |
| word_delimiter_token=" " | |
| ) | |
| feature_extractor = Wav2Vec2FeatureExtractor( | |
| feature_size=1, sampling_rate=16000, | |
| padding_value=0.0, do_normalize=True, | |
| return_attention_mask=True | |
| ) | |
| processor = Wav2Vec2Processor( | |
| feature_extractor=feature_extractor, | |
| tokenizer=tokenizer | |
| ) | |
| # ββ Model ββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| print("Loading MMS base model...") | |
| model = Wav2Vec2ForCTC.from_pretrained( | |
| "facebook/mms-1b-all", | |
| target_lang="ewe", | |
| ignore_mismatched_sizes=True | |
| ) | |
| model.lm_head = nn.Linear(model.config.hidden_size, VOCAB_SIZE, bias=True) | |
| model.config.vocab_size = VOCAB_SIZE | |
| print("Loading Mina adapter...") | |
| weights = torch.load(adapter_path, map_location="cpu") | |
| state = model.state_dict() | |
| state.update(weights) | |
| model.load_state_dict(state) | |
| model.eval() | |
| print("β God AI Mina model ready") | |
| # ββ Transcribe βββββββββββββββββββββββββββββββββββββββββββββββ | |
| def transcribe_mina(audio): | |
| if audio is None: | |
| return "No audio received" | |
| sample_rate, audio_array = audio | |
| audio_array = audio_array.astype(np.float32) | |
| if len(audio_array.shape) > 1: | |
| audio_array = audio_array.mean(axis=1) | |
| if sample_rate != 16000: | |
| import librosa | |
| audio_array = librosa.resample( | |
| audio_array, orig_sr=sample_rate, target_sr=16000 | |
| ) | |
| if audio_array.max() > 1.0: | |
| audio_array = audio_array / 32768.0 | |
| inputs = processor( | |
| audio_array, sampling_rate=16000, return_tensors="pt" | |
| ) | |
| with torch.no_grad(): | |
| logits = model(**inputs).logits | |
| predicted_ids = torch.argmax(logits, dim=-1) | |
| transcription = processor.decode(predicted_ids[0]) | |
| return transcription if transcription.strip() else "..." | |
| # ββ Gradio UI ββββββββββββββββββββββββββββββββββββββββββββββββ | |
| demo = gr.Interface( | |
| fn=transcribe_mina, | |
| inputs=gr.Audio( | |
| sources=["microphone", "upload"], | |
| type="numpy", | |
| label="Speak Mina β Parler en Mina" | |
| ), | |
| outputs=gr.Textbox( | |
| label="Transcription Mina", | |
| placeholder="Mina text will appear here..." | |
| ), | |
| title="God AI β Mina (LomΓ© Ewe) ASR", | |
| description=""" | |
| ## God AI β First Mina (LomΓ© Ewe) Speech Recognition | |
| Speak or upload audio in **Mina** (LomΓ©-Aneho Ewe dialect). | |
| The AI will transcribe it to Mina text. | |
| **Platform:** https://www.godai.africa | |
| **By:** Tech Salomon β LomΓ©, Togo | |
| """, | |
| allow_flagging="never", | |
| api_name="transcribe" | |
| ) | |
| demo.launch() |