File size: 3,749 Bytes
41e61b9 |
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 |
import os
import time
import shutil
import gradio as gr
from data4co import KaMISSolver, draw_mis_problem, draw_mis_solution
MIS_DEFAULT_PATH = "src/mis_default.png"
MIS_PROBLEM_PATH = "src/mis_problem.png"
MIS_SOLUTION_PATH = "src/mis_solution.png"
GPICKLE_PATH = "tmp/mis_problem.gpickle"
RESULT_PATH = "tmp/solve/mis_problem_unweighted.result"
def _handle_mis_solve(file_path: str):
if not os.path.exists("tmp"):
os.mkdir("tmp")
else:
shutil.rmtree("tmp")
os.mkdir("tmp")
shutil.move(file_path, GPICKLE_PATH)
start_time = time.time()
solver = KaMISSolver()
try:
solver.solve("tmp")
except:
solver.recompile_kamis()
solver.solve("tmp")
solved_time = time.time() - start_time
draw_mis_problem(
save_path=MIS_PROBLEM_PATH,
gpickle_path=GPICKLE_PATH
)
draw_mis_solution(
save_path=MIS_SOLUTION_PATH,
gpickle_path=GPICKLE_PATH,
result_path=RESULT_PATH,
pos_type="kamada_kawai_layout"
)
message = "Successfully solve the MIS problem, using time ({:.3f}s).".format(solved_time)
return message, MIS_PROBLEM_PATH, MIS_SOLUTION_PATH
def handle_mis_solve(file_path: str):
try:
message = _handle_mis_solve(file_path)
except Exception as e:
message = str(e)
return message
def handle_mis_clear():
shutil.copy(
src=MIS_DEFAULT_PATH,
dst=MIS_PROBLEM_PATH
)
shutil.copy(
src=MIS_DEFAULT_PATH,
dst=MIS_SOLUTION_PATH
)
message = "successfully clear the files!"
return message, MIS_PROBLEM_PATH, MIS_SOLUTION_PATH
def convert_image_path_to_bytes(image_path):
with open(image_path, "rb") as f:
image_bytes = f.read()
return image_bytes
with gr.Blocks() as mis_page:
gr.Markdown(
'''
This space displays the solution to the MIS problem.
## How to use this Space?
- Upload a '.gpickle' file.
- The images of the MIS problem and solution will be shown after you click the solve button.
- Click the 'clear' button to clear all the files.
'''
)
with gr.Row(variant="panel"):
with gr.Column(scale=7):
with gr.Row():
mis_file = gr.File(
file_types=[".gpickle"],
scale=3
)
info = gr.Textbox(
value="",
label="Log",
scale=4,
)
with gr.Column(scale=4):
mis_problem_img = gr.Image(
value="media/mis_problem.png",
type="filepath",
label="MIS Problem",
)
with gr.Column(scale=4):
mis_solution_img = gr.Image(
value="media/mis_solution.png",
type="filepath",
label="MIS Solution",
)
with gr.Row():
with gr.Column(scale=1, min_width=100):
solve_button = gr.Button(
value="Solve",
variant="primary",
scale=1
)
with gr.Column(scale=1, min_width=100):
clear_button = gr.Button(
"Clear",
variant="secondary",
scale=1
)
with gr.Column(scale=8):
pass
solve_button.click(
handle_mis_solve,
[mis_file],
outputs=[info, mis_problem_img, mis_solution_img]
)
clear_button.click(
handle_mis_clear,
inputs=None,
outputs=[info, mis_problem_img, mis_solution_img]
)
if __name__ == "__main__":
mis_page.launch(debug = True) |