Joey Callanan
commited on
Commit
Β·
56cce54
1
Parent(s):
8722415
minor changes
Browse files- src/ui/components.py +2 -2
- src/ui/handlers.py +43 -21
src/ui/components.py
CHANGED
|
@@ -242,8 +242,8 @@ def create_chemical_variations_tab():
|
|
| 242 |
show_label=False,
|
| 243 |
elem_id="variations_gallery",
|
| 244 |
columns=3,
|
| 245 |
-
rows=
|
| 246 |
-
height=
|
| 247 |
object_fit="contain",
|
| 248 |
allow_preview=True,
|
| 249 |
show_share_button=False,
|
|
|
|
| 242 |
show_label=False,
|
| 243 |
elem_id="variations_gallery",
|
| 244 |
columns=3,
|
| 245 |
+
rows=4,
|
| 246 |
+
height='auto',
|
| 247 |
object_fit="contain",
|
| 248 |
allow_preview=True,
|
| 249 |
show_share_button=False,
|
src/ui/handlers.py
CHANGED
|
@@ -27,55 +27,77 @@ class VariationHandlers:
|
|
| 27 |
# -------------------------------------------------------------
|
| 28 |
def generate_variations_for_display(self, input_smiles, num_variations=12):
|
| 29 |
"""
|
| 30 |
-
|
| 31 |
-
|
| 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("
|
|
|
|
|
|
|
| 37 |
print(f"User input SMILES: {input_smiles}")
|
|
|
|
| 38 |
|
| 39 |
# ------------------------------------------------------------
|
| 40 |
-
#
|
| 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 |
-
|
|
|
|
| 48 |
|
| 49 |
big_image = Draw.MolToImage(mol_input, size=(400, 300))
|
| 50 |
|
|
|
|
|
|
|
| 51 |
# ------------------------------------------------------------
|
| 52 |
-
#
|
| 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
|
| 61 |
-
[], input_smiles, "
|
| 62 |
|
| 63 |
self.current_variations = variations
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
gallery_items = []
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
# -------------------------------------------------------------
|
| 81 |
# Handle selection from the variations grid
|
|
|
|
| 27 |
# -------------------------------------------------------------
|
| 28 |
def generate_variations_for_display(self, input_smiles, num_variations=12):
|
| 29 |
"""
|
| 30 |
+
Generate variations using Gen_PartialSMILES2.py, convert to RDKit images,
|
| 31 |
+
and return formatted gallery items while printing debugging info.
|
|
|
|
|
|
|
| 32 |
"""
|
| 33 |
|
| 34 |
+
print("\n==============================")
|
| 35 |
+
print("π generate_variations_for_display CALLED")
|
| 36 |
+
print("==============================")
|
| 37 |
print(f"User input SMILES: {input_smiles}")
|
| 38 |
+
print(f"Requested # variations: {num_variations}")
|
| 39 |
|
| 40 |
# ------------------------------------------------------------
|
| 41 |
+
# Create big image for the user input molecule
|
| 42 |
# ------------------------------------------------------------
|
| 43 |
from rdkit import Chem
|
| 44 |
from rdkit.Chem import Draw
|
| 45 |
+
|
| 46 |
mol_input = Chem.MolFromSmiles(input_smiles)
|
| 47 |
if mol_input is None:
|
| 48 |
+
print("β ERROR: Invalid SMILES input. Cannot generate big preview.")
|
| 49 |
+
return [], input_smiles, "Invalid SMILES"
|
| 50 |
|
| 51 |
big_image = Draw.MolToImage(mol_input, size=(400, 300))
|
| 52 |
|
| 53 |
+
print("β Input molecule rendered successfully.")
|
| 54 |
+
|
| 55 |
# ------------------------------------------------------------
|
| 56 |
+
# Run the generation helper
|
| 57 |
# ------------------------------------------------------------
|
| 58 |
variations = generate_variations_from_partial_smiles(
|
| 59 |
input_smiles,
|
| 60 |
n_to_gen=num_variations
|
| 61 |
)
|
| 62 |
|
| 63 |
+
print(f"Generator returned {len(variations)} variations.")
|
| 64 |
+
|
| 65 |
+
# No variations returned β UI failure β show empty grid
|
| 66 |
if not variations:
|
| 67 |
+
print("β No variations generated β returning empty gallery.")
|
| 68 |
+
return [], input_smiles, ""
|
| 69 |
|
| 70 |
self.current_variations = variations
|
| 71 |
|
| 72 |
+
# ------------------------------------------------------------
|
| 73 |
+
# Build gallery items for Gradio
|
| 74 |
+
# ------------------------------------------------------------
|
| 75 |
gallery_items = []
|
| 76 |
+
print("\nπ¦ Building gallery_items...\n")
|
| 77 |
+
|
| 78 |
+
for i, v in enumerate(variations):
|
| 79 |
+
img = v["image"]
|
| 80 |
+
smi = v["smiles"]
|
| 81 |
+
|
| 82 |
+
print(f" #{i+1}:")
|
| 83 |
+
print(f" SMILES: {smi}")
|
| 84 |
+
print(f" Image type: {type(img)}")
|
| 85 |
+
try:
|
| 86 |
+
print(f" Image size: {img.size}")
|
| 87 |
+
except:
|
| 88 |
+
print(" β Could not read image size")
|
| 89 |
|
| 90 |
+
gallery_items.append((img, smi))
|
| 91 |
|
| 92 |
+
print("\n==============================")
|
| 93 |
+
print("π Returning data to UI")
|
| 94 |
+
print(f"Total gallery items: {len(gallery_items)}")
|
| 95 |
+
print("==============================\n")
|
| 96 |
|
| 97 |
+
# ------------------------------------------------------------
|
| 98 |
+
# Return to Gradio exactly as before
|
| 99 |
+
# ------------------------------------------------------------
|
| 100 |
+
return gallery_items, input_smiles, ""
|
| 101 |
|
| 102 |
# -------------------------------------------------------------
|
| 103 |
# Handle selection from the variations grid
|