October 22, 2024
Chicago 12, Melborne City, USA
python

I was trying to use Azure translator based on the tutorial given by MS, but still when I hit translate button I get Internal Server error


from flask import Flask, redirect, url_for, request, render_template, session
import requests, os, uuid, json
from dotenv import load_dotenv
load_dotenv()

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, verify=False
    ) 
'

”And the error I get in the command prompt is:
requests.exceptions.SSLError: HTTPSConnectionPool(host=”api.cognitive.microsofttranslator.com”, port=443): Max retries exceeded with url: /translate?api-version=3.0&to=en (Caused by SSLError(SSLCertVerificationError(1, ‘[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1002)’)))

I tried to upgrade certifi still the issue is same. When I try to open the endpoint https://api.cognitive.microsofttranslator.com/ in a browser, I am not seeing a secure page sign in the browser.
Not sure what is wrong, it is a example given by MS but it doesn’t work.
Any help is appreciated.”’



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video