| import networkx as nx |
| import pyreason as pr |
| import gradio as gr |
| import pandas as pd |
|
|
| |
| |
| g = nx.Graph() |
|
|
| |
| workers = ['John', 'Mary', 'Justin'] |
| g.add_nodes_from(workers) |
|
|
| |
| equipment = ['Crane', 'Bulldozer'] |
| g.add_nodes_from(equipment) |
|
|
| |
| g.add_edge('John', 'Mary', colleagues=1) |
| g.add_edge('Mary', 'Justin', colleagues=1) |
| g.add_edge('John', 'Justin', colleagues=1) |
|
|
| |
| g.add_edge('John', 'Crane', uses=1) |
| g.add_edge('Mary', 'Crane', uses=1) |
| g.add_edge('Justin', 'Crane', uses=1) |
|
|
| |
| def display_graph_edges(graph): |
| print("\nGraph edges with attributes:") |
| for u, v, d in graph.edges(data=True): |
| print(f" {u} -- {v}, attributes: {d}") |
|
|
| display_graph_edges(g) |
|
|
| |
| pr.load_graph(g) |
| pr.settings.verbose = False |
| pr.settings.atom_trace = False |
|
|
| |
| |
| certification_rule_str = ( |
| 'certified(x) : [1,1] <-1 certified(y) : [1,1], ' |
| 'colleagues(x,y) : [1,1], uses(x,z) : [1,1], uses(y,z) : [1,1]' |
| ) |
| certification_rule = pr.Rule(certification_rule_str, 'certification_rule') |
| pr.add_rule(certification_rule) |
|
|
| |
| persistence_rule_str = 'certified(x) : [1,1] <-1 certified(x) : [1,1]' |
| persistence_rule = pr.Rule(persistence_rule_str, 'persistence_rule') |
| pr.add_rule(persistence_rule) |
|
|
| |
| |
| initial_fact = pr.Fact( |
| fact_text='certified(Mary) : true', |
| name='certified_fact', |
| start_time=0, |
| end_time=999 |
| ) |
| pr.add_fact(initial_fact) |
|
|
| |
| |
| def capture_state(graph, timestep): |
| |
| active_uses = [ |
| {'User': u, 'Equipment': v} for u, v, d in graph.edges(data=True) |
| if d.get('uses', 0) == 1 |
| ] |
| |
| |
| interpretation = pr.reason(timesteps=1) |
| dataframes = pr.filter_and_sort_nodes(interpretation, ['certified']) |
| certification_status = {} |
| |
| for df in dataframes: |
| for index, row in df.iterrows(): |
| component = row['component'] |
| status = row['certified'] |
| |
| certification_status[component] = 'Yes' if status[0] >= 1 else 'No' |
| |
| |
| certifications = [ |
| {'Worker': worker, 'Certified': status} |
| for worker, status in certification_status.items() |
| ] |
| |
| |
| uses_df = pd.DataFrame(active_uses) if active_uses else pd.DataFrame(columns=['User', 'Equipment']) |
| cert_df = pd.DataFrame(certifications) if certifications else pd.DataFrame(columns=['Worker', 'Certified']) |
| |
| return uses_df, cert_df |
|
|
| |
| timesteps = 4 |
| state_data = [] |
|
|
| for t in range(timesteps): |
| print(f"\n=== TIMESTEP - {t} ===") |
| |
| |
| if t == 1: |
| |
| if g.has_edge('John', 'Crane'): |
| g['John']['Crane']['uses'] = 0 |
| print("Timestep 1: John stops using the Crane.") |
| if t == 2: |
| |
| if g.has_edge('Justin', 'Crane'): |
| g['Justin']['Crane']['uses'] = 0 |
| print("Timestep 2: Justin stops using the Crane.") |
| if t == 3: |
| |
| if g.has_edge('Mary', 'Crane'): |
| g['Mary']['Crane']['uses'] = 0 |
| print("Timestep 3: Mary stops using the Crane.") |
| |
| |
| current_uses = [ |
| (u, v, d['uses']) for u, v, d in g.edges(data=True) |
| if 'uses' in d and d['uses'] == 1 |
| ] |
| print("\nCurrent 'uses' edges:") |
| if current_uses: |
| for edge in current_uses: |
| print(f" {edge[0]} uses {edge[1]}: {edge[2]}") |
| else: |
| print(" No active 'uses' edges.") |
| |
| |
| pr.load_graph(g) |
| |
| |
| uses_df, cert_df = capture_state(g, t) |
| state_data.append({ |
| 'timestep': t, |
| 'uses': uses_df, |
| 'certifications': cert_df |
| }) |
|
|
| |
| def visualize_timestep(timestep): |
| if timestep < 0 or timestep >= timesteps: |
| return "<p style='color:red;'>Invalid timestep selected.</p>", "<p style='color:red;'>Invalid timestep selected.</p>" |
| state = state_data[timestep] |
| uses_df = state['uses'] |
| cert_df = state['certifications'] |
| |
| |
| uses_html = uses_df.to_html(index=False) if not uses_df.empty else "<p>No active 'uses' edges.</p>" |
| cert_html = cert_df.to_html(index=False) if not cert_df.empty else "<p>No certifications found.</p>" |
| |
| return uses_html, cert_html |
|
|
| |
| with gr.Blocks() as demo: |
| gr.Markdown("# π§ Neuro-Symbolic AI: Certification Propagation Visualization") |
| gr.Markdown( |
| """ |
| Welcome to the **Certification Propagation Visualization** tool powered by **Neuro-Symbolic AI** and **PyReason**. |
| |
| ## **Understanding the Technology** |
| |
| ### **Neuro-Symbolic AI** |
| Neuro-Symbolic AI is an advanced approach that combines the pattern recognition capabilities of neural networks with the logical reasoning strengths of symbolic AI. This fusion enables systems to learn from data and perform complex reasoning tasks, offering both flexibility and interpretability. |
| |
| ### **PyReason** |
| PyReason is a symbolic reasoning engine that facilitates logical inferences on graph-based knowledge representations. It allows the definition of rules and facts to simulate reasoning processes, such as how certifications propagate among workers based on their relationships and equipment usage. |
| |
| ## **How It Works** |
| |
| 1. **Graph Representation:** Workers and equipment are represented as nodes in a graph, with edges denoting relationships like colleagueship and equipment usage. |
| 2. **Defining Rules:** Logical rules dictate how certifications spread based on these relationships and equipment usage. |
| 3. **Reasoning Process:** PyReason applies these rules iteratively to update certification statuses over time. |
| 4. **Interactive Visualization:** The Gradio interface allows you to observe these dynamics interactively across different timesteps. |
| |
| ## **Instructions** |
| |
| - **Select Timestep:** Use the slider to choose a timestep (0 to 3). |
| - **Visualize:** Click the "Visualize" button or simply move the slider to update the displays. |
| - **Interpret Results:** |
| - **Active Equipment Usages:** See which workers are currently using which equipment. |
| - **Certification Statuses:** View the certification status (`Yes` or `No`) of each worker. |
| """ |
| ) |
| |
| with gr.Row(): |
| with gr.Column(): |
| timestep_slider = gr.Slider( |
| minimum=0, |
| maximum=timesteps-1, |
| step=1, |
| label="π Select Timestep", |
| value=0 |
| ) |
| submit_button = gr.Button("π Visualize") |
| with gr.Column(): |
| uses_output = gr.HTML(label="πΌ Active Equipment Usages") |
| cert_output = gr.HTML(label="π
Certification Statuses") |
| |
| submit_button.click( |
| visualize_timestep, |
| inputs=timestep_slider, |
| outputs=[uses_output, cert_output] |
| ) |
| |
| |
| timestep_slider.change( |
| visualize_timestep, |
| inputs=timestep_slider, |
| outputs=[uses_output, cert_output] |
| ) |
| |
| gr.Markdown( |
| """ |
| --- |
| |
| ### **Behind the Scenes** |
| |
| - **Initialization:** The system starts by defining workers, equipment, and their initial relationships and equipment usage. |
| - **Rule Application:** At each timestep, rules defined in PyReason determine how certifications propagate based on current equipment usage and colleague relationships. |
| - **State Capture:** The active equipment usages and certification statuses are captured and displayed in tables for each timestep. |
| |
| ### **Benefits of This Approach** |
| |
| - **Interpretability:** The logical rules provide clear insights into how decisions (certifications) are made. |
| - **Flexibility:** Easily adjust rules or relationships to simulate different scenarios. |
| - **Scalability:** Can be extended to larger networks with more complex relationships and rules. |
| """ |
| ) |
|
|
| |
| demo.launch() |
|
|