Joey Callanan
commited on
Commit
·
8722415
1
Parent(s):
5a820b2
minor changes
Browse files- src/app.py +8 -4
- src/ui/handlers.py +41 -18
src/app.py
CHANGED
|
@@ -73,13 +73,17 @@ def create_app():
|
|
| 73 |
# Event handlers for Chemical Diagram Variations Tab
|
| 74 |
variation_components['generate_variations_btn'].click(
|
| 75 |
fn=variation_handlers.generate_variations_for_display,
|
| 76 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
| 77 |
outputs=[
|
| 78 |
-
variation_components['variations_grid'],
|
| 79 |
-
variation_components['selected_smiles_display'],
|
| 80 |
-
variation_components['selected_style_display']
|
| 81 |
]
|
| 82 |
)
|
|
|
|
| 83 |
|
| 84 |
variation_components['clear_variations_btn'].click(
|
| 85 |
fn=variation_handlers.clear_variations,
|
|
|
|
| 73 |
# Event handlers for Chemical Diagram Variations Tab
|
| 74 |
variation_components['generate_variations_btn'].click(
|
| 75 |
fn=variation_handlers.generate_variations_for_display,
|
| 76 |
+
inputs=[
|
| 77 |
+
variation_components['variation_smiles_input'],
|
| 78 |
+
variation_components['variation_count_slider']
|
| 79 |
+
],
|
| 80 |
outputs=[
|
| 81 |
+
variation_components['variations_grid'], # grid below big image
|
| 82 |
+
variation_components['selected_smiles_display'], # required
|
| 83 |
+
variation_components['selected_style_display'] # not used
|
| 84 |
]
|
| 85 |
)
|
| 86 |
+
|
| 87 |
|
| 88 |
variation_components['clear_variations_btn'].click(
|
| 89 |
fn=variation_handlers.clear_variations,
|
src/ui/handlers.py
CHANGED
|
@@ -25,34 +25,57 @@ class VariationHandlers:
|
|
| 25 |
# -------------------------------------------------------------
|
| 26 |
# Generate variations for the UI grid
|
| 27 |
# -------------------------------------------------------------
|
| 28 |
-
def generate_variations_for_display(self,
|
| 29 |
"""
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
"""
|
|
|
|
| 33 |
print("=== GENERATE_VARIATIONS_FOR_DISPLAY CALLED ===")
|
| 34 |
-
print(f"
|
| 35 |
-
print(f"Requested variations: {num_variations}")
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
# Save internally for selection / pagination
|
| 41 |
self.current_variations = variations
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
first_style = self.current_variations[0]["style"] if self.current_variations else "None"
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
# Outputs (in app/src/app.py wiring):
|
| 52 |
-
# variations_grid,
|
| 53 |
-
# selected_smiles_display,
|
| 54 |
-
# selected_style_display
|
| 55 |
-
return gallery_items, smiles, first_style
|
| 56 |
|
| 57 |
# -------------------------------------------------------------
|
| 58 |
# Handle selection from the variations grid
|
|
|
|
| 25 |
# -------------------------------------------------------------
|
| 26 |
# Generate variations for the UI grid
|
| 27 |
# -------------------------------------------------------------
|
| 28 |
+
def generate_variations_for_display(self, input_smiles, num_variations=12):
|
| 29 |
"""
|
| 30 |
+
1. Display the input molecule as the big image.
|
| 31 |
+
2. Generate scaffold variations using Gen_PartialSMILES2.py.
|
| 32 |
+
3. Convert all generated SMILES into 2D RDKit images.
|
| 33 |
+
4. Return them in a format the Gradio Gallery can render.
|
| 34 |
"""
|
| 35 |
+
|
| 36 |
print("=== GENERATE_VARIATIONS_FOR_DISPLAY CALLED ===")
|
| 37 |
+
print(f"User input SMILES: {input_smiles}")
|
|
|
|
| 38 |
|
| 39 |
+
# ------------------------------------------------------------
|
| 40 |
+
# Step 1 — Display input molecule as the big image
|
| 41 |
+
# ------------------------------------------------------------
|
| 42 |
+
from rdkit import Chem
|
| 43 |
+
from rdkit.Chem import Draw
|
| 44 |
+
|
| 45 |
+
mol_input = Chem.MolFromSmiles(input_smiles)
|
| 46 |
+
if mol_input is None:
|
| 47 |
+
return [], "", "Invalid SMILES"
|
| 48 |
+
|
| 49 |
+
big_image = Draw.MolToImage(mol_input, size=(400, 300))
|
| 50 |
+
|
| 51 |
+
# ------------------------------------------------------------
|
| 52 |
+
# Step 2 — Call generator (creates generated_molecules.csv)
|
| 53 |
+
# ------------------------------------------------------------
|
| 54 |
+
variations = generate_variations_from_partial_smiles(
|
| 55 |
+
input_smiles,
|
| 56 |
+
n_to_gen=num_variations
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
if not variations:
|
| 60 |
+
print("No variations returned.")
|
| 61 |
+
[], input_smiles, "No variations generated"
|
| 62 |
|
|
|
|
| 63 |
self.current_variations = variations
|
| 64 |
|
| 65 |
+
# ------------------------------------------------------------
|
| 66 |
+
# Step 3 — Format for Gradio Gallery
|
| 67 |
+
# ------------------------------------------------------------
|
| 68 |
+
gallery_items = []
|
| 69 |
+
for v in variations:
|
| 70 |
+
gallery_items.append((v["image"], v["smiles"]))
|
| 71 |
|
| 72 |
+
print(f"Generated {len(gallery_items)} icons.")
|
|
|
|
| 73 |
|
| 74 |
+
# ------------------------------------------------------------
|
| 75 |
+
# Return to Gradio
|
| 76 |
+
# ------------------------------------------------------------
|
| 77 |
+
return gallery_items, input_smiles, "" # (grid, selected SMILES, style)
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
# -------------------------------------------------------------
|
| 81 |
# Handle selection from the variations grid
|