Update app.py
Browse files
app.py
CHANGED
|
@@ -20,6 +20,8 @@ from rdkit.Chem import AllChem
|
|
| 20 |
from rdkit.Chem import MolFromSmiles
|
| 21 |
from io import BytesIO
|
| 22 |
import traceback
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# 配置日志
|
| 25 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
@@ -331,9 +333,6 @@ with gr.Blocks(title="CrystalGAT") as demo:
|
|
| 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():
|
|
@@ -354,6 +353,9 @@ with gr.Blocks(title="CrystalGAT") as demo:
|
|
| 354 |
# 键选择
|
| 355 |
with gr.Column():
|
| 356 |
gr.Markdown("### 添加键")
|
|
|
|
|
|
|
|
|
|
| 357 |
atom1_select = gr.Dropdown(
|
| 358 |
label="选择第一个原子",
|
| 359 |
choices=[],
|
|
@@ -373,7 +375,7 @@ with gr.Blocks(title="CrystalGAT") as demo:
|
|
| 373 |
bonds_list = gr.Dataframe(
|
| 374 |
label="键列表",
|
| 375 |
headers=["原子1", "原子2", "键类型"],
|
| 376 |
-
datatype=["
|
| 377 |
interactive=False
|
| 378 |
)
|
| 379 |
|
|
@@ -401,32 +403,31 @@ with gr.Blocks(title="CrystalGAT") as demo:
|
|
| 401 |
brittle_text = gr.Text(label="Brittle")
|
| 402 |
brittle_img = gr.Image(type="pil", label="Brittle attention visualization")
|
| 403 |
|
| 404 |
-
# 更新原子选
|
| 405 |
-
def
|
| 406 |
-
|
| 407 |
-
|
|
|
|
| 408 |
|
| 409 |
-
# 更新原子列表
|
| 410 |
def update_atoms_list(molecule):
|
| 411 |
-
atoms_data = [[
|
| 412 |
return atoms_data
|
| 413 |
|
| 414 |
-
# 更新键列表
|
| 415 |
def update_bonds_list(molecule):
|
| 416 |
bonds_data = []
|
| 417 |
for bond in molecule["bonds"]:
|
| 418 |
-
|
| 419 |
-
|
| 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"{
|
| 424 |
-
f"{
|
| 425 |
bond["type"]
|
| 426 |
])
|
| 427 |
return bonds_data
|
| 428 |
|
| 429 |
-
# 事件处理
|
| 430 |
add_atom_btn.click(
|
| 431 |
fn=lambda atom, mol: add_atom(mol, atom),
|
| 432 |
inputs=[atom_select, molecule_state],
|
|
@@ -436,38 +437,57 @@ with gr.Blocks(title="CrystalGAT") as demo:
|
|
| 436 |
inputs=molecule_state,
|
| 437 |
outputs=atoms_list
|
| 438 |
).then(
|
| 439 |
-
fn=
|
| 440 |
inputs=molecule_state,
|
| 441 |
-
outputs=
|
| 442 |
).then(
|
| 443 |
-
|
| 444 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 445 |
)
|
| 446 |
|
|
|
|
| 447 |
add_bond_btn.click(
|
| 448 |
-
fn=lambda atom1, atom2, bond, mol:
|
| 449 |
inputs=[atom1_select, atom2_select, bond_select, molecule_state],
|
| 450 |
-
outputs=
|
| 451 |
).then(
|
| 452 |
fn=update_bonds_list,
|
| 453 |
inputs=molecule_state,
|
| 454 |
outputs=bonds_list
|
| 455 |
)
|
| 456 |
|
|
|
|
| 457 |
clear_btn.click(
|
| 458 |
fn=init_molecule,
|
| 459 |
outputs=molecule_state
|
| 460 |
).then(
|
| 461 |
-
fn=lambda: [
|
| 462 |
-
outputs=
|
| 463 |
).then(
|
| 464 |
-
fn=lambda:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 465 |
outputs=[atom1_select, atom2_select]
|
| 466 |
).then(
|
| 467 |
-
fn=lambda:
|
| 468 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
| 469 |
)
|
| 470 |
|
|
|
|
| 471 |
generate_btn.click(
|
| 472 |
fn=generate_smiles,
|
| 473 |
inputs=molecule_state,
|
|
@@ -476,9 +496,6 @@ with gr.Blocks(title="CrystalGAT") as demo:
|
|
| 476 |
fn=visualize_molecule,
|
| 477 |
inputs=molecule_state,
|
| 478 |
outputs=molecule_img
|
| 479 |
-
).then(
|
| 480 |
-
fn=lambda: "分子生成完成!",
|
| 481 |
-
outputs=status_msg
|
| 482 |
)
|
| 483 |
|
| 484 |
# 设置交互
|
|
|
|
| 20 |
from rdkit.Chem import MolFromSmiles
|
| 21 |
from io import BytesIO
|
| 22 |
import traceback
|
| 23 |
+
import base64
|
| 24 |
+
import json
|
| 25 |
|
| 26 |
# 配置日志
|
| 27 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
|
|
| 333 |
# 初始化分子状态
|
| 334 |
molecule_state = gr.State(init_molecule())
|
| 335 |
|
|
|
|
|
|
|
|
|
|
| 336 |
with gr.Row():
|
| 337 |
# 原子选择
|
| 338 |
with gr.Column():
|
|
|
|
| 353 |
# 键选择
|
| 354 |
with gr.Column():
|
| 355 |
gr.Markdown("### 添加键")
|
| 356 |
+
# 创建原子选项列表
|
| 357 |
+
atom_options = gr.State([])
|
| 358 |
+
|
| 359 |
atom1_select = gr.Dropdown(
|
| 360 |
label="选择第一个原子",
|
| 361 |
choices=[],
|
|
|
|
| 375 |
bonds_list = gr.Dataframe(
|
| 376 |
label="键列表",
|
| 377 |
headers=["原子1", "原子2", "键类型"],
|
| 378 |
+
datatype=["str", "str", "str"],
|
| 379 |
interactive=False
|
| 380 |
)
|
| 381 |
|
|
|
|
| 403 |
brittle_text = gr.Text(label="Brittle")
|
| 404 |
brittle_img = gr.Image(type="pil", label="Brittle attention visualization")
|
| 405 |
|
| 406 |
+
# 更新原子选项列表
|
| 407 |
+
def update_atom_options(molecule):
|
| 408 |
+
# 创建原子选项列表
|
| 409 |
+
options = [f"{i}: {atom['type']}" for i, atom in enumerate(molecule["atoms"])]
|
| 410 |
+
return options
|
| 411 |
|
| 412 |
+
# 更新原子列表显示
|
| 413 |
def update_atoms_list(molecule):
|
| 414 |
+
atoms_data = [[i, atom["type"]] for i, atom in enumerate(molecule["atoms"])]
|
| 415 |
return atoms_data
|
| 416 |
|
| 417 |
+
# 更新键列表显示
|
| 418 |
def update_bonds_list(molecule):
|
| 419 |
bonds_data = []
|
| 420 |
for bond in molecule["bonds"]:
|
| 421 |
+
atom1_type = molecule["atoms"][bond["atom1"]]["type"]
|
| 422 |
+
atom2_type = molecule["atoms"][bond["atom2"]]["type"]
|
|
|
|
|
|
|
| 423 |
bonds_data.append([
|
| 424 |
+
f"{bond['atom1']}: {atom1_type}",
|
| 425 |
+
f"{bond['atom2']}: {atom2_type}",
|
| 426 |
bond["type"]
|
| 427 |
])
|
| 428 |
return bonds_data
|
| 429 |
|
| 430 |
+
# 事件处理 - 添加原子
|
| 431 |
add_atom_btn.click(
|
| 432 |
fn=lambda atom, mol: add_atom(mol, atom),
|
| 433 |
inputs=[atom_select, molecule_state],
|
|
|
|
| 437 |
inputs=molecule_state,
|
| 438 |
outputs=atoms_list
|
| 439 |
).then(
|
| 440 |
+
fn=update_atom_options,
|
| 441 |
inputs=molecule_state,
|
| 442 |
+
outputs=atom_options
|
| 443 |
).then(
|
| 444 |
+
lambda options: [
|
| 445 |
+
gr.Dropdown.update(choices=options, value=options[0] if options else None),
|
| 446 |
+
gr.Dropdown.update(choices=options, value=options[0] if options else None)
|
| 447 |
+
],
|
| 448 |
+
inputs=atom_options,
|
| 449 |
+
outputs=[atom1_select, atom2_select]
|
| 450 |
)
|
| 451 |
|
| 452 |
+
# 事件处理 - 添加键
|
| 453 |
add_bond_btn.click(
|
| 454 |
+
fn=lambda atom1, atom2, bond, mol: add_bond(mol, int(atom1.split(":")[0]), int(atom2.split(":")[0]), bond),
|
| 455 |
inputs=[atom1_select, atom2_select, bond_select, molecule_state],
|
| 456 |
+
outputs=molecule_state
|
| 457 |
).then(
|
| 458 |
fn=update_bonds_list,
|
| 459 |
inputs=molecule_state,
|
| 460 |
outputs=bonds_list
|
| 461 |
)
|
| 462 |
|
| 463 |
+
# 事件处理 - 清除所有
|
| 464 |
clear_btn.click(
|
| 465 |
fn=init_molecule,
|
| 466 |
outputs=molecule_state
|
| 467 |
).then(
|
| 468 |
+
fn=lambda: [],
|
| 469 |
+
outputs=atoms_list
|
| 470 |
).then(
|
| 471 |
+
fn=lambda: [],
|
| 472 |
+
outputs=bonds_list
|
| 473 |
+
).then(
|
| 474 |
+
fn=lambda: [],
|
| 475 |
+
outputs=atom_options
|
| 476 |
+
).then(
|
| 477 |
+
lambda: [
|
| 478 |
+
gr.Dropdown.update(choices=[], value=None),
|
| 479 |
+
gr.Dropdown.update(choices=[], value=None)
|
| 480 |
+
],
|
| 481 |
outputs=[atom1_select, atom2_select]
|
| 482 |
).then(
|
| 483 |
+
fn=lambda: None,
|
| 484 |
+
outputs=molecule_img
|
| 485 |
+
).then(
|
| 486 |
+
fn=lambda: "",
|
| 487 |
+
outputs=molecule_smiles
|
| 488 |
)
|
| 489 |
|
| 490 |
+
# 事件处理 - 生成分子
|
| 491 |
generate_btn.click(
|
| 492 |
fn=generate_smiles,
|
| 493 |
inputs=molecule_state,
|
|
|
|
| 496 |
fn=visualize_molecule,
|
| 497 |
inputs=molecule_state,
|
| 498 |
outputs=molecule_img
|
|
|
|
|
|
|
|
|
|
| 499 |
)
|
| 500 |
|
| 501 |
# 设置交互
|