pustozerov commited on
Commit
5f36b24
1 Parent(s): 075ef09

Implemented NER into the Streamlit interface.

Browse files
Files changed (2) hide show
  1. app.py +9 -2
  2. modules/nlp/nemo_ner.py +3 -3
app.py CHANGED
@@ -1,4 +1,3 @@
1
- import glob
2
  import random
3
  import os
4
  import numpy as np
@@ -9,6 +8,7 @@ from datasets import load_dataset
9
  from scipy.io.wavfile import write
10
 
11
  from modules.diarization.nemo_diarization import diarization
 
12
 
13
  FOLDER_WAV_DB = "data/database/"
14
  FOLDER_USER_DATA = "data/user_data/"
@@ -32,12 +32,19 @@ if st.button('Try a random sample from the database'):
32
  st.audio(audio_file.read())
33
  st.write("Starting transcription. Estimated processing time: %0.1f seconds" % (f.frames / (f.samplerate * 5)))
34
  result = diarization(os.path.join(FOLDER_WAV_DB, file_name + '.wav'))
 
 
 
 
35
  with open("info/transcripts/pred_rttms/" + file_name + ".txt") as f:
36
  transcript = f.read()
37
- st.write("Transcription completed.")
 
 
38
  st.write("Number of speakers: %s" % result[file_name]["speaker_count"])
39
  st.write("Sentences: %s" % len(result[file_name]["sentences"]))
40
  st.write("Words: %s" % len(result[file_name]["words"]))
 
41
  st.download_button(
42
  label="Download audio transcript",
43
  data=transcript,
 
 
1
  import random
2
  import os
3
  import numpy as np
 
8
  from scipy.io.wavfile import write
9
 
10
  from modules.diarization.nemo_diarization import diarization
11
+ from modules.nlp.nemo_ner import detect_ner
12
 
13
  FOLDER_WAV_DB = "data/database/"
14
  FOLDER_USER_DATA = "data/user_data/"
 
32
  st.audio(audio_file.read())
33
  st.write("Starting transcription. Estimated processing time: %0.1f seconds" % (f.frames / (f.samplerate * 5)))
34
  result = diarization(os.path.join(FOLDER_WAV_DB, file_name + '.wav'))
35
+ sentences = result[file_name]["sentences"]
36
+ all_strings = ""
37
+ for sentence in sentences:
38
+ all_strings = all_strings + sentence["sentence"] + "\n"
39
  with open("info/transcripts/pred_rttms/" + file_name + ".txt") as f:
40
  transcript = f.read()
41
+ st.write("Transcription completed. Starting named entity recognition.")
42
+ tagged_string, tags_summary = detect_ner(all_strings)
43
+ transcript = transcript + '\n' + tagged_string
44
  st.write("Number of speakers: %s" % result[file_name]["speaker_count"])
45
  st.write("Sentences: %s" % len(result[file_name]["sentences"]))
46
  st.write("Words: %s" % len(result[file_name]["words"]))
47
+ st.write("Found named entities: %s" % tags_summary)
48
  st.download_button(
49
  label="Download audio transcript",
50
  data=transcript,
modules/nlp/nemo_ner.py CHANGED
@@ -9,8 +9,8 @@ pretrained_ner_model = nemo_nlp.models.TokenClassificationModel.from_pretrained(
9
  model_name="ner_en_bert", override_config_path=new_config)
10
 
11
 
12
- def detect_ner(input_string):
13
- tagged_string = pretrained_ner_model.add_predictions([input_string.replace('[', '').replace(']', '')])[0]
14
  tags = re.findall('\[.*?]', tagged_string)
15
- tags_summary = "Found named entities: " + str(dict(Counter(tags)))[1:-1]
16
  return tagged_string, tags_summary
 
9
  model_name="ner_en_bert", override_config_path=new_config)
10
 
11
 
12
+ def detect_ner(input_strings):
13
+ tagged_string = pretrained_ner_model.add_predictions([input_strings.replace('[', '').replace(']', '')])[0]
14
  tags = re.findall('\[.*?]', tagged_string)
15
+ tags_summary = str(dict(Counter(tags)))[1:-1]
16
  return tagged_string, tags_summary