File size: 14,275 Bytes
43e7ae4 56cce54 43e7ae4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
"""
UI Components Module
This module contains reusable UI components and layouts
for the drug discovery application.
"""
import gradio as gr
from .handlers import VariationHandlers
def create_molecular_analysis_tab():
"""Create the molecular analysis tab components."""
with gr.Column(scale=1):
gr.Markdown("## π¬ Molecular Analysis")
smiles_input = gr.Textbox(
label="Enter SMILES string",
placeholder="e.g., C[C@H](N)C(=O)O",
info="Enter a valid SMILES string for molecular analysis"
)
# Real-time validation status
validation_status = gr.Markdown("β
Ready to analyze")
preview_image = gr.Image(
label="Preview",
show_download_button=False,
width=200,
height=200,
visible=False
)
# Bookmark functionality
with gr.Row():
bookmark_name = gr.Textbox(
placeholder="Enter molecule name (optional)",
label="Molecule Name",
scale=2
)
bookmark_btn = gr.Button("π Bookmark", variant="secondary", size="sm", scale=1)
bookmark_status = gr.Markdown("")
gr.Markdown("### π Analysis Results")
analysis_output = gr.Markdown()
with gr.Column(scale=1):
gr.Markdown("### π§ͺ Molecular Structure")
molecule_image = gr.Image(
label="Chemical Structure",
show_download_button=False,
width=400,
height=400
)
return {
'smiles_input': smiles_input,
'validation_status': validation_status,
'preview_image': preview_image,
'bookmark_name': bookmark_name,
'bookmark_btn': bookmark_btn,
'bookmark_status': bookmark_status,
'analysis_output': analysis_output,
'molecule_image': molecule_image
}
def create_chemical_variations_tab():
"""Create the consolidated drug discovery tab with molecular analysis and variations."""
# Add notification container
gr.HTML("""
<div id="notification-container"></div>
<script>
function showNotification(message, type = 'success', duration = 2000) {
// Remove existing notifications
const container = document.getElementById('notification-container');
container.innerHTML = '';
// Create notification element
const notification = document.createElement('div');
notification.className = `temp-notification ${type}`;
notification.textContent = message;
// Add to container
container.appendChild(notification);
// Auto-remove after duration
setTimeout(() => {
if (notification.parentNode) {
notification.style.opacity = '0';
notification.style.transform = 'translateX(100%)';
setTimeout(() => {
if (notification.parentNode) {
notification.remove();
}
}, 300);
}
}, duration);
}
// Listen for validation status changes
document.addEventListener('DOMContentLoaded', function() {
const validationStatus = document.getElementById('validation_status');
if (validationStatus) {
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type === 'childList' || mutation.type === 'characterData') {
const text = validationStatus.textContent || validationStatus.innerText;
if (text && text.trim()) {
if (text.includes('β
') && text.includes('Ready to analyze')) {
showNotification('β
Ready to analyze', 'success');
} else if (text.includes('β
') && text.includes('Valid SMILES')) {
showNotification('β
Valid SMILES', 'success');
} else if (text.includes('β')) {
showNotification('β Invalid SMILES', 'error');
}
}
}
});
});
observer.observe(validationStatus, { childList: true, characterData: true, subtree: true });
}
});
</script>
""", visible=False)
with gr.Row():
with gr.Column(scale=3):
# Left side: Molecular Analysis
variation_smiles_input = gr.Textbox(
label="Enter SMILES string",
placeholder="e.g., C[C@H](N)C(=O)O",
info="Enter a valid SMILES string for molecular analysis"
)
# Hidden validation status for popup notifications
validation_status = gr.Markdown(
value="",
visible=False,
elem_id="validation_status"
)
preview_image = gr.Image(
label="Preview",
show_download_button=False,
width=200,
height=200,
visible=False
)
# Generate variations controls - moved up for better UX
with gr.Row():
generate_variations_btn = gr.Button("π Generate Variations", variant="primary")
clear_variations_btn = gr.Button("ποΈ Clear", variant="secondary")
# Bookmark functionality
with gr.Row():
bookmark_name = gr.Textbox(
placeholder="Enter molecule name (optional)",
label="Molecule Name",
scale=2
)
bookmark_btn = gr.Button("π Bookmark", variant="secondary", size="sm", scale=1)
bookmark_status = gr.Markdown("")
# Hidden components for internal use (not displayed to user)
selected_smiles_display = gr.Textbox(
visible=False,
elem_id="selected_smiles"
)
selected_style_display = gr.Textbox(
visible=False,
elem_id="selected_style"
)
# Enhanced AI Chat Interface
gr.Markdown("#### π€ AI Drug Discovery Assistant")
gr.Markdown("Ask the AI about molecular properties, generate new structures, or get drug discovery insights")
# AI Settings
with gr.Row():
hf_token_input = gr.Textbox(
label="Hugging Face Token",
type="password",
placeholder="Enter your Hugging Face token for AI features",
scale=2
)
ai_temperature = gr.Slider(
minimum=0.1,
maximum=2.0,
value=0.7,
step=0.1,
label="AI Temperature",
scale=1
)
# Main chat interface
ai_chatbot = gr.Chatbot(
label="AI Drug Discovery Chat",
height=400,
elem_id="ai_chatbot",
type="messages"
)
with gr.Row():
ai_chat_input = gr.Textbox(
placeholder="e.g., 'Explain the drug-likeness of this molecule' or 'Generate a more soluble derivative'",
label="Your message",
scale=4
)
ai_send_btn = gr.Button("Send", variant="primary", scale=1)
# AI Generated Structures display
gr.Markdown("#### 𧬠AI Generated Structures")
ai_generated_grid = gr.Gallery(
label="AI Generated Structures",
show_label=False,
elem_id="ai_generated_grid",
columns=3,
rows=2,
height=200,
object_fit="contain",
allow_preview=True
)
with gr.Column(scale=4):
# Right side: Main structure display + variations grid
main_structure_display = gr.Image(
show_download_button=False,
elem_id="main_structure"
)
# Seamless properties display
properties_display = gr.Markdown(
value="*Click 'Analyze Molecule' or select a variation to see properties*",
elem_id="properties_display",
elem_classes="seamless-properties"
)
# Seamless variations section
with gr.Column(elem_id="variations_container"):
variations_grid = gr.Gallery(
show_label=False,
elem_id="variations_gallery",
columns=3,
rows=4,
height='auto',
object_fit="contain",
allow_preview=True,
show_share_button=False,
show_download_button=False
)
# Navigation controls
with gr.Row():
prev_page_btn = gr.Button("β¬
οΈ Previous", size="sm")
page_info = gr.Markdown("Page 1 of 1", elem_classes="page-info")
next_page_btn = gr.Button("β‘οΈ Next", size="sm")
# Grid controls
with gr.Row(elem_classes="grid-controls"):
grid_size_slider = gr.Slider(
minimum=4,
maximum=8,
value=4,
step=1,
label="Grid Columns",
elem_id="grid_size_slider"
)
variation_count_slider = gr.Slider(
minimum=6,
maximum=24,
value=12,
step=6,
label="Number of Variations",
elem_id="variation_count_slider"
)
return {
# Molecular Analysis components
'variation_smiles_input': variation_smiles_input,
'validation_status': validation_status,
'preview_image': preview_image,
'bookmark_name': bookmark_name,
'bookmark_btn': bookmark_btn,
'bookmark_status': bookmark_status,
'main_structure_display': main_structure_display,
'properties_display': properties_display,
# Variations components
'generate_variations_btn': generate_variations_btn,
'clear_variations_btn': clear_variations_btn,
'selected_smiles_display': selected_smiles_display,
'selected_style_display': selected_style_display,
'variations_grid': variations_grid,
'prev_page_btn': prev_page_btn,
'page_info': page_info,
'next_page_btn': next_page_btn,
'grid_size_slider': grid_size_slider,
'variation_count_slider': variation_count_slider,
# AI components
'hf_token_input': hf_token_input,
'ai_temperature': ai_temperature,
'ai_chatbot': ai_chatbot,
'ai_chat_input': ai_chat_input,
'ai_send_btn': ai_send_btn,
'ai_generated_grid': ai_generated_grid
}
def create_molecular_gallery_tab():
"""Create the molecular gallery tab components."""
gr.Markdown("### Common Drug Discovery Molecules")
image_components = []
# Display images in a 3x3 grid
with gr.Row():
for row in range(3):
with gr.Row():
for col in range(3):
idx = row * 3 + col
img = gr.Image(
show_download_button=False,
width=200,
height=200,
label=f"Molecule {idx + 1}",
)
image_components.append(img)
# Bookmarked molecules section
gr.Markdown("### π Your Bookmarked Molecules")
bookmarked_gallery = gr.Gallery(
label="Bookmarked Structures",
show_label=False,
elem_id="bookmarked_gallery",
columns=4,
rows=1,
height=200,
object_fit="contain"
)
return {
'image_components': image_components,
'bookmarked_gallery': bookmarked_gallery
}
def create_drug_library_tab():
"""Create the drug discovery library tab components."""
gr.Markdown("### NSAID Drug Series")
drug_images = []
# Display drug molecules
with gr.Row():
for i in range(3): # First row
img = gr.Image(
show_download_button=False,
width=200,
height=200,
label=f"Drug {i + 1}",
)
drug_images.append(img)
with gr.Row():
for i in range(2): # Second row
img = gr.Image(
show_download_button=False,
width=200,
height=200,
label=f"Drug {i + 4}",
)
drug_images.append(img)
return {
'drug_images': drug_images
}
def create_new_experiment_tab():
"""Create a blank workspace tab for new experiments."""
with gr.Column():
gr.Markdown("## π§ͺ New Experiment Workspace")
gr.Markdown(
"This tab is intentionally left blank so you can prototype new "
"ideas without affecting the existing lab, gallery, or library views."
)
gr.Markdown(
"- Add new components here as you explore ideas\n"
"- Copy elements from other tabs if needed\n"
"- Wire up handlers in `src/app.py` once ready"
)
gr.HTML(
"<div style='border:1px dashed #bbb; padding:2rem; text-align:center;'>"
"Your custom UI goes here"
"</div>"
)
return {}
|