File size: 1,251 Bytes
465c18c
10efe81
 
 
 
 
 
 
 
 
465c18c
 
10efe81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import gradio as gr
import os
from rdkit import Chem
from motif_sample import demo
import tempfile
from rdkit.Chem import AllChem
import numpy as np
from PIL import Image
import io
from rdkit.Chem import Draw


# Function to serve the file via Gradio
def create_and_return_sdf(Protein_index: int):
    # Ensure input is an integer, as it's coming from the interface.
    number = Protein_index
    number = int(number)

    # Generate SDF file (you'll replace this with your actual logic)
    sdf_filename = demo(number)

    suppl = Chem.SDMolSupplier(sdf_filename)
    mol = next(suppl)
    # AllChem.UFFOptimizeMolecule(mol)
    mol = Chem.MolFromSmiles(Chem.MolToSmiles(mol))

    for atom in mol.GetAtoms():
        atom.SetAtomMapNum(0)

    mol_image = Draw.MolToImage(mol)

    np_image = np.array(mol_image)
    np_image = np_image[:, :, :3]

    return np_image, sdf_filename


# Define Gradio interface
iface = gr.Interface(
    fn=create_and_return_sdf,
    inputs="text",
    outputs=[
        gr.outputs.Image(type="numpy", label="Molecule Image"),
        gr.outputs.File(label="Download SDF")
    ],
    live=False  # The function should only be called when the user submits the form
)

# Launch the interface
iface.launch(share=True)