Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,3 @@
|
|
1 |
-
# التثبيت (لو مش مثبت)
|
2 |
-
!pip install rdkit-pypi py3Dmol transformers selfies biopython gradio -q
|
3 |
-
|
4 |
-
# الاستيراد
|
5 |
from Bio import PDB
|
6 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
7 |
from rdkit import Chem
|
@@ -18,18 +14,15 @@ torch.manual_seed(int(time.time()))
|
|
18 |
if torch.cuda.is_available():
|
19 |
torch.cuda.manual_seed_all(int(time.time()))
|
20 |
|
21 |
-
# تحميل النموذج
|
22 |
model_name = "ncfrey/ChemGPT-1.2B"
|
23 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
24 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
25 |
|
26 |
-
# تحميل ملف PDB
|
27 |
def load_pdb(file_obj):
|
28 |
parser = PDB.PDBParser(QUIET=True)
|
29 |
structure = parser.get_structure('protein', file_obj)
|
30 |
return structure
|
31 |
|
32 |
-
# عرض البروتين ثلاثي الأبعاد
|
33 |
def get_protein_3d_html(pdb_str):
|
34 |
view = py3Dmol.view(width=600, height=400)
|
35 |
view.addModel(pdb_str, "pdb")
|
@@ -37,7 +30,6 @@ def get_protein_3d_html(pdb_str):
|
|
37 |
view.zoomTo()
|
38 |
return view._make_html()
|
39 |
|
40 |
-
# تنظيف وتحويل SELFIES إلى SMILES
|
41 |
def clean_and_decode_selfies(raw_output):
|
42 |
tokens = re.findall(r'\[[^\[\]]+\]', raw_output)
|
43 |
valid_tokens = [t for t in tokens if all(x not in t for x in ['Branch', 'Ring', 'expl'])]
|
@@ -50,7 +42,6 @@ def clean_and_decode_selfies(raw_output):
|
|
50 |
except:
|
51 |
return None
|
52 |
|
53 |
-
# توليد SMILES
|
54 |
def generate_multiple_valid_smiles(prompt, n=10, max_length=100):
|
55 |
valid_smiles = set()
|
56 |
tries = 0
|
@@ -71,15 +62,9 @@ def generate_multiple_valid_smiles(prompt, n=10, max_length=100):
|
|
71 |
tries += 1
|
72 |
return list(valid_smiles)
|
73 |
|
74 |
-
# الوظيفة الرئيسية
|
75 |
def generate_from_pdb(pdb_file):
|
76 |
try:
|
77 |
-
|
78 |
-
pdb_str = pdb_file if isinstance(pdb_file, str) else pdb_file.decode('utf-8', errors='ignore')
|
79 |
-
else:
|
80 |
-
pdb_bytes = pdb_file.read()
|
81 |
-
pdb_str = pdb_bytes.decode('utf-8', errors='ignore')
|
82 |
-
|
83 |
if len(pdb_str.strip()) == 0:
|
84 |
return "❌ الملف فارغ أو غير صالح", None, None
|
85 |
|
@@ -90,7 +75,6 @@ def generate_from_pdb(pdb_file):
|
|
90 |
return f"❌ خطأ أثناء تحليل ملف PDB:\n{str(e)}", None, None
|
91 |
|
92 |
html_3d = get_protein_3d_html(pdb_str)
|
93 |
-
|
94 |
prompt = "Generate a molecule in SELFIES that binds to the mutated KRAS protein"
|
95 |
smiles_list = generate_multiple_valid_smiles(prompt, n=10)
|
96 |
|
@@ -103,17 +87,14 @@ def generate_from_pdb(pdb_file):
|
|
103 |
f.write(smiles_txt)
|
104 |
|
105 |
return "✅ تم توليد المركبات بنجاح", html_3d, smiles_file_path
|
106 |
-
|
107 |
except Exception as e:
|
108 |
return f"❌ حدث خطأ:\n{str(e)}", None, None
|
109 |
|
110 |
-
# CSS لتجميل الواجهة
|
111 |
css = """
|
112 |
body {background-color: #f0f9ff;}
|
113 |
h1 {color: #004d66; text-align: center;}
|
114 |
"""
|
115 |
|
116 |
-
# واجهة Gradio
|
117 |
with gr.Blocks(css=css) as demo:
|
118 |
gr.Markdown("<h1>🔬 Drug-like Molecule Generation from PDB using ChemGPT</h1>")
|
119 |
gr.Markdown("🧪 Upload a PDB file containing mutations in the KRAS protein. The system will generate suitable SMILES drug candidates.")
|
@@ -123,10 +104,6 @@ with gr.Blocks(css=css) as demo:
|
|
123 |
status = gr.Textbox(label="📢 Status")
|
124 |
view3d = gr.HTML(label="🧬 3D Structure Viewer")
|
125 |
file_output = gr.File(label="📄 Download SMILES File")
|
126 |
-
run_btn.click(
|
127 |
-
fn=generate_from_pdb,
|
128 |
-
inputs=pdb_input,
|
129 |
-
outputs=[status, view3d, file_output]
|
130 |
-
)
|
131 |
|
132 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
1 |
from Bio import PDB
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
from rdkit import Chem
|
|
|
14 |
if torch.cuda.is_available():
|
15 |
torch.cuda.manual_seed_all(int(time.time()))
|
16 |
|
|
|
17 |
model_name = "ncfrey/ChemGPT-1.2B"
|
18 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
19 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
20 |
|
|
|
21 |
def load_pdb(file_obj):
|
22 |
parser = PDB.PDBParser(QUIET=True)
|
23 |
structure = parser.get_structure('protein', file_obj)
|
24 |
return structure
|
25 |
|
|
|
26 |
def get_protein_3d_html(pdb_str):
|
27 |
view = py3Dmol.view(width=600, height=400)
|
28 |
view.addModel(pdb_str, "pdb")
|
|
|
30 |
view.zoomTo()
|
31 |
return view._make_html()
|
32 |
|
|
|
33 |
def clean_and_decode_selfies(raw_output):
|
34 |
tokens = re.findall(r'\[[^\[\]]+\]', raw_output)
|
35 |
valid_tokens = [t for t in tokens if all(x not in t for x in ['Branch', 'Ring', 'expl'])]
|
|
|
42 |
except:
|
43 |
return None
|
44 |
|
|
|
45 |
def generate_multiple_valid_smiles(prompt, n=10, max_length=100):
|
46 |
valid_smiles = set()
|
47 |
tries = 0
|
|
|
62 |
tries += 1
|
63 |
return list(valid_smiles)
|
64 |
|
|
|
65 |
def generate_from_pdb(pdb_file):
|
66 |
try:
|
67 |
+
pdb_str = pdb_file.read().decode('utf-8', errors='ignore')
|
|
|
|
|
|
|
|
|
|
|
68 |
if len(pdb_str.strip()) == 0:
|
69 |
return "❌ الملف فارغ أو غير صالح", None, None
|
70 |
|
|
|
75 |
return f"❌ خطأ أثناء تحليل ملف PDB:\n{str(e)}", None, None
|
76 |
|
77 |
html_3d = get_protein_3d_html(pdb_str)
|
|
|
78 |
prompt = "Generate a molecule in SELFIES that binds to the mutated KRAS protein"
|
79 |
smiles_list = generate_multiple_valid_smiles(prompt, n=10)
|
80 |
|
|
|
87 |
f.write(smiles_txt)
|
88 |
|
89 |
return "✅ تم توليد المركبات بنجاح", html_3d, smiles_file_path
|
|
|
90 |
except Exception as e:
|
91 |
return f"❌ حدث خطأ:\n{str(e)}", None, None
|
92 |
|
|
|
93 |
css = """
|
94 |
body {background-color: #f0f9ff;}
|
95 |
h1 {color: #004d66; text-align: center;}
|
96 |
"""
|
97 |
|
|
|
98 |
with gr.Blocks(css=css) as demo:
|
99 |
gr.Markdown("<h1>🔬 Drug-like Molecule Generation from PDB using ChemGPT</h1>")
|
100 |
gr.Markdown("🧪 Upload a PDB file containing mutations in the KRAS protein. The system will generate suitable SMILES drug candidates.")
|
|
|
104 |
status = gr.Textbox(label="📢 Status")
|
105 |
view3d = gr.HTML(label="🧬 3D Structure Viewer")
|
106 |
file_output = gr.File(label="📄 Download SMILES File")
|
107 |
+
run_btn.click(fn=generate_from_pdb, inputs=pdb_input, outputs=[status, view3d, file_output])
|
|
|
|
|
|
|
|
|
108 |
|
109 |
+
demo.launch()
|