Spaces:
Running
Running
add app.py and requirements
Browse files- app.py +118 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
|
4 |
+
import assemblyai as aai
|
5 |
+
|
6 |
+
import io
|
7 |
+
from scipy.io.wavfile import write
|
8 |
+
|
9 |
+
|
10 |
+
title = """<h1 align="center">🔥AssemblyAI: Conformer-2 Demo🔥</h1>"""
|
11 |
+
|
12 |
+
subtitle = (
|
13 |
+
"""<p align="center">Automatic Speech Recognition using the AssemblyAI API</p>"""
|
14 |
+
)
|
15 |
+
link = """<p align="center"><a href="https://www.assemblyai.com/blog/conformer-2/">Click here to learn more about the Conformer-2 model</a></p>"""
|
16 |
+
|
17 |
+
|
18 |
+
def submit_to_AAI(api_key, radio, audio_file, mic_recording):
|
19 |
+
|
20 |
+
if radio == "Audio File":
|
21 |
+
audio_data = audio_file
|
22 |
+
elif radio == "Record Audio":
|
23 |
+
audio_data = mic_recording
|
24 |
+
|
25 |
+
if not api_key:
|
26 |
+
return "Error! Did you use a valid API key?"
|
27 |
+
|
28 |
+
aai.settings.api_key = api_key
|
29 |
+
transcriber = aai.Transcriber()
|
30 |
+
|
31 |
+
# Create temporary "file" and write data to it
|
32 |
+
sr, aud = audio_data
|
33 |
+
|
34 |
+
bytes_wav = bytes()
|
35 |
+
temp_file = io.BytesIO(bytes_wav)
|
36 |
+
write(temp_file, sr, aud)
|
37 |
+
|
38 |
+
# Workaround to upload a file-like object before transcribing
|
39 |
+
# This should be abstracted away in future SDK versions:
|
40 |
+
try:
|
41 |
+
upload_url = aai.api.upload_file(aai.Client.get_default().http_client, temp_file)
|
42 |
+
except aai.types.TranscriptError as e:
|
43 |
+
return str(e)
|
44 |
+
|
45 |
+
# Now we can transcibe the url
|
46 |
+
transcript = transcriber.transcribe(upload_url)
|
47 |
+
|
48 |
+
if transcript.error is not None:
|
49 |
+
return transcript.error
|
50 |
+
|
51 |
+
paragraphs = transcript.get_paragraphs()
|
52 |
+
return "\n\n".join(p.text for p in paragraphs)
|
53 |
+
|
54 |
+
|
55 |
+
def change_audio_source(radio):
|
56 |
+
if radio == "Audio File":
|
57 |
+
return [gr.Audio.update(visible=True), gr.Audio.update(visible=False)]
|
58 |
+
elif radio == "Record Audio":
|
59 |
+
return [gr.Audio.update(visible=False), gr.Audio.update(visible=True)]
|
60 |
+
|
61 |
+
|
62 |
+
with gr.Blocks(
|
63 |
+
css="""#col_container {width: 1000px; margin-left: auto; margin-right: auto;}
|
64 |
+
#chatbot {height: 520px; overflow: auto;}"""
|
65 |
+
) as demo:
|
66 |
+
gr.HTML(
|
67 |
+
'<center><a href="https://www.assemblyai.com/"><img src="file/images/logo.png" width="180px"></a></center>'
|
68 |
+
)
|
69 |
+
gr.HTML(title)
|
70 |
+
gr.HTML(subtitle)
|
71 |
+
gr.HTML(link)
|
72 |
+
gr.HTML(
|
73 |
+
"""<center><a href="https://huggingface.co/spaces/assemblyai/Conformer1-Demo?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>Duplicate the Space and run securely with your AssemblyAI API Key. <a href="https://www.assemblyai.com/dashboard/signup">Get a free key here.</a></center>"""
|
74 |
+
)
|
75 |
+
|
76 |
+
with gr.Column(elem_id="col_container"):
|
77 |
+
api_key = gr.Textbox(
|
78 |
+
type="password", label="Enter your AssemblyAI API key here"
|
79 |
+
)
|
80 |
+
|
81 |
+
with gr.Box():
|
82 |
+
# Selector for audio source
|
83 |
+
radio = gr.Radio(
|
84 |
+
["Audio File", "Record Audio"], label="Audio Source", value="Audio File"
|
85 |
+
)
|
86 |
+
# Audio object for both file and microphone data
|
87 |
+
audio_file = gr.Audio()
|
88 |
+
mic_recording = gr.Audio(source="microphone", visible=False)
|
89 |
+
|
90 |
+
gr.Examples(
|
91 |
+
[
|
92 |
+
os.path.join(os.path.dirname(__file__), "audio/audio_sample1.flac"),
|
93 |
+
os.path.join(
|
94 |
+
os.path.dirname(__file__), "audio/assemblyai_company.mp3"
|
95 |
+
),
|
96 |
+
],
|
97 |
+
audio_file,
|
98 |
+
)
|
99 |
+
|
100 |
+
btn = gr.Button("Run")
|
101 |
+
|
102 |
+
out = gr.Textbox(
|
103 |
+
placeholder="Your formatted transcript will appear here ...", lines=10
|
104 |
+
)
|
105 |
+
|
106 |
+
# Changing audio source changes Audio input component
|
107 |
+
radio.change(
|
108 |
+
fn=change_audio_source, inputs=[radio], outputs=[audio_file, mic_recording]
|
109 |
+
)
|
110 |
+
|
111 |
+
# Clicking "submit" uploads selected audio to AssemblyAI, performs requested analyses, and displays results
|
112 |
+
btn.click(
|
113 |
+
fn=submit_to_AAI,
|
114 |
+
inputs=[api_key, radio, audio_file, mic_recording],
|
115 |
+
outputs=out,
|
116 |
+
)
|
117 |
+
|
118 |
+
demo.queue(max_size=20, concurrency_count=10).launch(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
assemblyai==0.17.0
|
2 |
+
gradio
|
3 |
+
numpy
|
4 |
+
scipy==1.9.1
|