File size: 2,307 Bytes
0833e1a
 
 
 
 
 
 
 
 
 
 
 
12e07a7
b079c85
 
 
 
 
12e07a7
 
 
 
 
 
 
 
 
 
0833e1a
12e07a7
bd125dc
6c6a65d
12e07a7
 
 
e359fe1
0833e1a
12e07a7
0833e1a
 
 
 
 
 
 
 
 
75aee7b
6d438a7
f69f557
0833e1a
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
import gradio as gr
from transformers import pipeline
from pydub import AudioSegment
import wordtodigits

model = pipeline("automatic-speech-recognition",
                 "facebook/wav2vec2-base-960h")
                 
model2 = gr.Interface.load("huggingface/facebook/fastspeech2-en-ljspeech")


def asr(speech):
  try:
      transcript = model(speech)['text']
      strings = transcript.split()
      text = ""
      equation = ""
      symbols = {"plus":"+","minus":"-","times":"*","divide":"/"}
      for i in range(len(strings)):
          if strings[i].lower() in symbols:
              text = wordtodigits.convert(text)
              equation += text + symbols[strings[i].lower()]
              text=""
              continue
          text += strings[i].lower() + " "
          if i == len(strings)-1:
              text = wordtodigits.convert(text)
              equation += text
  
      ans = round(eval(equation),2)
      speech = transcript + " is equal to "+str(ans)
  except:
      transcript = "Error in Translation/Format of Audio"
      equation = "Error in Translation/Format of Audio"
      ans = "Error in Translation/Format of Audio"
      speech = "Error in Translation or Format of Audio"
  
  return transcript, equation, ans, model2(speech)

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="Equation"),
						gr.outputs.Textbox(type="str", label="Answer"),
						gr.outputs.Audio(type="file", label="Speech Answer")],
             title = "Speech Equation Solver",
             description = "This app aims to translate speech into an equation, solve the equation and generate a speech to tell the user the answer to a problem <br> <b>Addition:</b> x plus y <br> <b>Subtraction:</b> x minus y <br> <b>Multiplication:</b> x times y <br> <b>Division:</b> x divide y",
			 article = "Models: Wav2Vec2-Base-960h, fastspeech2-en-ljspeech",
             examples=["additionTest.mp3","minusTest.mp3","multiplyTest.mp3","divideTest.mp3"]
             ).launch()