File size: 4,509 Bytes
0cf3992
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from core.state import AgenticState
import json
from loguru import logger


@logger.catch
async def node_5_beautiful_presentation_api(state: AgenticState) -> AgenticState:
    """
    Node 5 Prepare nice presentation with API
    """


    logger.info("🚀 Node 5: Beautiful Presentation started...")

    llm = state.llm 
    if not llm:
        state.errors.append("LLM not available in state")
        logger.error("LLM not available in state")
        return state

    structured = getattr(state, "structured_script", {})
    metadata = getattr(state, "video_metadata", {})

    if not structured:
        state.errors.append({"type": "missing_structure", "message": "No structured script"})
        logger.error("No structured script")
        return state

    video_id = (
        getattr(state, "video_id", None)
        or video_metadata.get("video_id")
        or "UNKNOWN"
    )

    title = (
        getattr(state, "title", None)
        or video_metadata.get("title")
        or (sections[0].get("title") if sections else None)
        or "YouTube Video Summary"
    )

    channel = (
        getattr(state, "channel", None)
        or video_metadata.get("channel")
        or "Unknown Channel"
    )

    duration_human = (
        getattr(state, "duration_human", None)
        or video_metadata.get("duration_human")
        or None
    )

    # Upload date
    upload_date = (
        getattr(state, "upload_date", None)
        or video_metadata.get("upload_date")
        or None
    )
    # Build summary prompt
    content_sample = "\n".join(
        sec.get("summary", "") for sec in structured.get("sections", [])
    )[:3000]

    prompt = f"""
You are a professional content summarizer.

Generate:

1. Executive summary (3-5 sentences)
2. TLDR (one powerful sentence)

Return JSON:

{{
 "executive_summary": "...",
 "tldr": "..."
}}

CONTENT:
{content_sample}
"""

    try:
        response = llm.invoke(prompt)

        json_str = response.content if hasattr(response, "content") else str(response)

        if "```json" in json_str:
            json_str = json_str.split("```json")[1].split("```")[0].strip()
        elif "```" in json_str:
            json_str = json_str.split("```")[1].split("```")[0].strip()

        summaries = json.loads(json_str)

        exec_summary = summaries.get("executive_summary", "")
        tldr = summaries.get("tldr", "")

    except Exception as e:
        state.errors.append({"type": "summary_failed", "message": str(e)})
        exec_summary = "Summary unavailable."
        tldr = "TLDR unavailable."
        logger.error("Summary unavailable")

    # Table of contents
    toc = "## Table of Contents\n"
    for i, sec in enumerate(structured.get("sections", []), 1):
        toc += f"{i}. {sec.get('title','Section')}\n"

    # Sections
    sections_md = []

    for i, sec in enumerate(structured.get("sections", []), 1):

        title_sec = sec.get("title", f"Section {i}")
        summary = sec.get("summary", "")
        content = sec.get("content", "")
        key_points = sec.get("key_points", [])
        start_time = sec.get("start_time")

        timestamp = f"⏱ {start_time}\n\n" if start_time else ""

        section_md = f"""

### {i}. {title_sec}

{timestamp}

**Summary**

{summary}

**Explanation**

{content}

**Key Insights**

{chr(10).join(f"- {p}" for p in key_points[:5])}


"""

        sections_md.append(section_md)

    # Topics
    topics_md = ""
    if state.main_topics:
        topics_md = "## Main Topics\n" + "\n".join(f"- {t}" for t in state.main_topics)

    # Quotes
    quotes_md = ""
    if state.key_quotes:
        quotes_md = "## Key Quotes\n\n"  
        for q in state.key_quotes:
            quotes_md += f'> "{q}"\n\n'  

    # Entities 
    entities_md = ""
    if state.mentioned_entities:
        entities_md = "## Key Mentions\n\n"  
        for e in state.mentioned_entities:
            entities_md += f"- {e}\n"  

    final_md = f"""
# {title}

*Channel: {channel}*  
*Video ID: {video_id}*  
*Duration: {duration_human}*  
*Updated: {upload_date}*

---

## Executive Summary

{exec_summary}

## TL;DR

{tldr}

---

{topics_md}

---

{toc}

---

{chr(10).join(sections_md)}

---

{quotes_md}

---

{entities_md}

---

*Generated with Groq API + YouTubeScriptMaster 2026*
"""

    state.final_formatted_markdown = final_md
    state.presentation_complete = True

    logger.info(" ✅ Node 5 complete | Markdown size: {len_final_md}", len_final_md=len(final_md))

    return state