AngelinaZanardi commited on
Commit
37b5da4
·
verified ·
1 Parent(s): 6bf2e4b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -9
app.py CHANGED
@@ -3,6 +3,7 @@ import os
3
  import re
4
 
5
  import torch
 
6
 
7
  import gradio as gr
8
  import spaces
@@ -21,6 +22,8 @@ lang = "no"
21
 
22
  logo_path = os.path.join(os.path.dirname(__file__), "Logo_2.png")
23
 
 
 
24
  share = (os.environ.get("SHARE", "False")[0].lower() in "ty1") or None
25
  auth_token = os.environ.get("AUTH_TOKEN") or True
26
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
@@ -52,11 +55,25 @@ def format_output(text):
52
  return text
53
 
54
  def transcribe(file, return_timestamps=False):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  if not return_timestamps:
56
- text = pipe(file)["text"]
57
  formatted_text = format_output(text)
58
  else:
59
- chunks = pipe(file, return_timestamps=True)["chunks"]
60
  text = []
61
  for chunk in chunks:
62
  start_time = time.strftime('%H:%M:%S', time.gmtime(chunk["timestamp"][0])) if chunk["timestamp"][0] is not None else "??:??:??"
@@ -64,7 +81,19 @@ def transcribe(file, return_timestamps=False):
64
  line = f"[{start_time} -> {end_time}] {chunk['text']}"
65
  text.append(line)
66
  formatted_text = "\n".join(text)
 
 
 
 
 
 
 
 
 
 
67
  formatted_text += "<br><br><i>Transkribert med NB-Whisper demo</i>"
 
 
68
  return formatted_text
69
 
70
  def _return_yt_html_embed(yt_url):
@@ -101,13 +130,6 @@ demo = gr.Blocks()
101
 
102
  with demo:
103
  with gr.Row():
104
- #with gr.Column(scale=1, min_width=120):
105
- #gr.Markdown(
106
- #f"""
107
- #gr.Markdown("!local img[]({logo_path})")
108
-
109
- #"""
110
- #)
111
  gr.HTML("<img src='file/Logo_2.png'>")
112
  with gr.Column(scale=8):
113
  # Use Markdown for title and description
 
3
  import re
4
 
5
  import torch
6
+ import torchaudio
7
 
8
  import gradio as gr
9
  import spaces
 
22
 
23
  logo_path = os.path.join(os.path.dirname(__file__), "Logo_2.png")
24
 
25
+ max_audio_length= 1 * 60
26
+
27
  share = (os.environ.get("SHARE", "False")[0].lower() in "ty1") or None
28
  auth_token = os.environ.get("AUTH_TOKEN") or True
29
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 
55
  return text
56
 
57
  def transcribe(file, return_timestamps=False):
58
+ waveform, sample_rate = torchaudio.load(file)
59
+ audio_duration = waveform.size(1) / sample_rate
60
+
61
+ if audio_duration > MAX_AUDIO_LENGTH:
62
+ # Trim the waveform to the first 30 minutes
63
+ waveform = waveform[:, :int(MAX_AUDIO_LENGTH * sample_rate)]
64
+ truncated_file = "truncated_audio.wav"
65
+ torchaudio.save(truncated_file, waveform, sample_rate)
66
+ file_to_transcribe = truncated_file
67
+ truncated = True
68
+ else:
69
+ file_to_transcribe = file
70
+ truncated = False
71
+
72
  if not return_timestamps:
73
+ text = pipe(file_to_transcribe)["text"]
74
  formatted_text = format_output(text)
75
  else:
76
+ chunks = pipe(file_to_transcribe, return_timestamps=True)["chunks"]
77
  text = []
78
  for chunk in chunks:
79
  start_time = time.strftime('%H:%M:%S', time.gmtime(chunk["timestamp"][0])) if chunk["timestamp"][0] is not None else "??:??:??"
 
81
  line = f"[{start_time} -> {end_time}] {chunk['text']}"
82
  text.append(line)
83
  formatted_text = "\n".join(text)
84
+
85
+ if truncated:
86
+ disclaimer = (
87
+ "\n\nDette er en demo. Det er ikke tillatt å bruke denne teksten i profesjonell sammenheng. "
88
+ "Vi anbefaler at hvis du trenger å transkribere lengre opptak, så kjører du enten modellen lokalt "
89
+ "eller sjekker denne siden for å se hvem som leverer løsninger basert på NB-Whisper: "
90
+ "https://github.com/NbAiLab/nostram/blob/main/leverandorer.md"
91
+ )
92
+ formatted_text += f"<br><br><i>{disclaimer}</i>"
93
+
94
  formatted_text += "<br><br><i>Transkribert med NB-Whisper demo</i>"
95
+
96
+
97
  return formatted_text
98
 
99
  def _return_yt_html_embed(yt_url):
 
130
 
131
  with demo:
132
  with gr.Row():
 
 
 
 
 
 
 
133
  gr.HTML("<img src='file/Logo_2.png'>")
134
  with gr.Column(scale=8):
135
  # Use Markdown for title and description