Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,20 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import numpy as np
|
| 3 |
-
import sounddevice as sd
|
| 4 |
from transformers import pipeline
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
sd.wait()
|
| 20 |
-
return audio_data.flatten()
|
| 21 |
-
|
| 22 |
def find_most_similar_command(statement, command_list):
|
| 23 |
best_match = None
|
| 24 |
highest_similarity = 0
|
|
@@ -33,10 +29,28 @@ def find_most_similar_command(statement, command_list):
|
|
| 33 |
i+=1
|
| 34 |
|
| 35 |
return best_match,reply
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
iface = gr.Interface(
|
| 38 |
fn=transcribe_the_command,
|
| 39 |
-
inputs=gr.inputs.
|
| 40 |
outputs="text",
|
| 41 |
title="Whisper Small Urdu Command",
|
| 42 |
description="Realtime demo for Urdu speech recognition using a fine-tuned Whisper small model and outputting the estimated command on the basis of speech transcript.",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
+
asr_pipe = pipeline("automatic-speech-recognition", model="Abdullah17/whisper-small-urdu")
|
| 3 |
+
from difflib import SequenceMatcher
|
| 4 |
|
| 5 |
+
# List of commands
|
| 6 |
+
commands = [
|
| 7 |
+
"نمائندے ایجنٹ نمائندہ",
|
| 8 |
+
" سم ایکٹیویٹ ",
|
| 9 |
+
" سم بلاک بند ",
|
| 10 |
+
"موبائل پیکیجز انٹرنیٹ پیکیج",
|
| 11 |
+
" چالان جمع ",
|
| 12 |
+
" گانا سنانا"
|
| 13 |
+
]
|
| 14 |
+
# replies = [
|
| 15 |
+
# 1,2,
|
| 16 |
+
# ]
|
| 17 |
+
# Function to find the most similar command
|
|
|
|
|
|
|
|
|
|
| 18 |
def find_most_similar_command(statement, command_list):
|
| 19 |
best_match = None
|
| 20 |
highest_similarity = 0
|
|
|
|
| 29 |
i+=1
|
| 30 |
|
| 31 |
return best_match,reply
|
| 32 |
+
def transcribe_the_command(audio):
|
| 33 |
+
import soundfile as sf
|
| 34 |
+
sample_rate, audio_data = audio
|
| 35 |
+
file_name = "recorded_audio.wav"
|
| 36 |
+
sf.write(file_name, audio_data, sample_rate)
|
| 37 |
+
# Convert stereo to mono by averaging the two channels
|
| 38 |
+
print(file_name)
|
| 39 |
+
|
| 40 |
+
transcript = asr_pipe(file_name)["text"]
|
| 41 |
+
most_similar_command,reply = find_most_similar_command(transcript, commands)
|
| 42 |
+
print(f"Given Statement: {transcript}")
|
| 43 |
+
print(f"Most Similar Command: {most_similar_command}\n")
|
| 44 |
+
print(reply)
|
| 45 |
+
|
| 46 |
+
return reply
|
| 47 |
+
# get_text_from_voice("urdu.wav")
|
| 48 |
+
import gradio as gr
|
| 49 |
+
|
| 50 |
|
| 51 |
iface = gr.Interface(
|
| 52 |
fn=transcribe_the_command,
|
| 53 |
+
inputs=gr.inputs.Audio(label="Recorded Audio",source="microphone"),
|
| 54 |
outputs="text",
|
| 55 |
title="Whisper Small Urdu Command",
|
| 56 |
description="Realtime demo for Urdu speech recognition using a fine-tuned Whisper small model and outputting the estimated command on the basis of speech transcript.",
|