This file contains 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
import openai | |
import re | |
# Define GPT-3.5-turbo API key | |
openai_api_key = 'YOUR-OPENAI-API-KEY' | |
openai.api_key = openai_api_key | |
# Read the Bengali text from the file | |
with open('/home/ehz/Downloads/nlu.txt', 'r', encoding='utf-8') as file: | |
bengali_text = file.read() | |
# Split the text into sentences using regex (modify as needed) | |
bengali_sentences = re.split(r'(?<=[।?!])\s+', bengali_text.strip()) | |
# Translate the sentences | |
translated_sentences = [] | |
for sentence in bengali_sentences: | |
response = openai.Completion.create( | |
engine="text-davinci-003", | |
prompt=f"Translate the following Bengali sentence to English: {sentence}", | |
max_tokens=60 | |
) | |
translated_sentences.append(response.choices[0].text.strip()) | |
# Write the translated sentences to a new file, each on a new line | |
with open('/home/ehz/Downloads/translated_text.txt', 'w', encoding='utf-8') as file: | |
file.write('\n'.join(translated_sentences)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment