File size: 4,276 Bytes
d0e0a14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
async function deleteRecord(clickedElement, type) {
    const recordElement = clickedElement.closest('.record');
    makeLoading()
    if (recordElement) {
        const recordID = recordElement.getAttribute('record-id');
        const response = await fetch('/delete-record', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({'record_id': parseInt(recordID), 'type': type})
        });
        if (response.ok) {
            stopLoading()
            recordElement.remove();
        }
    }
}

async function getRecordTranscription(recordId, type) {
    try {
        makeLoading()
        const url = `/record/${recordId}/${type}/transcription`;
        const response = await fetch(url);

        if (!response.ok) {
            throw new Error(`Error: ${response.status}`);
        }
        const result = await response.json();
        transcriptionReportBlock.innerHTML = marked.parse(result)
        stopLoading()
    } catch (error) {
        console.error('Failed to fetch transcription:', error);
        stopLoading()
        transcriptionReportBlock.innerText = '';
    }
}

async function playRecord(recordId) {
    try {
        const url = `/record/${recordId}/audio/play`;
        makeLoading()
        const response = await fetch(url);

        if (!response.ok) {
            throw new Error(`Error: ${response.status}`);
        }
        const audioBase64 = await response.json();
        const audioSrc = `data:audio/mp3;base64,${audioBase64}`;
        const audio = new Audio(audioSrc);
        stopLoading()
        await audio.play();
    } catch (error) {
        stopLoading()
        console.error('Failed to fetch transcription:', error);
    }
}

async function createAIReport() {
    try {
        transcriptionReportBlock.innerText = ''
        const records = document.getElementsByClassName('record');
        const imageRecordIDs = [];
        const audioRecordIDs = [];

        for (let i = 0; i < records.length; i++) {
            const recordID = records[i].getAttribute('record-id');
            const objectType = records[i].getAttribute('object-type');

            if (objectType === 'image') {
                imageRecordIDs.push(recordID);
            } else if (objectType === 'audio') {
                audioRecordIDs.push(recordID);
            }
        }

        if (imageRecordIDs.length === 0 && audioRecordIDs.length === 0) {
            alert('Record at least one audio or image.');
        }
        const requestBody = JSON.stringify({
            audio_ids: audioRecordIDs,
            image_ids: imageRecordIDs,
            language: selectLanguage.value
        });
        makeLoading()
        const response = await fetch('/report', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: requestBody
        });
        if (!response.ok) {
            const errorData = await response.json()
            throw new Error(errorData.detail);
        }
        const result = await response.json();
        transcriptionReportBlock.innerHTML = marked.parse(result)
        stopLoading()
    } catch (error) {
        console.log(error)
        transcriptionReportBlock.innerHTML = `<h4 style="padding-top: 0.7em">${error}</h4>`;
        stopLoading()
    }
}

function generateUUID() {
    const arr = new Uint8Array(16);
    window.crypto.getRandomValues(arr);

    arr[6] = (arr[6] & 0x0f) | 0x40;
    arr[8] = (arr[8] & 0x3f) | 0x80;

    return ([...arr].map((b, i) =>
        (i === 4 || i === 6 || i === 8 || i === 10 ? "-" : "") + b.toString(16).padStart(2, "0")
    ).join(""));
}

function signOut() {
    const confirmed = confirm('Are you sure you want to sign out?');
    if (!confirmed) {
        return false;
    }
    fetch('/auth/jwt/logout', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        }
    })
        .then(function (response) {
            if (response.ok) {
                window.location.href = 'http://' + window.location.host + '/user/login';
            } else {
                console.error('Error logging out user');
            }
        })
}