Last active
March 30, 2024 14:37
-
-
Save shehbajdhillon/ef4791db290fcf716d0aeefc8287bebc to your computer and use it in GitHub Desktop.
Translate transcribed text using GPT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Previous Code (Unchanged) | |
| def translate_segment(input_text, input_language): | |
| systemMessage = ''' | |
| You are an expert translator that can translate texts to and from different languages. | |
| You will translate the input text and will only output the translation | |
| ''' | |
| userMessage = f''' | |
| Translate the following text to colloquial {input_language}: {input_text} | |
| ''' | |
| completion = openai.ChatCompletion.create( | |
| model="gpt-4", | |
| messages=[ | |
| { "role": "system", "content": systemMessage }, | |
| { "role": "user", "content": userMessage } | |
| ] | |
| ) | |
| return completion.choices[0].message.content | |
| def main(): | |
| file_path, input_language = sys.argv[1:] | |
| input_language = str(input_language).lower() | |
| transcript = transcribe_audio(file_path=file_path) | |
| segments = transcript.segments | |
| for segment in segments: | |
| print("Original Text:", segment.text) | |
| print("Translated Text:", translate_segment(segment.text, input_language)) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment