RamAnanth1 commited on
Commit
203bf3f
·
1 Parent(s): 8963f6c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +140 -5
app.py CHANGED
@@ -7,13 +7,65 @@ import time
7
  # AssemblyAI transcript endpoint (where we submit the file)
8
  transcript_endpoint = "https://api.assemblyai.com/v2/transcript"
9
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  def get_transcript_url(url, api_token):
11
  headers={
12
  "Authorization": api_token,
13
  "Content-Type": "application/json"
14
  }
 
15
  # JSON that tells the API which file to trancsribe
16
- json={"audio_url": url}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  response = requests.post(
19
  transcript_endpoint,
@@ -32,23 +84,106 @@ def get_transcript_url(url, api_token):
32
  time.sleep(3)
33
  return transcription_result['text']
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  title = """<h1 align="center">🔥Conformer-1 API </h1>"""
36
  description = """
37
- In this demo, you can explore the outputs of a Conformer-1 Speech Recognition Model from AssemblyAI.
38
  """
39
 
40
  with gr.Blocks(css = """#col_container {width: 1000px; margin-left: auto; margin-right: auto;}
41
  """) as demo:
42
  gr.HTML(title)
43
  gr.Markdown(description)
 
 
 
44
  with gr.Column(elem_id = "col_container"):
45
- assemblyai_api_key = gr.Textbox(type='password', label="Enter your AssemblyAI API key here")
46
- inputs = gr.Textbox(label = "Enter the url for the audio file")
47
- b1 = gr.Button()
 
 
 
 
 
 
48
  transcript = gr.Textbox(label = "Transcript Result" )
49
 
50
  inputs.submit(get_transcript_url, [inputs, assemblyai_api_key], [transcript])
51
  b1.click(get_transcript_url, [inputs, assemblyai_api_key], [transcript])
 
 
52
 
53
 
54
  demo.queue().launch(debug=True)
 
7
  # AssemblyAI transcript endpoint (where we submit the file)
8
  transcript_endpoint = "https://api.assemblyai.com/v2/transcript"
9
 
10
+ upload_endpoint = "https://api.assemblyai.com/v2/upload"
11
+
12
+
13
+ # Helper function to upload data
14
+ def _read_file(filename, chunk_size=5242880):
15
+ with open(filename, "rb") as f:
16
+ while True:
17
+ data = f.read(chunk_size)
18
+ if not data:
19
+ break
20
+ yield data
21
+
22
  def get_transcript_url(url, api_token):
23
  headers={
24
  "Authorization": api_token,
25
  "Content-Type": "application/json"
26
  }
27
+
28
  # JSON that tells the API which file to trancsribe
29
+ json={
30
+ # URL of the audio file to process
31
+ "audio_url": url,
32
+
33
+ # Turn on speaker labels
34
+ "speaker_labels": True,
35
+
36
+ # Turn on cusom vocabulary
37
+ "word_boost": ["assembly ai"],
38
+
39
+ # Turn on custom spelling
40
+ "custom_spelling": [
41
+ {"from": ["assembly AI"], "to": "AssemblyAI"},
42
+ {"from": ["assembly AI's"], "to": "AssemblyAI's"}
43
+ ],
44
+
45
+ # Turn on PII Redaction and specify policies
46
+ "redact_pii": True,
47
+ "redact_pii_policies": ["drug", "injury", "person_name"],
48
+ "redact_pii_audio": True,
49
+
50
+ # Turn on Auto Highlights
51
+ "auto_highlights": True,
52
+
53
+ # Turn on Content Moderation
54
+ "content_safety": True,
55
+
56
+ # Turn on Topic Detection
57
+ "iab_categories": True,
58
+
59
+ # Turn on Sentiment Analysis
60
+ "sentiment_analysis": True,
61
+
62
+ # Turn on Summarization and specify configuration
63
+ "summarization": True,
64
+ "summary_model": "informative",
65
+ "summary_type": "bullets",
66
+
67
+ # Turn on Entity Detection
68
+ "entity_detection": True,}
69
 
70
  response = requests.post(
71
  transcript_endpoint,
 
84
  time.sleep(3)
85
  return transcription_result['text']
86
 
87
+ def get_transcript_file(filename, api_token):
88
+ headers={
89
+ "Authorization": api_token,
90
+ "Content-Type": "application/json"
91
+ }
92
+
93
+ upload_response = requests.post(
94
+ upload_endpoint,
95
+ headers=headers,
96
+ data=_read_file(filename))
97
+
98
+ # JSON that tells the API which file to trancsribe
99
+ json = {
100
+ # URL of the audio file to process
101
+ "audio_url": upload_response.json()['upload_url'],
102
+
103
+ # Turn on speaker labels
104
+ "speaker_labels": True,
105
+
106
+ # Turn on cusom vocabulary
107
+ "word_boost": ["assembly ai"],
108
+
109
+ # Turn on custom spelling
110
+ "custom_spelling": [
111
+ {"from": ["assembly AI"], "to": "AssemblyAI"},
112
+ {"from": ["assembly AI's"], "to": "AssemblyAI's"}
113
+ ],
114
+
115
+ # Turn on PII Redaction and specify policies
116
+ "redact_pii": True,
117
+ "redact_pii_policies": ["drug", "injury", "person_name"],
118
+ "redact_pii_audio": True,
119
+
120
+ # Turn on Auto Highlights
121
+ "auto_highlights": True,
122
+
123
+ # Turn on Content Moderation
124
+ "content_safety": True,
125
+
126
+ # Turn on Topic Detection
127
+ "iab_categories": True,
128
+
129
+ # Turn on Sentiment Analysis
130
+ "sentiment_analysis": True,
131
+
132
+ # Turn on Summarization and specify configuration
133
+ "summarization": True,
134
+ "summary_model": "informative",
135
+ "summary_type": "bullets",
136
+
137
+ # Turn on Entity Detection
138
+ "entity_detection": True,
139
+ }
140
+
141
+ response = requests.post(
142
+ transcript_endpoint,
143
+ json=json,
144
+ headers=headers # Authorization to link this transcription with your account
145
+ )
146
+
147
+ polling_endpoint = f"https://api.assemblyai.com/v2/transcript/{response.json()['id']}"
148
+ while True:
149
+ transcription_result = requests.get(polling_endpoint, headers=headers).json()
150
+ if transcription_result['status'] == 'completed':
151
+ break
152
+ elif transcription_result['status'] == 'error':
153
+ raise RuntimeError(f"Transcription failed: {transcription_result['error']}")
154
+ else:
155
+ time.sleep(3)
156
+ return transcription_result['text']
157
+
158
+
159
  title = """<h1 align="center">🔥Conformer-1 API </h1>"""
160
  description = """
161
+ ## In this demo, you can explore the outputs of a Conformer-1 Speech Recognition Model from AssemblyAI.
162
  """
163
 
164
  with gr.Blocks(css = """#col_container {width: 1000px; margin-left: auto; margin-right: auto;}
165
  """) as demo:
166
  gr.HTML(title)
167
  gr.Markdown(description)
168
+
169
+ assemblyai_api_key = gr.Textbox(type='password', label="Enter your AssemblyAI API key here")
170
+
171
  with gr.Column(elem_id = "col_container"):
172
+
173
+ with gr.Tab("Audio URL file")
174
+ inputs = gr.Textbox(label = "Enter the url for the audio file")
175
+ b1 = gr.Button('Transcribe')
176
+
177
+ with gr.Tab("Upload Audio as File"):
178
+ audio_input_u = gr.Audio(label = 'Upload Audio',source="upload",type="filepath")
179
+ transcribe_audio_u = gr.Button('Transcribe')
180
+
181
  transcript = gr.Textbox(label = "Transcript Result" )
182
 
183
  inputs.submit(get_transcript_url, [inputs, assemblyai_api_key], [transcript])
184
  b1.click(get_transcript_url, [inputs, assemblyai_api_key], [transcript])
185
+ audio_input_u.submit(get_transcript_file, [audio_input_u, assemblyai_api_key], [transcript])
186
+ transcibe_audio_u.click(get_transcript_file, [audio_input_u, assemblyai_api_key], [transcript])
187
 
188
 
189
  demo.queue().launch(debug=True)