Hunzla commited on
Commit
ad7b749
1 Parent(s): bfa35b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -47
app.py CHANGED
@@ -1,53 +1,24 @@
 
 
 
1
  from transformers import pipeline
2
- import soundfile as sf
3
 
4
  # Load ASR model
5
  asr_model = "Abdullah17/whisper-small-urdu"
6
  asr_pipe = pipeline("automatic-speech-recognition", model=asr_model)
7
 
8
- # Rest of your code
9
- commands = [
10
- "نمائندے ایجنٹ نمائندہ",
11
- "سم ایکٹیویٹ",
12
- "سم بلاک بند",
13
- "موبائل پیکیجز انٹرنیٹ پیکیج",
14
- "چالان جمع",
15
- "گانا سنانا"
16
- ]
17
 
18
- # Function to transcribe the command from audio
19
- def transcribe_the_command(audio_list):
20
- transcriptions = []
21
-
22
- # Process each audio in the batch
23
- for audio_data, sample_rate in audio_list:
24
- file_name = "recorded_audio.wav"
25
- sf.write(file_name, audio_data, sample_rate)
26
-
27
- # Convert stereo to mono by averaging the two channels
28
- transcript = asr_pipe(file_name)[0]["text"]
29
- most_similar_command, reply = find_most_similar_command(transcript, commands)
30
-
31
- transcriptions.append((transcript, most_similar_command, reply))
32
-
33
- return transcriptions
34
- # from transformers import pipeline
35
- # asr_pipe = pipeline("automatic-speech-recognition", model="Abdullah17/whisper-small-urdu")
36
- # from difflib import SequenceMatcher
37
 
38
- # # List of commands
39
- # commands = [
40
- # "نمائندے ایجنٹ نمائندہ",
41
- # " سم ایکٹیویٹ ",
42
- # " سم بلاک بند ",
43
- # "موبائل پیکیجز انٹرنیٹ پیکیج",
44
- # " چالان جمع ",
45
- # " گانا سنانا"
46
- # ]
47
- # # replies = [
48
- # # 1,2,
49
- # # ]
50
- # # Function to find the most similar command
51
  def find_most_similar_command(statement, command_list):
52
  best_match = None
53
  highest_similarity = 0
@@ -62,14 +33,10 @@ def find_most_similar_command(statement, command_list):
62
  i+=1
63
 
64
  return best_match,reply
65
- # x
66
- # get_text_from_voice("urdu.wav")
67
- import gradio as gr
68
-
69
 
70
  iface = gr.Interface(
71
  fn=transcribe_the_command,
72
- inputs=gr.inputs.Audio(label="Recorded Audio",source="microphone"),
73
  outputs="text",
74
  title="Whisper Small Urdu Command",
75
  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
+ 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
  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.",