Mbonea commited on
Commit
f55cf0b
1 Parent(s): bb2cbff

better logging

Browse files
App/Transcription/Schemas.py CHANGED
@@ -4,5 +4,13 @@ from passlib.context import CryptContext
4
  from fastapi import UploadFile
5
 
6
 
 
 
 
 
 
 
 
 
7
  class UserDetails(BaseModel):
8
  userId: str
 
4
  from fastapi import UploadFile
5
 
6
 
7
+ class TranscriptionMetadata(BaseModel):
8
+ duration: int = 0
9
+ language: str = "0"
10
+ logs: str = 0
11
+ percentage: str = "0"
12
+ state: str = "PENDING"
13
+
14
+
15
  class UserDetails(BaseModel):
16
  userId: str
App/Transcription/Utils/audio_transcription.py CHANGED
@@ -1,42 +1,52 @@
1
  from faster_whisper import WhisperModel
2
  from tqdm import tqdm
3
  import os
 
4
 
5
  model_size = "tiny"
6
 
7
 
8
  def transcribe_file(state, file_path, model_size="tiny"):
 
 
 
9
  state.update_state(
10
  state="PROGRESS",
11
- meta={"logs": "Starting"},
12
  )
13
- result = {}
14
  model = WhisperModel(model_size, device="cpu", compute_type="int8")
15
  segments, info = model.transcribe(file_path, beam_size=5)
16
 
17
  total_duration = round(info.duration, 2)
 
 
 
 
 
 
18
  state.update_state(
19
  state="PROGRESS",
20
- meta={
21
- "logs": "Detected language '%s' with probability %f"
22
- % (info.language, info.language_probability),
23
- },
24
  )
25
 
26
- with tqdm(total=total_duration, unit=" seconds") as pbar:
27
- for segment in segments:
28
- segment_duration = segment.end - segment.start
29
- time_stamp = "[%.2fs -> %.2fs]" % (segment.start, segment.end)
30
- result[time_stamp] = segment.text
31
- state.update_state(
32
- state="PROGRESS",
33
- meta={
34
- "done": segment.end,
35
- "total": total_duration,
36
- "percentage": f"{((segment.end / total_duration)*100)}",
37
- },
38
- )
39
- pbar.update(segment_duration)
 
 
 
 
40
 
41
  # delete file
42
  os.remove(file_path)
 
1
  from faster_whisper import WhisperModel
2
  from tqdm import tqdm
3
  import os
4
+ from App.Transcription.Schemas import TranscriptionMetadata
5
 
6
  model_size = "tiny"
7
 
8
 
9
  def transcribe_file(state, file_path, model_size="tiny"):
10
+ result = {}
11
+ metadata = TranscriptionMetadata()
12
+ metadata.logs = "STARTING"
13
  state.update_state(
14
  state="PROGRESS",
15
+ meta=metadata.dict(),
16
  )
 
17
  model = WhisperModel(model_size, device="cpu", compute_type="int8")
18
  segments, info = model.transcribe(file_path, beam_size=5)
19
 
20
  total_duration = round(info.duration, 2)
21
+ metadata.logs = (
22
+ "Detected language '%s' with probability %f"
23
+ % (info.language, info.language_probability),
24
+ )
25
+ metadata.language = info.language
26
+
27
  state.update_state(
28
  state="PROGRESS",
29
+ meta=metadata.dict(),
 
 
 
30
  )
31
 
32
+ try:
33
+ with tqdm(total=total_duration, unit=" seconds") as pbar:
34
+ for segment in segments:
35
+ segment_duration = segment.end - segment.start
36
+ time_stamp = "[%.2fs -> %.2fs]" % (segment.start, segment.end)
37
+ result[time_stamp] = segment.text
38
+ metadata.logs = "Transcribing.."
39
+ metadata.percentage = f"{((segment.end / total_duration)*100)}"
40
+ state.update_state(state="PROGRESS", meta=metadata.dict())
41
+ pbar.update(segment_duration)
42
+ except Exception as e:
43
+ metadata.logs = f"Falied error {e}"
44
+ state.update_state(
45
+ state="FAILED",
46
+ meta=metadata.dict(),
47
+ )
48
+ os.remove(file_path)
49
+ return
50
 
51
  # delete file
52
  os.remove(file_path)