Module 3: Creating AI Application and Deploy using Flask

Looking for ‘Developing AI Applications with Python and Flask Module 3 Answers’?

In this post, I provide complete, accurate, and detailed explanations for the answers to Module 3: Creating AI Application and Deploy using Flask of Course 7: Developing AI Applications with Python and FlaskIBM AI Developer Professional Certificate .

Whether you’re preparing for quizzes or brushing up on your knowledge, these insights will help you master the concepts effectively. Let’s dive into the correct answers and detailed explanations for each question!

Overview of Software Engineering

Practice Assignment

Task1: Clone the project repository

Command:

git clone https://github.com/yourusername/emotion-detector

Folder structure should look like:

emotion-detector/
├── emotion_detector.py
├── server.py
└── tests/
└── test_emotion_detector.py
 
Screenshot to save as: 1_folder_structure.png

Task 2: Create an emotion detection application using Watson NLP

 

2a. Code for application function

File: emotion_detector.py

import requests
import json
 
def emotion_predictor(text_to_analyze):
url = "https://watson-api-endpoint/emotion"
params = {"text": text_to_analyze}
response = requests.get(url, params=params)
if response.status_code != 200:
return None
result = response.json()

 

return {

“anger”: result[“emotion”][“anger”],

“disgust”: result[“emotion”][“disgust”],

“fear”: result[“emotion”][“fear”],

“joy”: result[“emotion”][“joy”],

“sadness”: result[“emotion”][“sadness”],

“dominant_emotion”: max(result[“emotion”],

key=result[“emotion”].get)}

Screenshot: Save as 2a_emotion_detection.png

 

2b. Import the application without errors

Command:

from emotion_detector import emotion_predictor

Screenshot: Save as 2b_application_creation.png

Task 3: Format the output of the application

 

3a. Return output in the specified format

Expected output format:

{
"anger": 0.02,
"disgust": 0.01,
"fear": 0.03,
"joy": 0.85,
"sadness": 0.09,
"dominant_emotion": "joy"
}
Screenshot of function output: 3a_output_formatting.png
 
 

3b. Terminal test

Test in terminal:

emotion_predictor("I am very happy today!")

 Screenshot: Save as 3b_formatted_output_test.png

Task 4: Package the application

 

4a. Folder + __init__.py

Structure:

emotion_package/
├──
__init__.py
└── emotion_detector.py

__init__.py:

from .emotion_detector import emotion_predictor

Screenshot: Save as 4a_packaging.png

 

4b. Validate output

from emotion_package import emotion_predictor
emotion_predictor("I'm feeling good today")

Screenshot: Save as 4b_packaging_test.png

Task 5: Unit Testing

 

5a. Create unit test

File: test_emotion_detector.py

import unittest
from emotion_package import emotion_predictor

class TestEmotionPredictor(unittest.TestCase):
def test_predict_joy(self):
result = emotion_predictor(“I am feeling wonderful today”)
self.assertEqual(result[“dominant_emotion”], “joy”)

Screenshot: Save as 5a_unit_testing.png

 

5b. Run test

python -m unittest discover

Screenshot: Save as 5b_unit_testing_result.png

Task 6: Web Deployment with Flask

 

6a. server.py

from flask import Flask, request, jsonify
from emotion_package import emotion_predictor

app = Flask(__name__)

@app.route(“/emotionDetector”, methods=[“GET”])
def get_emotion():
text = request.args.get(“textToAnalyze”)
if not text:
return “Error: No text provided”, 400

result = emotion_predictor(text)
return jsonify(result)

if __name__ == “__main__”:
app.run(debug=True)

Screenshot: Save as 6a_server.png

 

6b. Test in browser

URL: http://localhost:5000/emotionDetector?textToAnalyze=I%20am%20very%20happy

Screenshot: Save as 6b_deployment_test.png

Task 7: Error Handling

 

7a. Handle error in emotion_detector.py

if not text_to_analyze.strip():
return {"error": "Empty input"}, 400

Screenshot: Save as 7a_error_handling_function.png

 

7b. Handle error in server.py

if not text:
return jsonify({"error": "Text is required"}), 400

Screenshot: Save as 7b_error_handling_server.png

 

7c. Test empty input in browser

URL:

http://localhost:5000/emotionDetector?textToAnalyze=

Screenshot: Save as 7c_error_handling_interface.png

Task 8: Static Code Analysis

 

8a. Cleaned server.py

  • No unused imports
  • Docstrings added
  • Proper spacing

Screenshot: Save as 8a_server_modified.png

 

8b. Run pylint

pylint server.py

Screenshot showing 10.00/10: Save as 8b_static_code_analysis.png

 

Final Step: Submission

Once you’ve taken all screenshots, upload them to the respective sections and submit. Be sure to check the Honor Code box before submitting.

Leave a Reply