Microsoft Learn(Python と Flask を使用して AI Web アプリをビルドする)でのKey Error

実現したいこと

Microsoft Learn(Python と Flask を使用して AI Web アプリをビルドする)での最後のページでの
「Translator サービスを呼び出す」でページをテストをしたのですがエラーとなってしまうので解決したいです。
↓Microsoft Learnページ
https://learn.microsoft.com/ja-jp/training/modules/python-flask-build-ai-web-app/6-exercise-call-translator

発生している問題・分からないこと

”http://127.0.0.1:5000” で適当な日本語を入れて翻訳ボタンを押すと、
”Internal Server Error"と表示されてしまいます。
Visual Studio Codeでのターミナルでエラー表示されてしますがエラーが解読出来ません。

エラーメッセージ

error

1(venv) (base) ****MacBook-Air contoso % flask run 2 * Debug mode: off 3WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. 4 * Running on http://127.0.0.1:5000 5Press CTRL+C to quit 6127.0.0.1 - - [16/Jun/2024 13:25:10] "GET / HTTP/1.1" 200 - 7[2024-06-16 13:25:27,996] ERROR in app: Exception on / [POST] 8Traceback (most recent call last): 9 File "/Users/****/contoso/venv/lib/python3.11/site-packages/flask/app.py", line 1473, in wsgi_app 10 response = self.full_dispatch_request() 11 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 12 File "/Users/****/contoso/venv/lib/python3.11/site-packages/flask/app.py", line 882, in full_dispatch_request 13 rv = self.handle_user_exception(e) 14 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 15 File "/Users/****/contoso/venv/lib/python3.11/site-packages/flask/app.py", line 880, in full_dispatch_request 16 rv = self.dispatch_request() 17 ^^^^^^^^^^^^^^^^^^^^^^^ 18 File "/Users/****/contoso/venv/lib/python3.11/site-packages/flask/app.py", line 865, in dispatch_request 19 return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return] 20 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 21 File "/Users/****/contoso/app.py", line 20, in index_post 22 key = os.environ['KEY'] 23 ~~~~~~~~~~^^^^^^^ 24 File "<frozen os>", line 679, in __getitem__ 25KeyError: 'KEY' 26127.0.0.1 - - [16/Jun/2024 13:25:28] "POST / HTTP/1.1" 500 - 27127.0.0.1 - - [16/Jun/2024 19:46:29] "GET / HTTP/1.1" 200 - 28[2024-06-16 19:46:37,358] ERROR in app: Exception on / [POST] 29Traceback (most recent call last): 30 File "/Users/****/contoso/venv/lib/python3.11/site-packages/flask/app.py", line 1473, in wsgi_app 31 response = self.full_dispatch_request() 32 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 33 File "/Users/****/contoso/venv/lib/python3.11/site-packages/flask/app.py", line 882, in full_dispatch_request 34 rv = self.handle_user_exception(e) 35 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 36 File "/Users/****/contoso/venv/lib/python3.11/site-packages/flask/app.py", line 880, in full_dispatch_request 37 rv = self.dispatch_request() 38 ^^^^^^^^^^^^^^^^^^^^^^^ 39 File "/Users/****/contoso/venv/lib/python3.11/site-packages/flask/app.py", line 865, in dispatch_request 40 return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return] 41 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 42 File "/Users/****/contoso/app.py", line 20, in index_post 43 key = os.environ['KEY'] 44 ~~~~~~~~~~^^^^^^^ 45 File "<frozen os>", line 679, in __getitem__ 46KeyError: 'KEY' 47127.0.0.1 - - [16/Jun/2024 19:46:37] "POST / HTTP/1.1" 500 -

該当のソースコード

app.py

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

試したこと・調べたこと

上記の詳細・結果

GoogleでKeyError: 'KEY'と検索してみたのですが解決方法を見つけることが出来ませんでした。
よろしくお願いします。

補足

特になし

コメントを投稿

0 コメント