Update app.py
Browse files
app.py
CHANGED
|
@@ -331,6 +331,9 @@ with gr.Blocks(title="CrystalGAT") as demo:
|
|
| 331 |
# 初始化分子状态
|
| 332 |
molecule_state = gr.State(init_molecule())
|
| 333 |
|
|
|
|
|
|
|
|
|
|
| 334 |
with gr.Row():
|
| 335 |
# 原子选择
|
| 336 |
with gr.Column():
|
|
@@ -398,27 +401,31 @@ with gr.Blocks(title="CrystalGAT") as demo:
|
|
| 398 |
brittle_text = gr.Text(label="Brittle")
|
| 399 |
brittle_img = gr.Image(type="pil", label="Brittle attention visualization")
|
| 400 |
|
| 401 |
-
# 更新原子
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
def update_atoms_list(molecule):
|
| 403 |
-
atoms_data = [[
|
| 404 |
return atoms_data
|
| 405 |
|
| 406 |
-
# 更新键列表
|
| 407 |
def update_bonds_list(molecule):
|
| 408 |
bonds_data = []
|
| 409 |
for bond in molecule["bonds"]:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 410 |
bonds_data.append([
|
| 411 |
-
|
| 412 |
-
|
| 413 |
bond["type"]
|
| 414 |
])
|
| 415 |
return bonds_data
|
| 416 |
|
| 417 |
-
# 更新原子选择下拉菜单
|
| 418 |
-
def update_atom_dropdowns(molecule):
|
| 419 |
-
atom_choices = [f"原子 {i}: {atom['type']}" for i, atom in enumerate(molecule["atoms"])]
|
| 420 |
-
return gr.Dropdown.update(choices=atom_choices), gr.Dropdown.update(choices=atom_choices)
|
| 421 |
-
|
| 422 |
# 事件处理
|
| 423 |
add_atom_btn.click(
|
| 424 |
fn=lambda atom, mol: add_atom(mol, atom),
|
|
@@ -432,17 +439,15 @@ with gr.Blocks(title="CrystalGAT") as demo:
|
|
| 432 |
fn=update_atom_dropdowns,
|
| 433 |
inputs=molecule_state,
|
| 434 |
outputs=[atom1_select, atom2_select]
|
|
|
|
|
|
|
|
|
|
| 435 |
)
|
| 436 |
|
| 437 |
add_bond_btn.click(
|
| 438 |
-
fn=lambda
|
| 439 |
-
mol,
|
| 440 |
-
int(atom1_str.split(":")[0].split(" ")[1]), # 提取原子ID
|
| 441 |
-
int(atom2_str.split(":")[0].split(" ")[1]), # 提取原子ID
|
| 442 |
-
bond
|
| 443 |
-
),
|
| 444 |
inputs=[atom1_select, atom2_select, bond_select, molecule_state],
|
| 445 |
-
outputs=molecule_state
|
| 446 |
).then(
|
| 447 |
fn=update_bonds_list,
|
| 448 |
inputs=molecule_state,
|
|
@@ -458,6 +463,9 @@ with gr.Blocks(title="CrystalGAT") as demo:
|
|
| 458 |
).then(
|
| 459 |
fn=lambda: (gr.Dropdown.update(choices=[]), gr.Dropdown.update(choices=[])),
|
| 460 |
outputs=[atom1_select, atom2_select]
|
|
|
|
|
|
|
|
|
|
| 461 |
)
|
| 462 |
|
| 463 |
generate_btn.click(
|
|
@@ -468,6 +476,9 @@ with gr.Blocks(title="CrystalGAT") as demo:
|
|
| 468 |
fn=visualize_molecule,
|
| 469 |
inputs=molecule_state,
|
| 470 |
outputs=molecule_img
|
|
|
|
|
|
|
|
|
|
| 471 |
)
|
| 472 |
|
| 473 |
# 设置交互
|
|
|
|
| 331 |
# 初始化分子状态
|
| 332 |
molecule_state = gr.State(init_molecule())
|
| 333 |
|
| 334 |
+
# 状态消息
|
| 335 |
+
status_msg = gr.Textbox(label="状态", interactive=False, value="请添加原子开始构建分子")
|
| 336 |
+
|
| 337 |
with gr.Row():
|
| 338 |
# 原子选择
|
| 339 |
with gr.Column():
|
|
|
|
| 401 |
brittle_text = gr.Text(label="Brittle")
|
| 402 |
brittle_img = gr.Image(type="pil", label="Brittle attention visualization")
|
| 403 |
|
| 404 |
+
# 更新原子选择下拉菜单
|
| 405 |
+
def update_atom_dropdowns(molecule):
|
| 406 |
+
atom_choices = [f"{atom['id']}: {atom['type']}" for atom in molecule["atoms"]]
|
| 407 |
+
return gr.Dropdown.update(choices=atom_choices), gr.Dropdown.update(choices=atom_choices)
|
| 408 |
+
|
| 409 |
+
# 更新原子列表
|
| 410 |
def update_atoms_list(molecule):
|
| 411 |
+
atoms_data = [[atom["id"], atom["type"]] for atom in molecule["atoms"]]
|
| 412 |
return atoms_data
|
| 413 |
|
| 414 |
+
# 更新键列表
|
| 415 |
def update_bonds_list(molecule):
|
| 416 |
bonds_data = []
|
| 417 |
for bond in molecule["bonds"]:
|
| 418 |
+
atom1_id = bond["atom1"]
|
| 419 |
+
atom2_id = bond["atom2"]
|
| 420 |
+
atom1_type = next((a["type"] for a in molecule["atoms"] if a["id"] == atom1_id), "?")
|
| 421 |
+
atom2_type = next((a["type"] for a in molecule["atoms"] if a["id"] == atom2_id), "?")
|
| 422 |
bonds_data.append([
|
| 423 |
+
f"{atom1_id}: {atom1_type}",
|
| 424 |
+
f"{atom2_id}: {atom2_type}",
|
| 425 |
bond["type"]
|
| 426 |
])
|
| 427 |
return bonds_data
|
| 428 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 429 |
# 事件处理
|
| 430 |
add_atom_btn.click(
|
| 431 |
fn=lambda atom, mol: add_atom(mol, atom),
|
|
|
|
| 439 |
fn=update_atom_dropdowns,
|
| 440 |
inputs=molecule_state,
|
| 441 |
outputs=[atom1_select, atom2_select]
|
| 442 |
+
).then(
|
| 443 |
+
fn=lambda: "原子添加成功!",
|
| 444 |
+
outputs=status_msg
|
| 445 |
)
|
| 446 |
|
| 447 |
add_bond_btn.click(
|
| 448 |
+
fn=lambda atom1, atom2, bond, mol: (add_bond(mol, int(atom1.split(":")[0]), int(atom2.split(":")[0]), bond)) if atom1 and atom2 else (mol, "请先选择两个原子!"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 449 |
inputs=[atom1_select, atom2_select, bond_select, molecule_state],
|
| 450 |
+
outputs=[molecule_state, status_msg]
|
| 451 |
).then(
|
| 452 |
fn=update_bonds_list,
|
| 453 |
inputs=molecule_state,
|
|
|
|
| 463 |
).then(
|
| 464 |
fn=lambda: (gr.Dropdown.update(choices=[]), gr.Dropdown.update(choices=[])),
|
| 465 |
outputs=[atom1_select, atom2_select]
|
| 466 |
+
).then(
|
| 467 |
+
fn=lambda: "已清除所有原子和键",
|
| 468 |
+
outputs=status_msg
|
| 469 |
)
|
| 470 |
|
| 471 |
generate_btn.click(
|
|
|
|
| 476 |
fn=visualize_molecule,
|
| 477 |
inputs=molecule_state,
|
| 478 |
outputs=molecule_img
|
| 479 |
+
).then(
|
| 480 |
+
fn=lambda: "分子生成完成!",
|
| 481 |
+
outputs=status_msg
|
| 482 |
)
|
| 483 |
|
| 484 |
# 设置交互
|