Skip to content

Instantly share code, notes, and snippets.

@ehzawad
Created August 23, 2023 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ehzawad/73248e3d77e3971df8dfc1219350f9e7 to your computer and use it in GitHub Desktop.
Save ehzawad/73248e3d77e3971df8dfc1219350f9e7 to your computer and use it in GitHub Desktop.
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