admet / app.py
mohamed20003's picture
Update app.py
ddbb91a verified
raw
history blame
2.64 kB
import gradio as gr
from rdkit import Chem
from rdkit.Chem import Descriptors
import pandas as pd
import io
import base64
# تحليل ADMET
def get_admet(smiles):
mol = Chem.MolFromSmiles(smiles)
if not mol:
return pd.Series([None]*7, index=["MW", "LogP", "TPSA", "HBA", "HBD", "RotB", "Lipinski"])
mw = Descriptors.MolWt(mol)
logp = Descriptors.MolLogP(mol)
tpsa = Descriptors.TPSA(mol)
hba = Descriptors.NumHAcceptors(mol)
hbd = Descriptors.NumHDonors(mol)
rotb = Descriptors.NumRotatableBonds(mol)
lipinski = "✅" if (mw <= 500 and logp <= 5 and hba <= 10 and hbd <= 5) else "❌"
return pd.Series([mw, logp, tpsa, hba, hbd, rotb, lipinski],
index=["MW", "LogP", "TPSA", "HBA", "HBD", "RotB", "Lipinski"])
# المعالجة الرئيسية
def analyze_smiles(file):
try:
with open(file.name, "r", encoding="utf-8") as f:
content = f.read()
smiles_list = [line.strip() for line in content.splitlines() if line.strip()]
df = pd.DataFrame({"SMILES": smiles_list})
admet_df = df["SMILES"].apply(get_admet)
final_df = pd.concat([df, admet_df], axis=1)
# Excel file
excel_buffer = io.BytesIO()
final_df.to_excel(excel_buffer, index=False, engine="openpyxl")
excel_data = excel_buffer.getvalue()
excel_b64 = base64.b64encode(excel_data).decode()
# Markdown table
try:
import tabulate # Try to import it explicitly
table_md = final_df.to_markdown(index=False)
except ImportError:
table_md = "❌ مكتبة 'tabulate' مطلوبة لعرض الجدول. استخدم: pip install tabulate"
return (
"✅ التحليل ناجح!",
table_md,
f'<a download="ADMET_Analysis.xlsx" href="data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,{excel_b64}">⬇️ تحميل ملف Excel</a>'
)
except Exception as e:
return f"❌ خطأ أثناء المعالجة: {str(e)}", "", ""
# واجهة Gradio
with gr.Blocks() as demo:
gr.Markdown("## 🧪 تحليل ADMET من قائمة SMILES")
with gr.Row():
smiles_file = gr.File(label="📄 ارفع ملف .txt يحتوي على SMILES")
run_btn = gr.Button("🚀 تحليل ADMET")
status = gr.Textbox(label="📢 الحالة")
table = gr.Textbox(label="📊 جدول النتائج (Markdown)", lines=12)
download = gr.HTML()
run_btn.click(fn=analyze_smiles, inputs=[smiles_file], outputs=[status, table, download])
demo.launch()