Translate language (Json / Web Request)

also the use of OpenAI can be a solution (no library needed just an OpenAI account)

import sys
import clr
import System
from System.Text import *
from System.Net import *
clr.AddReference("System.Net.Http")
from System.Net.Http import *
import json

def translate_OpenAI(text="maison", source="french", target="english"):
	input = "Hello translate this {0} word '{2}' in {1}".format(source, target, text)
	client = HttpClient()
	client.DefaultRequestHeaders.Add("Authorization", r"Bearer YOUR_API_KEY")
	content = StringContent("{\"prompt\": \"" \
								+ input \
								+ "\", \"model\": \"text-davinci-003\", \"max_tokens\":100,\"temperature\":0.5}", 
								Encoding.UTF8, 
								"application/json")
	#
	response = client.PostAsync("https://api.openai.com/v1/completions", content).Result
	responseString = response.Content.ReadAsStringAsync().Result
	dict_result = json.loads(responseString)
	return dict_result["choices"][0]["text"].strip("\n")

OUT = translate_OpenAI(IN[0])
4 Likes