File size: 5,441 Bytes
6c0c37c
 
 
 
97fc69c
15a946c
97fc69c
 
6c0c37c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72c589b
6c0c37c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97fc69c
 
6c0c37c
97fc69c
6c0c37c
97fc69c
6c0c37c
 
 
 
 
 
 
 
 
 
97fc69c
6c0c37c
97fc69c
 
6c0c37c
97fc69c
 
 
6c0c37c
97fc69c
 
 
6c0c37c
 
97fc69c
bdefa77
97fc69c
bdefa77
b2bf7f6
e636262
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
import gradio as gr
import importlib
from PIL import Image
import json
import os

# === Load the GPT-4o module only
from models import gpt4o_pix2struct_ocr

# === Format Raw JSON Output
def format_result_json(output):
    if isinstance(output, dict):
        return json.dumps(output, indent=2)
    else:
        return str(output).strip()

# === Prettified Output View
def format_pretty_view(output):
    if not isinstance(output, dict):
        return "No structured JSON found.\n\n" + str(output)

    lines = []
    process = output.get("process", output)

    if "name" in process:
        lines.append(f"πŸ“¦ Process Name: {process['name']}\n")

    if "startEvent" in process:
        start = process["startEvent"]
        name = start.get("name", "")
        type_ = start.get("type", "")
        desc = start.get("description", "")
        line = f"▢️ Start: {name}"
        if type_:
            line += f" ({type_})"
        if desc:
            line += f" - {desc}"
        lines.append(line)

    if "endEvent" in process:
        end = process["endEvent"]
        name = end.get("name", "")
        type_ = end.get("type", "")
        desc = end.get("description", "")
        line = f"⏹ End: {name}"
        if type_:
            line += f" ({type_})"
        if desc:
            line += f" - {desc}"
        lines.append(line)

    if "tasks" in process:
        lines.append("\nπŸ”Ή Tasks:")
        for t in process["tasks"]:
            name = t.get("name", "")
            type_ = t.get("type", "")
            desc = t.get("description", "")
            line = f" - {name}"
            if type_:
                line += f" ({type_})"
            if desc:
                line += f" - {desc}"
            lines.append(line)

    if "events" in process:
        lines.append("\nπŸ“¨ Events:")
        for e in process["events"]:
            name = e.get("name", "")
            type_ = e.get("type", "")
            desc = e.get("description", "")
            line = f" - {name}"
            if type_:
                line += f" ({type_})"
            if desc:
                line += f" - {desc}"
            lines.append(line)

    if "gateways" in process:
        lines.append("\nπŸ”€ Gateways:")
        for g in process["gateways"]:
            name = g.get("name", "")
            type_ = g.get("type", "")
            label = g.get("label", "")
            desc = g.get("description", "")
            line = f" - {name}"
            if type_:
                line += f" ({type_})"
            if label:
                line += f" | Label: {label}"
            if desc:
                line += f" - {desc}"
            lines.append(line)

    if "sequenceFlows" in process:
        lines.append("\n➑️ Sequence Flows:")
        for f in process["sequenceFlows"]:
            src = f.get("sourceTask") or f.get("sourceEvent") or "Unknown"
            tgt = f.get("targetTask") or f.get("targetEvent") or "Unknown"
            condition = f.get("condition", "")
            line = f" - {src} β†’ {tgt}"
            if condition:
                line += f" [Condition: {condition}]"
            lines.append(line)

    if "connections" in process:
        lines.append("\nπŸ”— Connections:")
        for c in process["connections"]:
            src = c.get("sourceTask") or c.get("sourceEvent") or "Unknown"
            tgt = c.get("targetTask") or c.get("targetEvent") or "Unknown"
            condition = c.get("condition", "")
            line = f" - {src} β†’ {tgt}"
            if condition:
                line += f" [Condition: {condition}]"
            lines.append(line)

    if "relationships" in process:
        lines.append("\nπŸ”— Relationships:")
        for r in process["relationships"]:
            source = r.get("source")
            target = r.get("target")
            src = source.get("ref", "Unknown") if isinstance(source, dict) else str(source)
            tgt = target.get("ref", "Unknown") if isinstance(target, dict) else str(target)
            desc = r.get("description", "")
            line = f" - {src} β†’ {tgt}"
            if desc:
                line += f" | {desc}"
            lines.append(line)

    return "\n".join(lines).strip()

# === Inference Handler (GPT-4o only)
def process_image(image_file):
    image = Image.open(image_file.name).convert("RGB")
    result = gpt4o_pix2struct_ocr.run_model(image)
    parsed_json = result.get("json")
    raw_text = result.get("raw")

    if parsed_json:
        json_output = format_result_json(parsed_json)
        pretty_output = format_pretty_view(parsed_json)
    else:
        json_output = "(No valid JSON extracted)"
        pretty_output = "(No structured content extracted)\n\n⚠️ Raw Model Output:\n" + raw_text

    return image, json_output, pretty_output

# === Gradio Interface
iface = gr.Interface(
    fn=process_image,
    inputs=[gr.File(file_types=["image"], label="Upload a BPMN Diagram Image")],
    outputs=[
        gr.Image(label="πŸ“· Input Image"),
        gr.Textbox(label="🧠 Raw JSON Output", lines=20),
        gr.Textbox(label="πŸ“‹ Prettified View", lines=25)
    ],
    title="🧩 BPMN Extractor using GPT-4o + OCR",
    description="Upload a BPMN diagram image. Extracts structured JSON using GPT-4o and Pix2Struct OCR. Runs on CPU-only Space.",
    allow_flagging="never"
)

# === Launch without GPU
def main():
    iface.launch(ssr=False)

if __name__ == "__main__":
    main()