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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
import json
import textwrap
from typing import Dict, Any, List, Tuple, Optional

import gradio as gr
import requests
import matplotlib.pyplot as plt
from matplotlib.figure import Figure


# ============================================================
#  LLM CALLER (OPENAI-COMPATIBLE, GPT-4.1 BY DEFAULT)
# ============================================================

def call_chat_completion(
    api_key: str,
    base_url: str,
    model: str,
    system_prompt: str,
    user_prompt: str,
    max_completion_tokens: int = 1800,
) -> str:
    """
    OpenAI-compatible /v1/chat/completions helper.

    - Uses new-style `max_completion_tokens` (for GPT-4.1, GPT-4o, etc.)
    - Falls back to legacy `max_tokens` if needed.
    - Does NOT send temperature/top_p so it's safe with strict models.
    """
    if not api_key:
        raise ValueError("LLM API key is required.")

    if not base_url:
        base_url = "https://api.openai.com"

    url = base_url.rstrip("/") + "/v1/chat/completions"

    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json",
    }

    payload = {
        "model": model,
        "messages": [
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": user_prompt},
        ],
        "max_completion_tokens": max_completion_tokens,
    }

    resp = requests.post(url, headers=headers, json=payload, timeout=60)

    # Fallback for providers that still expect `max_tokens`
    if resp.status_code == 400 and "max_completion_tokens" in resp.text:
        payload.pop("max_completion_tokens", None)
        payload["max_tokens"] = max_completion_tokens
        resp = requests.post(url, headers=headers, json=payload, timeout=60)

    if resp.status_code != 200:
        raise RuntimeError(
            f"LLM API error {resp.status_code}: {resp.text[:500]}"
        )

    data = resp.json()
    try:
        return data["choices"][0]["message"]["content"]
    except Exception as e:
        raise RuntimeError(
            f"Unexpected LLM response format: {e}\n\n{json.dumps(data, indent=2)}"
        )


# ============================================================
#  FIRECRAWL SCRAPER (OPTIONAL)
# ============================================================

def call_firecrawl_scrape(
    firecrawl_key: str,
    url: str,
    formats: Optional[List[str]] = None,
) -> str:
    """
    Calls Firecrawl's /v0/scrape endpoint to get cleaned markdown/HTML
    for a single URL.

    Docs: https://docs.firecrawl.dev/api-reference/endpoint/scrape
    """
    if not firecrawl_key:
        raise ValueError("Firecrawl API key is missing.")

    if not url:
        raise ValueError("URL is required to use Firecrawl.")

    api_url = "https://api.firecrawl.dev/v0/scrape"
    headers = {
        "Authorization": f"Bearer {firecrawl_key}",
        "Content-Type": "application/json",
    }

    payload: Dict[str, Any] = {"url": url}
    if formats:
        payload["formats"] = formats

    resp = requests.post(api_url, headers=headers, json=payload, timeout=60)

    if resp.status_code != 200:
        raise RuntimeError(
            f"Firecrawl error {resp.status_code}: {resp.text[:400]}"
        )

    data = resp.json()
    # Default: try markdown first, fall back to raw HTML or text if structure differs
    # Common shape: { "data": { "markdown": "..." } }
    if isinstance(data, dict):
        # Nested under "data"
        inner = data.get("data", {})
        if isinstance(inner, dict):
            if "markdown" in inner and isinstance(inner["markdown"], str):
                return inner["markdown"]
            if "html" in inner and isinstance(inner["html"], str):
                return inner["html"]
        # If the service changes shape, last fallback: stringify
    return json.dumps(data)


# ============================================================
#  ANALYSIS PROMPT + PARSING
# ============================================================

ANALYSIS_SYSTEM_PROMPT = """
You are an expert strategy analyst.

Given some web content (or pasted text) plus a short user description,
you will produce a concise, executive-ready analysis in JSON.

Return ONLY JSON using this schema:

{
  "executive_summary": "string",
  "key_points": ["string", ...],
  "opportunities": ["string", ...],
  "risks": ["string", ...],
  "recommended_actions": [
    {
      "title": "string",
      "area": "string",
      "description": "string"
    }
  ]
}
"""

def build_analysis_user_prompt(
    url: str,
    content_preview: str,
    user_notes: str,
    focus: str,
) -> str:
    truncated = content_preview[:6000]  # keep context reasonable
    return f"""
Source URL: {url or "N/A"}

Focus area: {focus}

User notes / context:
{user_notes or "N/A"}

Scraped or pasted content (truncated if long):
\"\"\"{truncated}\"\"\"
""".strip()


