timgremore commited on
Commit
f76a27b
1 Parent(s): 4d87f7f

fix: Transcription server test

Browse files
lib/medical_transcription/transcription_server.ex CHANGED
@@ -38,8 +38,9 @@ defmodule Medicode.TranscriptionServer do
38
  end
39
 
40
  @impl GenServer
41
- def handle_continue(:start, {:transcription, transcription} = state) do
42
- Transcriptions.update_transcription(transcription, %{status: :transcribing})
 
43
 
44
  Phoenix.PubSub.broadcast(
45
  :medicode_pubsub,
@@ -49,7 +50,7 @@ defmodule Medicode.TranscriptionServer do
49
 
50
  stream_transcription_and_search(transcription.filename)
51
 
52
- {:noreply, state}
53
  end
54
 
55
  @impl GenServer
@@ -78,10 +79,6 @@ defmodule Medicode.TranscriptionServer do
78
  {:noreply, state}
79
  end
80
 
81
- def handle_info({:summary, _result}, state) do
82
- {:noreply, state}
83
- end
84
-
85
  def handle_info(:finished, state) do
86
  {:stop, :normal, state}
87
  end
@@ -90,7 +87,8 @@ defmodule Medicode.TranscriptionServer do
90
  def terminate(reason, state) do
91
  {:transcription, transcription} = state
92
 
93
- Transcriptions.update_transcription(transcription, %{status: :finished})
 
94
 
95
  %Transcription{id: id} = transcription
96
 
@@ -111,39 +109,21 @@ defmodule Medicode.TranscriptionServer do
111
  # well
112
  defp stream_transcription_and_search(audio_file_path) do
113
  # audio transcription + semantic search
114
- summary_text =
115
- Medicode.TranscriptionServing
116
- |> Nx.Serving.batched_run({:file, audio_file_path})
117
- |> Stream.with_index()
118
- |> Stream.map(fn {chunk, index} ->
119
- send_result(:chunk, chunk, index + 1)
120
- chunk.text
121
- end)
122
- |> Enum.to_list()
123
- |> Enum.join()
124
-
125
- summary_chunk = %{
126
- text: summary_text,
127
- start_timestamp_seconds: nil,
128
- end_timestamp_seconds: nil
129
- }
130
-
131
- send_result(:summary, summary_chunk, 0)
132
 
133
  send(self(), :finished)
134
  end
135
 
136
- defp send_result(status, chunk, index) when status in [:chunk, :summary] do
137
- result = %{
138
- id: index,
139
- start_mark: format_timestamp(chunk.start_timestamp_seconds),
140
- end_mark: format_timestamp(chunk.end_timestamp_seconds),
141
- text: chunk.text
142
- }
143
-
144
- send(self(), {status, result})
145
- end
146
-
147
  defp format_timestamp(seconds) when is_nil(seconds), do: nil
148
 
149
  defp format_timestamp(seconds) do
 
38
  end
39
 
40
  @impl GenServer
41
+ def handle_continue(:start, {:transcription, transcription}) do
42
+ {:ok, transcription} =
43
+ Transcriptions.update_transcription(transcription, %{status: :transcribing})
44
 
45
  Phoenix.PubSub.broadcast(
46
  :medicode_pubsub,
 
50
 
51
  stream_transcription_and_search(transcription.filename)
52
 
53
+ {:noreply, {:transcription, transcription}}
54
  end
55
 
56
  @impl GenServer
 
79
  {:noreply, state}
80
  end
81
 
 
 
 
 
82
  def handle_info(:finished, state) do
83
  {:stop, :normal, state}
84
  end
 
87
  def terminate(reason, state) do
88
  {:transcription, transcription} = state
89
 
90
+ {:ok, transcription} =
91
+ Transcriptions.update_transcription(transcription, %{status: :finished})
92
 
93
  %Transcription{id: id} = transcription
94
 
 
109
  # well
110
  defp stream_transcription_and_search(audio_file_path) do
111
  # audio transcription + semantic search
112
+ Medicode.TranscriptionServing
113
+ |> Nx.Serving.batched_run({:file, audio_file_path})
114
+ |> Enum.each(fn chunk ->
115
+ result = %{
116
+ start_mark: format_timestamp(chunk.start_timestamp_seconds),
117
+ end_mark: format_timestamp(chunk.end_timestamp_seconds),
118
+ text: chunk.text
119
+ }
120
+
121
+ send(self(), {:chunk, result})
122
+ end)
 
 
 
 
 
 
 
123
 
124
  send(self(), :finished)
125
  end
126
 
 
 
 
 
 
 
 
 
 
 
 
127
  defp format_timestamp(seconds) when is_nil(seconds), do: nil
128
 
129
  defp format_timestamp(seconds) do
test/medical_transcription/transcription_server_test.exs CHANGED
@@ -21,7 +21,9 @@ defmodule Medicode.TranscriptionServerTest do
21
  end
22
 
23
  test "transcribe and tag audio", %{transcription: transcription} do
24
- spec = {TranscriptionServer, {:transcription, transcription}}
 
 
25
 
26
  {:ok, pid} = start_supervised(spec, restart: :transient)
27
 
@@ -30,7 +32,8 @@ defmodule Medicode.TranscriptionServerTest do
30
  # for further explanation.
31
  ref = Process.monitor(pid)
32
 
33
- assert_receive({:DOWN, ^ref, :process, _object, _pid}, 5_000)
 
34
 
35
  transcription = Transcriptions.get_transcription!(transcription.id, true)
36
  assert 14 == Enum.count(transcription.chunks)
 
21
  end
22
 
23
  test "transcribe and tag audio", %{transcription: transcription} do
24
+ spec =
25
+ {TranscriptionServer,
26
+ %{transcription: transcription, name: "transcription:#{transcription.id}"}}
27
 
28
  {:ok, pid} = start_supervised(spec, restart: :transient)
29
 
 
32
  # for further explanation.
33
  ref = Process.monitor(pid)
34
 
35
+ # NOTE: This timeout of 15 seconds is arbitrary and may need to be adjusted
36
+ assert_receive({:DOWN, ^ref, :process, _object, _pid}, 15_000)
37
 
38
  transcription = Transcriptions.get_transcription!(transcription.id, true)
39
  assert 14 == Enum.count(transcription.chunks)