LPXian/Graph-News.AI
0
1from fastapi import APIRouter, HTTPException2from app.graph.builder import app_graph3from app.services.database import db_service4from typing import List, Dict5 6router = APIRouter()7 8@router.api_route("/generate-news", methods=["GET", "POST"])9async def generate_news():10 """11 Triggers the LangGraph pipeline to fetch, summarize, draft, and save news.12 """13 try:14 # Initial state15 initial_state = {16 "raw_articles": [],17 "clean_articles": [],18 "summarized_items": [],19 "final_draft": "",20 "critic_feedback": "None",21 "iteration_count": 0,22 "is_approved": False,23 "final_payload": {},24 "total_tokens": 0,25 "metadata": {}26 }27 28 # Run the graph29 result = app_graph.invoke(initial_state)30 31 return {32 "status": "success",33 "data": result.get("final_payload")34 }35 except Exception as e:36 raise HTTPException(status_code=500, detail=str(e))37 38@router.get("/news")39async def get_news(limit: int = 10):40 """41 Fetches the latest news articles from the database.42 """43 try:44 articles = db_service.get_latest_news(limit=limit)45 return articles46 except Exception as e:47 raise HTTPException(status_code=500, detail=str(e))48 