Spaces:
Sleeping
Sleeping
select default channel 0
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import re
|
3 |
+
import subprocess
|
4 |
+
import math
|
5 |
+
import shutil
|
6 |
+
import soundfile as sf
|
7 |
+
import tempfile
|
8 |
+
import os
|
9 |
+
import requests
|
10 |
+
import time
|
11 |
+
|
12 |
+
|
13 |
+
def _return_yt_html_embed(yt_url):
|
14 |
+
video_id = yt_url.split("?v=")[-1]
|
15 |
+
HTML_str = (
|
16 |
+
f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
|
17 |
+
" </center>"
|
18 |
+
)
|
19 |
+
return HTML_str
|
20 |
+
|
21 |
+
|
22 |
+
def transcribe_base(audio, language):
|
23 |
+
start_time = time.time()
|
24 |
+
|
25 |
+
d, sr = sf.read(audio)
|
26 |
+
if len(d.shape) == 2:
|
27 |
+
d = d[:,0]
|
28 |
+
data = {'audio': d.tolist(),
|
29 |
+
'sampling_rate': sr,
|
30 |
+
'language': language}
|
31 |
+
print(data)
|
32 |
+
response = requests.post(os.getenv("api_url"), json=data).json()
|
33 |
+
result = response["text"]
|
34 |
+
|
35 |
+
end_time = time.time()
|
36 |
+
print("-"*50)
|
37 |
+
print(len(data["audio"])/float(sr))
|
38 |
+
print(end_time-start_time)
|
39 |
+
print("-"*50)
|
40 |
+
|
41 |
+
return result
|
42 |
+
|
43 |
+
|
44 |
+
def transcribe(audio_microphone, audio_upload, language):
|
45 |
+
print("Transcription request")
|
46 |
+
print(audio_microphone, audio_upload, language)
|
47 |
+
audio = audio_microphone if audio_microphone is not None else audio_upload
|
48 |
+
return transcribe_base(audio, language)
|
49 |
+
|
50 |
+
|
51 |
+
demo = gr.Blocks()
|
52 |
+
with demo:
|
53 |
+
gr.Markdown("# Speech recognition using Whisper models")
|
54 |
+
gr.Markdown("Orai NLP Technologies")
|
55 |
+
|
56 |
+
with gr.Tab("Trancribe Audio"):
|
57 |
+
iface = gr.Interface(
|
58 |
+
fn=transcribe,
|
59 |
+
inputs=[
|
60 |
+
gr.Audio(sources="microphone", type="filepath"),
|
61 |
+
gr.Audio(sources="upload", type="filepath"),
|
62 |
+
gr.Dropdown(choices=[("Basque", "eu"),
|
63 |
+
("Spanish", "es"),
|
64 |
+
("English", "en")],
|
65 |
+
#("French", "fr"),
|
66 |
+
#("Italian", "it"),
|
67 |
+
value="eu")
|
68 |
+
],
|
69 |
+
outputs=[
|
70 |
+
gr.Textbox(label="Transcription", autoscroll=False)
|
71 |
+
],
|
72 |
+
allow_flagging="never",
|
73 |
+
)
|
74 |
+
demo.queue(max_size=1)
|
75 |
+
demo.launch(share=False, max_threads=3, auth=(os.getenv("username"), os.getenv("password")), auth_message="Please provide a username and a password.")
|