Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ import pandas as pd
|
|
5 |
import matplotlib.pyplot as plt
|
6 |
import io
|
7 |
import base64
|
|
|
8 |
|
9 |
# دالة لحساب خصائص ADMET
|
10 |
def get_admet(smiles):
|
@@ -21,25 +22,26 @@ def get_admet(smiles):
|
|
21 |
return pd.Series([mw, logp, tpsa, hba, hbd, rotb, lipinski],
|
22 |
index=["MW", "LogP", "TPSA", "HBA", "HBD", "RotB", "Lipinski"])
|
23 |
|
24 |
-
#
|
25 |
def analyze_smiles(file):
|
26 |
try:
|
27 |
-
# قراءة
|
28 |
-
|
|
|
|
|
|
|
29 |
smiles_list = [line.strip() for line in content.splitlines() if line.strip()]
|
30 |
-
|
31 |
-
# إنشاء DataFrame وتحليل ADMET
|
32 |
df = pd.DataFrame({"SMILES": smiles_list})
|
33 |
admet_df = df["SMILES"].apply(get_admet)
|
34 |
final_df = pd.concat([df, admet_df], axis=1)
|
35 |
|
36 |
-
#
|
37 |
excel_buffer = io.BytesIO()
|
38 |
final_df.to_excel(excel_buffer, index=False, engine="openpyxl")
|
39 |
excel_data = excel_buffer.getvalue()
|
40 |
excel_b64 = base64.b64encode(excel_data).decode()
|
41 |
|
42 |
-
#
|
43 |
plot_df = final_df.dropna(subset=["LogP"])
|
44 |
plt.figure(figsize=(10, 6))
|
45 |
plt.barh(plot_df["SMILES"], plot_df["LogP"], color="skyblue")
|
@@ -53,7 +55,7 @@ def analyze_smiles(file):
|
|
53 |
img_b64 = base64.b64encode(img_buffer.read()).decode()
|
54 |
plt.close()
|
55 |
|
56 |
-
#
|
57 |
table_md = final_df.to_markdown(index=False)
|
58 |
|
59 |
return (
|
@@ -68,8 +70,8 @@ def analyze_smiles(file):
|
|
68 |
|
69 |
# واجهة Gradio
|
70 |
with gr.Blocks() as demo:
|
71 |
-
gr.Markdown("## 🧪 تحليل ADMET من
|
72 |
-
|
73 |
with gr.Row():
|
74 |
smiles_file = gr.File(label="📄 ارفع ملف .txt يحتوي على SMILES", file_types=[".txt"])
|
75 |
run_btn = gr.Button("🚀 تحليل ADMET")
|
@@ -79,10 +81,6 @@ with gr.Blocks() as demo:
|
|
79 |
download = gr.HTML()
|
80 |
image = gr.HTML()
|
81 |
|
82 |
-
run_btn.click(
|
83 |
-
fn=analyze_smiles,
|
84 |
-
inputs=[smiles_file],
|
85 |
-
outputs=[status, table, download, image]
|
86 |
-
)
|
87 |
|
88 |
demo.launch()
|
|
|
5 |
import matplotlib.pyplot as plt
|
6 |
import io
|
7 |
import base64
|
8 |
+
import os
|
9 |
|
10 |
# دالة لحساب خصائص ADMET
|
11 |
def get_admet(smiles):
|
|
|
22 |
return pd.Series([mw, logp, tpsa, hba, hbd, rotb, lipinski],
|
23 |
index=["MW", "LogP", "TPSA", "HBA", "HBD", "RotB", "Lipinski"])
|
24 |
|
25 |
+
# المعالجة الرئيسية
|
26 |
def analyze_smiles(file):
|
27 |
try:
|
28 |
+
# قراءة المحتوى باستخدام os
|
29 |
+
file_path = file.name if hasattr(file, "name") else file
|
30 |
+
with open(file_path, "r", encoding="utf-8") as f:
|
31 |
+
content = f.read()
|
32 |
+
|
33 |
smiles_list = [line.strip() for line in content.splitlines() if line.strip()]
|
|
|
|
|
34 |
df = pd.DataFrame({"SMILES": smiles_list})
|
35 |
admet_df = df["SMILES"].apply(get_admet)
|
36 |
final_df = pd.concat([df, admet_df], axis=1)
|
37 |
|
38 |
+
# Excel file
|
39 |
excel_buffer = io.BytesIO()
|
40 |
final_df.to_excel(excel_buffer, index=False, engine="openpyxl")
|
41 |
excel_data = excel_buffer.getvalue()
|
42 |
excel_b64 = base64.b64encode(excel_data).decode()
|
43 |
|
44 |
+
# Plot image
|
45 |
plot_df = final_df.dropna(subset=["LogP"])
|
46 |
plt.figure(figsize=(10, 6))
|
47 |
plt.barh(plot_df["SMILES"], plot_df["LogP"], color="skyblue")
|
|
|
55 |
img_b64 = base64.b64encode(img_buffer.read()).decode()
|
56 |
plt.close()
|
57 |
|
58 |
+
# Markdown table
|
59 |
table_md = final_df.to_markdown(index=False)
|
60 |
|
61 |
return (
|
|
|
70 |
|
71 |
# واجهة Gradio
|
72 |
with gr.Blocks() as demo:
|
73 |
+
gr.Markdown("## 🧪 تحليل ADMET من ملف SMILES (.txt)")
|
74 |
+
|
75 |
with gr.Row():
|
76 |
smiles_file = gr.File(label="📄 ارفع ملف .txt يحتوي على SMILES", file_types=[".txt"])
|
77 |
run_btn = gr.Button("🚀 تحليل ADMET")
|
|
|
81 |
download = gr.HTML()
|
82 |
image = gr.HTML()
|
83 |
|
84 |
+
run_btn.click(fn=analyze_smiles, inputs=[smiles_file], outputs=[status, table, download, image])
|
|
|
|
|
|
|
|
|
85 |
|
86 |
demo.launch()
|