Skip to content

Commit

Permalink
Add language to Assembly AI (FujiwaraChoki#155)
Browse files Browse the repository at this point in the history
* updated openai library

* Adding language code to Assembly AI

* mapping more languages

---------

Co-authored-by: FujiwaraChoki <78088687+FujiwaraChoki@users.noreply.github.com>
  • Loading branch information
phalkmin and FujiwaraChoki authored Feb 11, 2024
1 parent 7b7534e commit f2a71fd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Backend/gpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def generate_response(prompt: str, ai_model: str) -> str:

model_name = "gpt-3.5-turbo" if ai_model == "gpt3.5-turbo" else "gpt-4-1106-preview"

response = openai.ChatCompletion.create(
response = openai.chat.completions.create(

model=model_name,

Expand Down
6 changes: 5 additions & 1 deletion Backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,14 @@ def generate():
)

voice = data["voice"]
voice_prefix = voice[:2]


if not voice:
print(colored("[!] No voice was selected. Defaulting to \"en_us_001\"", "yellow"))
voice = "en_us_001"
voice_prefix = voice[:2]


# Generate a script
script = generate_script(data["videoSubject"], paragraph_number, ai_model, voice) # Pass the AI model to the script generation
Expand Down Expand Up @@ -206,7 +210,7 @@ def generate():
final_audio.write_audiofile(tts_path)

try:
subtitles_path = generate_subtitles(audio_path=tts_path, sentences=sentences, audio_clips=paths)
subtitles_path = generate_subtitles(audio_path=tts_path, sentences=sentences, audio_clips=paths, voice=voice_prefix)
except Exception as e:
print(colored(f"[-] Error generating subtitles: {e}", "red"))
subtitles_path = None
Expand Down
21 changes: 17 additions & 4 deletions Backend/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def save_video(video_url: str, directory: str = "../temp") -> str:
return video_path


def __generate_subtitles_assemblyai(audio_path: str) -> str:
def __generate_subtitles_assemblyai(audio_path: str, voice: str) -> str:
"""
Generates subtitles from a given audio file and returns the path to the subtitles.
Expand All @@ -48,8 +48,21 @@ def __generate_subtitles_assemblyai(audio_path: str) -> str:
str: The generated subtitles
"""

language_mapping = {
"br": "pt",
"id": "en", #AssemblyAI doesn't have Indonesian
"jp": "ja",
"kr": "ko",
}

if voice in language_mapping:
lang_code = language_mapping[voice]
else:
lang_code = voice

aai.settings.api_key = ASSEMBLY_AI_API_KEY
transcriber = aai.Transcriber()
config = aai.TranscriptionConfig(language_code=lang_code)
transcriber = aai.Transcriber(config=config)
transcript = transcriber.transcribe(audio_path)
subtitles = transcript.export_subtitles_srt()

Expand Down Expand Up @@ -89,7 +102,7 @@ def convert_to_srt_time_format(total_seconds):
return "\n".join(subtitles)


def generate_subtitles(audio_path: str, sentences: List[str], audio_clips: List[AudioFileClip]) -> str:
def generate_subtitles(audio_path: str, sentences: List[str], audio_clips: List[AudioFileClip], voice: str) -> str:
"""
Generates subtitles from a given audio file and returns the path to the subtitles.
Expand All @@ -111,7 +124,7 @@ def equalize_subtitles(srt_path: str, max_chars: int = 10) -> None:

if ASSEMBLY_AI_API_KEY is not None and ASSEMBLY_AI_API_KEY != "":
print(colored("[+] Creating subtitles using AssemblyAI", "blue"))
subtitles = __generate_subtitles_assemblyai(audio_path)
subtitles = __generate_subtitles_assemblyai(audio_path, voice)
else:
print(colored("[+] Creating subtitles locally", "blue"))
subtitles = __generate_subtitles_locally(sentences, audio_clips)
Expand Down

0 comments on commit f2a71fd

Please sign in to comment.