Preeti Dave commited on
Commit
097146d
·
1 Parent(s): 0aa1914
Files changed (1) hide show
  1. app.py +69 -85
app.py CHANGED
@@ -1,98 +1,82 @@
1
  import gradio as gr
2
- import networkx as nx
3
- import matplotlib.pyplot as plt
4
- import io
5
- from PIL import Image
6
 
7
- # Your sample Jus Mundi data (same as before)
8
- jusmundi_data = {
9
- "data": [
10
- {"type": "cases", "id": "46690", "attributes": {"commencement_date": "2025-03-31T00:00:00Z", "organization": "ICSID", "outcome": "Pending", "title": "United Operations v. Comoros"}, "links": {"self": "http://stanford.service.gra.prod.consul/cases/46690"}, "relationships": {"decisions": {"data": [{"type": "decisions", "id": "79078"}], "links": {"related": "http://stanford.service.gra.prod.consul/cases/46690/decisions"}}, "parties": {"data": [{"type": "parties", "id": "3535"}, {"type": "parties", "id": "64825"}], "links": {"related": "http://stanford.service.gra.prod.consul/cases/46690/parties"}}}},
11
- {"type": "cases", "id": "46609", "attributes": {"commencement_date": "1913-07-31T00:00:00Z", "organization": "PCA", "outcome": "Concluded", "title": "Expropriated Religious Properties"}, "links": {"self": "http://stanford.service.gra.prod.consul/cases/46609"}, "relationships": {"decisions": {"data": [{"type": "decisions", "id": "78948"}], "links": {"related": "http://stanford.service.gra.prod.consul/cases/46609/decisions"}}, "parties": {"data": [{"type": "parties", "id": "7302"}, {"type": "parties", "id": "7319"}, {"type": "parties", "id": "7790"}], "links": {"related": "http://stanford.service.gra.prod.consul/cases/46609/parties"}}}},
12
- {"type": "cases", "id": "46542", "attributes": {"commencement_date": "2025-03-20T00:00:00Z", "organization": "ICSID", "outcome": "Pending", "title": "Bellwether and Tecnipetrol v. Ecuador and Petroecuador"}, "links": {"self": "http://stanford.service.gra.prod.consul/cases/46542"}, "relationships": {"decisions": {"data": [{"type": "decisions", "id": "78809"}], "links": {"related": "http://stanford.service.gra.prod.consul/cases/46542/decisions"}}, "parties": {"data": [{"type": "parties", "id": "7569"}, {"type": "parties", "id": "13856"}, {"type": "parties", "id": "64647"}, {"type": "parties", "id": "64648"}], "links": {"related": "http://stanford.service.gra.prod.consul/cases/46542/parties"}}}},
13
- {"type": "cases", "id": "46486", "attributes": {"commencement_date": "2025-03-14T00:00:00Z", "organization": "ICSID", "outcome": "Pending", "title": "Dirk Andres as insolvency administrator regarding the assets of HBH v. Algeria"}, "links": {"self": "http://stanford.service.gra.prod.consul/cases/46486"}, "relationships": {"decisions": {"data": [{"type": "decisions", "id": "78693"}], "links": {"related": "http://stanford.service.gra.prod.consul/cases/46486/decisions"}}, "parties": {"data": [{"type": "parties", "id": "7345"}, {"type": "parties", "id": "64617"}], "links": {"related": "http://stanford.service.gra.prod.consul/cases/46486/parties"}}}},
14
- {"type": "cases", "id": "46422", "attributes": {"commencement_date": "2025-03-12T00:00:00Z", "organization": "ICSID", "outcome": "Pending", "title": "Brookfield v. Peru"}, "links": {"self": "http://stanford.service.gra.prod.consul/cases/46422"}, "relationships": {"decisions": {"data": [{"type": "decisions", "id": "78955"}], "links": {"related": "http://stanford.service.gra.prod.consul/cases/46422/decisions"}}, "parties": {"data": [{"type": "parties", "id": "1479"}, {"type": "parties", "id": "64717"}], "links": {"related": "http://stanford.service.gra.prod.consul/cases/46422/parties"}}}},
15
- {"type": "cases", "id": "42525", "attributes": {"commencement_date": "2025-03-05T00:00:00Z", "organization": "ICSID", "outcome": "Pending", "title": "Orano v. Niger (III)"}, "links": {"self": "http://stanford.service.gra.prod.consul/cases/42525"}, "relationships": {"decisions": {"data": [{"type": "decisions", "id": "73961"}], "links": {"related": "http://stanford.service.gra.prod.consul/cases/42525/decisions"}}, "parties": {"data": [{"type": "parties", "id": "5442"}, {"type": "parties", "id": "58131"}], "links": {"related": "http://stanford.service.gra.prod.consul/cases/42525/parties"}}}},
16
- {"type": "cases", "id": "42289", "attributes": {"commencement_date": "2021-03-08T00:00:00Z", "organization": "ICSID", "outcome": "Discontinued", "title": "Ahmed Hussein v. Egypt (I)"}, "links": {"self": "http://stanford.service.gra.prod.consul/cases/42289"}, "relationships": {"parties": {"data": [{"type": "parties", "id": "352"}, {"type": "parties", "id": "59703"}, {"type": "parties", "id": "59703"}], "links": {"related": "http://stanford.service.gra.prod.consul/cases/42289/parties"}}}},
17
- {"type": "cases", "id": "42131", "attributes": {"commencement_date": "2020-01-01T00:00:00Z", "organization": "PCA", "outcome": "Concluded", "title": "Claimant v. Respondent"}, "links": {"self": "http://stanford.service.gra.prod.consul/cases/42131"}, "relationships": {"decisions": {"data": [{"type": "decisions", "id": "73273"}], "links": {"related": "http://stanford.service.gra.prod.consul/cases/42131/decisions"}}, "parties": {"data": [{"type": "parties", "id": "59479"}, {"type": "parties", "id": "59480"}], "links": {"related": "http://stanford.service.gra.prod.consul/cases/42131/parties"}}}},
18
- {"type": "cases", "id": "42025", "attributes": {"commencement_date": "2025-02-18T00:00:00Z", "organization": "ICSID", "outcome": "Pending", "title": "Draslovka Holding v. Mexico"}, "links": {"self": "http://stanford.service.gra.prod.consul/cases/42025"}, "relationships": {"decisions": {"data": [{"type": "decisions", "id": "73113"}], "links": {"related": "http://stanford.service.gra.prod.consul/cases/42025/decisions"}}, "parties": {"data": [{"type": "parties", "id": "11038"}, {"type": "parties", "id": "59408"}], "links": {"related": "http://stanford.service.gra.prod.consul/cases/42025/parties"}}}},
19
- {"type": "cases", "id": "41696", "attributes": {"commencement_date": "2025-02-06T00:00:00Z", "organization": "ICSID", "outcome": "Pending", "title": "Togo v. ContourGlobal"}, "links": {"self": "http://stanford.service.gra.prod.consul/cases/41696"}, "relationships": {"decisions": {"data": [{"type": "decisions", "id": "72520"}], "links": {"related": "http://stanford.service.gra.prod.consul/cases/41696/decisions"}}, "parties": {"data": [{"type": "parties", "id": "14333"}, {"type": "parties", "id": "59179"}], "links": {"related": "http://stanford.service.gra.prod.consul/cases/41696/parties"}}}}
20
- ],
21
- "links": {"self": "http://stanford.service.gra.prod.consul/cases?page=1&count=10", "first": "http://stanford.service.gra.prod.consul/cases?page=1&count=10", "next": "http://stanford.service.gra.prod.consul/cases?page=2&count=10", "last": "http://stanford.service.gra.prod.consul/cases?page=154&count=10"},
22
- "meta": {"totalPages": 154, "totalRecords": 1539, "currentPage": 1, "perPage": 10}
23
- }
 
 
 
