Vaishnav14220 commited on
Commit
ea475f3
·
1 Parent(s): 7357bca

Add autocomplete for chemical names with 100+ common chemicals, shows top 10 matches from 1st character

Browse files
Files changed (1) hide show
  1. app.py +123 -28
app.py CHANGED
@@ -6,6 +6,32 @@ import cirpy
6
 
7
  # RDKit API with multiple endpoints
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  def _mol_from_smiles(smiles: str):
11
  mol = Chem.MolFromSmiles(smiles)
@@ -342,15 +368,108 @@ smiles_interface = gr.Interface(
342
  description="Convert an input SMILES string to its canonical form.",
343
  )
344
 
 
 
 
 
 
 
 
 
 
 
345
  name_interface = gr.Interface(
346
  fn=name_to_smiles,
347
- inputs=gr.Textbox(label="Chemical Name", placeholder="e.g., aspirin, caffeine, benzene"),
 
 
 
 
348
  outputs=gr.Textbox(label="SMILES"),
349
  api_name="name_to_smiles",
350
- description="Convert a chemical name to SMILES notation.",
351
- examples=[["aspirin"], ["caffeine"], ["benzene"], ["ethanol"]],
352
  )
353
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
354
  mw_interface = gr.Interface(
355
  fn=molecular_weight,
356
  inputs=gr.Textbox(label="SMILES"),
@@ -375,33 +494,9 @@ tpsa_interface = gr.Interface(
375
  description="Calculate the topological polar surface area (TPSA).",
376
  )
377
 
378
- molecule_3d_interface = gr.Interface(
379
- fn=name_to_3d_molecule,
380
- inputs=[
381
- gr.Textbox(label="Chemical Name", placeholder="e.g., benzene, aspirin, caffeine, glucose"),
382
- gr.Checkbox(label="Show Electron Orbitals (s, p, d shapes)", value=False, info="Toggle to see s-orbitals (spheres), p-orbitals (dumbbells), and d-orbitals (cloverleaf) around atoms")
383
- ],
384
- outputs=[
385
- gr.HTML(label="2D Structure"),
386
- gr.Plot(label="3D Interactive Molecule Viewer - Rotate and Zoom!"),
387
- gr.Textbox(label="3D SDF Content (Optional - for external viewers)", lines=10, max_lines=20, visible=False)
388
- ],
389
- api_name="name_to_molecule",
390
- description="🔬 View 2D structure and interactive 3D molecule with atom labels. Click and drag to rotate, scroll to zoom! ✨ Enable 'Show Electron Orbitals' to see s-orbitals (spheres), p-orbitals (dumbbells), and d-orbitals (cloverleaf shapes).",
391
- cache_examples=False,
392
- examples=[
393
- ["benzene", False],
394
- ["benzene", True],
395
- ["water", True],
396
- ["aspirin", False],
397
- ["caffeine", True],
398
- ["glucose", False]
399
- ],
400
- )
401
-
402
 
403
  demo = gr.TabbedInterface(
404
- [name_interface, molecule_3d_interface, smiles_interface, mw_interface, logp_interface, tpsa_interface],
405
  [
406
  "Name to SMILES",
407
  "Molecule Viewer",
 
6
 
7
  # RDKit API with multiple endpoints
8
 
9
+ # Common chemical names for autocomplete
10
+ COMMON_CHEMICALS = [
11
+ "acetaldehyde", "acetic acid", "acetone", "acetonitrile", "acetylsalicylic acid",
12
+ "adenine", "alanine", "ammonia", "amphetamine", "aniline", "anthracene", "arginine",
13
+ "aspirin", "atrazine", "azulene",
14
+ "benzaldehyde", "benzene", "benzoic acid", "benzophenone", "biphenyl", "butane", "butanol",
15
+ "caffeine", "camphor", "capsaicin", "carbazole", "carbon dioxide", "carbon monoxide",
16
+ "catechol", "cellulose", "chlorobenzene", "chloroform", "cholesterol", "citric acid",
17
+ "cocaine", "codeine", "cortisol", "coumarin", "cresol", "cumene", "cyanide", "cyclohexane",
18
+ "cysteine", "cytosine",
19
+ "decane", "dextrose", "diazepam", "dichloromethane", "diethyl ether", "dimethylformamide",
20
+ "dimethyl sulfoxide", "dioxane", "dopamine", "ethane", "ethanol", "ethyl acetate",
21
+ "ethylbenzene", "ethylene", "ethylene glycol",
22
+ "formaldehyde", "formic acid", "fructose", "furan", "galactose", "glucose", "glutamic acid",
23
+ "glutamine", "glycerol", "glycine", "guanine", "heptane", "hexane", "histidine", "hydrazine",
24
+ "hydrogen peroxide", "hydroquinone", "ibuprofen", "imidazole", "indole", "isobutane",
25
+ "isoleucine", "isopropanol", "lactose", "leucine", "limonene", "lysine", "maltose", "menthol",
26
+ "methane", "methanol", "methionine", "methyl acetate", "methylamine", "morphine", "naphthalene",
27
+ "nicotine", "nitrobenzene", "octane", "paracetamol", "pentane", "phenol", "phenylalanine",
28
+ "phthalic acid", "proline", "propane", "propanol", "pyridine", "pyrimidine", "pyrrole",
29
+ "pyruvic acid", "quinoline", "resorcinol", "ribose", "salicylic acid", "serine", "serotonin",
30
+ "styrene", "sucrose", "terephthalic acid", "testosterone", "tetrahydrofuran", "thiophene",
31
+ "threonine", "thymine", "toluene", "tryptophan", "tyrosine", "uracil", "urea", "valine",
32
+ "vanillin", "vinyl chloride", "water", "xylene", "xylose"
33
+ ]
34
+
35
 
36
  def _mol_from_smiles(smiles: str):
37
  mol = Chem.MolFromSmiles(smiles)
 
368
  description="Convert an input SMILES string to its canonical form.",
369
  )
370
 
371
+ # Function to get autocomplete suggestions
372
+ def get_suggestions(text):
373
+ """Return top 10 matching chemical names"""
374
+ if not text or len(text) < 1:
375
+ return COMMON_CHEMICALS[:10]
376
+
377
+ text_lower = text.lower()
378
+ matches = [chem for chem in COMMON_CHEMICALS if chem.lower().startswith(text_lower)]
379
+ return matches[:10] if matches else COMMON_CHEMICALS[:10]
380
+
381
  name_interface = gr.Interface(
382
  fn=name_to_smiles,
383
+ inputs=gr.Textbox(
384
+ label="Chemical Name",
385
+ placeholder="Start typing... (e.g., asp, caf, ben)",
386
+ info="Type at least 1 character to see suggestions"
387
+ ),
388
  outputs=gr.Textbox(label="SMILES"),
389
  api_name="name_to_smiles",
390
+ description="Convert a chemical name to SMILES notation. Start typing to see autocomplete suggestions!",
391
+ examples=[["aspirin"], ["caffeine"], ["benzene"], ["ethanol"], ["glucose"]],
392
  )
393
 
394
+ # Create Blocks interface for molecule viewer with autocomplete
395
+ with gr.Blocks() as molecule_3d_block:
396
+ gr.Markdown("## 🔬 3D Molecule Viewer with Electron Orbitals")
397
+ gr.Markdown("Enter a chemical name to view its 2D and 3D structure. Toggle orbitals to see s, p, and d orbital shapes!")
398
+
399
+ with gr.Row():
400
+ with gr.Column():
401
+ chem_name_input = gr.Textbox(
402
+ label="Chemical Name",
403
+ placeholder="Start typing... (e.g., benzene, aspirin, water)",
404
+ info="Type 1+ characters to see suggestions",
405
+ elem_id="chem_name"
406
+ )
407
+ orbital_checkbox = gr.Checkbox(
408
+ label="Show Electron Orbitals (s, p, d shapes)",
409
+ value=False,
410
+ info="Toggle to see s-orbitals (spheres), p-orbitals (dumbbells), and d-orbitals (cloverleaf) around atoms"
411
+ )
412
+ submit_btn = gr.Button("Generate 3D Molecule", variant="primary")
413
+
414
+ # Autocomplete dropdown
415
+ suggestions_dropdown = gr.Dropdown(
416
+ choices=COMMON_CHEMICALS[:10],
417
+ label="Quick Select (or type above)",
418
+ interactive=True,
419
+ allow_custom_value=True
420
+ )
421
+
422
+ with gr.Row():
423
+ html_output = gr.HTML(label="2D Structure")
424
+
425
+ with gr.Row():
426
+ plot_output = gr.Plot(label="3D Interactive Molecule Viewer - Rotate and Zoom!")
427
+
428
+ # Update suggestions as user types
429
+ def update_suggestions(text):
430
+ suggestions = get_suggestions(text)
431
+ return gr.Dropdown(choices=suggestions)
432
+
433
+ # Update text input when dropdown selection changes
434
+ def update_from_dropdown(selected):
435
+ return selected
436
+
437
+ chem_name_input.change(
438
+ fn=update_suggestions,
439
+ inputs=[chem_name_input],
440
+ outputs=[suggestions_dropdown]
441
+ )
442
+
443
+ suggestions_dropdown.change(
444
+ fn=update_from_dropdown,
445
+ inputs=[suggestions_dropdown],
446
+ outputs=[chem_name_input]
447
+ )
448
+
449
+ submit_btn.click(
450
+ fn=name_to_3d_molecule,
451
+ inputs=[chem_name_input, orbital_checkbox],
452
+ outputs=[html_output, plot_output, gr.Textbox(visible=False)],
453
+ api_name="name_to_molecule"
454
+ )
455
+
456
+ # Examples
457
+ gr.Examples(
458
+ examples=[
459
+ ["benzene", False],
460
+ ["benzene", True],
461
+ ["water", True],
462
+ ["aspirin", False],
463
+ ["caffeine", True],
464
+ ["glucose", False],
465
+ ["dopamine", True]
466
+ ],
467
+ inputs=[chem_name_input, orbital_checkbox],
468
+ outputs=[html_output, plot_output],
469
+ fn=name_to_3d_molecule,
470
+ cache_examples=False
471
+ )
472
+
473
  mw_interface = gr.Interface(
474
  fn=molecular_weight,
475
  inputs=gr.Textbox(label="SMILES"),
 
494
  description="Calculate the topological polar surface area (TPSA).",
495
  )
496
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
497
 
498
  demo = gr.TabbedInterface(
499
+ [name_interface, molecule_3d_block, smiles_interface, mw_interface, logp_interface, tpsa_interface],
500
  [
501
  "Name to SMILES",
502
  "Molecule Viewer",