File size: 18,955 Bytes
94bf1e0
d289335
94bf1e0
 
 
 
 
 
 
 
46324f5
 
fcc0582
 
55d59a6
fcc0582
f749fc6
46324f5
f749fc6
 
 
46324f5
f749fc6
 
46324f5
 
 
 
 
 
 
 
 
f749fc6
 
46324f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91a6397
 
46324f5
 
 
 
 
 
 
91a6397
 
 
46324f5
 
 
 
 
 
 
 
 
 
 
 
 
91a6397
 
46324f5
91a6397
46324f5
91a6397
46324f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f749fc6
91a6397
 
 
 
 
 
f749fc6
fcc0582
46324f5
 
 
 
 
 
 
fcc0582
 
 
 
 
 
d289335
fcc0582
94bf1e0
 
 
fcc0582
 
 
 
 
 
 
 
 
94bf1e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fcc0582
94bf1e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8867999
 
 
 
 
 
 
94bf1e0
 
 
 
 
 
 
 
 
 
 
 
46324f5
 
 
 
 
 
 
94bf1e0
 
 
 
fcc0582
94bf1e0
 
 
fcc0582
94bf1e0
 
 
fcc0582
94bf1e0
46324f5
 
94bf1e0
 
fcc0582
 
94bf1e0
fcc0582
 
94bf1e0
 
 
 
 
 
 
 
 
fcc0582
94bf1e0
fcc0582
 
 
 
 
 
 
 
 
 
 
 
 
 
94bf1e0
 
 
46324f5
fcc0582
94bf1e0
 
 
 
fcc0582
94bf1e0
fcc0582
 
46324f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fcc0582
94bf1e0
fcc0582
 
94bf1e0
 
fcc0582
94bf1e0
 
fcc0582
 
 
 
46324f5
 
 
 
 
 
 
 
 
 
 
 
 
 
fcc0582
 
 
 
 
 
 
 
 
 
 
 
 
 
35e452a
fcc0582
 
 
 
 
 
 
 
 
46324f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fcc0582
46324f5
 
 
f749fc6
46324f5
91a6397
46324f5
 
 
 
 
 
fcc0582
46324f5
fcc0582
46324f5
91a6397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46324f5
 
91a6397
 
 
 
 
 
 
 
 
 
 
 
 
46324f5
 
91a6397
46324f5
 
 
 
91a6397
fcc0582
46324f5
 
 
fcc0582
 
 
 
 
 
 
 
 
 
 
 
 
46324f5
 
 
 
 
 
 
 
 
 
 
 
 
 
fcc0582
91a6397
46324f5
 
 
fcc0582
 
94bf1e0
fcc0582
94bf1e0
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
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
import spaces
import gradio as gr
from phi3_instruct_graph import MODEL_LIST, Phi3InstructGraph
import rapidjson
from pyvis.network import Network
import networkx as nx
import spacy
from spacy import displacy
from spacy.tokens import Span
import random
import os
import pickle

# Constants
TITLE = "🌐 GraphMind: Phi-3 Instruct Graph Explorer"
SUBTITLE = "✨ Extract and visualize knowledge graphs from any text in multiple languages"

