ubuntu commited on
Commit
41e61b9
1 Parent(s): a0e561d

Initial Commit

Browse files
app.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import time
3
+ import shutil
4
+ import gradio as gr
5
+ from data4co import KaMISSolver, draw_mis_problem, draw_mis_solution
6
+
7
+
8
+ MIS_DEFAULT_PATH = "src/mis_default.png"
9
+ MIS_PROBLEM_PATH = "src/mis_problem.png"
10
+ MIS_SOLUTION_PATH = "src/mis_solution.png"
11
+ GPICKLE_PATH = "tmp/mis_problem.gpickle"
12
+ RESULT_PATH = "tmp/solve/mis_problem_unweighted.result"
13
+
14
+
15
+ def _handle_mis_solve(file_path: str):
16
+ if not os.path.exists("tmp"):
17
+ os.mkdir("tmp")
18
+ else:
19
+ shutil.rmtree("tmp")
20
+ os.mkdir("tmp")
21
+ shutil.move(file_path, GPICKLE_PATH)
22
+ start_time = time.time()
23
+ solver = KaMISSolver()
24
+ try:
25
+ solver.solve("tmp")
26
+ except:
27
+ solver.recompile_kamis()
28
+ solver.solve("tmp")
29
+ solved_time = time.time() - start_time
30
+ draw_mis_problem(
31
+ save_path=MIS_PROBLEM_PATH,
32
+ gpickle_path=GPICKLE_PATH
33
+ )
34
+ draw_mis_solution(
35
+ save_path=MIS_SOLUTION_PATH,
36
+ gpickle_path=GPICKLE_PATH,
37
+ result_path=RESULT_PATH,
38
+ pos_type="kamada_kawai_layout"
39
+ )
40
+ message = "Successfully solve the MIS problem, using time ({:.3f}s).".format(solved_time)
41
+
42
+ return message, MIS_PROBLEM_PATH, MIS_SOLUTION_PATH
43
+
44
+
45
+ def handle_mis_solve(file_path: str):
46
+ try:
47
+ message = _handle_mis_solve(file_path)
48
+ except Exception as e:
49
+ message = str(e)
50
+ return message
51
+
52
+
53
+ def handle_mis_clear():
54
+ shutil.copy(
55
+ src=MIS_DEFAULT_PATH,
56
+ dst=MIS_PROBLEM_PATH
57
+ )
58
+ shutil.copy(
59
+ src=MIS_DEFAULT_PATH,
60
+ dst=MIS_SOLUTION_PATH
61
+ )
62
+ message = "successfully clear the files!"
63
+ return message, MIS_PROBLEM_PATH, MIS_SOLUTION_PATH
64
+
65
+
66
+ def convert_image_path_to_bytes(image_path):
67
+ with open(image_path, "rb") as f:
68
+ image_bytes = f.read()
69
+ return image_bytes
70
+
71
+
72
+ with gr.Blocks() as mis_page:
73
+
74
+ gr.Markdown(
75
+ '''
76
+ This space displays the solution to the MIS problem.
77
+
78
+ ## How to use this Space?
79
+ - Upload a '.gpickle' file.
80
+ - The images of the MIS problem and solution will be shown after you click the solve button.
81
+ - Click the 'clear' button to clear all the files.
82
+ '''
83
+ )
84
+
85
+ with gr.Row(variant="panel"):
86
+ with gr.Column(scale=7):
87
+ with gr.Row():
88
+ mis_file = gr.File(
89
+ file_types=[".gpickle"],
90
+ scale=3
91
+ )
92
+ info = gr.Textbox(
93
+ value="",
94
+ label="Log",
95
+ scale=4,
96
+ )
97
+ with gr.Column(scale=4):
98
+ mis_problem_img = gr.Image(
99
+ value="media/mis_problem.png",
100
+ type="filepath",
101
+ label="MIS Problem",
102
+ )
103
+ with gr.Column(scale=4):
104
+ mis_solution_img = gr.Image(
105
+ value="media/mis_solution.png",
106
+ type="filepath",
107
+ label="MIS Solution",
108
+ )
109
+ with gr.Row():
110
+ with gr.Column(scale=1, min_width=100):
111
+ solve_button = gr.Button(
112
+ value="Solve",
113
+ variant="primary",
114
+ scale=1
115
+ )
116
+ with gr.Column(scale=1, min_width=100):
117
+ clear_button = gr.Button(
118
+ "Clear",
119
+ variant="secondary",
120
+ scale=1
121
+ )
122
+ with gr.Column(scale=8):
123
+ pass
124
+
125
+ solve_button.click(
126
+ handle_mis_solve,
127
+ [mis_file],
128
+ outputs=[info, mis_problem_img, mis_solution_img]
129
+ )
130
+
131
+ clear_button.click(
132
+ handle_mis_clear,
133
+ inputs=None,
134
+ outputs=[info, mis_problem_img, mis_solution_img]
135
+ )
136
+
137
+
138
+ if __name__ == "__main__":
139
+ mis_page.launch(debug = True)
example/mis_er_20_20_0.gpickle ADDED
Binary file (1.03 kB). View file
 
example/mis_er_20_20_1.gpickle ADDED
Binary file (1.02 kB). View file
 
example/mis_er_20_20_2.gpickle ADDED
Binary file (1.06 kB). View file
 
example/mis_er_20_20_3.gpickle ADDED
Binary file (1.05 kB). View file
 
example/mis_er_20_20_4.gpickle ADDED
Binary file (1.05 kB). View file
 
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio
2
+ data4co
3
+ matplotlib
4
+ fastapi
5
+ aiohttp
src/mis_default.png ADDED
src/mis_problem.png ADDED
src/mis_solution.png ADDED