import gradio as gr import openai import requests from transformers import pipeline from langchain.llms import OpenAI as LangOpenAI from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from rdkit import Chem from rdkit.Chem import AllChem, Draw from rdkit.Chem.Draw import rdMolDraw2D import base64 from io import BytesIO import py3Dmol import re # === Advanced LLM Configs === openai.api_key = "your-openai-api-key" # BioGPT and OpenAI Coordinated Agents def get_literature_insights(disease, symptoms): bio_gpt = pipeline("text-generation", model="microsoft/BioGPT-Large") prompt = f"Recent drug research for {disease} with symptoms: {symptoms}." return bio_gpt(prompt, max_length=200)[0]['generated_text'] def get_openai_smiles(disease, symptoms): prompt = f"Suggest 3 valid, drug-like SMILES strings that can potentially treat {disease} (symptoms: {symptoms}). Return only SMILES strings separated by space." response = openai.Completion.create( engine="text-davinci-003", prompt=prompt, max_tokens=100 ) return response.choices[0].text.strip() # === Multi-agent Molecular Pipeline === def drug_discovery(disease, symptoms): # Agent 1: Literature from BioGPT literature = get_literature_insights(disease, symptoms) # Agent 2: Molecule SMILES from OpenAI smiles_result = get_openai_smiles(disease, symptoms) smiles_matches = re.findall(r"(? 2D Molecule
๐Ÿ’Š 2D Drug Structure
''' # RDKit 3D Molecule + py3Dmol mol3d = Chem.AddHs(mol) AllChem.EmbedMolecule(mol3d) AllChem.UFFOptimizeMolecule(mol3d) molblock = Chem.MolToMolBlock(mol3d) viewer = py3Dmol.view(width=420, height=420) viewer.addModel(molblock, "mol") viewer.setStyle({"stick": {"colorscheme": "cyanCarbon"}}) viewer.setBackgroundColor("black") viewer.zoomTo() viewer.spin(True) viewer_html_raw = viewer._make_html() viewer_html = f'''
๐Ÿงฌ 3D Molecule
''' return literature, smiles, img_html, viewer_html # === Gradio UI === disease_input = gr.Textbox(label="๐Ÿงฌ Disease (e.g., glioblastoma)", value="glioblastoma") symptom_input = gr.Textbox(label="๐Ÿฉธ Symptoms (e.g., seizures, nausea)", value="seizures, nausea") lit_output = gr.Textbox(label="๐Ÿ“š Literature from BioGPT") smiles_output = gr.Textbox(label="๐Ÿงช SMILES Representation") img_output = gr.HTML(label="๐Ÿ”ฌ 2D Structure") viewer_output = gr.HTML(label="๐Ÿงฌ 3D Molecule") custom_css = """ @keyframes fadeIn { from {opacity: 0;} to {opacity: 1;} } @keyframes zoomIn { from {transform: scale(0.5); opacity: 0;} to {transform: scale(1); opacity: 1;} } body { background: linear-gradient(to right, #141e30, #243b55); color: #ffffff; font-family: 'Segoe UI', sans-serif; } .gradio-container { animation: fadeIn 2s ease-in-out; } """ 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 System (CS3235 Project)", description="This real-time LLM-based platform suggests drugs for diseases without known treatments, generates 2D/3D molecules, and provides literature justifications using BioGPT + OpenAI + RDKit.", css=custom_css ) iface.launch(share=True)