-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
58 lines (46 loc) · 1.81 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import requests, os, uuid, json
from dotenv import load_dotenv
load_dotenv()
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route("/", methods=["GET"])
def index():
return render_template("index.html")
@app.route("/", methods=["POST"])
def index_post():
# Read the values from the form
original_text = request.form["text"]
target_language = request.form["language"]
# Load the values from .env
key = os.environ["KEY"]
endpoint = os.environ["ENDPOINT"]
location = os.environ["LOCATION"]
# Indicate that we want to translate and the API version (3.0) and the target language
path = "/translate?api-version=3.0"
# Add the target language parameter
target_language_parameter = "&to=" + target_language
# Create the full URL
constructed_url = endpoint + path + target_language_parameter
# Set up the header information, which includes our subscription key
headers = {
"Ocp-Apim-Subscription-Key": key,
"Ocp-Apim-Subscription-Region": location,
"Content-type": "application/json",
"X-ClientTraceId": str(uuid.uuid4()),
}
# Create the body of the request with the text to be translated
body = [{"text": original_text}]
# Make the call using post
translator_request = requests.post(constructed_url, headers=headers, json=body)
# Retrieve the JSON response
translator_response = translator_request.json()
# Retrieve the translation
translated_text = translator_response[0]["translations"][0]["text"]
# Call render template, passing the translated text,
# original text, and target language to the template
return render_template(
"results.html",
translated_text=translated_text,
original_text=original_text,
target_language=target_language,
)