AI Level 3 Task 4 Analyze Press and Media with AI

I have ran the below code and getting correct output without error still I am getting error as script exited with non-zero status code.

import os
import json
from openai import OpenAI

API_KEY = os.environ.get(“OPENAI_API_KEY”)
API_BASE = os.environ.get(“OPENAI_API_BASE”)

client = OpenAI(api_key=API_KEY, base_url=API_BASE)

MODEL = “openai/gpt-4.1-mini”

def generate_press_release(brief_path: str, output_path: str) → str:
“”"
Reads a product brief, asks the model to write an exciting 150-word
press release based on it, and saves the result to output_path.
“”"
with open(brief_path, “r”, encoding=“utf-8”) as f:
brief_content = f.read()

response = client.chat.completions.create(
    model=MODEL,
    temperature=0.7,
    max_tokens=200,
    messages=[
        {
            "role": "system",
            "content": (
                "You are a PR specialist. Based on this product brief, "
                "write an exciting, 150-word professional press release."
            ),
        },
        {"role": "user", "content": brief_content},
    ],
)

press_release_text = response.choices[0].message.content.strip()

with open(output_path, "w", encoding="utf-8") as f:
    f.write(press_release_text)

return press_release_text

def analyze_press_release(pr_path: str) → str:
“”"
Reads the generated press release and asks the model, acting as a
skeptical journalist, to return the top 3 toughest questions as a
raw JSON object (no markdown fences, no extra text).
“”"
with open(pr_path, “r”, encoding=“utf-8”) as f:
press_release_content = f.read()

response = client.chat.completions.create(
    model=MODEL,
    temperature=0.5,
    max_tokens=200,
    messages=[
        {
            "role": "system",
            "content": (
                "You are a cynical, skeptical tech journalist. Read the "
                "following press release. Identify the 'Top 3 Toughest "
                "Questions' a journalist would ask about this product. "
                "Return a JSON object with a single key `tough_questions`, "
                "which is a list of strings. Return only the raw JSON "
                "object, with no additional text, explanations, or "
                "Markdown code fences."
            ),
        },
        {"role": "user", "content": press_release_content},
    ],
)

return response.choices[0].message.content.strip()

if name == “main”:
brief_file = “product_brief.txt”
press_release_file = “press_release.txt”

generate_press_release(brief_file, press_release_file)
final_json = analyze_press_release(press_release_file)

print(final_json)

I have ran the below code and getting correct output without error still I am getting error as script exited with non-zero status code.
please share solution on this.

import os
import json
from openai import OpenAI

API_KEY = os.environ.get(“OPENAI_API_KEY”)
API_BASE = os.environ.get(“OPENAI_API_BASE”)

client = OpenAI(api_key=API_KEY, base_url=API_BASE)

MODEL = “openai/gpt-4.1-mini”

def generate_press_release(brief_path: str, output_path: str) → str:
“”"
Reads a product brief, asks the model to write an exciting 150-word
press release based on it, and saves the result to output_path.
“”"
with open(brief_path, “r”, encoding=“utf-8”) as f:
brief_content = f.read()

response = client.chat.completions.create(
    model=MODEL,
    temperature=0.7,
    max_tokens=200,
    messages=[
        {
            "role": "system",
            "content": (
                "You are a PR specialist. Based on this product brief, "
                "write an exciting, 150-word professional press release."
            ),
        },
        {"role": "user", "content": brief_content},
    ],
)

press_release_text = response.choices[0].message.content.strip()

with open(output_path, "w", encoding="utf-8") as f:
    f.write(press_release_text)

return press_release_text

def analyze_press_release(pr_path: str) → str:
“”"
Reads the generated press release and asks the model, acting as a
skeptical journalist, to return the top 3 toughest questions as a
raw JSON object (no markdown fences, no extra text).
“”"
with open(pr_path, “r”, encoding=“utf-8”) as f:
press_release_content = f.read()

response = client.chat.completions.create(
    model=MODEL,
    temperature=0.5,
    max_tokens=200,
    messages=[
        {
            "role": "system",
            "content": (
                "You are a cynical, skeptical tech journalist. Read the "
                "following press release. Identify the 'Top 3 Toughest "
                "Questions' a journalist would ask about this product. "
                "Return a JSON object with a single key `tough_questions`, "
                "which is a list of strings. Return only the raw JSON "
                "object, with no additional text, explanations, or "
                "Markdown code fences."
            ),
        },
        {"role": "user", "content": press_release_content},
    ],
)

return response.choices[0].message.content.strip()

if name == “main”:
brief_file = “product_brief.txt”
press_release_file = “press_release.txt”

generate_press_release(brief_file, press_release_file)
final_json = analyze_press_release(press_release_file)

print(final_json)

One thing I notice is if name == “main”:

That needs to be if __name__ == "__main__":

There seems to be an issue with the validation in this lab.
I have reported the issue to them team, and will update you once it is resolved.

Thank you :slight_smile: And same thing happening for AI Level 3 Task 5

The labs are fixed now. Task 5 should also work fine.

Thanks for your support.

Hello @Santosh_KodeKloud, please is this under the 100 Days of MLOps Job prep or is it under a specific course? Thanks for your help in advance.

It’s its own thing. KodeKloud Engineer labs don’t necessarily connect with any specific course, and often you prepare to do them by doing a bit of internet research to find out about the relevant libraries used. And they’re specifically distinct from the 100 Days of MLOps, which covers a slightly different topic.

Hello sir, thanks for your swift response. Yeah, I know but this questions says - AI Level 3 Task 4 Analyze Press and Media with AI. That’s why I’m asking if this is for a specific course or Job Prep?

If those are your categories, I’d say “Job Prep” – it’s general knowledge that KKE is helping you build.