AdrianM0 commited on
Commit
3369de4
·
verified ·
1 Parent(s): b3f7b39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -34
app.py CHANGED
@@ -244,7 +244,7 @@ def load_model_and_tokenizers():
244
  logging.warning(
245
  "CUDA is available, but forcing CPU for Gradio app simplicity. Modify if GPU is intended."
246
  )
247
- device = torch.device("cpu") # Uncomment if GPU is intended
248
  # device = torch.device("cuda")
249
  # logging.info("CUDA available, using GPU.")
250
  else:
@@ -521,53 +521,52 @@ Translation uses **greedy decoding** (picks the most likely next word at each st
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",
556
- lines=3,
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
564
- inputs=smiles_input, # Input component directly
565
- outputs=output_text, # Output component
566
- title=title,
567
- description=description,
568
- allow_flagging="never",
569
- theme=gr.themes.Soft(primary_hue="blue", secondary_hue="cyan"),
570
- )
571
  # --- Launch the App ---
 
572
  if __name__ == "__main__":
573
- iface.launch()
 
 
 
244
  logging.warning(
245
  "CUDA is available, but forcing CPU for Gradio app simplicity. Modify if GPU is intended."
246
  )
247
+ device = torch.device("cuda") # Uncomment if you want to use GPU
248
  # device = torch.device("cuda")
249
  # logging.info("CUDA available, using GPU.")
250
  else:
 
521
  """
522
 
523
 
524
+ # Use gr.Blocks exclusively
525
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="cyan")) as iface: # 'iface' is created here
 
526
  gr.Markdown(f"# {title}")
527
  gr.Markdown(description)
528
+
529
  with gr.Row():
530
+ with gr.Column(scale=1): # Adjust scale if needed
531
  smiles_input = gr.Textbox(
532
  label="SMILES String",
533
  placeholder="Enter SMILES string here (e.g., CCO for Ethanol)",
534
  lines=1,
535
+ # interactive=True # Default is True
536
  )
 
 
537
  submit_btn = gr.Button("Translate")
538
+
539
+ with gr.Column(scale=2): # Give output more space if desired
540
  output_text = gr.Textbox(
541
  label="Predicted IUPAC Name",
542
+ lines=5, # Increased lines slightly
543
  show_copy_button=True,
544
+ # interactive=False # Output shouldn't be user-editable
545
  )
 
 
 
 
 
 
 
 
 
 
 
 
546
 
547
+ # --- Define Event Listeners INSIDE the gr.Blocks context ---
548
+ # When the button is clicked, call predict_iupac
549
+ submit_btn.click(
550
+ fn=predict_iupac,
551
+ inputs=smiles_input,
552
+ outputs=output_text,
553
+ api_name="translate_smiles" # Optional: name for API endpoint
554
+ )
555
+
556
+ # Optional: Trigger prediction when text changes (can be resource-intensive)
557
+ # If you uncomment this, consider adding a debounce or throttle if using Gradio >= 3.20
558
+ # smiles_input.change(fn=predict_iupac, inputs=smiles_input, outputs=output_text)
559
+
560
+ # Optional: Trigger prediction when text is submitted (e.g., pressing Enter)
561
+ smiles_input.submit(
562
+ fn=predict_iupac,
563
+ inputs=smiles_input,
564
+ outputs=output_text
565
+ )
566
 
 
 
 
 
 
 
 
 
 
 
567
  # --- Launch the App ---
568
+ # The 'iface' variable is already defined by the 'with gr.Blocks(...)' statement
569
  if __name__ == "__main__":
570
+ # You can add server_name="0.0.0.0" if running in Docker/Spaces
571
+ # and share=True if you want a public link (usually handled by Spaces automatically)
572
+ iface.launch()