Songyou commited on
Commit
ac67f65
·
verified ·
1 Parent(s): 7d89a88

Upload clm-frontend-dev.py

Browse files
Files changed (1) hide show
  1. clm-frontend-dev.py +233 -0
clm-frontend-dev.py ADDED
@@ -0,0 +1,233 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from fastapi import FastAPI
3
+ from starlette.staticfiles import StaticFiles
4
+ import uvicorn
5
+ import logging
6
+ from pydantic import BaseModel
7
+ import pandas as pd
8
+ import time
9
+
10
+ logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
11
+ logger = logging.getLogger(__name__)
12
+
13
+ class SmilesData(BaseModel):
14
+ smiles: str
15
+
16
+ app = FastAPI()
17
+
18
+ # 将 ketcher 目录挂载为静态资源,确保 ketcher/index.html 存在
19
+ app.mount("/ketcher", StaticFiles(directory="ketcher"), name="ketcher")
20
+
21
+ # 接收前端发送的SMILES数据的接口
22
+ @app.post("/update_smiles")
23
+ async def update_smiles(data: SmilesData):
24
+ logger.info(f"Received SMILES from front-end: {data.smiles}")
25
+ print(f"[PRINT-Backend] Received SMILES from front-end: {data.smiles}")
26
+ return {"status": "ok", "received_smiles": data.smiles}
27
+
28
+ # 前端嵌入的HTML和脚本
29
+ KETCHER_HTML = r'''
30
+ <iframe id="ifKetcher" src="/ketcher/index.html" width="100%" height="600px" style="border: 1px solid #ccc;"></iframe>
31
+
32
+ <script>
33
+ console.log("[Front-end] Ketcher-Gradio integration script loaded.");
34
+
35
+ let ketcher = null;
36
+ let lastSmiles = '';
37
+
38
+ function findSmilesInput() {
39
+ const inputContainer = document.getElementById('combined_smiles_input');
40
+ if (!inputContainer) {
41
+ console.warn("[Front-end] combined_smiles_input element not found.");
42
+ return null;
43
+ }
44
+ const input = inputContainer.querySelector('input[type="text"]');
45
+ return input;
46
+ }
47
+
48
+ function updateGradioInput(smiles) {
49
+ const input = findSmilesInput();
50
+ if (input && input.value !== smiles) {
51
+ input.value = smiles;
52
+ // 手动触发input事件让 Gradio 更新状态
53
+ input.dispatchEvent(new Event('input', { bubbles: true }));
54
+ console.log("[Front-end] Updated Gradio input with SMILES:", smiles);
55
+ }
56
+ }
57
+
58
+ async function handleKetcherChange() {
59
+ console.log("[Front-end] handleKetcherChange called, retrieving SMILES...");
60
+ try {
61
+ const smiles = await ketcher.getSmiles({ arom: false });
62
+ console.log("[Front-end] SMILES retrieved from Ketcher:", smiles);
63
+ if (smiles !== lastSmiles) {
64
+ lastSmiles = smiles;
65
+ updateGradioInput(smiles);
66
+
67
+ // 将SMILES发送给后端 (可以保留,但可能不是核心需求)
68
+ console.log("[Front-end] Sending SMILES to backend...");
69
+ fetch('/update_smiles', {
70
+ method: 'POST',
71
+ headers: {'Content-Type': 'application/json'},
72
+ body: JSON.stringify({smiles: smiles})
73
+ })
74
+ .then(res => res.json())
75
+ .then(data => {
76
+ console.log("[Front-end] Backend response:", data);
77
+ })
78
+ .catch(err => console.error("[Front-end] Error sending SMILES to backend:", err));
79
+ }
80
+ } catch (err) {
81
+ console.error("[Front-end] Error getting SMILES from Ketcher:", err);
82
+ }
83
+ }
84
+
85
+ function initKetcher() {
86
+ console.log("[Front-end] initKetcher started.");
87
+ const iframe = document.getElementById('ifKetcher');
88
+ if (!iframe) {
89
+ console.error("[Front-end] iframe not found.");
90
+ setTimeout(initKetcher, 500);
91
+ return;
92
+ }
93
+
94
+ const ketcherWindow = iframe.contentWindow;
95
+ if (!ketcherWindow || !ketcherWindow.ketcher) {
96
+ console.log("[Front-end] ketcher not yet available in iframe, retrying...");
97
+ setTimeout(initKetcher, 500);
98
+ return;
99
+ }
100
+
101
+ ketcher = ketcherWindow.ketcher;
102
+ console.log("[Front-end] Ketcher instance acquired:", ketcher);
103
+
104
+ // 设置初始分子
105
+ ketcher.setMolecule('C').then(() => {
106
+ console.log("[Front-end] Initial molecule set to 'C'.");
107
+ });
108
+
109
+ const editor = ketcher.editor;
110
+ console.log("[Front-end] Editor object:", editor);
111
+
112
+ // 尝试绑定变化事件
113
+ let eventBound = false;
114
+ if (editor && typeof editor.subscribe === 'function') {
115
+ console.log("[Front-end] Using editor.subscribe('change', ...)");
116
+ editor.subscribe('change', handleKetcherChange);
117
+ eventBound = true;
118
+ }
119
+
120
+ if (!eventBound) {
121
+ console.error("[Front-end] No suitable event binding found. Check Ketcher version and event API.");
122
+ }
123
+ }
124
+
125
+ document.getElementById('ifKetcher').addEventListener('load', () => {
126
+ console.log("[Front-end] iframe loaded. Initializing Ketcher in 1s...");
127
+ setTimeout(initKetcher, 1000);
128
+ });
129
+ </script>
130
+ '''
131
+
132
+ # --- 模拟后端功能 (来自第二个版本) ---
133
+ def smiles_to_structure(smiles):
134
+ """Dummy function"""
135
+ time.sleep(1)
136
+ return f"Structure Generated from: {smiles}"
137
+
138
+ def fragment_molecule(smiles):
139
+ """Dummy function"""
140
+ time.sleep(2)
141
+ return "Fragment1", "Fragment2", "1-2"
142
+
143
+ def generate_analogs(main_cls, minor_cls, number, delta_value):
144
+ """Dummy function"""
145
+ time.sleep(3)
146
+ return [
147
+ {"SMILE": "c1cccc1", "MolWt": 100, "TPSA": 20, "SLogP": 1, "SA": 30, "QED": 0.8},
148
+ {"SMILE": "c1ccccc1", "MolWt": 105, "TPSA": 25, "SLogP": 1.2, "SA": 32, "QED": 0.9},
149
+ ]
150
+
151
+ def update_output_table(data):
152
+ df = pd.DataFrame(data)
153
+ return df
154
+
155
+ # --- Gradio 界面 ---
156
+ def create_combined_interface():
157
+ with gr.Blocks() as demo:
158
+ gr.Markdown("# Fragment Optimization Tools with Ketcher")
159
+
160
+ with gr.Row():
161
+ with gr.Column(scale=2):
162
+ gr.HTML(KETCHER_HTML) # 嵌入 Ketcher
163
+
164
+ with gr.Column(scale=1):
165
+ with gr.Group():
166
+ gr.Markdown("### Input SMILES (From Ketcher)")
167
+ combined_smiles_input = gr.Textbox(
168
+ label="",
169
+ value="C",
170
+ placeholder="SMILES from Ketcher will appear here",
171
+ elem_id="combined_smiles_input"
172
+ )
173
+ with gr.Row():
174
+ # 可以保留一个按钮来触发某些操作,例如手动同步
175
+ get_ketcher_smiles_btn = gr.Button("Get SMILES from Ketcher")
176
+ # 示例:将当前输入框的 SMILES 设置到 Ketcher (需要额外的前端 JavaScript)
177
+ # set_ketcher_smiles_btn = gr.Button("Set SMILES to Ketcher")
178
+ fragment_btn = gr.Button("Fragmentize Molecule")
179
+
180
+ with gr.Group():
181
+ with gr.Row():
182
+ constant_frag_input = gr.Textbox(label="Constant Fragment", placeholder="SMILES of constant fragment")
183
+ variable_frag_input = gr.Textbox(label="Variable Fragment", placeholder="SMILES of variable fragment")
184
+ attach_order_input = gr.Textbox(label="Attachment Order", placeholder="Attachment Order of SMILES")
185
+
186
+ with gr.Group():
187
+ gr.Markdown("### Generate analogs")
188
+ with gr.Row():
189
+ main_cls_dropdown = gr.Dropdown(label="Main Cls", choices=["None", "Cl", "Br"])
190
+ minor_cls_dropdown = gr.Dropdown(label="Minor Cls", choices=["None", "Cl", "Br"])
191
+ number_input = gr.Number(label="Number", value=5, step=1)
192
+
193
+ delta_value_slider = gr.Slider(minimum=0, maximum=10, step=1, label="Delta Value", interactive=True)
194
+ generate_analogs_btn = gr.Button("Generate")
195
+
196
+ with gr.Row():
197
+ with gr.Column():
198
+ selected_columns = gr.CheckboxGroup(["SMILE", "MolWt", "TPSA", "SLogP", "SA", "QED"], value=["SMILE", "MolWt", "TPSA", "SLogP"], label="")
199
+
200
+ output_table = gr.Dataframe(headers=["SMILE", "MolWt", "TPSA", "SLogP", "SA", "QED"])
201
+
202
+ with gr.Row():
203
+ download_all_btn = gr.Button("Download All")
204
+ download_selected_btn = gr.Button("Download Selected")
205
+
206
+ # --- 事件处理 ---
207
+ # 当点击按钮时,手动从 Ketcher 获取 SMILES 并更新输入框
208
+ get_ketcher_smiles_btn.click(
209
+ fn=None,
210
+ inputs=None,
211
+ outputs=combined_smiles_input,
212
+ js="async () => { const iframe = document.getElementById('ifKetcher'); if(iframe && iframe.contentWindow && iframe.contentWindow.ketcher) { const smiles = await iframe.contentWindow.ketcher.getSmiles(); return smiles; } else { console.error('Ketcher not ready'); return ''; } }"
213
+ )
214
+
215
+ fragment_btn.click(fragment_molecule, inputs=combined_smiles_input, outputs=[constant_frag_input, variable_frag_input, attach_order_input])
216
+
217
+ def update_table_with_analogs(main_cls, minor_cls, number, delta_value):
218
+ analogs_data = generate_analogs(main_cls, minor_cls, number, delta_value)
219
+ return update_output_table(analogs_data)
220
+
221
+ generate_analogs_btn.click(update_table_with_analogs,
222
+ inputs=[main_cls_dropdown, minor_cls_dropdown, number_input, delta_value_slider],
223
+ outputs=output_table)
224
+
225
+ # TODO: 添加下载功能的回调
226
+
227
+ return demo
228
+
229
+ combined_demo = create_combined_interface()
230
+ app = gr.mount_gradio_app(app, combined_demo, path="/")
231
+
232
+ if __name__ == "__main__":
233
+ uvicorn.run(app, host="127.0.0.1", port=7860)