Spaces:
Sleeping
Sleeping
Commit
·
8c8ff11
1
Parent(s):
a5acade
update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import time
|
3 |
+
import json
|
4 |
+
import gradio as gr
|
5 |
+
secret_key = "6d74a4fac397423a8bd3011180b9b979"
|
6 |
+
|
7 |
+
# retrieve transcription results for the task
|
8 |
+
def get_results(config):
|
9 |
+
# endpoint to check status of the transcription task
|
10 |
+
endpoint = "https://api.speechtext.ai/results?"
|
11 |
+
# use a loop to check if the task is finished
|
12 |
+
while True:
|
13 |
+
results = requests.get(endpoint, params=config).json()
|
14 |
+
if "status" not in results:
|
15 |
+
break
|
16 |
+
# print("Task status: {}".format(results["status"]))
|
17 |
+
if results["status"] == 'failed':
|
18 |
+
print("The task is failed: {}".format(results))
|
19 |
+
break
|
20 |
+
if results["status"] == 'finished':
|
21 |
+
break
|
22 |
+
# sleep for 15 seconds if the task has the status - 'processing'
|
23 |
+
time.sleep(15)
|
24 |
+
return results
|
25 |
+
|
26 |
+
# loads the audio into memory
|
27 |
+
def spt(audio_file):
|
28 |
+
with open(audio_file, mode="rb") as file:
|
29 |
+
post_body = file.read()
|
30 |
+
|
31 |
+
# endpoint to start a transcription task
|
32 |
+
endpoint = "https://api.speechtext.ai/recognize?"
|
33 |
+
header = {'Content-Type': "application/octet-stream"}
|
34 |
+
|
35 |
+
# transcription task options
|
36 |
+
config = {
|
37 |
+
"key" : secret_key,
|
38 |
+
"language" : "en-US",
|
39 |
+
"punctuation" : True,
|
40 |
+
"format" : "m4a"
|
41 |
+
}
|
42 |
+
|
43 |
+
# send an audio transcription request
|
44 |
+
r = requests.post(endpoint, headers = header, params = config, data = post_body).json()
|
45 |
+
|
46 |
+
# get the id of the speech recognition task
|
47 |
+
task = r["id"]
|
48 |
+
# print("Task ID: {}".format(task))
|
49 |
+
|
50 |
+
# get transcription results, summary, and highlights
|
51 |
+
config = {
|
52 |
+
"key" : secret_key,
|
53 |
+
"task" : task,
|
54 |
+
"summary" : True,
|
55 |
+
"summary_size" : 15,
|
56 |
+
"highlights" : True,
|
57 |
+
"max_keywords" : 10
|
58 |
+
}
|
59 |
+
|
60 |
+
transcription = get_results(config)
|
61 |
+
p=transcription['results']['transcript'].replace('<kw>','').replace('</kw>','')
|
62 |
+
return p
|
63 |
+
k=gr.Interface(fn=spt, inputs=gr.Audio(source="microphone", type="filepath"), outputs="text")
|
64 |
+
k.launch()
|