Spaces:
Afrinetwork
/
Running on TPU v5e

Afrinetwork7 commited on
Commit
29850f3
1 Parent(s): 32e4bd8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -11
app.py CHANGED
@@ -3,6 +3,8 @@ import math
3
  import os
4
  import tempfile
5
  import time
 
 
6
  import yt_dlp as youtube_dl
7
  from fastapi import FastAPI, UploadFile, Form, HTTPException
8
  from fastapi.responses import HTMLResponse
@@ -48,8 +50,22 @@ random_timestamps = pipeline.forward(random_inputs, batch_size=BATCH_SIZE, retur
48
  compile_time = time.time() - start
49
  logger.debug(f"Compiled in {compile_time}s")
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  @app.post("/transcribe_audio")
52
- async def transcribe_chunked_audio(audio_file: UploadFile, task: str = "transcribe", return_timestamps: bool = False):
 
53
  logger.debug("Starting transcribe_chunked_audio function")
54
  logger.debug(f"Received parameters - task: {task}, return_timestamps: {return_timestamps}")
55
 
@@ -85,17 +101,18 @@ async def transcribe_chunked_audio(audio_file: UploadFile, task: str = "transcri
85
 
86
  logger.debug("Calling tqdm_generate to transcribe audio")
87
  try:
88
- text, runtime = tqdm_generate(inputs, task=task, return_timestamps=return_timestamps)
89
  logger.debug(f"Transcription completed. Runtime: {runtime:.2f}s")
90
  except Exception as e:
91
  logger.error(f"Error in tqdm_generate: {str(e)}", exc_info=True)
92
  raise HTTPException(status_code=500, detail=f"Error transcribing audio: {str(e)}")
93
 
94
  logger.debug("Transcribe_chunked_audio function completed successfully")
95
- return {"text": text, "runtime": runtime}
96
 
97
  @app.post("/transcribe_youtube")
98
- async def transcribe_youtube(yt_url: str = Form(...), task: str = "transcribe", return_timestamps: bool = False):
 
99
  logger.debug("Loading YouTube file...")
100
  try:
101
  html_embed_str = _return_yt_html_embed(yt_url)
@@ -126,14 +143,15 @@ async def transcribe_youtube(yt_url: str = Form(...), task: str = "transcribe",
126
 
127
  try:
128
  logger.debug("Calling tqdm_generate to transcribe YouTube audio")
129
- text, runtime = tqdm_generate(inputs, task=task, return_timestamps=return_timestamps)
130
  except Exception as e:
131
  logger.error("Error transcribing YouTube audio:", exc_info=True)
132
  raise HTTPException(status_code=500, detail="Error transcribing YouTube audio")
133
 
134
- return {"html_embed": html_embed_str, "text": text, "runtime": runtime}
135
 
136
  def tqdm_generate(inputs: dict, task: str, return_timestamps: bool):
 
137
  logger.debug(f"Starting tqdm_generate - task: {task}, return_timestamps: {return_timestamps}")
138
 
139
  inputs_len = inputs["array"].shape[0]
@@ -153,7 +171,7 @@ def tqdm_generate(inputs: dict, task: str, return_timestamps: bool):
153
  raise
154
 
155
  model_outputs = []
156
- start_time = time.time()
157
  logger.debug("Starting transcription...")
158
 
159
  try:
@@ -166,8 +184,8 @@ def tqdm_generate(inputs: dict, task: str, return_timestamps: bool):
166
  logger.error(f"Error during batch processing: {str(e)}", exc_info=True)
167
  raise
168
 
169
- runtime = time.time() - start_time
170
- logger.debug(f"Transcription completed in {runtime:.2f}s")
171
 
172
  logger.debug("Post-processing transcription results")
173
  try:
@@ -186,8 +204,12 @@ def tqdm_generate(inputs: dict, task: str, return_timestamps: bool):
186
  ]
187
  text = "\n".join(str(feature) for feature in timestamps)
188
 
 
189
  logger.debug("tqdm_generate function completed successfully")
190
- return text, runtime
 
 
 
191
 
192
  def _return_yt_html_embed(yt_url):
193
  video_id = yt_url.split("?v=")[-1]
@@ -246,4 +268,4 @@ def format_timestamp(seconds: float, always_include_hours: bool = False, decimal
246
  return f"{hours_marker}{minutes:02d}:{seconds:02d}{decimal_marker}{milliseconds:03d}"
247
  else:
248
  # we have a malformed timestamp so just return it as is
249
- return seconds
 
3
  import os
4
  import tempfile
5
  import time
6
+ from typing import Dict, Any
7
+
8
  import yt_dlp as youtube_dl
9
  from fastapi import FastAPI, UploadFile, Form, HTTPException
10
  from fastapi.responses import HTMLResponse
 
50
  compile_time = time.time() - start
51
  logger.debug(f"Compiled in {compile_time}s")
52
 
53
+ def timeit(func):
54
+ async def wrapper(*args, **kwargs):
55
+ start_time = time.time()
56
+ result = await func(*args, **kwargs)
57
+ end_time = time.time()
58
+ execution_time = end_time - start_time
59
+ if isinstance(result, dict):
60
+ result['total_execution_time'] = execution_time
61
+ else:
62
+ result = {'result': result, 'total_execution_time': execution_time}
63
+ return result
64
+ return wrapper
65
+
66
  @app.post("/transcribe_audio")
67
+ @timeit
68
+ async def transcribe_chunked_audio(audio_file: UploadFile, task: str = "transcribe", return_timestamps: bool = False) -> Dict[str, Any]:
69
  logger.debug("Starting transcribe_chunked_audio function")
70
  logger.debug(f"Received parameters - task: {task}, return_timestamps: {return_timestamps}")
71
 
 
101
 
102
  logger.debug("Calling tqdm_generate to transcribe audio")
103
  try:
104
+ text, runtime, timing_info = tqdm_generate(inputs, task=task, return_timestamps=return_timestamps)
105
  logger.debug(f"Transcription completed. Runtime: {runtime:.2f}s")
106
  except Exception as e:
107
  logger.error(f"Error in tqdm_generate: {str(e)}", exc_info=True)
108
  raise HTTPException(status_code=500, detail=f"Error transcribing audio: {str(e)}")
109
 
110
  logger.debug("Transcribe_chunked_audio function completed successfully")
111
+ return {"text": text, "runtime": runtime, "timing_info": timing_info}
112
 
113
  @app.post("/transcribe_youtube")
114
+ @timeit
115
+ async def transcribe_youtube(yt_url: str = Form(...), task: str = "transcribe", return_timestamps: bool = False) -> Dict[str, Any]:
116
  logger.debug("Loading YouTube file...")
117
  try:
118
  html_embed_str = _return_yt_html_embed(yt_url)
 
143
 
144
  try:
145
  logger.debug("Calling tqdm_generate to transcribe YouTube audio")
146
+ text, runtime, timing_info = tqdm_generate(inputs, task=task, return_timestamps=return_timestamps)
147
  except Exception as e:
148
  logger.error("Error transcribing YouTube audio:", exc_info=True)
149
  raise HTTPException(status_code=500, detail="Error transcribing YouTube audio")
150
 
151
+ return {"html_embed": html_embed_str, "text": text, "runtime": runtime, "timing_info": timing_info}
152
 
153
  def tqdm_generate(inputs: dict, task: str, return_timestamps: bool):
154
+ start_time = time.time()
155
  logger.debug(f"Starting tqdm_generate - task: {task}, return_timestamps: {return_timestamps}")
156
 
157
  inputs_len = inputs["array"].shape[0]
 
171
  raise
172
 
173
  model_outputs = []
174
+ transcription_start_time = time.time()
175
  logger.debug("Starting transcription...")
176
 
177
  try:
 
184
  logger.error(f"Error during batch processing: {str(e)}", exc_info=True)
185
  raise
186
 
187
+ transcription_runtime = time.time() - transcription_start_time
188
+ logger.debug(f"Transcription completed in {transcription_runtime:.2f}s")
189
 
190
  logger.debug("Post-processing transcription results")
191
  try:
 
204
  ]
205
  text = "\n".join(str(feature) for feature in timestamps)
206
 
207
+ total_processing_time = time.time() - start_time
208
  logger.debug("tqdm_generate function completed successfully")
209
+ return text, transcription_runtime, {
210
+ "transcription_time": transcription_runtime,
211
+ "total_processing_time": total_processing_time
212
+ }
213
 
214
  def _return_yt_html_embed(yt_url):
215
  video_id = yt_url.split("?v=")[-1]
 
268
  return f"{hours_marker}{minutes:02d}:{seconds:02d}{decimal_marker}{milliseconds:03d}"
269
  else:
270
  # we have a malformed timestamp so just return it as is
271
+ return seconds