File size: 7,567 Bytes
b8712f3
abc89d1
b8712f3
abc89d1
 
 
351252d
 
 
 
 
 
 
 
 
 
 
abc89d1
 
d4b107b
cf8326e
b8712f3
85002a1
b8712f3
3d1368b
85002a1
b8712f3
 
c5571fa
dbca570
b8712f3
 
 
 
 
2f03bd6
dbca570
abc89d1
b8712f3
cf8326e
04f2c63
b8712f3
 
 
cf8326e
b8712f3
 
dbca570
b8712f3
 
 
 
 
 
 
73a1be0
3d1368b
5f04fc5
b8712f3
5f04fc5
 
3d1368b
 
 
5f04fc5
 
3d1368b
5f04fc5
73a1be0
b8712f3
 
5f04fc5
 
b8712f3
 
5f04fc5
361f8d0
b8712f3
5f04fc5
9d34978
5c44de8
5f04fc5
b8712f3
 
5f04fc5
9d34978
4a5b260
5f04fc5
 
 
 
3d1368b
b8712f3
5f04fc5
0a65978
a543e39
 
5f04fc5
 
9e722fb
b8712f3
 
 
 
 
 
2f48f87
b8712f3
1622cb0
b8712f3
 
 
 
 
 
abc89d1
 
 
 
c0228d9
abc89d1
18c392d
8ec53db
abc89d1
 
 
 
 
 
 
 
2fb8a5f
102fb89
 
1667a9d
fa68d0f
b8712f3
102fb89
 
 
1667a9d
fa68d0f
b8712f3
 
102fb89
 
dee4184
102fb89
abc89d1
6a67784
abc89d1
ad6d7c2
85002a1
5ca37ae
abc89d1
49113b6
f185ce3
 
 
 
53b4978
f185ce3
8fb6f57
 
aebda00
abc89d1
18c392d
1813060
102fb89
b8712f3
 
 
1813060
abc89d1
 
102fb89
b8712f3
73a1be0
56823a6
 
9d34978
 
 
 
 
 
 
 
 
 
 
 
b8712f3
 
8758d03
b8712f3
 
 
 
73a1be0
 
 
0a9e0ca
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python
### -----------------------------------------------------------------------
### (test_BASE, Revised) version_1.07 ALPHA, app.py
### -----------------------------------------------------------------------

# -------------------------------------------------------------------------
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -------------------------------------------------------------------------

import os
import re
import uuid
import time
import psutil
import pydub
import subprocess
from tqdm import tqdm

import tempfile
from fpdf import FPDF
from pathlib import Path

import numpy as np
import torch
from transformers import pipeline 

from gpuinfo import GPUInfo

import gradio as gr


###############################################################################
# Configuration.
###############################################################################

#if not torch.cuda.is_available():
    #DESCRIPTION += "\n<p>⚠️Running on CPU, This may not work on CPU.</p>"

CACHE_EXAMPLES = torch.device('cuda') and os.getenv("CACHE_EXAMPLES", "0") == "1"
#CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES", "0") == "1"
#USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
#ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"

device = torch.device('cuda')
#device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

#@spaces.GPU
def transcribe(file_upload, progress=gr.Progress(track_tqdm=True)): # microphone
    
    file = file_upload # microphone if microphone is not None else 
    start_time = time.time()
    
    #--------------____________________________________________--------------"

    with torch.no_grad():
        pipe = pipeline("automatic-speech-recognition", model="NbAiLab/nb-whisper-large", device=device)

    text = pipe(file)["text"] 

    #--------------____________________________________________--------------"
    
    end_time = time.time()
    output_time = end_time - start_time
    
    # --Word count
    word_count = len(text.split())

    # --Memory metrics
    memory = psutil.virtual_memory()
    
    # --CPU metric
    cpu_usage = psutil.cpu_percent(interval=1)

    # --GPU metric
    gpu_utilization, gpu_memory = GPUInfo.gpu_usage()
    
    # --system info string
    system_info = f"""
    Processing time: {output_time:.2f} seconds.
    Number of words: {word_count}
    GPU Memory: {gpu_memory}"""
 
    #--------------____________________________________________--------------"
    
    #CPU Usage: {cpu_usage}%
    #Memory used: {memory.percent}%
    #GPU Utilization: {gpu_utilization}%
    
    return text, system_info


###############################################################################
# Interface.
###############################################################################

