File size: 1,295 Bytes
c2fb848
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# summarizer_module/__init__.py

from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
from device_config import get_device
import torch

device = get_device()

# Use a small local model (e.g., Phi-2)
MODEL_ID = "microsoft/phi-2"  # Ensure it's downloaded and cached locally

# Load model and tokenizer
model = AutoModelForCausalLM.from_pretrained(MODEL_ID).to(device)
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
summarizer = pipeline("text-generation", model=model, tokenizer=tokenizer)

def summarize_flowchart(flowchart_json):
    """
    Given a flowchart JSON with 'start' and 'steps', returns a plain English explanation
    formatted as bullets and sub-bullets.

    Args:
        flowchart_json (dict): Structured representation of flowchart

    Returns:
        str: Bullet-style natural language summary of the logic
    """
    prompt = (
        "Turn the following flowchart into a bullet-point explanation in plain English.\n"
        "Use bullets for steps and sub-bullets for branches.\n"
        "\n"
        f"Flowchart JSON:\n{flowchart_json}\n"
        "\nExplanation:"
    )

    result = summarizer(prompt, max_new_tokens=300, do_sample=False)[0]["generated_text"]
    explanation = result.split("Explanation:")[-1].strip()
    return explanation