24
 
25
- def get_common_cases_and_parties(party_id_1, party_id_2):
26
- common_cases = []
27
- for case in jusmundi_data['data']:
28
- parties_in_case = [party['id'] for party in case['relationships']['parties']['data']]
29
- if party_id_1 in parties_in_case and party_id_2 in parties_in_case:
30
- common_cases.append(case)
31
- return common_cases
32
 
33
- def visualize_relationships(common_cases, party_id_1, party_id_2):
34
- if not common_cases:
35
- return "No common cases found to visualize."
36
 
37
- graph = nx.Graph()
38
- all_involved_parties = set([party_id_1, party_id_2])
 
 
39
 
40
- for case in common_cases:
41
- case_title = case['attributes']['title']
42
- parties_in_case = [party['id'] for party in case['relationships']['parties']['data']]
43
- for party_id in parties_in_case:
44
- graph.add_node(party_id)
45
- all_involved_parties.add(party_id)
46
- # Create edges between all pairs of parties within the same case
47
- for i in range(len(parties_in_case)):
48
- for j in range(i + 1, len(parties_in_case)):
49
- u, v = parties_in_case[i], parties_in_case[j]
50
- graph.add_edge(u, v, case=case_title)
51
 
52
- if not graph.nodes():
53
- return "No parties to visualize."
54
 
55
- # Highlight the input parties
56
- node_colors = ['red' if node == party_id_1 or node == party_id_2 else 'lightblue' for node in graph.nodes()]
 
 
 
 
 
 
 
57
 
58
- plt.figure(figsize=(10, 8))
59
- pos = nx.spring_layout(graph) # You can try other layouts like circular_layout
60
- nx.draw(graph, pos, with_labels=True, node_color=node_colors, node_size=2000, font_size=10, font_weight='bold')
61
- edge_labels = nx.get_edge_attributes(graph, 'case')
62
- nx.draw_networkx_edge_labels(graph, pos, edge_labels=edge_labels, font_size=8)
63
 
