|
|
|
|
|
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline |
|
from device_config import get_device |
|
import torch |
|
import json |
|
|
|
device = get_device() |
|
|
|
|
|
MODEL_ID = "microsoft/Phi-4-mini-instruct" |
|
|
|
|
|
model = AutoModelForCausalLM.from_pretrained( |
|
MODEL_ID, |
|
device_map="auto", |
|
|
|
torch_dtype=torch.float16 |
|
).to(device) |
|
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) |
|
summarizer = pipeline("text-generation", model=model, tokenizer=tokenizer) |
|
|
|
def summarize_flowchart(flowchart_json): |
|
""" |
|
Generates a human-friendly explanation from flowchart JSON. |
|
|
|
Args: |
|
flowchart_json (dict): Contains "start" node and a list of "steps". |
|
|
|
Returns: |
|
str: Bullet-style explanation with proper nesting and flow. |
|
""" |
|
|
|
prompt = ( |
|
"You are an expert in visual reasoning and instruction generation.\n" |
|
"Convert the following flowchart JSON into a clear, step-by-step summary using bullets.\n" |
|
"- Each bullet represents a process step.\n" |
|
"- Use indented sub-bullets to explain decision branches (Yes/No).\n" |
|
"- Maintain order based on dependencies and parent-child links.\n" |
|
"- Avoid repeating the same step more than once.\n" |
|
"- Do not include JSON in the output, only human-readable text.\n" |
|
"\nFlowchart:\n{flowchart}\n\nBullet Explanation:" |
|
).format(flowchart=json.dumps(flowchart_json, indent=2)) |
|
|
|
result = summarizer(prompt, max_new_tokens=400, do_sample=False)[0]["generated_text"] |
|
|
|
if "Bullet Explanation:" in result: |
|
explanation = result.split("Bullet Explanation:")[-1].strip() |
|
else: |
|
explanation = result.strip() |
|
return explanation |
|
|