File size: 2,290 Bytes
7b72da9
 
ba1ce2e
7b72da9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba1ce2e
7b72da9
ba1ce2e
 
 
 
 
7b72da9
 
 
 
ba1ce2e
 
 
 
 
 
 
 
 
 
 
 
 
 
7b72da9
 
ba1ce2e
 
 
 
 
 
 
 
 
7b72da9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import gradio as gr
from transformers import pipeline
from pydub import AudioSegment

pipe = pipeline("automatic-speech-recognition", "facebook/wav2vec2-base-960h")

dictionary = {' ': '/', 
	"'": '.----.', 
	'0': '-----', 
	'1': '.----', 
	'2': '..---', 
	'3': '...--', 
	'4': '....-', 
	'5': '.....', 
	'6': '-....', 
	'7': '--...', 
	'8': '---..', 
	'9': '----.', 
	'?': '..--..', 
	'A': '.-', 
	'B': '-...', 
	'C': '-.-.', 
	'D': '-..', 
	'E': '.', 
	'F': '..-.', 
	'G': '--.', 
	'H': '....', 
	'I': '..', 
	'J': '.---', 
	'K': '-.-', 
	'L': '.-..', 
	'M': '--', 
	'N': '-.', 
	'O': '---', 
	'P': '.--.', 
	'Q': '--.-', 
	'R': '.-.', 
	'S': '...', 
	'T': '-', 
	'U': '..-', 
	'V': '...-', 
	'W': '.--', 
	'X': '-..-', 
	'Y': '-.--', 
	'Z': '--..', 
}



def asr(speech):
  dummy = AudioSegment.from_file("dummy.mp3", format="mp3")
  dot = AudioSegment.from_file("dot.mp3", format="mp3")
  dash = AudioSegment.from_file("dash.mp3", format="mp3")
  space = AudioSegment.from_file("space.mp3", format="mp3")
  i=0
  transcript = pipe(speech)['text']
  morseCode = ""
  for character in transcript:
    morseCode += dictionary[character] + " "
  while i<len(morseCode):
    if morseCode[i] == ".":
      dummy = dummy + dot
      i = i+1
    elif morseCode[i] == "-":
      dummy = dummy + dash
      i = i+1
    elif morseCode[i] == " ":
      dummy = dummy + space
      i = i+1
    else:
      i = i+1
  morseAudio = dummy.export("output.mp3", format="mp3")
  return transcript, morseCode, "output.mp3"
  
gr.Interface(fn=asr,
             inputs = gr.inputs.Audio(source="microphone", type="filepath", optional=False, label="Please record your voice"),
#             inputs = gr.inputs.Audio(source="upload", type="filepath", label="Upload your audio file here"),
						 outputs = [gr.outputs.Textbox(type="str", label="Text Translation"),
						            gr.outputs.Textbox(type="str", label="Morse Code"),
												gr.outputs.Audio(type="file", label="Audio Translation")],
             title = "Speech to Morse Code",
             description = "This app will translate your speech into text, morse code, and audio.",
						 examples = [["hello world.mp3"], ["intro.mp3"]],
						 article = "Author: <a href=\"https://huggingface.co/epdavid2\">Edson</a>"
             ).launch()