def parse_analysis_json(raw_text: str) -> Dict[str, Any]:
    """Strip fences and extract JSON payload."""
    txt = raw_text.strip()

    if txt.startswith("```"):
        parts = txt.split("```")
        txt = next((p for p in parts if "{" in p and "}" in p), parts[-1])

    first = txt.find("{")
    last = txt.rfind("}")
    if first == -1 or last == -1:
        raise ValueError("No JSON detected in model output.")

    return json.loads(txt[first:last + 1])


def analysis_to_markdown(analysis: Dict[str, Any]) -> str:
    """Render the JSON analysis as a short executive brief in Markdown."""

    def bullet(items: List[str]) -> str:
        if not items:
            return "_None identified._"
        return "\n".join(f"- {i}" for i in items)

    md: List[str] = []

    md.append("## Executive Summary")
    md.append(analysis.get("executive_summary", "N/A"))

    md.append("\n## Key Points")
    md.append(bullet(analysis.get("key_points", [])))

    md.append("\n## Opportunities")
    md.append(bullet(analysis.get("opportunities", [])))

    md.append("\n## Risks")
    md.append(bullet(analysis.get("risks", [])))

    md.append("\n## Recommended Actions")
    actions = analysis.get("recommended_actions", [])
    if not actions:
        md.append("_None suggested yet β€” refine your prompt or focus._")
    else:
        for idx, act in enumerate(actions, start=1):
            title = act.get("title", f"Action {idx}")
            area = act.get("area", "General")
            desc = act.get("description", "")
            md.append(f"### {idx}. {title}")
            md.append(f"**Area:** {area}")
            md.append(desc or "_No description provided._")

    return "\n\n".join(md)


# ============================================================
#  SIMPLE DATA VISUAL β€” COUNTS BY CATEGORY
# ============================================================

def analysis_to_figure(analysis: Dict[str, Any]) -> Figure:
    """
    Basic bar chart: how many items per category (points, opportunities, risks, actions).
    Visualizes "density" of insights.
    """
    labels = ["Key Points", "Opportunities", "Risks", "Actions"]
    values = [
        len(analysis.get("key_points", []) or []),
        len(analysis.get("opportunities", []) or []),
        len(analysis.get("risks", []) or []),
        len(analysis.get("recommended_actions", []) or []),
    ]

    fig, ax = plt.subplots(figsize=(5, 3))
    ax.bar(labels, values)
    ax.set_ylabel("Count")
    ax.set_title("Insight Density by Category")
    fig.tight_layout()
    return fig


# ============================================================
#  SAMPLE PRESETS
# ============================================================

SAMPLE_CONFIGS: Dict[str, Dict[str, str]] = {
    "AI / Tech Policy Article": {
        "url": "https://www.whitehouse.gov/briefing-room/",
        "notes": "Focus on AI policy, workforce impact, and org-readiness.",
        "focus": "Policy / Regulation",
    },
    "Competitor Product Page": {
        "url": "https://example.com/",
        "notes": "Assume this is a competitor's SaaS pricing page.",
        "focus": "Product / Market",
    },
    "Industry Research Report": {
        "url": "https://example.org/report",
        "notes": "Treat as a long-form industry trend report.",
        "focus": "Industry / Strategy",
    },
}

def load_sample(name: str) -> Tuple[str, str, str]:
    if not name or name not in SAMPLE_CONFIGS:
        return "", "", "General insight synthesis"
    cfg = SAMPLE_CONFIGS[name]
    return cfg["url"], cfg["notes"], cfg["focus"]


# ============================================================
#  MAIN HANDLER FOR GRADIO
# ============================================================

def generate_brief_ui(
    llm_key_state: str,
    llm_key_input: str,
    base_url: str,
    model_name: str,
    firecrawl_key: str,
    url: str,
    pasted_text: str,
    user_notes: str,
    focus: str,
):
    """
    Master UI handler:
    - decides whether to call Firecrawl (if key + URL)
    - merges scraped content with pasted text
    - calls LLM and renders outputs
    """
    llm_key = llm_key_input or llm_key_state
    if not llm_key:
        return (
            "⚠️ Please enter your LLM API key in the left panel.",
            "",
            analysis_to_figure({"key_points": [], "opportunities": [], "risks": [], "recommended_actions": []}),
            llm_key_state,
        )

    if not url and not pasted_text:
        return (
            "⚠️ Provide at least a URL or some pasted text.",
            "",
            analysis_to_figure({"key_points": [], "opportunities": [], "risks": [], "recommended_actions": []}),
            llm_key_state,
        )

    # 1. Scrape via Firecrawl if URL + key are set
    scraped_content = ""
    if url and firecrawl_key:
        try:
            scraped_content = call_firecrawl_scrape(firecrawl_key, url, formats=["markdown"])
        except Exception as e:
            scraped_content = f"(Firecrawl error: {e})"

    # 2. Compose content preview (scraped + pasted)
    content_preview_parts = []
    if scraped_content:
        content_preview_parts.append(scraped_content)
    if pasted_text:
        content_preview_parts.append("\n\nUser-pasted text:\n" + pasted_text)

    content_preview = "\n\n".join(content_preview_parts)

    # 3. Build prompt and call LLM
    user_prompt = build_analysis_user_prompt(url, content_preview, user_notes, focus)
    model = model_name or "gpt-4.1"

    try:
        raw = call_chat_completion(
            api_key=llm_key,
            base_url=base_url,
            model=model,
            system_prompt=ANALYSIS_SYSTEM_PROMPT,
            user_prompt=user_prompt,
            max_completion_tokens=1800,
        )

        analysis = parse_analysis_json(raw)
        md = analysis_to_markdown(analysis)
        fig = analysis_to_figure(analysis)
        json_out = json.dumps(analysis, indent=2, ensure_ascii=False)

        return md, json_out, fig, llm_key

    except Exception as e:
        empty_fig = analysis_to_figure({"key_points": [], "opportunities": [], "risks": [], "recommended_actions": []})
        return f"❌ Error generating brief:\n\n{e}", "", empty_fig, llm_key_state


