File size: 1,488 Bytes
823b2ec
 
 
 
a001ad9
823b2ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd1eb26
823b2ec
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import requests
from pprint import pprint
from time import sleep

api_key = 'YOUR_API_KEY'
audio_url = 'https://listentogenius.com/recordings4/Innocence.mp3'

def audio_to_text(api_key, audio_url):
  endpoint = "https://api.assemblyai.com/v2/transcript"

  headers = {
      'authorization': api_key, 
      'content-type': 'application/json'
  }

  body = {
      'audio_url': audio_url
  }

  # 1. Submitting Files for Transcription

  res = requests.post(endpoint, json=body, headers=headers)
  transcript_id = res.json().get('id')
  print(transcript_id)

  # 2. Getting the Transcription Result

  endpoint_full = f"https://api.assemblyai.com/v2/transcript/{transcript_id}"
  status = 'processing'
  while transcript_id and status != 'completed':
      print('Getting the transcription result ....')
      sleep(5)
      res_text = requests.get(endpoint_full, headers=headers)
      status = res_text.json().get('status')
  data = res_text.json().get('text')

  return data


import gradio as gr

demo = gr.Blocks()

with demo:
    gr.Markdown(
    """
    # Convert Audio files to Text files using AssemblyAI. 
    ### Get AssemblyAI API Key from here https://app.assemblyai.com/
    """)
    inp = [gr.Textbox(label='API Key', placeholder="What is your API Key?"), gr.Textbox(label='Audio File URL', placeholder="Audio file URL?")]
    out = gr.Textbox(label='Output')
    text_button = gr.Button("Flip")

    text_button.click(audio_to_text, inputs=inp, outputs=out)

demo.launch()