Skip to content

Commit cafc4be

Browse files
committed
test added for transcribe.py
1 parent 2d73441 commit cafc4be

5 files changed

Lines changed: 32 additions & 17 deletions

File tree

PythonRpcServer/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def TranscribeAudioRPC(self, request, context):
136136
logging.info(f"Starting transcription for file: {request.filePath}")
137137
transcription_result = LogWorker(
138138
f"TranscribeAudioRPC({request.filePath})",
139-
lambda: transcribe_audio(request.filePath)
139+
lambda: transcribe_audio(request.filePath, request.testing)
140140
)
141141
logging.info(f"Transcription completed successfully for: {request.filePath}")
142142
return ct_pb2.JsonString(json=json.dumps(transcription_result))

PythonRpcServer/transcribe.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,18 @@ def convert_video_to_wav(input_filepath, offset=None):
3838
print("Exception during conversion:" + str(e))
3939
raise e
4040

41-
def transcribe_audio(media_filepath):
41+
def transcribe_audio(media_filepath, testing=False):
42+
if testing:
43+
json_output_path = f"/PythonRpcServer/transcribe_hellohellohello.wav.json"
44+
with open(json_output_path, 'r') as json_file:
45+
transcription_result = json.load(json_file)
46+
47+
# Print the transcription result (testing purpose)
48+
print("Transcription result:")
49+
print(json.dumps(transcription_result, indent=4))
4250

51+
return transcription_result
52+
4353
if media_filepath == 'TEST-transcribe_example_result':
4454
result_json_file = 'transcribe_exampleffmp_result.json'
4555
with open(result_json_file, 'r') as json_file:
@@ -51,8 +61,10 @@ def transcribe_audio(media_filepath):
5161
raise FileNotFoundError(f"Media file not found: {media_filepath}")
5262

5363
# convert video to wav if needed
64+
wav_created = False # Track if WAV was created
5465
if not media_filepath.endswith('.wav'):
5566
media_filepath, _ = convert_video_to_wav(media_filepath)
67+
wav_created = True # WAV file was created
5668

5769

5870
# Path to the output JSON file that Whisper will generate
@@ -87,26 +99,27 @@ def transcribe_audio(media_filepath):
8799
transcription_result = json.load(json_file)
88100

89101
# Print the transcription result (testing purpose)
90-
# print("Transcription result:")
91-
# print(json.dumps(transcription_result, indent=4))
102+
print("Transcription result:")
103+
print(json.dumps(transcription_result, indent=4))
92104

93105
# Delete the JSON file after reading it
94106
os.remove(json_output_path)
95107
print(f"Deleted the JSON file: {json_output_path}")
96108

109+
if wav_created:
110+
try:
111+
os.remove(media_filepath)
112+
print(f"Deleted the WAV file: {media_filepath}")
113+
except Exception as e:
114+
print(f"Error deleting WAV file: {str(e)}")
115+
97116
return transcription_result
98117

99118
# Example usage
100119
if __name__ == '__main__':
101120
# Example media file path inside the container (the actual path will depend on where the file is located)
102-
import sys
103-
if len(sys.argv) > 1:
104-
audio_filepath = sys.argv[1]
105-
else:
106-
audio_filepath = 'sharedVolume/recording0.wav' # Update this path as needed
107-
108-
try:
109-
transcription_result = transcribe_audio(audio_filepath)
110-
print("Transcription Result:", json.dumps(transcription_result, indent=4))
111-
except Exception as e:
112-
print(f"Error: {str(e)}")
121+
json_output_path = f"/PythonRpcServer/transcribe_hellohellohello.wav.json"
122+
with open(json_output_path, 'r') as json_file:
123+
transcription_result = json.load(json_file)
124+
125+
print("Transcription Result:", json.dumps(transcription_result, indent=4))

TaskEngine/Tasks/LocalTranscriptionTask.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ protected async override Task OnConsume(string videoId, TaskParameters taskParam
9898
LogId = videoId,
9999
FilePath = video.Video1.VMPath,
100100
Model = "en",
101-
Language = "en"
101+
Language = "en",
102+
Testing = true
102103
// PhraseHints = phraseHints,
103104
// CourseHints = "",
104105
// OutputLanguages = "en"

TaskEngine/Tasks/QueueAwakerTask.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ protected async override Task OnConsume(JObject jObject, TaskParameters taskPara
404404
else if (type == TaskType.TranscribeVideo.ToString())
405405
{
406406
var id = jObject["videoOrMediaId"].ToString();
407-
407+
408408

409409
GetLogger().LogInformation($"{type}:{id}");
410410
var video = await _context.Videos.FindAsync(id);

ct.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ message TranscriptionRequest {
2929
string model = 2; // Whisper model to use (e.g., 'base-en', 'tiny-en')
3030
string language = 3; // Language in audio.
3131
string logId = 4;
32+
bool testing = 5;
3233
}
3334

3435

0 commit comments

Comments
 (0)