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

Fix autocomplete: use filterable Dropdown instead of Textbox for native autocomplete support

Browse files
Files changed (1) hide show
  1. app.py +12 -47
app.py CHANGED
@@ -368,26 +368,16 @@ smiles_interface = gr.Interface(
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
 
@@ -398,11 +388,15 @@ with gr.Blocks() as molecule_3d_block:
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)",
@@ -410,14 +404,6 @@ with gr.Blocks() as molecule_3d_block:
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")
@@ -425,27 +411,6 @@ with gr.Blocks() as molecule_3d_block:
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],
 
368
  description="Convert an input SMILES string to its canonical form.",
369
  )
370
 
 
 
 
 
 
 
 
 
 
 
371
  name_interface = gr.Interface(
372
  fn=name_to_smiles,
373
  inputs=gr.Textbox(
374
  label="Chemical Name",
375
+ placeholder="Enter chemical name (e.g., aspirin, caffeine, benzene)",
376
+ info="Enter the full chemical name"
377
  ),
378
  outputs=gr.Textbox(label="SMILES"),
379
  api_name="name_to_smiles",
380
+ description="Convert a chemical name to SMILES notation.",
381
  examples=[["aspirin"], ["caffeine"], ["benzene"], ["ethanol"], ["glucose"]],
382
  )
383
 
 
388
 
389
  with gr.Row():
390
  with gr.Column():
391
+ # Use Dropdown with allow_custom_value for autocomplete
392
+ chem_name_input = gr.Dropdown(
393
+ choices=COMMON_CHEMICALS,
394
+ label="Chemical Name - Type to Search",
395
+ info="Start typing to filter from 100+ chemicals",
396
+ allow_custom_value=True,
397
+ value=None,
398
+ interactive=True,
399
+ filterable=True
400
  )
401
  orbital_checkbox = gr.Checkbox(
402
  label="Show Electron Orbitals (s, p, d shapes)",
 
404
  info="Toggle to see s-orbitals (spheres), p-orbitals (dumbbells), and d-orbitals (cloverleaf) around atoms"
405
  )
406
  submit_btn = gr.Button("Generate 3D Molecule", variant="primary")
 
 
 
 
 
 
 
 
407
 
408
  with gr.Row():
409
  html_output = gr.HTML(label="2D Structure")
 
411
  with gr.Row():
412
  plot_output = gr.Plot(label="3D Interactive Molecule Viewer - Rotate and Zoom!")
413
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
414
  submit_btn.click(
415
  fn=name_to_3d_molecule,
416
  inputs=[chem_name_input, orbital_checkbox],