seamless_communication icon indicating copy to clipboard operation
seamless_communication copied to clipboard

How to save text translation to txt file?

Open SoftologyPro opened this issue 11 months ago • 2 comments

What do I need to do line 3 here to save to a txt file?

output_tokens = model.generate(**text_inputs, tgt_lang=args2.output_language, generate_speech=False)
translated_text_from_text = processor.decode(output_tokens[0].tolist()[0], skip_special_tokens=True)
#how do I save the text translation to a txt file here?

Thanks.

SoftologyPro avatar Mar 04 '24 22:03 SoftologyPro

def writeStringToFile(text, file_path):
    """
    Writes a string to a text file.

    Args:
        text (str): The string to be written to the file.
        file_path (str): The path to the output text file.
    """
    try:
        with open(file_path, 'w', encoding='utf-8') as file:
            file.write(text)
        print(f"Text written to '{file_path}'.")
    except Exception as e:
        print(f"Error: {e}")

output_file_path = '/content/story.txt'
writeStringToFile(translated_text_from_text, output_file_path)

RageshAntonyHM avatar Mar 07 '24 17:03 RageshAntonyHM

Thank you. Works fine.

SoftologyPro avatar Mar 08 '24 08:03 SoftologyPro