in genai-for-marketing/backend_apis/app/main.py [0:0]
def post_summarize_news(data: NewsSummaryRequest,request: Request
) -> NewsSummaryResponse:
"""Summarize news related to keyword(s)
Parameters:
keywords: list[str]
max_records: int
max_days: int = 10
Returns:
summaries: list[dict[str, str]]
"""
# Step 1 - Retrieve documents with keywords from GDELT
# We look at the last 5 days to retrieve News Articles
end_date = datetime.now()
start_date = end_date - timedelta(data.max_days)
try:
documents = trendspotting.get_relevant_documents(
data.keywords,
start_date.strftime('%Y%m%d%H%M%S'),
end_date.strftime('%Y%m%d%H%M%S'),
max_records=data.max_records)
except:
raise HTTPException(
status_code=400,
detail="No articles found. Try different keywords.")
try:
summaries = []
for doc in documents:
summary = trendspotting.summarize_news_article(
doc["page_content"],
gemini_llm)
summaries.append({
"original_headline": doc["title"],
"summary":summary,
"url": doc["url"]
})
except Exception as e:
raise HTTPException(
status_code=400,
detail=f"Something went wrong. Could not summarize news articles. {str(e)}")
return NewsSummaryResponse(
summaries=summaries
)