# Enhanced Custom CSS for styling with improved visuals
CUSTOM_CSS = """
.gradio-container {
    font-family: 'Inter', 'Segoe UI', Roboto, sans-serif;
    background: linear-gradient(to bottom, #f9fafb, #f3f4f6);
}
.gr-button-primary {
    background-color: #4f46e5 !important;
    border: none !important;
    color: white !important;
    border-radius: 8px !important;
}
.gr-button-primary:hover {
    background-color: #4338ca !important;
    transform: translateY(-1px);
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}
.gr-button-secondary {
    border-color: #4f46e5 !important;
    color: #4f46e5 !important;
    border-radius: 8px !important;
}
.gr-button-secondary:hover {
    background-color: #eef2ff !important;
    transform: translateY(-1px);
}
.gr-box, .gr-input, .gr-textarea, .gr-dropdown {
    border-radius: 8px !important;
    border: 1px solid #e5e7eb !important;
    box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05) !important;
}
.gr-padded {
    padding: 16px !important;
}
.gr-form {
    border: none !important;
    background: transparent !important;
}
.gr-input:focus, .gr-textarea:focus, .gr-dropdown:focus {
    border-color: #4f46e5 !important;
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.2) !important;
}
.gr-panel {
    border-radius: 12px !important;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06) !important;
    background-color: white !important;
}
.gr-heading {
    font-weight: 700 !important;
    color: #111827 !important;
}
.gr-examples-table {
    border-radius: 8px !important;
    overflow: hidden !important;
    box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06) !important;
}
.gr-prose p {
    margin-bottom: 0.75rem !important;
    color: #4b5563 !important;
}
.gr-prose h1, .gr-prose h2, .gr-prose h3 {
    font-weight: 700 !important;
    color: #111827 !important;
}
.gr-tab {
    border-radius: 8px 8px 0 0 !important;
}
.gr-tab-selected {
    border-color: #4f46e5 !important;
    color: #4f46e5 !important;
    font-weight: 600 !important;
}
.header-container {
    margin-bottom: 24px !important;
}
.sidebar-container {
    background-color: white !important;
    border-radius: 12px !important;
    padding: 16px !important;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06) !important;
}
.visualization-container {
    margin-top: 24px !important;
}
.app-title {
    background: linear-gradient(90deg, #4f46e5, #8b5cf6) !important;
    -webkit-background-clip: text !important;
    -webkit-text-fill-color: transparent !important;
    font-weight: 800 !important;
    font-size: 2.25rem !important;
    margin-bottom: 0.5rem !important;
}
.app-subtitle {
    color: #6b7280 !important;
    font-size: 1.25rem !important;
    margin-bottom: 2rem !important;
}
.examples-container {
    background-color: white !important;
    border-radius: 12px !important;
    padding: 16px !important;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06) !important;
    margin-bottom: 16px !important;
}
.results-container {
    background-color: #f8fafc !important;
    padding: 20px !important;
    border-radius: 12px !important;
    margin-top: 1rem !important;
    margin-bottom: 1rem !important;
    border: 1px solid #e2e8f0 !important;
}
.language-badge {
    display: inline-block !important;
    background-color: #4f46e5 !important;
    color: white !important;
    padding: 4px 12px !important;
    border-radius: 16px !important;
    font-weight: 600 !important;
    font-size: 0.875rem !important;
    margin-right: 8px !important;
}
.graph-container iframe {
    width: 100% !important;
    height: 700px !important;
    border-radius: 12px !important;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06) !important;
}
"""

# Cache directory and file paths
CACHE_DIR = "cache"
EXAMPLE_CACHE_FILE = os.path.join(CACHE_DIR, "first_example_cache.pkl")

# Create cache directory if it doesn't exist
os.makedirs(CACHE_DIR, exist_ok=True)

# Color utilities
def get_random_light_color():
    r = random.randint(140, 255)
    g = random.randint(140, 255)
    b = random.randint(140, 255)
    return f"#{r:02x}{g:02x}{b:02x}"

# Text preprocessing
def handle_text(text):
    return " ".join(text.split())

# Main processing functions
@spaces.GPU
def extract(text, model):
    try:
        model = Phi3InstructGraph(model=model)    
        result = model.extract(text)
        return rapidjson.loads(result)
    except Exception as e:
        raise gr.Error(f"Extraction error: {str(e)}")

def find_token_indices(doc, substring, text):
    result = []
    start_index = text.find(substring)
    
    while start_index != -1:
        end_index = start_index + len(substring)
        start_token = None
        end_token = None

        for token in doc:
            if token.idx == start_index:
                start_token = token.i
            if token.idx + len(token) == end_index:
                end_token = token.i + 1

        if start_token is not None and end_token is not None:
            result.append({
                "start": start_token,
                "end": end_token
            })
        
        # Search for next occurrence
        start_index = text.find(substring, end_index)

    return result