HEADER_INFO = """
    # SWITCHVOX ✨|🇳🇴 *Transkribering av lydfiler til norsk bokmål.*
""".strip()
LOGO = "https://cdn-lfs-us-1.huggingface.co/repos/fe/3b/fe3bd7c8beece8b087fddcc2278295e7f56c794c8dcf728189f4af8bddc585e1/5112f67899d65e9797a7a60d05f983cf2ceefbe2f7cba74eeca93a4e7061becc?response-content-disposition=inline%3B+filename*%3DUTF-8%27%27logo.png%3B+filename%3D%22logo.png%22%3B&response-content-type=image%2Fpng&Expires=1725531489&Policy=eyJTdGF0ZW1lbnQiOlt7IkNvbmRpdGlvbiI6eyJEYXRlTGVzc1RoYW4iOnsiQVdTOkVwb2NoVGltZSI6MTcyNTUzMTQ4OX19LCJSZXNvdXJjZSI6Imh0dHBzOi8vY2RuLWxmcy11cy0xLmh1Z2dpbmdmYWNlLmNvL3JlcG9zL2ZlLzNiL2ZlM2JkN2M4YmVlY2U4YjA4N2ZkZGNjMjI3ODI5NWU3ZjU2Yzc5NGM4ZGNmNzI4MTg5ZjRhZjhiZGRjNTg1ZTEvNTExMmY2Nzg5OWQ2NWU5Nzk3YTdhNjBkMDVmOTgzY2YyY2VlZmJlMmY3Y2JhNzRlZWNhOTNhNGU3MDYxYmVjYz9yZXNwb25zZS1jb250ZW50LWRpc3Bvc2l0aW9uPSomcmVzcG9uc2UtY29udGVudC10eXBlPSoifV19&Signature=vNqupMg9p-Wx9BvqVlK5BhhyOeS58YjZW2-bEP4OVd0azdA0AfW0QG8EGxZ1h7UwuJnMwlPGayA0P46Ob9DSRH48BGjH176UgbPBcasSAI43jb9PJO9qIznrv9orzPt3ZrqTll0d9cKayQ96iPWQond-G5xbl0bNYb9qLXh9w3Ww%7EELKIFU9KeDOvIKww9cHftCeVFqCFJC%7Etimk-eOHo9g4xVfAaVMFoVNeJOVVpTW-MzPb1EGccyN9-3WJaF9Nwg3fkb7FRazg8IYcAatS2PahLpfp-zJup7y-ywnPzb8jJPgN3TBu6-M7hE4OHVcRmxeXk3VDRgSFVfbmnrlc%7Ew__&Key-Pair-Id=K24J24Z295AEI9"
SIDEBAR_INFO = f"""
<div align="center">
    <img src="{LOGO}" style="width: 100%; height: auto;"/>
</div>
"""

def save_to_pdf(text, summary):
    pdf = FPDF()
    pdf.add_page()
    pdf.set_font("Arial", size=12)

    if text:
        pdf.multi_cell(0, 10, "Transkribert Tekst:\n" + text)

    pdf.ln(10)  # Paragraph metric

    if summary:
        pdf.multi_cell(0, 10, "Summary:\n" + summary)

    pdf_output_path = "transcription_.pdf"
    pdf.output(pdf_output_path)
    return pdf_output_path

css = """
#transcription_output textarea {
    background-color: #000000;  /* black */
    color: #00FF00 !important;  /* text color */
    font-size: 18px;  /* font size */
}

#system_info_box textarea {
    background-color: #ffe0b3;  /* orange */
    color: black !important;  /* text color */
    font-size: 16px;  /* font size */
    font-weight: bold;  /* bold font */
}
"""

iface = gr.Blocks(css=css)

with iface:

    gr.HTML(SIDEBAR_INFO)
    gr.Markdown(HEADER_INFO)

    with gr.Row():
        gr.Markdown('''
        ##### 🔊 Last opp lydfila 
        ##### ☕️ Trykk på "Transkriber" knappen og vent på svar
        ##### ⚡️ Går rimelig bra kjapt med Norwegian NB-Whisper Large..
        ##### 😅 Planlegger tilleggs-funksjoner senere
        
        ''')
        #microphone = gr.Audio(label="Microphone", sources="microphone", type="filepath")
        upload = gr.Audio(label="Upload audio", sources="upload", type="filepath")
        transcribe_btn = gr.Button("Transkriber")

    with gr.Row():   
        with gr.Column(scale=3):
            text_output = gr.Textbox(label="Transkribert Tekst", elem_id="transcription_output")
        with gr.Column(scale=1):
            system_info = gr.Textbox(label="Antall sekunder, ord:", elem_id="system_info_box")
        
    
    with gr.Tabs():
        with gr.TabItem("Download PDF"):
            pdf_text_only = gr.Button("Last ned pdf med resultat")
            pdf_output = gr.File(label="/.pdf")

            pdf_text_only.click(fn=lambda text: save_to_pdf(text, ""), inputs=[text_output], outputs=[pdf_output])

    with gr.Row():
        gr.Markdown('''
        <div align="center">
            <a href="https://opensource.com/resources/what-open-source">
                <img src="https://badgen.net/badge/Open%20Source%20%3F/Yes%21/blue?icon=github" alt="Open Source? Yes!">
        </a>
        <span style="display:inline-block; width: 20px;"></span> 
        <a href="https://opensource.org/licenses/Apache-2.0">
            <img src="https://img.shields.io/badge/License-Apache_2.0-blue.svg" alt="License: Apache 2.0">
        </a>
        </div>
        ''')
    transcribe_btn.click(
        fn=transcribe, 
        inputs=[upload], # microphone 
        outputs=[text_output, system_info]
    )    
    
    #transcribe_btn.click(fn=transcribe, inputs=[microphone, upload], outputs=[text_output, system_info])



iface.launch(share=True,debug=True)