|
1 | | -class SpeechToText: |
2 | | - SLASHML_BASE_URL = 'https://api.slashml.com/v1/speech-to-text' |
3 | | - SLASHML_UPLOAD_URL = SLASHML_BASE_URL+'/upload' |
4 | | - SLASHML_TRANSCRIPT_URL = SLASHML_BASE_URL+'/transcribe' |
5 | | - SLASHML_TRANSCRIPT_STATUS_URL = lambda self,id: f"{SpeechToText.SLASHML_TRANSCRIPT_URL}/{id}" |
| 1 | +from django.http import JsonResponse |
| 2 | +import requests |
| 3 | +import os |
| 4 | +from pathlib import Path |
6 | 5 |
|
| 6 | +class SpeechToText: |
| 7 | + SLASHML_BASE_URL = 'https://api.slashml.com/speech-to-text/v1' |
| 8 | + SLASHML_UPLOAD_URL = SLASHML_BASE_URL+'/upload/' |
| 9 | + SLASHML_TRANSCRIPT_URL = SLASHML_BASE_URL+'/transcribe/' |
| 10 | + SLASHML_STATUS_URL = SLASHML_BASE_URL+'/transcription' |
| 11 | + SLASHML_TRANSCRIPT_STATUS_URL = lambda self,id: f"{SpeechToText.SLASHML_STATUS_URL}/{id}/" |
| 12 | + |
| 13 | + HEADERS:dict = {} |
| 14 | + |
| 15 | + def __init__(self) -> None: |
| 16 | + self.HEADERS = {'authorization': os.environ.get('SLASHML_API_KEY')} |
7 | 17 |
|
8 | | - def upload_audio(self, file_location) : |
| 18 | + def upload_audio(self, file_location:str, API_KEY: str, model_choice: str ): |
9 | 19 | # here we can also add the service? assemblyai, aws, gcp? |
10 | | - return self.SLASHML_UPLOAD_URL #response.json() |
11 | 20 |
|
12 | | - def transcribe(self,upload_url, model_params:dict()): |
| 21 | + token="Token {0}".format(API_KEY) |
| 22 | + headers = {'authorization': token} |
| 23 | + payload = { 'model':model_choice } |
| 24 | + files=[ |
| 25 | + ('audio',('test_audio.mp3',open(file_location,'rb'),'audio/mpeg')) |
| 26 | + ] |
| 27 | + #import pdb |
| 28 | + #pdb.set_trace() |
| 29 | + response = requests.post(self.SLASHML_UPLOAD_URL, |
| 30 | + headers=headers, |
| 31 | + data=payload,files=files) |
| 32 | + return response.text |
| 33 | + |
| 34 | + def transcribe(self,upload_url:str, API_KEY: str, model_choice: str ): |
13 | 35 | # here we can add more model params |
14 | 36 | transcript_request = {'audio_url': upload_url} |
15 | | - #transcript_response = requests.post( self.SLASHML_TRANSCRIPT_URL, json=transcript_request) |
16 | | - job_id=self.SLASHML_TRANSCRIPT_URL |
17 | | - return job_id |
| 37 | + token="Token {0}".format(API_KEY) |
| 38 | + headers = {'authorization': token} |
| 39 | + payload = { |
| 40 | + "uploaded_audio_url": upload_url, |
| 41 | + "model": model_choice |
| 42 | + } |
| 43 | + |
| 44 | + response = requests.post(self.SLASHML_TRANSCRIPT_URL, headers=headers, data=payload) |
| 45 | + return response.text |
18 | 46 |
|
19 | | - def status(self, job_id:str): |
20 | | - #options={"status1"="queue","status2":"completed","status3":"deleted","text":"output_api_result"...} |
21 | | - return self.SLASHML_TRANSCRIPT_STATUS_URL(job_id) |
| 47 | + def status(self,job_id:str, API_KEY: str, model_choice: str ): |
| 48 | + token="Token {0}".format(API_KEY) |
| 49 | + headers = {'authorization': token} |
| 50 | + payload = { |
| 51 | + "model": model_choice |
| 52 | + } |
| 53 | + #import pdb |
| 54 | + # pdb.set_trace() |
| 55 | + response = requests.get(self.SLASHML_TRANSCRIPT_STATUS_URL(job_id) , headers=headers, data=payload) |
| 56 | + return response.text |
22 | 57 |
|
0 commit comments