def create_custom_entity_viz(data, full_text):
    nlp = spacy.blank("xx")
    doc = nlp(full_text)

    spans = []
    colors = {}
    for node in data["nodes"]:
        entity_spans = find_token_indices(doc, node["id"], full_text)
        for dataentity in entity_spans:
            start = dataentity["start"]
            end = dataentity["end"]
            
            if start < len(doc) and end <= len(doc):
                # Check for overlapping spans
                overlapping = any(s.start < end and start < s.end for s in spans)
                if not overlapping:                
                    span = Span(doc, start, end, label=node["type"])
                    spans.append(span)
                    if node["type"] not in colors:
                        colors[node["type"]] = get_random_light_color()

    doc.set_ents(spans, default="unmodified")
    doc.spans["sc"] = spans

    options = {
        "colors": colors,
        "ents": list(colors.keys()),
        "style": "ent",
        "manual": True
    }

    html = displacy.render(doc, style="span", options=options)
    # Add custom styling to the entity visualization
    styled_html = f"""
    <div style="padding: 20px; border-radius: 12px; background-color: white; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);">
        {html}
    </div>
    """
    return styled_html

def create_graph(json_data):
    G = nx.Graph()

    # Add nodes with tooltips
    for node in json_data['nodes']:
        G.add_node(node['id'], title=f"{node['type']}: {node['detailed_type']}")

    # Add edges with labels
    for edge in json_data['edges']:
        G.add_edge(edge['from'], edge['to'], title=edge['label'], label=edge['label'])

    # Create network visualization
    nt = Network(
        width="100%",
        height="700px",
        directed=True,
        notebook=False,
        bgcolor="#f8fafc", 
        font_color="#1e293b"
    )
    
    # Configure network display
    nt.from_nx(G)
    nt.barnes_hut(
        gravity=-3000,
        central_gravity=0.3,
        spring_length=50,
        spring_strength=0.001,
        damping=0.09,
        overlap=0,
    )
    
    # Customize edge appearance
    for edge in nt.edges:
        edge['width'] = 2
        edge['arrows'] = {'to': {'enabled': True, 'type': 'arrow'}}
        edge['color'] = {'color': '#6366f1', 'highlight': '#4f46e5'}
        edge['font'] = {'size': 12, 'color': '#4b5563', 'face': 'Arial'}

    # Customize node appearance
    for node in nt.nodes:
        node['color'] = {'background': '#e0e7ff', 'border': '#6366f1', 'highlight': {'background': '#c7d2fe', 'border': '#4f46e5'}}
        node['font'] = {'size': 14, 'color': '#1e293b'}
        node['shape'] = 'dot'
        node['size'] = 25

    # Generate HTML with iframe to isolate styles
    html = nt.generate_html()
    html = html.replace("'", '"')

    return f"""<iframe style="width: 100%; height: 700px; margin: 0 auto; border-radius: 12px; box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -4px rgba(0, 0, 0, 0.1);" 
        name="result" allow="midi; geolocation; microphone; camera; display-capture; encrypted-media;" 
        sandbox="allow-modals allow-forms allow-scripts allow-same-origin allow-popups 
        allow-top-navigation-by-user-activation allow-downloads" allowfullscreen="" 
        allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>"""

def process_and_visualize(text, model, progress=gr.Progress()):
    if not text or not model:
        raise gr.Error("⚠️ Both text and model must be provided.")
    
    # Check if we're processing the first example for caching
    is_first_example = text == EXAMPLES[0][0]
    
    # Try to load from cache if it's the first example
    if is_first_example and os.path.exists(EXAMPLE_CACHE_FILE):
        try:
            progress(0.3, desc="Loading from cache...")
            with open(EXAMPLE_CACHE_FILE, 'rb') as f:
                cache_data = pickle.load(f)
                
            progress(1.0, desc="Loaded from cache!")
            return cache_data["graph_html"], cache_data["entities_viz"], cache_data["json_data"], cache_data["stats"]
        except Exception as e:
            print(f"Cache loading error: {str(e)}")
            # Continue with normal processing if cache fails
    
    progress(0, desc="Starting extraction...")
    json_data = extract(text, model)
    
    progress(0.5, desc="Creating entity visualization...")
    entities_viz = create_custom_entity_viz(json_data, text)
    
    progress(0.8, desc="Building knowledge graph...")
    graph_html = create_graph(json_data)
    
    node_count = len(json_data["nodes"])
    edge_count = len(json_data["edges"])
    stats = f"πŸ“Š Extracted {node_count} entities and {edge_count} relationships"
    
    # Save to cache if it's the first example
    if is_first_example:
        try:
            cache_data = {
                "graph_html": graph_html,
                "entities_viz": entities_viz,
                "json_data": json_data,
                "stats": stats
            }
            with open(EXAMPLE_CACHE_FILE, 'wb') as f:
                pickle.dump(cache_data, f)
        except Exception as e:
            print(f"Cache saving error: {str(e)}")
    
    progress(1.0, desc="Complete!")
    return graph_html, entities_viz, json_data, stats

