trayansh commited on
Commit
fa0ba20
1 Parent(s): 1ada5b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -63
app.py CHANGED
@@ -1,66 +1,13 @@
1
  import time
2
- import os
3
- os.system("pip install git+https://github.com/openai/whisper.git")
4
  import gradio as gr
5
- import whisper
6
  import config as cfg
7
  import openai
8
- # import speech_recognition as sr
9
  from gtts import gTTS
10
- from gradio_client import Client
11
- # import numpy as np
12
 
13
- model = whisper.load_model("small")
14
 
15
 
16
 
17
- def transcribe_file(audio):
18
- audio = whisper.load_audio(audio)
19
- audio = whisper.pad_or_trim(audio)
20
-
21
- mel = whisper.log_mel_spectrogram(audio).to(model.device)
22
-
23
- _, probs = model.detect_language(mel)
24
-
25
- options = whisper.DecodingOptions(fp16 = False)
26
- result = whisper.decode(model, mel, options)
27
-
28
- # print(result.text)
29
- return result.text
30
-
31
-
32
- # openai.api_key = 'Your Key and Uncomment'
33
- # r = sr.Recognizer()
34
-
35
- # WHISPER_TIMEOUT = int(os.environ.get("WHISPER_TIMEOUT", 45))
36
 
37
- # whisper_client = Client("https://sanchit-gandhi-whisper-large-v2.hf.space/")
38
-
39
- # def transcribe_file(wav_path):
40
- # try:
41
- # # get result from whisper and strip it to delete begin and end space
42
- # return whisper_client.predict(
43
- # wav_path, # str (filepath or URL to file) in 'inputs' Audio component
44
- # "transcribe", # str in 'Task' Radio component
45
- # api_name="/predict"
46
- # ).strip()
47
- # except:
48
- # gr.Warning("There was a problem with Whisper endpoint, telling a joke for you.")
49
- # return "There was a problem with my voice, tell me joke"
50
-
51
-
52
- # def transcribe_file(speech_file):
53
- # """Transcribe the audio file."""
54
- # text = ""
55
- # speech = np.array(speech_file,dtype=np.float32)
56
- # audio_data = sr.AudioData(speech.tobytes(), 44100)
57
- # with sr.AudioFile(audio_data) as audio_file:
58
- # content = r.record(audio_file)
59
-
60
- # content = r.recognize_google(content)
61
-
62
- # text +=content
63
- # return text
64
 
65
  def add_user_input(history, text):
66
  """Add user input to chat hostory."""
@@ -104,24 +51,16 @@ with gr.Blocks() as bot_interface:
104
  )
105
  with gr.Column(min_width=70, scale=1):
106
  submitBtn = gr.Button("Send")
107
- with gr.Row():
108
- audio_input= gr.Audio(
109
- label="Input Audio",
110
- show_label=False,
111
- sources="microphone",
112
- type="filepath"
113
- )
114
  with gr.Row():
115
  output = gr.Audio(speak,visible=True,autoplay=True)
116
- speakBtn = gr.Button("Speak")
117
 
118
 
119
 
120
  input_msg = user_input.submit(add_user_input, [chatbot, user_input], [chatbot, user_input], queue=False).then(bot_response, chatbot, chatbot)
121
  submitBtn.click(add_user_input, [chatbot, user_input], [chatbot, user_input], queue=False).then(bot_response, chatbot, chatbot)
122
  input_msg.then(lambda: gr.update(interactive=True), None, [user_input], queue=False)
123
- inputs_event = audio_input.stop_recording(transcribe_file,inputs=[audio_input], outputs=[user_input],api_name="predict").then(add_user_input, [chatbot, user_input], [chatbot, user_input], queue=False).then(bot_response, chatbot, chatbot)
124
- # inputs_event.then(lambda: gr.update(interactive=False), None, [user_input], queue=False)
125
  speakBtn.click(fn = speak,outputs=output)
126
  bot_interface.title = cfg.bot["title"]
127
  bot_interface.launch(share=True)
 
1
  import time
 
 
2
  import gradio as gr
 
3
  import config as cfg
4
  import openai
 
5
  from gtts import gTTS
 
 
6
 
 
7
 
8
 
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  def add_user_input(history, text):
13
  """Add user input to chat hostory."""
 
51
  )
52
  with gr.Column(min_width=70, scale=1):
53
  submitBtn = gr.Button("Send")
54
+
 
 
 
 
 
 
55
  with gr.Row():
56
  output = gr.Audio(speak,visible=True,autoplay=True)
57
+ speakBtn = gr.Button("Listen")
58
 
59
 
60
 
61
  input_msg = user_input.submit(add_user_input, [chatbot, user_input], [chatbot, user_input], queue=False).then(bot_response, chatbot, chatbot)
62
  submitBtn.click(add_user_input, [chatbot, user_input], [chatbot, user_input], queue=False).then(bot_response, chatbot, chatbot)
63
  input_msg.then(lambda: gr.update(interactive=True), None, [user_input], queue=False)
 
 
64
  speakBtn.click(fn = speak,outputs=output)
65
  bot_interface.title = cfg.bot["title"]
66
  bot_interface.launch(share=True)