# ============================================================
#  GRADIO UI
# ============================================================

with gr.Blocks(title="ZEN Web Insight Brief Builder") as demo:
    gr.Markdown(
        """
# 🌐 ZEN Web Insight Brief Builder

Turn any URL (plus optional Firecrawl scrape) into a structured,
actionable executive brief:

1. **Configure API keys** (LLM + optional Firecrawl)  
2. **Paste a URL and/or text**  
3. **Get an executive summary, risks, opportunities, and actions**
"""
    )

    llm_key_state = gr.State("")

    with gr.Row():
        # LEFT: API + samples
        with gr.Column(scale=1):
            gr.Markdown("### 1 β€” API & Model Settings")

            llm_key_input = gr.Textbox(
                label="LLM API Key",
                placeholder="OpenAI or compatible key",
                type="password",
            )

            base_url = gr.Textbox(
                label="LLM Base URL",
                value="https://api.openai.com",
                placeholder="e.g. https://api.openai.com",
            )

            model_name = gr.Textbox(
                label="Model Name",
                value="gpt-4.1",
                placeholder="e.g. gpt-4.1, gpt-4o, etc.",
            )

            gr.Markdown("#### Optional β€” Firecrawl (URL Scraper)")
            firecrawl_key = gr.Textbox(
                label="Firecrawl API Key (optional)",
                placeholder="Only needed if you want automatic URL scraping",
                type="password",
            )

            gr.Markdown("#### Sample Config")
            sample_dropdown = gr.Dropdown(
                label="Load a sample scenario",
                choices=list(SAMPLE_CONFIGS.keys()),
                value=None,
            )
            load_sample_btn = gr.Button("Load Sample")

        # RIGHT: content + config
        with gr.Column(scale=2):
            gr.Markdown("### 2 β€” Content & Focus")

            url_input = gr.Textbox(
                label="Source URL",
                placeholder="Paste a URL to analyze (works best with Firecrawl key, but optional)",
            )

            pasted_text = gr.Textbox(
                label="Or paste content manually",
                placeholder="Paste article text, notes, or report sections here.",
                lines=8,
            )

            user_notes = gr.Textbox(
                label="Your context / what you care about",
                placeholder="Example: Focus on youth workforce impacts and funding opportunities.",
                lines=3,
            )

            focus = gr.Dropdown(
                label="Focus lens",
                choices=[
                    "Policy / Regulation",
                    "Product / Market",
                    "Industry / Strategy",
                    "Risk & Compliance",
                    "Custom / Other",
                ],
                value="Industry / Strategy",
            )

            generate_btn = gr.Button("πŸš€ Generate Insight Brief", variant="primary")

    gr.Markdown("### 3 β€” Executive Brief")

    with gr.Row():
        with gr.Column(scale=3):
            brief_md = gr.Markdown(
                label="Brief",
                value="Your executive brief will appear here after generation.",
            )
        with gr.Column(scale=2):
            brief_json = gr.Code(
                label="Raw JSON (for automation / export)",
                language="json",
            )

    gr.Markdown("### 4 β€” Insight Density Visual")
    brief_fig = gr.Plot(label="Insight Density by Category")

    # Wiring
    load_sample_btn.click(
        load_sample,
        inputs=[sample_dropdown],
        outputs=[url_input, user_notes, focus],
    )

    generate_btn.click(
        generate_brief_ui,
        inputs=[
            llm_key_state,
            llm_key_input,
            base_url,
            model_name,
            firecrawl_key,
            url_input,
            pasted_text,
            user_notes,
            focus,
        ],
        outputs=[brief_md, brief_json, brief_fig, llm_key_state],
    )

if __name__ == "__main__":
    demo.launch()