File size: 2,206 Bytes
0a0ea7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# utils/transcription_utils.py - Audio Transcription Utilities
import json
import os
from typing import Optional, Dict, Any

import aiohttp


async def _send_bytes_as_form_data(
        file_bytes: bytes,
        endpoint_url: str,
        field_name: str = "file",
        auth_token: Optional[str] = None,
        content_type: str = "application/octet-stream"
) -> Dict[str, Any]:
    """
    Send bytes as multipart form data POST request to an endpoint.

    Args:
        file_bytes: Bytes content to send
        endpoint_url: URL endpoint to send the POST request to
        field_name: Form field name for the file (default: "file")
        auth_token: Optional bearer token for authorization
        content_type: MIME type of the content (default: "application/octet-stream")

    Returns:
        Dictionary containing response status and data
    """
    # Create form data with the bytes
    data = aiohttp.FormData()
    data.add_field(
        field_name,
        file_bytes,
        content_type=content_type
    )

    # Prepare headers
    headers = {}
    if auth_token:
        headers['Authorization'] = f'Bearer {auth_token}'

    # Send POST request with form data
    async with aiohttp.ClientSession() as session:
        async with session.post(
                endpoint_url,
                data=data,
                headers=headers if headers else None
        ) as response:
            response_text = await response.text()

            return {
                'status': response.status,
                'success': response.status < 400,
                'response': response_text,
                'headers': dict(response.headers)
            }


async def transcribe(_bytes: bytes) -> dict:
    """
    Transcribe audio bytes using Modal endpoint

    Args:
        _bytes: Audio file bytes

    Returns:
        Dictionary containing transcription results
    """
    auth_token = os.environ['MODAL_AUTH_TOKEN']
    response = await _send_bytes_as_form_data(
        file_bytes=_bytes,
        endpoint_url='https://yigitsekerci6174--transcribe-audio.modal.run',
        auth_token=auth_token,
        field_name='file'
    )

    return json.loads(response['response'])