Hunzla commited on
Commit
4f9e8c5
1 Parent(s): bccdd21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -20
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
- # Load ASR model
7
- asr_model = "Abdullah17/whisper-small-urdu"
8
- asr_pipe = pipeline("automatic-speech-recognition", model=asr_model)
9
-
10
- # Function to transcribe the command from audio samples
11
- def transcribe_the_command(audio_samples):
12
- transcript = asr_pipe(np.array(audio_samples))[0]["text"]
13
- most_similar_command, reply = find_most_similar_command(transcript, commands)
14
- return f"Transcript: {transcript}\nMost Similar Command: {most_similar_command}"
15
-
16
- # Capture audio samples from the microphone
17
- def capture_audio(rec_duration=6, sample_rate=16000):
18
- audio_data = sd.rec(int(rec_duration * sample_rate), samplerate=sample_rate, channels=1)
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.Function(capture_audio, label="Recorded Audio"),
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.",