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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import sys
|
| 3 |
+
import os
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
# Add parent to path for local testing
|
| 7 |
+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 8 |
+
|
| 9 |
+
# Try importing pharmacore modules
|
| 10 |
+
try:
|
| 11 |
+
from pharmacore.discovery import DeNovoDiscoveryEngine
|
| 12 |
+
from pharmacore.repurposing import DrugRepurposingEngine, KNOWN_DRUGS
|
| 13 |
+
MODULES_AVAILABLE = True
|
| 14 |
+
except ImportError:
|
| 15 |
+
MODULES_AVAILABLE = False
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# --- De Novo Discovery ---
|
| 19 |
+
def run_discovery(target_name, target_sequence, n_molecules, seed):
|
| 20 |
+
if not MODULES_AVAILABLE:
|
| 21 |
+
return format_discovery_demo(target_name)
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
engine = DeNovoDiscoveryEngine(seed=int(seed))
|
| 25 |
+
start = time.time()
|
| 26 |
+
result = engine.discover(
|
| 27 |
+
target_name=target_name,
|
| 28 |
+
target_sequence=target_sequence if target_sequence.strip() else None,
|
| 29 |
+
n_molecules=int(n_molecules),
|
| 30 |
+
)
|
| 31 |
+
elapsed = time.time() - start
|
| 32 |
+
|
| 33 |
+
lines = [f"## Results for {target_name}", f"Generated {len(result.molecules)} candidates in {elapsed:.1f}s\n"]
|
| 34 |
+
lines.append("| Rank | Name | Score | Scaffold | SMILES |")
|
| 35 |
+
lines.append("|------|------|-------|----------|--------|")
|
| 36 |
+
for i, mol in enumerate(result.molecules, 1):
|
| 37 |
+
lines.append(f"| {i} | {mol.name} | {mol.composite_score:.3f} | {mol.scaffold_name} | `{mol.smiles}` |")
|
| 38 |
+
|
| 39 |
+
# Top candidate details
|
| 40 |
+
top = result.molecules[0]
|
| 41 |
+
lines.append(f"\n### Top Candidate: {top.name}")
|
| 42 |
+
lines.append(f"- **Scaffold:** {top.scaffold_name}")
|
| 43 |
+
lines.append(f"- **QED (Drug-likeness):** {top.qed:.3f}")
|
| 44 |
+
lines.append(f"- **Target Compatibility:** {top.target_score:.3f}")
|
| 45 |
+
lines.append(f"- **Synthetic Accessibility:** {top.sa_score:.3f}")
|
| 46 |
+
lines.append(f"- **Lipinski:** {'PASS' if top.lipinski_pass else 'FAIL'}")
|
| 47 |
+
|
| 48 |
+
return "\n".join(lines)
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return f"Error: {str(e)}"
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def format_discovery_demo(target_name):
|
| 54 |
+
"""Fallback demo output when modules aren't available (for HF Space)"""
|
| 55 |
+
return f"""## Results for {target_name}
|
| 56 |
+
Generated 5 candidates in ~8s (demo mode β full inference requires Apple Silicon)
|
| 57 |
+
|
| 58 |
+
| Rank | Name | Score | Scaffold | SMILES |
|
| 59 |
+
|------|------|-------|----------|--------|
|
| 60 |
+
| 1 | PC-{target_name[:4].upper()}-0001 | 0.849 | quinazoline | `NC(=O)c1c(O)ccc2ncc(-c3ccncc3)nc12` |
|
| 61 |
+
| 2 | PC-{target_name[:4].upper()}-0002 | 0.799 | quinoline | `FC(F)(F)c1ccc2cccnc2c1` |
|
| 62 |
+
| 3 | PC-{target_name[:4].upper()}-0003 | 0.795 | benzimidazole | `CNC(=O)c1ccc2[nH]cnc2c1` |
|
| 63 |
+
| 4 | PC-{target_name[:4].upper()}-0004 | 0.791 | quinoline | `c1cnc2ccc(-c3ccncc3)cc2c1` |
|
| 64 |
+
| 5 | PC-{target_name[:4].upper()}-0005 | 0.770 | indole | `O=C(O)c1cc2[nH]ccc2c(C(=O)O)c1C(=O)O` |
|
| 65 |
+
|
| 66 |
+
### Top Candidate: PC-{target_name[:4].upper()}-0001
|
| 67 |
+
- **Scaffold:** quinazoline (known kinase inhibitor scaffold)
|
| 68 |
+
- **QED (Drug-likeness):** 0.731
|
| 69 |
+
- **Target Compatibility:** 0.900
|
| 70 |
+
- **Synthetic Accessibility:** 1.000
|
| 71 |
+
- **Lipinski:** PASS
|
| 72 |
+
|
| 73 |
+
> π‘ *This is a demo preview. For real-time inference, clone the repo and run on Apple Silicon.*
|
| 74 |
+
"""
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
# --- Drug Repurposing ---
|
| 78 |
+
def run_repurposing(target_name, target_sequence, reference_smiles, top_k):
|
| 79 |
+
if not MODULES_AVAILABLE:
|
| 80 |
+
return format_repurposing_demo(target_name)
|
| 81 |
+
|
| 82 |
+
try:
|
| 83 |
+
engine = DrugRepurposingEngine()
|
| 84 |
+
start = time.time()
|
| 85 |
+
result = engine.screen(
|
| 86 |
+
target_name=target_name,
|
| 87 |
+
target_sequence=target_sequence if target_sequence.strip() else None,
|
| 88 |
+
reference_smiles=reference_smiles if reference_smiles.strip() else None,
|
| 89 |
+
top_k=int(top_k),
|
| 90 |
+
)
|
| 91 |
+
elapsed = time.time() - start
|
| 92 |
+
|
| 93 |
+
lines = [f"## Repurposing Screen for {target_name}", f"Screened {len(KNOWN_DRUGS)} FDA-approved drugs in {elapsed:.1f}s\n"]
|
| 94 |
+
lines.append("| Rank | Drug | Score | Confidence | Original Indication |")
|
| 95 |
+
lines.append("|------|------|-------|------------|---------------------|")
|
| 96 |
+
for i, c in enumerate(result.candidates, 1):
|
| 97 |
+
lines.append(f"| {i} | {c.drug_name} | {c.composite_score:.3f} | {c.confidence} | {c.original_indication} |")
|
| 98 |
+
|
| 99 |
+
top = result.candidates[0]
|
| 100 |
+
lines.append(f"\n### Top Candidate: {top.drug_name}")
|
| 101 |
+
lines.append(f"- **Original Use:** {top.original_indication}")
|
| 102 |
+
lines.append(f"- **Mechanism:** {top.mechanism}")
|
| 103 |
+
lines.append(f"- **Protein Compatibility:** {top.protein_score:.1%}")
|
| 104 |
+
lines.append(f"- **Molecular Similarity:** {top.molecular_similarity:.1%}")
|
| 105 |
+
|
| 106 |
+
return "\n".join(lines)
|
| 107 |
+
except Exception as e:
|
| 108 |
+
return f"Error: {str(e)}"
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def format_repurposing_demo(target_name):
|
| 112 |
+
"""Fallback demo output"""
|
| 113 |
+
return f"""## Repurposing Screen for {target_name}
|
| 114 |
+
Screened 12 FDA-approved drugs (demo mode)
|
| 115 |
+
|
| 116 |
+
| Rank | Drug | Score | Confidence | Original Indication |
|
| 117 |
+
|------|------|-------|------------|---------------------|
|
| 118 |
+
| 1 | Erlotinib | 0.699 | medium | Non-small cell lung cancer |
|
| 119 |
+
| 2 | Sorafenib | 0.312 | low | Renal cell carcinoma |
|
| 120 |
+
| 3 | Sildenafil | 0.288 | low | Erectile dysfunction |
|
| 121 |
+
| 4 | Celecoxib | 0.265 | low | Arthritis pain |
|
| 122 |
+
| 5 | Remdesivir | 0.264 | low | Ebola (repurposed for COVID-19) |
|
| 123 |
+
|
| 124 |
+
### Top Candidate: Erlotinib
|
| 125 |
+
- **Original Use:** Non-small cell lung cancer
|
| 126 |
+
- **Mechanism:** EGFR tyrosine kinase inhibitor
|
| 127 |
+
- **Protein Compatibility:** 14.0%
|
| 128 |
+
- **Molecular Similarity:** 100.0%
|
| 129 |
+
|
| 130 |
+
> β
Erlotinib is a known EGFR inhibitor β correctly identified as top candidate.
|
| 131 |
+
> π‘ *Demo preview. For real inference, run on Apple Silicon locally.*
|
| 132 |
+
"""
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
# --- Gradio Interface ---
|
| 136 |
+
with gr.Blocks(
|
| 137 |
+
title="PharmaCore β AI Drug Discovery",
|
| 138 |
+
theme=gr.themes.Soft(primary_hue="purple", secondary_hue="blue"),
|
| 139 |
+
) as demo:
|
| 140 |
+
gr.Markdown("""
|
| 141 |
+
# 𧬠PharmaCore β AI Drug Discovery on Apple Silicon
|
| 142 |
+
|
| 143 |
+
**The first AI drug discovery platform that runs entirely on consumer hardware.**
|
| 144 |
+
No cloud GPUs. No API keys. No data leaves your machine.
|
| 145 |
+
|
| 146 |
+
[GitHub](https://github.com/reacherwu/PharmaCore) | [Models](https://huggingface.co/collections/stephenjun8192/pharmacore-sparse-models-69e5842a51579e4b12d42f30)
|
| 147 |
+
""")
|
| 148 |
+
|
| 149 |
+
with gr.Tab("𧬠De Novo Discovery"):
|
| 150 |
+
gr.Markdown("Generate novel drug candidates for a protein target using sparse AI models.")
|
| 151 |
+
with gr.Row():
|
| 152 |
+
with gr.Column():
|
| 153 |
+
target_name_disc = gr.Textbox(label="Target Name", value="EGFR kinase", placeholder="e.g., EGFR kinase, BRAF V600E")
|
| 154 |
+
target_seq_disc = gr.Textbox(label="Target Sequence (optional)", value="", placeholder="Protein amino acid sequence...", lines=3)
|
| 155 |
+
n_mols = gr.Slider(minimum=3, maximum=10, value=5, step=1, label="Number of Molecules")
|
| 156 |
+
seed = gr.Number(label="Random Seed", value=42)
|
| 157 |
+
btn_disc = gr.Button("π Generate Candidates", variant="primary")
|
| 158 |
+
with gr.Column():
|
| 159 |
+
output_disc = gr.Markdown(label="Results")
|
| 160 |
+
btn_disc.click(run_discovery, inputs=[target_name_disc, target_seq_disc, n_mols, seed], outputs=output_disc)
|
| 161 |
+
|
| 162 |
+
with gr.Tab("π Drug Repurposing"):
|
| 163 |
+
gr.Markdown("Screen existing FDA-approved drugs for new therapeutic uses.")
|
| 164 |
+
with gr.Row():
|
| 165 |
+
with gr.Column():
|
| 166 |
+
target_name_rep = gr.Textbox(label="Target Name", value="EGFR", placeholder="e.g., EGFR, ACE2, BRAF")
|
| 167 |
+
target_seq_rep = gr.Textbox(label="Target Sequence (optional)", value="", placeholder="Protein amino acid sequence...", lines=3)
|
| 168 |
+
ref_smiles = gr.Textbox(label="Reference SMILES (optional)", value="COCCOc1cc2ncnc(Nc3cccc(C#C)c3)c2cc1OCCOC", placeholder="Known ligand SMILES for similarity scoring")
|
| 169 |
+
top_k = gr.Slider(minimum=3, maximum=12, value=5, step=1, label="Top K Results")
|
| 170 |
+
btn_rep = gr.Button("π Screen Drugs", variant="primary")
|
| 171 |
+
with gr.Column():
|
| 172 |
+
output_rep = gr.Markdown(label="Results")
|
| 173 |
+
btn_rep.click(run_repurposing, inputs=[target_name_rep, target_seq_rep, ref_smiles, top_k], outputs=output_rep)
|
| 174 |
+
|
| 175 |
+
with gr.Tab("βΉοΈ About"):
|
| 176 |
+
gr.Markdown("""
|
| 177 |
+
## How It Works
|
| 178 |
+
|
| 179 |
+
PharmaCore uses **sparse AI models** (50% pruned) for efficient inference:
|
| 180 |
+
|
| 181 |
+
| Model | Role | Params | Speed (M4) |
|
| 182 |
+
|-------|------|--------|------------|
|
| 183 |
+
| ESM-2 35M | Protein encoding | 33.5M β 16.7M | 7.8ms |
|
| 184 |
+
| ChemBERTa-zinc | Molecule encoding | 44.1M β 22M | 4.9ms |
|
| 185 |
+
|
| 186 |
+
### De Novo Discovery Pipeline
|
| 187 |
+
1. Encode protein target with sparse ESM-2
|
| 188 |
+
2. Enumerate drug-like scaffolds (quinazoline, quinoline, benzimidazole, etc.)
|
| 189 |
+
3. Score candidates: QED + target compatibility + synthetic accessibility
|
| 190 |
+
4. Rank and filter by Lipinski/Veber rules
|
| 191 |
+
|
| 192 |
+
### Drug Repurposing Pipeline
|
| 193 |
+
1. Encode target protein and reference ligand
|
| 194 |
+
2. Compute protein-drug compatibility for 12 FDA-approved drugs
|
| 195 |
+
3. Calculate molecular fingerprint similarity
|
| 196 |
+
4. Rank by composite score with confidence levels
|
| 197 |
+
|
| 198 |
+
### Key Differentiators
|
| 199 |
+
- **100% Local** β no data leaves your machine
|
| 200 |
+
- **Apple Silicon MPS** β optimized for M1/M2/M3/M4
|
| 201 |
+
- **Transparent** β full audit trail for every computation
|
| 202 |
+
- **Fast** β sub-10ms protein inference, sub-5ms molecular inference
|
| 203 |
+
- **Open Source** β MIT licensed, all models on HuggingFace
|
| 204 |
+
""")
|
| 205 |
+
|
| 206 |
+
if __name__ == "__main__":
|
| 207 |
+
demo.launch()
|