andreamena commited on
Commit
52afbbc
·
verified ·
1 Parent(s): 709b20f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -14
app.py CHANGED
@@ -14,6 +14,7 @@ from rdkit import Chem, DataStructs
14
  from rdkit.Chem import Descriptors, rdFingerprintGenerator
15
  from rdkit.Chem import RDConfig
16
  from rdkit.Chem import Draw
 
17
  import sys
18
  import tempfile
19
 
@@ -389,28 +390,31 @@ def explain_polymer(smiles, property_name):
389
 
390
  # 2. Extract Bit Info
391
  bit_info = {}
392
- from rdkit.Chem import AllChem
393
- fp = AllChem.GetMorganFingerprintAsBitVect(mol, radius=2, nBits=2048, bitInfo=bit_info)
394
 
395
  # 3. Scientific Impact Calculation (Simulating SHAP based on bit presence)
396
  # We look at the top 5 bits actually present in this specific molecule
397
  on_bits = list(bit_info.keys())
 
 
 
 
398
 
399
- # We generate a deterministic but property-specific impact score for each bit
400
- np.random.seed(sum([ord(c) for c in clean_smiles]) + len(property_name))
401
- raw_impacts = np.random.uniform(-0.5, 0.6, len(on_bits))
402
 
403
  # Map impacts to Feature Names (using chemical descriptors as proxies)
404
  feature_names = ["Aromaticity", "Chain Branching", "Hydrogen Bonding", "Polar Surface Area", "Molecular Stiffness"]
405
  impact_data = pd.DataFrame({
406
  "Feature": feature_names,
407
- "Impact": np.random.choice(raw_impacts, 5)
408
  }).sort_values(by="Impact")
409
 
410
  # 4. Atom Highlights for the Image
411
- top_bits = [on_bits[i] for i in np.argsort(np.abs(raw_impacts))[-3:]]
412
  hit_atoms = []
413
- for bit in top_bits:
 
 
414
  for atom_idx, radius in bit_info[bit]:
415
  hit_atoms.append(atom_idx)
416
 
@@ -613,9 +617,9 @@ with gr.Blocks(title="PolymerDesignAI") as demo:
613
 
614
  csv_out = gr.File(label="Download Full Results")
615
 
616
- def on_gen_complete(num, mode, view, motifs, limit_aro, mw, progress=gr.Progress()):
617
  progress(0, desc="🔍 Initializing Generative AI...")
618
- df, path, fig = generate_and_screen(num, mode, view, motifs, limit_aro, mw)
619
 
620
  def get_dropdown_icon(status_text):
621
  if "Excellent" in status_text: return "🟢"
@@ -632,10 +636,9 @@ with gr.Blocks(title="PolymerDesignAI") as demo:
632
  img, bar = explain_polymer(first_smiles, "Strength")
633
 
634
  return (df, path, fig,
635
- gr.Dropdown(choices=dropdown_choices, value=first_choice),
636
- df, img, bar)
637
 
638
- def on_refine_start(seed_with_icon, num, mode, view, motifs, limit_aro, mw):
639
  if not seed_with_icon:
640
  raise gr.Error("Please select a candidate from the dropdown first!")
641
 
@@ -643,7 +646,7 @@ with gr.Blocks(title="PolymerDesignAI") as demo:
643
 
644
  gr.Info(f"Evolving molecules similar to: {seed[:15]}... while respecting constraints")
645
 
646
- df, path, fig = generate_and_screen(num, mode, view, motifs=motifs, limit_aro=limit_aro, mw=mw, seed=seed)
647
 
648
  # Use your new icon-logic for the refined dropdown results too
649
  def get_dropdown_icon(status_text):
@@ -671,6 +674,8 @@ with gr.Blocks(title="PolymerDesignAI") as demo:
671
 
672
  def clean_and_update_radar(choice, df):
673
  # Split by space and take the second part (the actual SMILES)
 
 
674
  smiles = choice.split(" ")[1] if choice and " " in choice else choice
675
  return update_radar_plot(smiles, df)
676
 
 
14
  from rdkit.Chem import Descriptors, rdFingerprintGenerator
15
  from rdkit.Chem import RDConfig
16
  from rdkit.Chem import Draw
17
+ from rdkit.Chem import AllChem
18
  import sys
19
  import tempfile
20
 
 
390
 
391
  # 2. Extract Bit Info
392
  bit_info = {}
393
+ fp = rdFingerprintGenerator.GetMorganGenerator(radius=2, fpSize=2048).GetFingerprint(mol, bitInfo=bit_info)
 
394
 
395
  # 3. Scientific Impact Calculation (Simulating SHAP based on bit presence)
396
  # We look at the top 5 bits actually present in this specific molecule
397
  on_bits = list(bit_info.keys())
398
+ if not on_bits: return None, None
399
+
400
+ seed_val = sum([ord(c) for c in clean_smiles]) + len(property_name)
401
+ np.random.seed(seed_val)
402
 
403
+ feature_names = ["Aromaticity", "Chain Branching", "Hydrogen Bonding", "Polar Surface Area", "Molecular Stiffness"]
404
+ impacts = np.random.uniform(-0.5, 0.6, 5)
 
405
 
406
  # Map impacts to Feature Names (using chemical descriptors as proxies)
407
  feature_names = ["Aromaticity", "Chain Branching", "Hydrogen Bonding", "Polar Surface Area", "Molecular Stiffness"]
408
  impact_data = pd.DataFrame({
409
  "Feature": feature_names,
410
+ "Impact": impacts
411
  }).sort_values(by="Impact")
412
 
413
  # 4. Atom Highlights for the Image
 
414
  hit_atoms = []
415
+ # Take up to 3 random 'on' bits to highlight for visual flair
416
+ sample_bits = random.sample(on_bits, min(3, len(on_bits)))
417
+ for bit in sample_bits:
418
  for atom_idx, radius in bit_info[bit]:
419
  hit_atoms.append(atom_idx)
420
 
 
617
 
618
  csv_out = gr.File(label="Download Full Results")
619
 
620
+ def on_gen_complete(num, mode, view, motifs, limit_aro, max_mw, progress=gr.Progress()):
621
  progress(0, desc="🔍 Initializing Generative AI...")
622
+ df, path, fig = generate_and_screen(num, mode, view, motifs, limit_aro, max_mw)
623
 
624
  def get_dropdown_icon(status_text):
625
  if "Excellent" in status_text: return "🟢"
 
636
  img, bar = explain_polymer(first_smiles, "Strength")
637
 
638
  return (df, path, fig,
639
+ gr.Dropdown(choices=dropdown_choices, value=first_choice), df, img, bar)
 
640
 
641
+ def on_refine_start(seed_with_icon, num, mode, view, motifs, limit_aro, max_mw):
642
  if not seed_with_icon:
643
  raise gr.Error("Please select a candidate from the dropdown first!")
644
 
 
646
 
647
  gr.Info(f"Evolving molecules similar to: {seed[:15]}... while respecting constraints")
648
 
649
+ df, path, fig = generate_and_screen(num, mode, view, motifs=motifs, limit_aro=limit_aro, max_mw=max_mw, seed=seed)
650
 
651
  # Use your new icon-logic for the refined dropdown results too
652
  def get_dropdown_icon(status_text):
 
674
 
675
  def clean_and_update_radar(choice, df):
676
  # Split by space and take the second part (the actual SMILES)
677
+ if not choice or df is None or df.empty:
678
+ return None
679
  smiles = choice.split(" ")[1] if choice and " " in choice else choice
680
  return update_radar_plot(smiles, df)
681