import gradio as gr
from transformers import pipeline
from rdkit import Chem
from rdkit.Chem import AllChem
from rdkit.Chem.Draw import rdMolDraw2D
from rdkit.Chem import rdDepictor
import base64
from io import BytesIO
import py3Dmol
import re
# Function to generate literature and 3D molecule view
def drug_discovery(disease, symptoms):
# BioGPT pipeline
bio_gpt = pipeline("text-generation", model="microsoft/BioGPT-Large")
prompt = f"Recent treatments for {disease} with symptoms: {symptoms}."
literature = bio_gpt(prompt, max_length=200)[0]['generated_text']
# Generate SMILES using BioGPT with stricter filtering
molecule_prompt = f"List 5 different valid drug-like SMILES strings that can treat {disease} with symptoms {symptoms}. Only list SMILES separated by spaces."
smiles_result = bio_gpt(molecule_prompt, max_length=100)[0]['generated_text']
# Extract and validate SMILES strings
smiles_matches = re.findall(r"(?
๐ Visualized Drug Molecule (2D)
'''
# 3D molecule
mol3d = Chem.AddHs(mol)
AllChem.EmbedMolecule(mol3d)
AllChem.UFFOptimizeMolecule(mol3d)
mb = Chem.MolToMolBlock(mol3d)
viewer = py3Dmol.view(width=420, height=420)
viewer.addModel(mb, "mol")
viewer.setStyle({"stick": {"colorscheme": "cyanCarbon"}})
viewer.setBackgroundColor("black")
viewer.zoomTo()
viewer.spin(True)
viewer_html_raw = viewer._make_html()
viewer_html = f'''
๐งฌ Animated 3D Molecule (Stick View)
'''
return literature, smiles, img_html, viewer_html
# Gradio UI
disease_input = gr.Textbox(label="๐ฅ Enter Disease (e.g., lung cancer)", value="lung cancer")
symptom_input = gr.Textbox(label="๐ Enter Symptoms (e.g., cough, weight loss)", value="shortness of breath, weight loss")
lit_output = gr.Textbox(label="๐ฐ Literature Insights from BioGPT")
smiles_output = gr.Textbox(label="๐งช SMILES Representation")
img_output = gr.HTML(label="๐ผ๏ธ Molecule 2D Visualization")
viewer_output = gr.HTML(label="๐ฌ 3D Drug Molecule Animation")
custom_css = """
@keyframes fadeIn {
from {opacity: 0;}
to {opacity: 1;}
}
@keyframes slideUp {
from {transform: translateY(40px); opacity: 0;}
to {transform: translateY(0); opacity: 1;}
}
@keyframes zoomIn {
from {transform: scale(0.5); opacity: 0;}
to {transform: scale(1); opacity: 1;}
}
body {
background: linear-gradient(to right, #0f2027, #203a43, #2c5364);
color: #eeeeee;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.gradio-container {
animation: fadeIn 1.5s ease-in-out;
}
.gradio-container .block-label {
color: #ffffff;
}
"""
iface = gr.Interface(
fn=drug_discovery,
inputs=[disease_input, symptom_input],
outputs=[lit_output, smiles_output, img_output, viewer_output],
title="๐ฅ AI-Powered Drug Discovery for Hospitals",
description="This hospital-themed platform takes a disease and symptoms as input, retrieves biomedical insights using BioGPT, and visualizes potential drug molecules in 2D and animated 3D. Ideal for clinical research and pharma innovation.",
theme="default",
css=custom_css
)
iface.launch(share=True)