Update app.py
Browse files
app.py
CHANGED
@@ -438,6 +438,11 @@ def predict_iupac(smiles_string):
|
|
438 |
"""
|
439 |
Performs SMILES to IUPAC translation using the loaded model and greedy decoding.
|
440 |
"""
|
|
|
|
|
|
|
|
|
|
|
441 |
global model, smiles_tokenizer, iupac_tokenizer, device, config
|
442 |
|
443 |
if not all([model, smiles_tokenizer, iupac_tokenizer, device, config]):
|
@@ -515,14 +520,36 @@ Translation uses **greedy decoding** (picks the most likely next word at each st
|
|
515 |
**Note:** Model loaded on **{str(device).upper() if device else "N/A"}**. Performance may vary. Check `config.json` in the repo for model details.
|
516 |
"""
|
517 |
|
518 |
-
# Input component
|
519 |
-
smiles_input = gr.Textbox(
|
520 |
-
label="SMILES String",
|
521 |
-
placeholder="Enter SMILES string here (e.g., CCO for Ethanol)",
|
522 |
-
lines=1,
|
523 |
-
)
|
524 |
|
525 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
526 |
# Output component
|
527 |
output_text = gr.Textbox(
|
528 |
label="Predicted IUPAC Name",
|
@@ -530,19 +557,7 @@ output_text = gr.Textbox(
|
|
530 |
show_copy_button=True, # Reduced lines slightly
|
531 |
)
|
532 |
|
533 |
-
|
534 |
-
try:
|
535 |
-
return CanonSmiles(smi)
|
536 |
-
except Exception:
|
537 |
-
return ""
|
538 |
-
|
539 |
-
|
540 |
-
smiles_input.change(
|
541 |
-
fn=canon_smiles,
|
542 |
-
inputs=smiles_input,
|
543 |
-
outputs=smiles_input,
|
544 |
-
show_progress=False,
|
545 |
-
)
|
546 |
# Create the interface instance
|
547 |
iface = gr.Interface(
|
548 |
fn=predict_iupac, # The function to call
|
|
|
438 |
"""
|
439 |
Performs SMILES to IUPAC translation using the loaded model and greedy decoding.
|
440 |
"""
|
441 |
+
try:
|
442 |
+
smiles_string = CanonSmiles(smiles_string)
|
443 |
+
except Exception as e:
|
444 |
+
logging.error(f"Error during SMILES canonicalization: {e}", exc_info=True)
|
445 |
+
return f"Error: {e}"
|
446 |
global model, smiles_tokenizer, iupac_tokenizer, device, config
|
447 |
|
448 |
if not all([model, smiles_tokenizer, iupac_tokenizer, device, config]):
|
|
|
520 |
**Note:** Model loaded on **{str(device).upper() if device else "N/A"}**. Performance may vary. Check `config.json` in the repo for model details.
|
521 |
"""
|
522 |
|
|
|
|
|
|
|
|
|
|
|
|
|
523 |
|
524 |
|
525 |
+
# Replace your Interface code with this:
|
526 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="cyan")) as iface:
|
527 |
+
gr.Markdown(f"# {title}")
|
528 |
+
gr.Markdown(description)
|
529 |
+
|
530 |
+
with gr.Row():
|
531 |
+
with gr.Column():
|
532 |
+
smiles_input = gr.Textbox(
|
533 |
+
label="SMILES String",
|
534 |
+
placeholder="Enter SMILES string here (e.g., CCO for Ethanol)",
|
535 |
+
lines=1,
|
536 |
+
)
|
537 |
+
|
538 |
+
# Add a button for manual triggering
|
539 |
+
submit_btn = gr.Button("Translate")
|
540 |
+
|
541 |
+
with gr.Column():
|
542 |
+
output_text = gr.Textbox(
|
543 |
+
label="Predicted IUPAC Name",
|
544 |
+
lines=3,
|
545 |
+
show_copy_button=True,
|
546 |
+
)
|
547 |
+
|
548 |
+
# Connect the events properly
|
549 |
+
submit_btn.click(fn=predict_iupac, inputs=smiles_input, outputs=output_text)
|
550 |
+
|
551 |
+
# If you still want change event (auto-translation as user types)
|
552 |
+
smiles_input.change(fn=predict_iupac, inputs=smiles_input, outputs=output_text)
|
553 |
# Output component
|
554 |
output_text = gr.Textbox(
|
555 |
label="Predicted IUPAC Name",
|
|
|
557 |
show_copy_button=True, # Reduced lines slightly
|
558 |
)
|
559 |
|
560 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
561 |
# Create the interface instance
|
562 |
iface = gr.Interface(
|
563 |
fn=predict_iupac, # The function to call
|