def summarize_news_article()

in genai-for-marketing/backend_apis/app/utils_trendspotting.py [0:0]


def summarize_news_article(
        page_content: dict,
        llm: GenerativeModel):
    """Summarizes a news article.

    Args:
        document: 
            A dictionary containing the following keys:
                `page_content`: The text of the news article.
        llm: A language model that can be used to generate summaries.

    Returns:
        A dictionary containing the following keys:
            `page_content`: The original text of the news article.
            `summary`: A one-sentence summary of the news article.
    """
    prompt_template = (
        "Write a one sentence summary of the news article below:"
        f"input: {page_content}"
        "output:")

    try:
        summary = llm.generate_content(
            contents=prompt_template,
            generation_config={
                "max_output_tokens": 2048,
                "temperature": 0.8,
                "top_p": 0.8,
            },
            safety_settings = {
                generative_models.HarmCategory.HARM_CATEGORY_HATE_SPEECH: generative_models.HarmBlockThreshold.BLOCK_ONLY_HIGH,
                generative_models.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: generative_models.HarmBlockThreshold.BLOCK_ONLY_HIGH,
                generative_models.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: generative_models.HarmBlockThreshold.BLOCK_ONLY_HIGH,
                generative_models.HarmCategory.HARM_CATEGORY_HARASSMENT: generative_models.HarmBlockThreshold.BLOCK_ONLY_HIGH,
            },
            stream=False,
        )
    except Exception as e:
        print(e)
        return ""

    if isinstance(summary.text, str):
        return summary.text
    else:
        return ""