SheldonYC commited on
Commit
5a17bb9
1 Parent(s): 02bfcfa

fix app.py

Browse files
Files changed (2) hide show
  1. app.py +40 -19
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,22 +1,43 @@
1
  from transformers import pipeline
 
2
  import gradio as gr
3
 
4
- model = pipeline("automatic-speech-recognition")
5
-
6
- def transcribe_audio(mic=None, file=None):
7
- if mic is not None:
8
- audio = mic
9
- elif file is not None:
10
- audio = file
11
- else:
12
- return("You must either provide a mic recording or a file")
13
- transcription = model(audio)["text"]
14
- return transcription
15
-
16
- gr.Interface(
17
- fn=transcribe_audio,
18
- inputs=[gr.Audio(source="microphone", type="filepath", optional=True),
19
- gr.Audio(source ="upload", type="filepath", optional=True)],
20
- outputs="text",
21
- css=".footer{display:none !important}"
22
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from transformers import pipeline
2
+ import numpy as np
3
  import gradio as gr
4
 
5
+ def respond(message, chat_history):
6
+ bot_message = message
7
+ chat_history.append((message, bot_message))
8
+ return "", chat_history
9
+
10
+ def transcribe(audio):
11
+ sr, y = audio
12
+ y = y.astype(np.float32)
13
+ y /= np.max(np.abs(y))
14
+ result = asr_model({"sampling_rate": sr, "raw": y})["text"]
15
+ return result
16
+
17
+ asr_model_id = "openai/whisper-small.en"
18
+ asr_model = pipeline("automatic-speech-recognition", model=asr_model_id)
19
+
20
+ with gr.Blocks() as demo:
21
+ with gr.Column():
22
+ gr.Markdown(
23
+ """
24
+ # HKU Canteen VA
25
+ """)
26
+ va = gr.Chatbot(container=False)
27
+
28
+ with gr.Row(): # text input
29
+ text_input = gr.Textbox(placeholder="Ask me anything...", container=False, scale=1)
30
+ submit_btn = gr.Button("Submit", scale=0)
31
+
32
+ # with gr.Row(): # audio input
33
+ # recording = gr.Microphone(show_download_button=False, container=False)
34
+
35
+ with gr.Row(): # button toolbar
36
+ clear = gr.ClearButton([text_input, va])
37
+
38
+ text_input.submit(respond, [text_input, va], [text_input, va], queue=False)
39
+ submit_btn.click(respond, [text_input, va], [text_input, va], queue=False)
40
+ # recording.stop_recording(transcribe, [recording], [text_input]).then(respond, [text_input, va], [text_input, va], queue=False)
41
+
42
+ if __name__ == "__main__":
43
+ demo.launch()
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  torch
2
- transformers
 
 
1
  torch
2
+ transformers
3
+ numpy