facebookresearch / seamless_communication

Foundational Models for State-of-the-Art Speech and Text Translation
Other
10.94k stars 1.06k forks source link

How to save text translation to txt file? #371

Open SoftologyPro opened 8 months ago

SoftologyPro commented 8 months ago

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.

RageshAntonyHM commented 8 months ago
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)
SoftologyPro commented 8 months ago

Thank you. Works fine.