Spaces:
Runtime error
Runtime error
from transformers import pipeline | |
asr_pipe = pipeline("automatic-speech-recognition", model="Abdullah17/whisper-small-urdu") | |
from difflib import SequenceMatcher | |
# List of commands | |
commands = [ | |
" کھیل کھیلو", | |
"میوزک موسیقی", | |
"روشنی کم ", | |
"آج تاریخ ؟", | |
" لطیفہ", | |
"مدد"،" نمائندہ"،" ایجنٹ"،" مشکل"،" خدمت"،" توجہ"،" سوال"،" تفصیلات"،" راہنمائی"،" شکایت"،" تنقید"،" حل"،" پریشانی"،" تصدیق"،" بات چیت"،" معلومات"،" گفتگو"،" توجہ دیں"، "سمجھنا"،" توثیق"،" راہنمائی"،" مطلب"،" پوچھنا"،" بیان کرنا"،" سامنا"،" تفصیلی"،" تصدیق کریں"، "براہ کرم"، "بات کرنا" | |
] | |
replies = [ | |
"کیا آپ کھیل دیکھنا چاہتے ہیں؟","کیا آپ موسیقی سننا چاہتے ہیں؟","کیا آپ روشنی کم کرنا چاہتے ہیں؟","کیا آپ تاریخ جاننا چاہتے ہیں؟","کیا آپ لطیفہ سننا چاہتے ہیں؟" ,"کیا آپ نمائندے سے بات کرنا چاہتے ہیں؟" | |
] | |
# Function to find the most similar command | |
def find_most_similar_command(statement, command_list): | |
best_match = None | |
highest_similarity = 0 | |
i=0 | |
for command in command_list: | |
similarity = SequenceMatcher(None, statement, command).ratio() | |
print(similarity) | |
if similarity > highest_similarity: | |
highest_similarity = similarity | |
best_match = command | |
if i>4: | |
reply=replies[5] | |
else: | |
reply=replies[i] | |
i+=1 | |
return best_match,reply | |
def transcribe_the_command(audio): | |
import soundfile as sf | |
sample_rate, audio_data = audio | |
file_name = "recorded_audio.wav" | |
sf.write(file_name, audio_data, sample_rate) | |
# Convert stereo to mono by averaging the two channels | |
print(file_name) | |
transcript = asr_pipe(file_name)["text"] | |
most_similar_command,reply = find_most_similar_command(transcript, commands) | |
print(f"Given Statement: {transcript}") | |
print(f"Most Similar Command: {most_similar_command}\n") | |
print(reply) | |
return reply | |
# get_text_from_voice("urdu.wav") | |
import gradio as gr | |
iface = gr.Interface( | |
fn=transcribe_the_command, | |
inputs=gr.inputs.Audio(label="Recorded Audio",source="microphone"), | |
outputs="text", | |
title="Whisper Small Urdu Command", | |
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.", | |
) | |
iface.launch() |