# Example texts in different languages
EXAMPLES = [
    [handle_text("""Legendary rock band Aerosmith has officially announced their retirement from touring after 54 years, citing 
    lead singer Steven Tyler's unrecoverable vocal cord injury. 
    The decision comes after months of unsuccessful treatment for Tyler's fractured larynx, 
    which he suffered in September 2023.""")],
    
    [handle_text("""Pop star Justin Timberlake, 43, had his driver's license suspended by a New York judge during a virtual 
    court hearing on August 2, 2024. The suspension follows Timberlake's arrest for driving while intoxicated (DWI) 
    in Sag Harbor on June 18. Timberlake, who is currently on tour in Europe, 
    pleaded not guilty to the charges.""")],
    
    [handle_text("""세계적인 기술 κΈ°μ—… μ‚Όμ„±μ „μžλŠ” μƒˆλ‘œμš΄ 인곡지λŠ₯ 기반 μŠ€λ§ˆνŠΈν°μ„ μ˜¬ν•΄ ν•˜λ°˜κΈ°μ— μΆœμ‹œν•  μ˜ˆμ •μ΄λΌκ³  λ°œν‘œν–ˆλ‹€. 
    이 μŠ€λ§ˆνŠΈν°μ€ ν˜„μž¬ 개발 쀑인 κ°€λŸ­μ‹œ μ‹œλ¦¬μ¦ˆμ˜ μ΅œμ‹ μž‘μœΌλ‘œ, κ°•λ ₯ν•œ AI κΈ°λŠ₯κ³Ό ν˜μ‹ μ μΈ 카메라 μ‹œμŠ€ν…œμ„ νƒ‘μž¬ν•  κ²ƒμœΌλ‘œ μ•Œλ €μ‘Œλ‹€. 
    μ‚Όμ„±μ „μžμ˜ CEOλŠ” 이번 μ‹ μ œν’ˆμ΄ 슀마트폰 μ‹œμž₯에 μƒˆλ‘œμš΄ ν˜μ‹ μ„ κ°€μ Έμ˜¬ 것이라고 μ „λ§ν–ˆλ‹€.""")],
    
    [handle_text("""ν•œκ΅­ μ˜ν™” '기생좩'은 2020λ…„ 아카데미 μ‹œμƒμ‹μ—μ„œ μž‘ν’ˆμƒ, 감독상, 각본상, κ΅­μ œμ˜ν™”μƒ λ“± 4개 뢀문을 μˆ˜μƒν•˜λ©° 역사λ₯Ό μƒˆλ‘œ 썼닀. 
    λ΄‰μ€€ν˜Έ 감독이 μ—°μΆœν•œ 이 μ˜ν™”λŠ” ν•œκ΅­ μ˜ν™” 졜초둜 μΉΈ μ˜ν™”μ œ ν™©κΈˆμ’…λ €μƒλ„ μˆ˜μƒν–ˆμœΌλ©°, μ „ μ„Έκ³„μ μœΌλ‘œ μ—„μ²­λ‚œ ν₯ν–‰κ³Ό 
    ν‰λ‹¨μ˜ ν˜Έν‰μ„ λ°›μ•˜λ‹€.""")]
]

# Function to preprocess the first example when the app starts
def generate_first_example_cache():
    """Generate cache for the first example if it doesn't exist"""
    if not os.path.exists(EXAMPLE_CACHE_FILE):
        print("Generating cache for first example...")
        try:
            text = EXAMPLES[0][0]
            model = MODEL_LIST[0] if MODEL_LIST else None
            
            if model:
                # Extract data
                json_data = extract(text, model)
                entities_viz = create_custom_entity_viz(json_data, text)
                graph_html = create_graph(json_data)
                
                node_count = len(json_data["nodes"])
                edge_count = len(json_data["edges"])
                stats = f"πŸ“Š Extracted {node_count} entities and {edge_count} relationships"
                
                # Save to cache
                cache_data = {
                    "graph_html": graph_html,
                    "entities_viz": entities_viz,
                    "json_data": json_data,
                    "stats": stats
                }
                with open(EXAMPLE_CACHE_FILE, 'wb') as f:
                    pickle.dump(cache_data, f)
                
                print("First example cache generated successfully")
                return cache_data
        except Exception as e:
            print(f"Error generating first example cache: {str(e)}")
    else:
        print("First example cache already exists")
        try:
            with open(EXAMPLE_CACHE_FILE, 'rb') as f:
                return pickle.load(f)
        except Exception as e:
            print(f"Error loading existing cache: {str(e)}")
    
    return None

