mrodriguez commited on
Commit
1210c2f
·
verified ·
1 Parent(s): 7be8f82

feat: simple demo

Browse files
Files changed (1) hide show
  1. app.py +10 -41
app.py CHANGED
@@ -1,9 +1,10 @@
1
  import os
2
- import requests
3
 
4
  import gradio as gr
 
 
5
  from languages import LANGUAGES
6
- from time import time
7
 
8
  GLADIA_API_KEY = os.environ.get("GLADIA_API_KEY")
9
 
@@ -21,8 +22,6 @@ ACCEPTED_LANGUAGE_BEHAVIOUR = [
21
 
22
  def transcribe(
23
  audio: str = None,
24
- language_behaviour: str = ACCEPTED_LANGUAGE_BEHAVIOUR[2],
25
- language: str = "english",
26
  ) -> dict:
27
  """
28
  This function transcribes audio to text using the Gladia API.
@@ -30,26 +29,14 @@ def transcribe(
30
  Get your api key at gladia.io !
31
 
32
  Parameters:
33
- audio_url (str): The URL of the audio file to transcribe. If audio_url is provided, audio file will be ignored.
34
  audio (str): The path to the audio file to transcribe.
35
- video (str): The path to the video file. If provided, the audio field will be set to the content of this video.
36
- language_behaviour (str): Determines how language detection should be performed.
37
- Must be one of [
38
- "manual",
39
- "automatic single language",
40
- "automatic multiple languages"
41
- ]
42
- If "manual", the language field must be provided and the API will transcribe the audio in the given language.
43
- If "automatic single language", the language of the audio will be automatically detected by the API
44
- but will force the transcription to be in a single language.
45
- If "automatic multiple languages", the language of the audio will be automatically detected by the API for
46
- each sentence allowing code-switching over 97 languages.
47
-
48
- language (str): The language of the audio file. This field is ignored if language_behaviour is set to "automatic*".
49
 
50
  Returns:
51
  dict: A dictionary containing the transcribed text and other metadata about the transcription process. If an error occurs, the function returns a string with an error message.
52
  """
 
 
 
53
 
54
  # if video file is there then send the audio field as the content of the video
55
  files = {
@@ -59,14 +46,12 @@ def transcribe(
59
  # priority given to the audio or video
60
  if audio:
61
  files["audio"] = (audio, open(audio, "rb"), "audio/wav")
62
- else:
63
- files["audio_url"] = ((None, audio_url),)
64
 
65
  # if language is manual then send the language field
66
  # if it's there for language_behaviour == automatic*
67
  # it will ignored anyways
68
  if language_behaviour == "manual":
69
- files["language"] = (None, language)
70
 
71
  start_transfer = time()
72
  response = requests.post(
@@ -109,28 +94,12 @@ iface = gr.Interface(
109
  """,
110
  fn=transcribe,
111
  inputs=[
112
- gr.Audio(label="or Audio file to transcribe", source="upload", type="filepath"),
113
- gr.Dropdown(
114
- label="""Language transcription behaviour:\n
115
- If "manual", the language field must be provided and the API will transcribe the audio in the given language.
116
- If "automatic single language", the language of the audio will be automatically detected by the API
117
- but will force the transcription to be in a single language.
118
- If "automatic multiple languages", the language of the audio will be automatically detected by the API for
119
- each sentence allowing code-switching over 97 languages.
120
- """,
121
- choices=ACCEPTED_LANGUAGE_BEHAVIOUR,
122
- value=ACCEPTED_LANGUAGE_BEHAVIOUR[1]
123
- ),
124
- gr.Dropdown(
125
- choices=sorted([language_name for language_name in LANGUAGES.keys()]),
126
- label="Language (only if language behaviour is set to manual)",
127
- value="english"
128
- ),
129
  ],
130
  outputs="json",
131
  examples=[
132
- ["examples/good.will.hunting.wav", ACCEPTED_LANGUAGE_BEHAVIOUR[1], "english"],
133
- ["examples/wolf.of.wall.street.wav", ACCEPTED_LANGUAGE_BEHAVIOUR[1], "english"],
134
  ],
135
  )
136
  iface.queue()
 
1
  import os
2
+ from time import time
3
 
4
  import gradio as gr
5
+ import requests
6
+
7
  from languages import LANGUAGES
 
8
 
9
  GLADIA_API_KEY = os.environ.get("GLADIA_API_KEY")
10
 
 
22
 
23
  def transcribe(
24
  audio: str = None,
 
 
25
  ) -> dict:
26
  """
27
  This function transcribes audio to text using the Gladia API.
 
29
  Get your api key at gladia.io !
30
 
31
  Parameters:
 
32
  audio (str): The path to the audio file to transcribe.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  Returns:
35
  dict: A dictionary containing the transcribed text and other metadata about the transcription process. If an error occurs, the function returns a string with an error message.
36
  """
37
+ DEFAULT_MANUAL_LANGUAGE = "english"
38
+
39
+ language_behaviour = ACCEPTED_LANGUAGE_BEHAVIOUR[2]
40
 
41
  # if video file is there then send the audio field as the content of the video
42
  files = {
 
46
  # priority given to the audio or video
47
  if audio:
48
  files["audio"] = (audio, open(audio, "rb"), "audio/wav")
 
 
49
 
50
  # if language is manual then send the language field
51
  # if it's there for language_behaviour == automatic*
52
  # it will ignored anyways
53
  if language_behaviour == "manual":
54
+ files["language"] = (None, DEFAULT_MANUAL_LANGUAGE)
55
 
56
  start_transfer = time()
57
  response = requests.post(
 
94
  """,
95
  fn=transcribe,
96
  inputs=[
97
+ gr.Audio(label="Audio file", source="upload", type="filepath"),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  ],
99
  outputs="json",
100
  examples=[
101
+ ["examples/good.will.hunting.wav"],
102
+ ["examples/wolf.of.wall.street.wav"],
103
  ],
104
  )
105
  iface.queue()