Skip to content

Instantly share code, notes, and snippets.

@mrtushartiwari
Created August 31, 2022 11:09
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 mrtushartiwari/4b3afee38edab1552843d4ceb3e88193 to your computer and use it in GitHub Desktop.
Save mrtushartiwari/4b3afee38edab1552843d4ceb3e88193 to your computer and use it in GitHub Desktop.
Libre_translate
import requests
import json
API_URL = "http://127.0.0.1:5000/translate"
# Openning and reading a file with sample English Sentences.
try:
file1 = open('sentence.txt', 'r')
lines = file1.readlines()
except:
print("No such file exists")
params = {
'q': "India wins the world cup after 28 years",
'source': "en",
'target': "hi",
'format': "text",
'api_key': "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
# Creates a new files and writes the hindi Translation line by line
file2 = open('hindi_translation.txt','w')
for l in lines:
print(l)
params['q'] = str(l)
r=requests.post(url = API_URL, data = params)
translated_text = r.json(encoding='utf-8')
file2.write(translated_text['translatedText'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment