-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ubuntu
committed
Apr 15, 2024
1 parent
3c62681
commit bb52c65
Showing
10 changed files
with
301 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import os | ||
import time | ||
import shutil | ||
import gradio as gr | ||
from ml4co_kit import KaMISSolver, draw_mis_problem, draw_mis_solution | ||
|
||
|
||
MIS_DEFAULT_PATH = "assets/mis_default.png" | ||
MIS_PROBLEM_PATH = "assets/mis_problem.png" | ||
MIS_SOLUTION_PATH = "assets/mis_solution.png" | ||
GPICKLE_PATH = "tmp/mis_problem.gpickle" | ||
RESULT_PATH = "tmp/solve/mis_problem_unweighted.result" | ||
|
||
|
||
def _handle_mis_solve(file_path: str): | ||
# Create a blank temporary folder | ||
if not os.path.exists("tmp"): | ||
os.mkdir("tmp") | ||
else: | ||
shutil.rmtree("tmp") | ||
os.mkdir("tmp") | ||
|
||
# Move the file to the temporary folder | ||
shutil.move(file_path, GPICKLE_PATH) | ||
|
||
# Begin solve and record the solving time | ||
start_time = time.time() | ||
solver = KaMISSolver() | ||
solver.solve("tmp") | ||
solved_time = time.time() - start_time | ||
|
||
# Draw pictures | ||
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 | ||
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) | ||
return message | ||
except Exception as e: | ||
message = str(e) | ||
return message, MIS_PROBLEM_PATH, MIS_SOLUTION_PATH | ||
|
||
|
||
def handle_mis_clear(): | ||
# Replace the original image with the default image | ||
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. | ||
## Examples | ||
- You can get the test examples from our [MIS Dataset Repo.](https://huggingface.co/datasets/SJTU-TES/MIS) | ||
''' | ||
) | ||
|
||
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="assets/mis_problem.png", | ||
type="filepath", | ||
label="MIS Problem", | ||
) | ||
with gr.Column(scale=4): | ||
mis_solution_img = gr.Image( | ||
value="assets/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) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
gradio | ||
ml4co-kit | ||
matplotlib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import time | ||
import shutil | ||
import gradio as gr | ||
from ml4co_kit import TSPConcordeSolver, draw_tsp_problem, draw_tsp_solution | ||
|
||
|
||
TSP_DEFAULT_PATH = "assets/tsp_default.png" | ||
TSP_PROBLEM_PATH = "assets/tsp_problem.png" | ||
TSP_SOLUTION_PATH = "assets/tsp_solution.png" | ||
|
||
|
||
def _handle_tsp_solve( | ||
file_path: str, | ||
norm: str, | ||
): | ||
# Check file upload status | ||
if file_path is None: | ||
raise gr.Error("Please upload a '.tsp' file!") | ||
if norm == '': | ||
norm = "EUC_2D" | ||
if norm != "EUC_2D" and norm != "GEO": | ||
raise gr.Error("Invaild edge_weight_type! Only support 'GEO' and 'EUC_2D'.") | ||
|
||
# Begin solve and record the solving time | ||
start_time = time.time() | ||
solver = TSPConcordeSolver(scale=1) | ||
solver.from_tsp(file_path, norm=norm) | ||
solver.solve() | ||
tours = solver.tours | ||
points = solver.points | ||
solved_time = time.time() - start_time | ||
|
||
# Draw pictures | ||
draw_tsp_problem( | ||
save_path=TSP_PROBLEM_PATH, | ||
points=points, | ||
) | ||
draw_tsp_solution( | ||
save_path=TSP_SOLUTION_PATH, | ||
points=points, | ||
tours=tours | ||
) | ||
|
||
# Message | ||
message = "Successfully solve the TSP problem, using time ({:.3f}s).".format(solved_time) | ||
|
||
return message, TSP_PROBLEM_PATH, TSP_SOLUTION_PATH | ||
|
||
|
||
def handle_tsp_solve( | ||
file_path: str, | ||
norm: str, | ||
): | ||
try: | ||
message = _handle_tsp_solve(file_path, norm) | ||
return message | ||
except Exception as e: | ||
message = str(e) | ||
return message, TSP_PROBLEM_PATH, TSP_SOLUTION_PATH | ||
|
||
|
||
def handle_tsp_clear(): | ||
# Replace the original image with the default image | ||
shutil.copy( | ||
src=TSP_DEFAULT_PATH, | ||
dst=TSP_PROBLEM_PATH | ||
) | ||
shutil.copy( | ||
src=TSP_DEFAULT_PATH, | ||
dst=TSP_SOLUTION_PATH | ||
) | ||
message = "successfully clear the files!" | ||
return message, TSP_PROBLEM_PATH, TSP_SOLUTION_PATH | ||
|
||
|
||
with gr.Blocks() as tsp_page: | ||
|
||
gr.Markdown( | ||
''' | ||
This space displays the solution to the TSP problem. | ||
## How to use this Space? | ||
- Upload a '.tsp' file from tsplib . | ||
- The images of the TSP problem and solution will be shown after you click the solve button. | ||
- Click the 'clear' button to clear all the files. | ||
## Examples | ||
- You can get the test examples from our [TSP Dataset Repo.](https://huggingface.co/datasets/ML4CO/TSPLIBOriDataset) | ||
''' | ||
) | ||
|
||
with gr.Row(variant="panel"): | ||
with gr.Column(scale=7): | ||
with gr.Row(): | ||
tsp_file = gr.File( | ||
file_types=[".tsp"], | ||
scale=3 | ||
) | ||
info = gr.Textbox( | ||
value="", | ||
label="Log", | ||
scale=4, | ||
) | ||
norm = gr.Textbox( | ||
label="Please input the edge_weight_type of the TSP file", | ||
) | ||
with gr.Column(scale=4): | ||
tsp_problem_img = gr.Image( | ||
value=TSP_PROBLEM_PATH, | ||
type="filepath", | ||
label="TSP Problem", | ||
) | ||
with gr.Column(scale=4): | ||
tsp_solution_img = gr.Image( | ||
value=TSP_SOLUTION_PATH, | ||
type="filepath", | ||
label="TSP 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_tsp_solve, | ||
[tsp_file, norm], | ||
outputs=[info, tsp_problem_img, tsp_solution_img] | ||
) | ||
|
||
clear_button.click( | ||
handle_tsp_clear, | ||
inputs=None, | ||
outputs=[info, tsp_problem_img, tsp_solution_img] | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
tsp_page.launch(debug = True) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
gradio | ||
ml4co-kit | ||
matplotlib |