64
- # Save the plot to a BytesIO object
65
- buf = io.BytesIO()
66
- plt.savefig(buf, format='png')
67
- buf.seek(0)
68
- img = Image.open(buf)
69
- plt.close()
70
- return img
 
 
71
 
72
- def combined_function(party_id_1, party_id_2):
73
- common_cases = get_common_cases_and_parties(party_id_1, party_id_2)
74
- result_text = ""
75
- if common_cases:
76
- case_titles = [case['attributes']['title'] for case in common_cases]
77
- result_text = f"Parties '{party_id_1}' and '{party_id_2}' appear together in the following cases: {', '.join(case_titles)}"
78
- graph_image = visualize_relationships(common_cases, party_id_1, party_id_2)
79
- return result_text, graph_image
80
- else:
81
- result_text = f"Parties '{party_id_1}' and '{party_id_2}' do not appear together in any of the cases in this sample data."
82
- return result_text, None
83
 
84
- iface = gr.Interface(
85
- fn=combined_function,
86
- inputs=[
87
- gr.Textbox(label="Enter Party ID 1"),
88
- gr.Textbox(label="Enter Party ID 2")
89
- ],
90
- outputs=[
91
- gr.Textbox(label="Result"),
92
- gr.Image(label="Relationship Graph of Common Cases")
93
- ],
94
- title="Jus Mundi Party Relationship Checker",
95
- description="Enter two Party IDs to see if they appear together in any cases in the sample data and visualize their relationships within those cases."
96
- )
97
 
98
- iface.launch()
 
 
 
1
  import gradio as gr
2
+ from collections import defaultdict
 
 
 
3
 
4
+ # Function to detect conflicts between two cases
5
+ def detect_conflicts(case_1, case_2):
6
+ # Sample data - should be replaced with actual data fetching logic
7
+ cases_data = [
8
+ {
9
+ "id": "46690",
10
+ "title": "United Operations v. Comoros",
11
+ "parties": ["3535", "64825"]
12
+ },
13
+ {
14
+ "id": "46609",
15
+ "title": "Expropriated Religious Properties",
16
+ "parties": ["7302", "7319", "7790", "3535"] # Party "3535" added to cause a conflict
17
+ },
18
+ {
19
+ "id": "46542",
20
+ "title": "Bellwether and Tecnipetrol v. Ecuador and Petroecuador",
21
+ "parties": ["7569", "13856", "64647", "64648"]
22
+ }
23
+ ]
24
 
25
+ # A function to detect conflicts between two case inputs
26
+ conflicts = defaultdict(list)
 
 
 
 
 
27
 
28
+ # Convert case_1 and case_2 to IDs
29
+ case_1 = case_1.strip()
30
+ case_2 = case_2.strip()
31
 
32
+ # Find the two cases from the sample data
33
+ selected_cases = [
34
+ case for case in cases_data if case_1 == case["id"] or case_2 == case["id"]
35
+ ]
36
 
37
+ # If both cases are found, proceed to check conflicts
38
+ if len(selected_cases) == 2:
39
+ case_1_parties = set(selected_cases[0]["parties"])
40
+ case_2_parties = set(selected_cases[1]["parties"])
 
 
 
 
 
 
 
41
 
42
+ # Find the common parties
43
+ common_parties = case_1_parties.intersection(case_2_parties)
44
 
45
+ if common_parties:
46
+ conflicts_output = "Conflicts Detected:\n"
47
+ for party in common_parties:
48
+ conflicts_output += f"Party {party} is involved in the following cases:\n"
49
+ conflicts_output += f" - {selected_cases[0]['title']} (Case ID: {selected_cases[0]['id']})\n"
50
+ conflicts_output += f" - {selected_cases[1]['title']} (Case ID: {selected_cases[1]['id']})\n"
51
+ conflicts_output += "-" * 40 + "\n"
52
+ else:
53
+ conflicts_output = "No conflicts detected."
54
 
55
+ return conflicts_output
56
+ else:
57
+ return "Invalid Case IDs"
 
 
58
 
59
+ # Gradio UI Setup
60
+ def create_gradio_ui():
61
+ with gr.Blocks() as demo:
62
+ gr.Markdown("# Conflict Detection Tool")
63
+ gr.Markdown("This app detects conflicts between two cases based on party involvement.")
64
+
65
+ # User inputs for case IDs
66
+ case_id_1 = gr.Textbox(label="Enter Case ID 1", placeholder="Enter the first case ID here...")
67
+ case_id_2 = gr.Textbox(label="Enter Case ID 2", placeholder="Enter the second case ID here...")
68
 
69
+ # Output area for the result
70
+ output_area = gr.Textbox(label="Conflict Detection Output", lines=10)
71
+
72
+ # Button to trigger conflict detection
73
+ detect_button = gr.Button("Detect Conflicts")
74
+
75
+ # Define button click behavior
76
+ detect_button.click(detect_conflicts, inputs=[case_id_1, case_id_2], outputs=[output_area])
 
 
 
77
 
78
+ demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
+ # Run the Gradio app
81
+ if __name__ == "__main__":
82
+ create_gradio_ui()