Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import base64
|
| 3 |
+
import os
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import (
|
| 6 |
+
AutomaticSpeechRecognitionPipeline,
|
| 7 |
+
WhisperForConditionalGeneration,
|
| 8 |
+
WhisperTokenizer,
|
| 9 |
+
WhisperProcessor,
|
| 10 |
+
)
|
| 11 |
+
from peft import PeftModel, PeftConfig
|
| 12 |
+
peft_model_id = "Boadiwaa/LORA-colab-Distil-Whisper-medium2"
|
| 13 |
+
task = "transcribe"
|
| 14 |
+
peft_config = PeftConfig.from_pretrained(peft_model_id)
|
| 15 |
+
model = WhisperForConditionalGeneration.from_pretrained(
|
| 16 |
+
peft_config.base_model_name_or_path,device_map="auto"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
model = PeftModel.from_pretrained(model, peft_model_id)
|
| 20 |
+
tokenizer = WhisperTokenizer.from_pretrained(peft_config.base_model_name_or_path,task=task)
|
| 21 |
+
processor = WhisperProcessor.from_pretrained(peft_config.base_model_name_or_path,task=task)
|
| 22 |
+
feature_extractor = processor.feature_extractor
|
| 23 |
+
#forced_decoder_ids = processor.get_decoder_prompt_ids(language=language, task=task)
|
| 24 |
+
pipe = AutomaticSpeechRecognitionPipeline(model=model, tokenizer=tokenizer, feature_extractor=feature_extractor)
|
| 25 |
+
|
| 26 |
+
#api_key = os.getenv("HF_API_TOKEN")
|
| 27 |
+
|
| 28 |
+
def transcribe(data):
|
| 29 |
+
audio_string = data[0]
|
| 30 |
+
wav_file = open("temp.wav", "wb")
|
| 31 |
+
decode_string = base64.b64decode(audio_string)
|
| 32 |
+
wav_file.write(decode_string)
|
| 33 |
+
with torch.cuda.amp.autocast():
|
| 34 |
+
text = pipe(wav_file,max_new_tokens=255)["text"]
|
| 35 |
+
return text
|
| 36 |
+
|
| 37 |
+
#hf_writer = gr.HuggingFaceDatasetSaver(hf_token = api_key,dataset_name="interaction-log2")
|
| 38 |
+
|
| 39 |
+
demo = gr.Interface(
|
| 40 |
+
fn=transcribe,
|
| 41 |
+
inputs=data,
|
| 42 |
+
outputs="text",
|
| 43 |
+
title="Transcriber for Ghanaian-accented speech (English)",
|
| 44 |
+
description="Realtime demo for Ghanaian-accented speech recognition (in English).",
|
| 45 |
+
article = """
|
| 46 |
+
By using this app you consent to your voice being used to train the underlying open-source model further.
|
| 47 |
+
|
| 48 |
+
INSTRUCTIONS FOR USE:
|
| 49 |
+
1. Click on record and speak into your microphone
|
| 50 |
+
2. Click on stop and submit after you are done speaking.
|
| 51 |
+
3. Speech input should not exceed 40s for optimal results.
|
| 52 |
+
4. Please wait a few secs after input to see your results.
|
| 53 |
+
NB: You might see "no microphone detected" when you first open the app, CONSIDER THAT A MICROPHONE TEST, record anyway and submit. You might see an Error in the output. Now delete the input by clicking the 'x' at the top and record your main input.
|
| 54 |
+
The app should run seamlessly in the subsequent inputs.
|
| 55 |
+
""",
|
| 56 |
+
allow_flagging = "auto",flagging_callback=hf_writer
|
| 57 |
+
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
demo.launch(share=True)
|
| 61 |
+
|
| 62 |
+
if __name__ == "__main__":
|
| 63 |
+
demo.launch()
|
| 64 |
+
|