Skip to content

Commit 15c039f

Browse files
committed
first commit
1 parent a64653c commit 15c039f

12 files changed

+2538
-8
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ celerybeat.pid
102102
*.sage.py
103103

104104
# Environments
105+
default.env
105106
.env
106107
.venv
107108
env/

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM python:3.8
1+
FROM python:3.9
22
WORKDIR /usr/app/src
33
COPY requirements.txt ./
44
RUN pip install --no-cache-dir -r requirements.txt

README.md

+42-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,52 @@
1-
# python-template
2-
Template to run python in a Docker container using docker-compose
1+
# assembly-ai
2+
Run Assembly AI in a Docker container using docker-compose
3+
https://www.assemblyai.com/docs/reference#transcript
34

45
## pre-requisite
56

67
- [install docker](https://www.docker.com/get-started/)
8+
- create a `.env` file with your API key and content url
9+
10+
```bash
11+
ASSEMBLY_AI_TOKEN=
12+
CONTENT_URL=https://augie-public-test.s3.amazonaws.com/89e5915c-cf8b-4f18-9b22-31255e4155cc/e2dc554f-740b-4973-a944-3d53046621a8/434d1c6a-7974-4749-9416-a92a3a3fe597.mp3
13+
```
714

815
## run
916

1017
```sh
1118
docker-compose up --build
19+
```
20+
21+
## results
22+
23+
### paragraph
24+
25+
```json
26+
[
27+
{
28+
"text": "Storytelling transporting an audience into your own imagination. For over 60,000 years, humans have been sharing stories with each other. From around the fire to the first cave paintings, to artwork and to interactive TV shows. We've evolved our ability to more richly engage our audiences. It's never been easier to record videos of current events, sports, arts, comedy, anything you can imagine.",
29+
"start": 912,
30+
"end": 25197,
31+
"confidence": 0.90779,
32+
"words": [
33+
{
34+
"text": "Storytelling",
35+
"start": 912,
36+
"end": 1917,
37+
"confidence": 0.90779,
38+
"speaker": null
39+
},
40+
{
41+
"text": "transporting",
42+
"start": 2052,
43+
"end": 2727,
44+
"confidence": 0.89105,
45+
"speaker": null
46+
},
47+
...
48+
...
49+
]
50+
},
51+
]
1252
```

docker-compose.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ version: "3.7"
22
services:
33
operator:
44
build: .
5-
image: python_template:0.1.0
5+
image: assemgly_ai:0.1.0
6+
env_file:
7+
- default.env
68
volumes:
79
- ${PWD}:/usr/app/src/

main.py

+119-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,120 @@
1-
def main():
2-
print("main")
3-
4-
if __name__ == "__main__":
1+
import requests
2+
import time
3+
import os
4+
import json
5+
6+
endpoint = 'https://api.assemblyai.com/v2/transcript'
7+
8+
# get token from environment variable
9+
api_token = os.environ.get('ASSEMBLY_AI_TOKEN')
10+
content_url = os.environ.get('CONTENT_URL')
11+
12+
headers = {
13+
'authorization': api_token,
14+
'content-type': 'application/json'
15+
}
16+
transcript_endpoint = 'https://api.assemblyai.com/v2/transcript'
17+
18+
def request_transcript():
19+
transcript_request = {
20+
'audio_url': content_url,
21+
'filter_profanity': True, # Profanity Filtering https://www.assemblyai.com/docs/core-transcription#profanity-filtering
22+
'punctuate': True, # Automate Punctuation and Casing https://www.assemblyai.com/docs/core-transcription#automatic-punctuation-and-casing
23+
'language_detection': True, # Automatic Language Detection https://www.assemblyai.com/docs/core-transcription#automatic-language-detection
24+
'auto_highlights': True, # Detect Important Phrases and Words https://www.assemblyai.com/docs/audio-intelligence#detect-important-phrases-and-words
25+
'content_safety': True, # Content Moderation https://www.assemblyai.com/docs/audio-intelligence#content-moderation
26+
'iab_categories': True, # Topic Detection(IAB Categories) https://www.assemblyai.com/docs/audio-intelligence#topic-detection-iab-classification
27+
'sentiment_analysis': True, # Sentiment Analysis https://www.assemblyai.com/docs/audio-intelligence#sentiment-analysis
28+
'summary_type': 'bullets', # Summary bullets https://www.assemblyai.com/docs/audio-intelligence#summarization
29+
#'summary_type': 'gist', # Summary gist
30+
#'summary_type': 'headline', # Summary headline
31+
#'summary_type': 'paragraph', # Summary paragraph
32+
'auto_chapters': True, # Automatic Chapters https://www.assemblyai.com/docs/audio-intelligence#auto-chapters
33+
'entity_detection': True, # Entity Detection https://www.assemblyai.com/docs/audio-intelligence#entity-detection
34+
}
35+
transcript_response = requests.post(
36+
transcript_endpoint,
37+
json=transcript_request,
38+
headers=headers
39+
)
40+
return transcript_response.json()
41+
42+
def make_polling_endpoint(transcript_response):
43+
polling_endpoint = 'https://api.assemblyai.com/v2/transcript/'
44+
polling_endpoint += transcript_response['id']
45+
print('Polling endpoint: ' + polling_endpoint)
46+
return polling_endpoint
47+
48+
def wait_for_completion(polling_endpoint):
49+
while True:
50+
polling_response = requests.get(polling_endpoint, headers=headers)
51+
polling_response = polling_response.json()
52+
53+
if polling_response['status'] == 'completed':
54+
return polling_response
55+
56+
if polling_response['status'] == 'error':
57+
print('Error: ' + polling_response['error'])
58+
break
59+
60+
print ('Status: ' + polling_response['status'])
61+
time.sleep(5)
62+
63+
def get_paragraphs(polling_endpoint):
64+
response = requests.get(polling_endpoint + '/paragraphs', headers=headers)
65+
response = response.json()
66+
data = []
67+
for para in response['paragraphs']:
68+
data.append(para)
69+
return data
70+
71+
def get_sentences(polling_endpoint):
72+
response = requests.get(polling_endpoint + '/sentences', headers=headers)
73+
response = response.json()
74+
data = []
75+
for para in response['sentences']:
76+
data.append(para)
77+
return data
78+
79+
def main():
80+
# save request transcript
81+
transcript_response = request_transcript()
82+
json_object = json.dumps(transcript_response, indent=4)
83+
with open('request.json', 'w') as outfile:
84+
outfile.write(json_object)
85+
86+
# poll and wait
87+
polling_endpoint = make_polling_endpoint(transcript_response)
88+
transcription = wait_for_completion(polling_endpoint)
89+
90+
# save transcription
91+
json_object = json.dumps(transcription, indent=4)
92+
with open('transcription.json', 'w') as outfile:
93+
outfile.write(json_object)
94+
95+
# save paragraphs
96+
paragraphs = get_paragraphs(polling_endpoint)
97+
json_object = json.dumps(paragraphs, indent=4)
98+
with open('paragraphs.json', 'w') as outfile:
99+
outfile.write(json_object)
100+
101+
# save sentences
102+
sentences = get_sentences(polling_endpoint)
103+
json_object = json.dumps(sentences, indent=4)
104+
with open('sentences.json', 'w') as outfile:
105+
outfile.write(json_object)
106+
107+
# save srt
108+
response = requests.get(polling_endpoint + '/srt', headers=headers)
109+
response = response.text
110+
with open('srt.txt', 'w') as outfile:
111+
outfile.write(response)
112+
113+
# save vtt
114+
response = requests.get(polling_endpoint + '/vtt', headers=headers)
115+
response = response.text
116+
with open('vtt.txt', 'w') as outfile:
117+
outfile.write(response)
118+
119+
if __name__ == '__main__':
5120
main()

0 commit comments

Comments
 (0)