Jay Prajapati commited on
Commit
b8ffacb
·
1 Parent(s): 78175af
Files changed (5) hide show
  1. .gitignore +174 -0
  2. app.py +49 -0
  3. requirements.txt +5 -0
  4. src/model.py +61 -0
  5. src/predict.py +32 -0
.gitignore ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ #uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ #poetry.lock
109
+
110
+ # pdm
111
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
112
+ #pdm.lock
113
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
114
+ # in version control.
115
+ # https://pdm.fming.dev/latest/usage/project/#working-with-version-control
116
+ .pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
121
+ __pypackages__/
122
+
123
+ # Celery stuff
124
+ celerybeat-schedule
125
+ celerybeat.pid
126
+
127
+ # SageMath parsed files
128
+ *.sage.py
129
+
130
+ # Environments
131
+ .env
132
+ .venv
133
+ env/
134
+ venv/
135
+ ENV/
136
+ env.bak/
137
+ venv.bak/
138
+
139
+ # Spyder project settings
140
+ .spyderproject
141
+ .spyproject
142
+
143
+ # Rope project settings
144
+ .ropeproject
145
+
146
+ # mkdocs documentation
147
+ /site
148
+
149
+ # mypy
150
+ .mypy_cache/
151
+ .dmypy.json
152
+ dmypy.json
153
+
154
+ # Pyre type checker
155
+ .pyre/
156
+
157
+ # pytype static type analyzer
158
+ .pytype/
159
+
160
+ # Cython debug symbols
161
+ cython_debug/
162
+
163
+ # PyCharm
164
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
165
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
166
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
167
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
168
+ #.idea/
169
+
170
+ # Ruff stuff:
171
+ .ruff_cache/
172
+
173
+ # PyPI configuration file
174
+ .pypirc
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from src.predict import predict
3
+
4
+ def update_amino_acid(safety_param, amino_acid):
5
+ if safety_param == "KIBA Score":
6
+ return gr.update(visible=True, value=amino_acid)
7
+ else:
8
+ return gr.update(visible=False)
9
+
10
+ with gr.Blocks() as demo:
11
+ with gr.Row():
12
+ with gr.Column():
13
+ safety_param = gr.Dropdown(["Skin Reaction", "KIBA Score", "Liver Safety"])
14
+ drug_name = gr.Textbox(label="Drug Name")
15
+ amino_acid = gr.Textbox(label="Amino Acid Name", visible=False)
16
+ with gr.Column():
17
+ safety_report = gr.Textbox(label="Drug Analysis")
18
+
19
+ analyze_drug_btn = gr.Button("Get Drug Analysis")
20
+
21
+
22
+ analyze_drug_btn.click(
23
+ fn=predict,
24
+ inputs=[safety_param, drug_name, amino_acid],
25
+ outputs=safety_report,
26
+ )
27
+
28
+ safety_param.change(
29
+ fn=update_amino_acid,
30
+ inputs=[safety_param, amino_acid],
31
+ outputs=amino_acid,
32
+ )
33
+
34
+ examples = [
35
+ ["KIBA Score", "C1=CC=C(C=C1)C(=O)N", "RPDFCLEPPYTGPCKARIIRYFYNAKAGLCQTFVYGGCRAKRNNFKSAEDCMRTCGGA"],
36
+ ["KIBA Score", "C1=CC=C(C=C1)C(=O)N", "Humanin"],
37
+ ["Liver Safety", "benzamide", None],
38
+ ["Liver Safety", "CN1C(=O)CN=C(C2=CCCCC2)c2cc(Cl)ccc21", None],
39
+ ["Skin Reaction", "Amoxicillin", None],
40
+ ["Skin Reaction", "Atorvastatin", None]
41
+ ]
42
+
43
+ gr.Examples(
44
+ examples=examples,
45
+ inputs=[safety_param, drug_name, amino_acid],
46
+ )
47
+
48
+ if __name__ == "__main__":
49
+ demo.queue(max_size=10).launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio
2
+ accelerate
3
+ python-dotenv
4
+ huggingface_hub
5
+ transformers==4.48.3
src/model.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ from dotenv import load_dotenv
4
+ from huggingface_hub import hf_hub_download
5
+ from transformers import AutoTokenizer, AutoModelForCausalLM
6
+
7
+ load_dotenv()
8
+ HF_TOKEN = os.environ.get("HF_TOKEN")
9
+
10
+ tdc_prompts_filepath = hf_hub_download(
11
+ repo_id="google/txgemma-2b-predict",
12
+ filename="tdc_prompts.json",
13
+ )
14
+
15
+ tokenizer = AutoTokenizer.from_pretrained(
16
+ "google/txgemma-2b-predict",
17
+ token = HF_TOKEN
18
+ )
19
+
20
+ model = AutoModelForCausalLM.from_pretrained(
21
+ "google/txgemma-2b-predict",
22
+ device_map = "auto",
23
+ token = HF_TOKEN
24
+ )
25
+
26
+ with open(tdc_prompts_filepath, "r") as f:
27
+ tdc_prompts = json.load(f)
28
+
29
+ def txgemma_predict(prompt):
30
+ input_ids = tokenizer(prompt, return_tensors="pt").to("cpu")
31
+ outputs = model.generate(**input_ids)
32
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
33
+
34
+ def predict_kiba_score(drug_smile, amino_acid):
35
+ TDC_PROMPT = tdc_prompts["KIBA"].replace("{Drug SMILES}", drug_smile).replace("{Target amino acid sequence}", amino_acid)
36
+ response = txgemma_predict(TDC_PROMPT)
37
+ return response.split("Answer:")[1].strip()
38
+
39
+ def predict(task, drug_smile, amino_acid=None):
40
+ if task == "KIBA Score":
41
+ if amino_acid is None:
42
+ raise ValueError("amino_acid parameter is required for KIBA task")
43
+ kiba_score = predict_kiba_score(drug_smile, amino_acid)
44
+ return f"{kiba_score} Binding Affinity On Scale of 0-1000"
45
+
46
+
47
+ if task == "Skin Reaction":
48
+ TDC_PROMPT = tdc_prompts["Skin_Reaction"].replace("{Drug SMILES}", drug_smile)
49
+ response = txgemma_predict(TDC_PROMPT).split("Answer:")[1].strip()
50
+
51
+ if "(A)" in response: response = f"{drug_smile} does not cause a skin reaction!"
52
+ elif "(B)" in response: response = f"{drug_smile} causes a skin reaction!"
53
+
54
+ return response
55
+
56
+ if task == "Liver Safety":
57
+ TDC_PROMPT = tdc_prompts["DILI"].replace("{Drug SMILES}", drug_smile)
58
+ response = txgemma_predict(TDC_PROMPT).split("Answer:")[1].strip()
59
+
60
+ if "(A)" in response: response = f"{drug_smile} does not damage a liver!"
61
+ elif "(B)" in response: response = f"{drug_smile} can damage a liver!"
src/predict.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from src.model import tdc_prompts, txgemma_predict
2
+
3
+ def predict_kiba_score(drug_smile, amino_acid):
4
+ TDC_PROMPT = tdc_prompts["KIBA"].replace("{Drug SMILES}", drug_smile).replace("{Target amino acid sequence}", amino_acid)
5
+ response = txgemma_predict(TDC_PROMPT)
6
+ return response.split("Answer:")[1].strip()
7
+
8
+ def predict(task, drug_smile, amino_acid=None):
9
+ if task == "KIBA Score":
10
+ if amino_acid is None:
11
+ raise ValueError("amino_acid parameter is required for KIBA task")
12
+ kiba_score = predict_kiba_score(drug_smile, amino_acid)
13
+ return f"{kiba_score} Binding Affinity On Scale of 0-1000"
14
+
15
+
16
+ if task == "Skin Reaction":
17
+ TDC_PROMPT = tdc_prompts["Skin_Reaction"].replace("{Drug SMILES}", drug_smile)
18
+ response = txgemma_predict(TDC_PROMPT).split("Answer:")[1].strip()
19
+
20
+ if "(A)" in response: response = f"{drug_smile} does not cause a skin reaction!"
21
+ elif "(B)" in response: response = f"{drug_smile} causes a skin reaction!"
22
+
23
+ return response
24
+
25
+ if task == "Liver Safety":
26
+ TDC_PROMPT = tdc_prompts["DILI"].replace("{Drug SMILES}", drug_smile)
27
+ response = txgemma_predict(TDC_PROMPT).split("Answer:")[1].strip()
28
+
29
+ if "(A)" in response: response = f"{drug_smile} does not damage a liver!"
30
+ elif "(B)" in response: response = f"{drug_smile} can damage a liver!"
31
+
32
+ return response