def create_ui():
    # Try to generate/load the first example cache
    first_example_cache = generate_first_example_cache()
    
    with gr.Blocks(css=CUSTOM_CSS, title=TITLE) as demo:
        # Header with enhanced styling
        with gr.Row():
            with gr.Column():
                gr.Markdown(f"<h1 class='app-title'>{TITLE}</h1>")
                gr.Markdown(f"<p class='app-subtitle'>{SUBTITLE}</p>")
                
                with gr.Row():
                    gr.Markdown("<span class='language-badge'>English</span><span class='language-badge'>Korean</span><span class='language-badge'>+ More</span>")
        
        # Main content area - redesigned layout
        with gr.Row():
            # Left panel - Input controls
            with gr.Column(scale=1):
                with gr.Box():
                    input_model = gr.Dropdown(
                        MODEL_LIST, 
                        label="πŸ€– Select Model",
                        info="Choose a model to process your text",
                        value=MODEL_LIST[0] if MODEL_LIST else None
                    )
                    
                    input_text = gr.TextArea(
                        label="πŸ“ Input Text", 
                        info="Enter text in any language to extract a knowledge graph",
                        placeholder="Enter text here...",
                        lines=8,
                        value=EXAMPLES[0][0]  # Pre-fill with first example
                    )
                    
                    with gr.Row():
                        submit_button = gr.Button("πŸš€ Extract & Visualize", variant="primary", scale=2)
                        clear_button = gr.Button("πŸ”„ Clear", variant="secondary", scale=1)
                    
                    # Statistics will appear here
                    stats_output = gr.Markdown("", label="πŸ” Analysis Results")
            
            # Right panel - Examples moved to right side
            with gr.Column(scale=1):
                with gr.Box():
                    gr.Markdown("<h3>πŸ“š Example Texts</h3>")
                    # Removed elem_classes parameter that was causing the error
                    gr.Examples(
                        examples=EXAMPLES,
                        inputs=input_text,
                        label=""
                    )
                    
                    # JSON output moved to right side as well
                    with gr.Accordion("πŸ“Š JSON Data", open=False):
                        output_json = gr.JSON(label="")
        
        # Full width visualization area at the bottom
        with gr.Row():
            with gr.Column():
                # Tab container for visualizations
                with gr.Tabs():
                    with gr.Tab("🧩 Knowledge Graph"):
                        output_graph = gr.HTML(label="")
                    
                    with gr.Tab("🏷️ Entity Recognition"):
                        output_entity_viz = gr.HTML(label="")
        
        # Functionality
        submit_button.click(
            fn=process_and_visualize, 
            inputs=[input_text, input_model],
            outputs=[output_graph, output_entity_viz, output_json, stats_output]
        )
        
        clear_button.click(
            fn=lambda: [None, None, None, ""],
            inputs=[],
            outputs=[output_graph, output_entity_viz, output_json, stats_output]
        )
        
        # Set initial values from cache if available
        if first_example_cache:
            # Use this to set initial values when the app loads
            demo.load(
                lambda: [
                    first_example_cache["graph_html"], 
                    first_example_cache["entities_viz"], 
                    first_example_cache["json_data"], 
                    first_example_cache["stats"]
                ],
                inputs=None,
                outputs=[output_graph, output_entity_viz, output_json, stats_output]
            )
        
        # Footer
        with gr.Row():
            gr.Markdown("---")
            gr.Markdown("πŸ“‹ **Instructions:** Enter text in any language, select a model, and click 'Extract & Visualize' to generate a knowledge graph.")
            gr.Markdown("πŸ› οΈ Powered by Phi-3 Instruct Graph | Emergent Methods")
        
    return demo

demo = create_ui()
demo.launch(share=False)