Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,31 @@
|
|
| 1 |
"""
|
| 2 |
-
|
| 3 |
-
|
| 4 |
"""
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
import logging
|
| 8 |
import json
|
| 9 |
import os
|
| 10 |
-
from typing import Dict, Any, Tuple, List
|
| 11 |
-
import html
|
| 12 |
-
import requests
|
| 13 |
import time
|
|
|
|
| 14 |
import numpy as np
|
| 15 |
-
from dataclasses import dataclass
|
| 16 |
from datetime import datetime
|
| 17 |
-
import asyncio
|
| 18 |
-
import threading
|
| 19 |
-
from queue import Queue
|
| 20 |
|
| 21 |
# Configure logging
|
| 22 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
| 23 |
logger = logging.getLogger(__name__)
|
| 24 |
|
| 25 |
-
#
|
| 26 |
try:
|
| 27 |
import spaces
|
| 28 |
SPACES_AVAILABLE = True
|
|
|
|
| 29 |
except ImportError:
|
| 30 |
SPACES_AVAILABLE = False
|
|
|
|
|
|
|
| 31 |
class spaces:
|
| 32 |
@staticmethod
|
| 33 |
def GPU(duration=60):
|
|
@@ -35,266 +33,132 @@ except ImportError:
|
|
| 35 |
return func
|
| 36 |
return decorator
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
'model_version': 'mock_v1'
|
| 58 |
-
}
|
| 59 |
-
}
|
| 60 |
-
|
| 61 |
-
# Global model instance
|
| 62 |
-
mock_model = MockDNAModel()
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
margin: 0;
|
| 75 |
-
padding: 20px;
|
| 76 |
-
background: #0a0a0a;
|
| 77 |
-
color: #fff;
|
| 78 |
-
font-family: 'Arial', sans-serif;
|
| 79 |
-
}
|
| 80 |
-
|
| 81 |
-
.container {
|
| 82 |
-
max-width: 1200px;
|
| 83 |
-
margin: 0 auto;
|
| 84 |
-
}
|
| 85 |
-
|
| 86 |
-
.header {
|
| 87 |
-
text-align: center;
|
| 88 |
-
margin-bottom: 30px;
|
| 89 |
-
}
|
| 90 |
-
|
| 91 |
-
.title {
|
| 92 |
-
font-size: 3rem;
|
| 93 |
-
background: linear-gradient(45deg, #ff00ff, #00ffff);
|
| 94 |
-
-webkit-background-clip: text;
|
| 95 |
-
-webkit-text-fill-color: transparent;
|
| 96 |
-
margin: 0;
|
| 97 |
-
}
|
| 98 |
-
|
| 99 |
-
.sequence-display {
|
| 100 |
-
background: #1a1a1a;
|
| 101 |
-
border: 2px solid #00ff00;
|
| 102 |
-
border-radius: 10px;
|
| 103 |
-
padding: 20px;
|
| 104 |
-
font-family: monospace;
|
| 105 |
-
word-break: break-all;
|
| 106 |
-
margin: 20px 0;
|
| 107 |
-
min-height: 100px;
|
| 108 |
-
}
|
| 109 |
-
|
| 110 |
-
.controls {
|
| 111 |
-
display: flex;
|
| 112 |
-
gap: 20px;
|
| 113 |
-
justify-content: center;
|
| 114 |
-
margin: 20px 0;
|
| 115 |
-
}
|
| 116 |
-
|
| 117 |
-
.btn {
|
| 118 |
-
padding: 15px 30px;
|
| 119 |
-
background: linear-gradient(145deg, #ff0066, #ff00ff);
|
| 120 |
-
border: none;
|
| 121 |
-
border-radius: 25px;
|
| 122 |
-
color: white;
|
| 123 |
-
font-size: 18px;
|
| 124 |
-
cursor: pointer;
|
| 125 |
-
transition: all 0.3s;
|
| 126 |
-
}
|
| 127 |
-
|
| 128 |
-
.btn:hover {
|
| 129 |
-
transform: translateY(-2px);
|
| 130 |
-
box-shadow: 0 5px 20px rgba(255,0,255,0.5);
|
| 131 |
-
}
|
| 132 |
-
|
| 133 |
-
.stats {
|
| 134 |
-
display: grid;
|
| 135 |
-
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
| 136 |
-
gap: 20px;
|
| 137 |
-
margin: 30px 0;
|
| 138 |
-
}
|
| 139 |
-
|
| 140 |
-
.stat-card {
|
| 141 |
-
background: rgba(255,255,255,0.1);
|
| 142 |
-
border-radius: 10px;
|
| 143 |
-
padding: 20px;
|
| 144 |
-
text-align: center;
|
| 145 |
-
}
|
| 146 |
-
|
| 147 |
-
.stat-value {
|
| 148 |
-
font-size: 2rem;
|
| 149 |
-
color: #00ff00;
|
| 150 |
-
}
|
| 151 |
-
|
| 152 |
-
.loading {
|
| 153 |
-
display: none;
|
| 154 |
-
text-align: center;
|
| 155 |
-
margin: 20px 0;
|
| 156 |
-
}
|
| 157 |
-
|
| 158 |
-
.spinner {
|
| 159 |
-
border: 3px solid rgba(255,255,255,0.1);
|
| 160 |
-
border-top-color: #00ff00;
|
| 161 |
-
border-radius: 50%;
|
| 162 |
-
width: 40px;
|
| 163 |
-
height: 40px;
|
| 164 |
-
animation: spin 1s linear infinite;
|
| 165 |
-
margin: 0 auto;
|
| 166 |
-
}
|
| 167 |
-
|
| 168 |
-
@keyframes spin {
|
| 169 |
-
to { transform: rotate(360deg); }
|
| 170 |
-
}
|
| 171 |
-
</style>
|
| 172 |
-
</head>
|
| 173 |
-
<body>
|
| 174 |
-
<div class="container">
|
| 175 |
-
<div class="header">
|
| 176 |
-
<h1 class="title">🧬 DNA Casino 🧬</h1>
|
| 177 |
-
<p>Advanced Molecular Engineering Suite</p>
|
| 178 |
-
</div>
|
| 179 |
-
|
| 180 |
-
<div class="controls">
|
| 181 |
-
<select id="cellType" class="btn">
|
| 182 |
-
<option value="K562">K562</option>
|
| 183 |
-
<option value="GM12878">GM12878</option>
|
| 184 |
-
<option value="HepG2">HepG2</option>
|
| 185 |
-
</select>
|
| 186 |
-
<button class="btn" onclick="generateSequence()">Generate Sequence</button>
|
| 187 |
-
</div>
|
| 188 |
-
|
| 189 |
-
<div class="loading" id="loading">
|
| 190 |
-
<div class="spinner"></div>
|
| 191 |
-
<p>Generating sequence...</p>
|
| 192 |
-
</div>
|
| 193 |
-
|
| 194 |
-
<div class="sequence-display" id="sequenceDisplay">
|
| 195 |
-
Click "Generate Sequence" to start
|
| 196 |
-
</div>
|
| 197 |
-
|
| 198 |
-
<div class="stats" id="stats"></div>
|
| 199 |
-
</div>
|
| 200 |
-
|
| 201 |
-
<script>
|
| 202 |
-
function generateSequence() {
|
| 203 |
-
const cellType = document.getElementById('cellType').value;
|
| 204 |
-
const loading = document.getElementById('loading');
|
| 205 |
-
const display = document.getElementById('sequenceDisplay');
|
| 206 |
|
| 207 |
-
|
| 208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
-
|
| 211 |
-
window.parent.postMessage({
|
| 212 |
-
type: 'generate',
|
| 213 |
-
cellType: cellType
|
| 214 |
-
}, '*');
|
| 215 |
-
}
|
| 216 |
|
| 217 |
-
|
| 218 |
-
window.addEventListener('message', (event) => {
|
| 219 |
-
if (event.data.type === 'result') {
|
| 220 |
-
const loading = document.getElementById('loading');
|
| 221 |
-
const display = document.getElementById('sequenceDisplay');
|
| 222 |
-
const stats = document.getElementById('stats');
|
| 223 |
-
|
| 224 |
-
loading.style.display = 'none';
|
| 225 |
-
display.textContent = event.data.sequence;
|
| 226 |
-
|
| 227 |
-
// Update stats
|
| 228 |
-
stats.innerHTML = `
|
| 229 |
-
<div class="stat-card">
|
| 230 |
-
<div class="stat-label">Length</div>
|
| 231 |
-
<div class="stat-value">${event.data.length} bp</div>
|
| 232 |
-
</div>
|
| 233 |
-
<div class="stat-card">
|
| 234 |
-
<div class="stat-label">GC Content</div>
|
| 235 |
-
<div class="stat-value">${event.data.gc_content}%</div>
|
| 236 |
-
</div>
|
| 237 |
-
<div class="stat-card">
|
| 238 |
-
<div class="stat-label">Cell Type</div>
|
| 239 |
-
<div class="stat-value">${event.data.cell_type}</div>
|
| 240 |
-
</div>
|
| 241 |
-
`;
|
| 242 |
-
}
|
| 243 |
-
});
|
| 244 |
-
</script>
|
| 245 |
-
</body>
|
| 246 |
-
</html>
|
| 247 |
-
"""
|
| 248 |
|
| 249 |
-
#
|
| 250 |
-
|
| 251 |
-
|
|
|
|
|
|
|
|
|
|
| 252 |
|
| 253 |
def __init__(self):
|
| 254 |
self.current_sequence = ""
|
| 255 |
self.current_analysis = {}
|
| 256 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
-
def generate_sequence(self, cell_type: str, guidance_scale: float = 1.0):
|
| 259 |
-
"""Generate DNA sequence"""
|
| 260 |
try:
|
| 261 |
-
|
| 262 |
-
|
| 263 |
|
| 264 |
-
#
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
}
|
| 274 |
|
| 275 |
-
return
|
| 276 |
|
| 277 |
except Exception as e:
|
| 278 |
-
logger.error(f"
|
| 279 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
|
| 281 |
def analyze_sequence(self, sequence: str) -> Dict[str, Any]:
|
| 282 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
gc_count = sequence.count('G') + sequence.count('C')
|
| 284 |
-
gc_content = (gc_count / len(sequence) * 100
|
| 285 |
|
| 286 |
-
#
|
| 287 |
if len(sequence) < 14:
|
| 288 |
tm = 4 * (sequence.count('G') + sequence.count('C')) + 2 * (sequence.count('A') + sequence.count('T'))
|
| 289 |
else:
|
| 290 |
tm = 81.5 + 0.41 * gc_content - 675 / len(sequence)
|
| 291 |
|
| 292 |
# Find restriction sites
|
| 293 |
-
|
| 294 |
enzymes = {
|
| 295 |
'EcoRI': 'GAATTC',
|
| 296 |
'BamHI': 'GGATCC',
|
| 297 |
-
'HindIII': 'AAGCTT'
|
|
|
|
|
|
|
| 298 |
}
|
| 299 |
|
| 300 |
for enzyme, pattern in enzymes.items():
|
|
@@ -303,193 +167,350 @@ class DNAApp:
|
|
| 303 |
if sequence[i:i+len(pattern)] == pattern:
|
| 304 |
positions.append(i)
|
| 305 |
if positions:
|
| 306 |
-
|
|
|
|
|
|
|
|
|
|
| 307 |
|
| 308 |
return {
|
|
|
|
| 309 |
'gc_content': round(gc_content, 1),
|
| 310 |
'melting_temp': round(tm, 1),
|
| 311 |
-
'restriction_sites':
|
|
|
|
|
|
|
| 312 |
}
|
| 313 |
|
| 314 |
-
def
|
| 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 |
-
return response
|
| 340 |
|
| 341 |
# Create app instance
|
| 342 |
-
app =
|
| 343 |
|
| 344 |
-
|
| 345 |
-
|
|
|
|
| 346 |
|
| 347 |
# Custom CSS
|
| 348 |
css = """
|
| 349 |
.gradio-container {
|
| 350 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 351 |
}
|
| 352 |
"""
|
| 353 |
|
| 354 |
-
with gr.Blocks(css=css, title="DNA
|
| 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 |
-
placeholder="E.g., What's the GC content? Find restriction sites."
|
| 391 |
-
)
|
| 392 |
-
clear = gr.Button("Clear Chat")
|
| 393 |
|
| 394 |
# Event handlers
|
| 395 |
-
def
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 399 |
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 416 |
|
| 417 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
|
|
|
|
| 419 |
generate_btn.click(
|
| 420 |
-
fn=
|
| 421 |
-
inputs=[cell_type, guidance_scale],
|
| 422 |
-
outputs=[sequence_output, analysis_output,
|
| 423 |
)
|
| 424 |
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
|
| 431 |
-
|
| 432 |
-
|
|
|
|
|
|
|
|
|
|
| 433 |
|
| 434 |
-
#
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
"How do I design primers?",
|
| 440 |
-
"What's the melting temperature?"
|
| 441 |
-
],
|
| 442 |
-
inputs=msg
|
| 443 |
)
|
| 444 |
|
| 445 |
return demo
|
| 446 |
|
| 447 |
-
#
|
| 448 |
-
def safe_launch():
|
| 449 |
-
"""Launch the app with error handling"""
|
| 450 |
-
try:
|
| 451 |
-
demo = create_interface()
|
| 452 |
-
|
| 453 |
-
# Detect environment
|
| 454 |
-
import os
|
| 455 |
-
if os.getenv("SPACE_ID"):
|
| 456 |
-
# Running on Hugging Face Spaces
|
| 457 |
-
demo.launch(
|
| 458 |
-
server_name="0.0.0.0",
|
| 459 |
-
server_port=7860,
|
| 460 |
-
share=False
|
| 461 |
-
)
|
| 462 |
-
else:
|
| 463 |
-
# Local development
|
| 464 |
-
demo.launch(
|
| 465 |
-
share=True,
|
| 466 |
-
debug=True
|
| 467 |
-
)
|
| 468 |
-
except Exception as e:
|
| 469 |
-
logger.error(f"Failed to launch: {e}")
|
| 470 |
-
# Fallback to even simpler interface
|
| 471 |
-
print("Starting fallback interface...")
|
| 472 |
-
create_fallback_interface()
|
| 473 |
-
|
| 474 |
-
def create_fallback_interface():
|
| 475 |
-
"""Ultra-simple fallback interface"""
|
| 476 |
-
with gr.Blocks() as demo:
|
| 477 |
-
gr.Markdown("# DNA Sequence Generator (Fallback Mode)")
|
| 478 |
-
|
| 479 |
-
with gr.Row():
|
| 480 |
-
cell_type = gr.Dropdown(["K562", "GM12878", "HepG2"], value="K562")
|
| 481 |
-
generate_btn = gr.Button("Generate")
|
| 482 |
-
|
| 483 |
-
output = gr.Textbox(label="Generated Sequence", lines=5)
|
| 484 |
-
|
| 485 |
-
def simple_generate(cell_type):
|
| 486 |
-
import random
|
| 487 |
-
seq = ''.join(random.choice(['A','T','C','G']) for _ in range(200))
|
| 488 |
-
return seq
|
| 489 |
-
|
| 490 |
-
generate_btn.click(simple_generate, cell_type, output)
|
| 491 |
-
|
| 492 |
-
demo.launch()
|
| 493 |
-
|
| 494 |
if __name__ == "__main__":
|
| 495 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
+
DNA-Diffusion Gradio Application - Fixed for Hugging Face Spaces
|
| 3 |
+
Properly implements @spaces.GPU decorator for ZeroGPU environment
|
| 4 |
"""
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
import logging
|
| 8 |
import json
|
| 9 |
import os
|
| 10 |
+
from typing import Dict, Any, Tuple, List
|
|
|
|
|
|
|
| 11 |
import time
|
| 12 |
+
import random
|
| 13 |
import numpy as np
|
|
|
|
| 14 |
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Configure logging
|
| 17 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
| 18 |
logger = logging.getLogger(__name__)
|
| 19 |
|
| 20 |
+
# Import spaces and handle ZeroGPU
|
| 21 |
try:
|
| 22 |
import spaces
|
| 23 |
SPACES_AVAILABLE = True
|
| 24 |
+
logger.info("Spaces module loaded successfully")
|
| 25 |
except ImportError:
|
| 26 |
SPACES_AVAILABLE = False
|
| 27 |
+
logger.warning("Spaces module not available - running without GPU")
|
| 28 |
+
# Create dummy decorator
|
| 29 |
class spaces:
|
| 30 |
@staticmethod
|
| 31 |
def GPU(duration=60):
|
|
|
|
| 33 |
return func
|
| 34 |
return decorator
|
| 35 |
|
| 36 |
+
# Genetic code table
|
| 37 |
+
CODON_TABLE = {
|
| 38 |
+
'TTT': 'F', 'TTC': 'F', 'TTA': 'L', 'TTG': 'L',
|
| 39 |
+
'TCT': 'S', 'TCC': 'S', 'TCA': 'S', 'TCG': 'S',
|
| 40 |
+
'TAT': 'Y', 'TAC': 'Y', 'TAA': '*', 'TAG': '*',
|
| 41 |
+
'TGT': 'C', 'TGC': 'C', 'TGA': '*', 'TGG': 'W',
|
| 42 |
+
'CTT': 'L', 'CTC': 'L', 'CTA': 'L', 'CTG': 'L',
|
| 43 |
+
'CCT': 'P', 'CCC': 'P', 'CCA': 'P', 'CCG': 'P',
|
| 44 |
+
'CAT': 'H', 'CAC': 'H', 'CAA': 'Q', 'CAG': 'Q',
|
| 45 |
+
'CGT': 'R', 'CGC': 'R', 'CGA': 'R', 'CGG': 'R',
|
| 46 |
+
'ATT': 'I', 'ATC': 'I', 'ATA': 'I', 'ATG': 'M',
|
| 47 |
+
'ACT': 'T', 'ACC': 'T', 'ACA': 'T', 'ACG': 'T',
|
| 48 |
+
'AAT': 'N', 'AAC': 'N', 'AAA': 'K', 'AAG': 'K',
|
| 49 |
+
'AGT': 'S', 'AGC': 'S', 'AGA': 'R', 'AGG': 'R',
|
| 50 |
+
'GTT': 'V', 'GTC': 'V', 'GTA': 'V', 'GTG': 'V',
|
| 51 |
+
'GCT': 'A', 'GCC': 'A', 'GCA': 'A', 'GCG': 'A',
|
| 52 |
+
'GAT': 'D', 'GAC': 'D', 'GAA': 'E', 'GAG': 'E',
|
| 53 |
+
'GGT': 'G', 'GGC': 'G', 'GGA': 'G', 'GGG': 'G'
|
| 54 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
# Mock DNA model for demonstration
|
| 57 |
+
class DNAModel:
|
| 58 |
+
"""Mock DNA generation model"""
|
| 59 |
+
def generate(self, cell_type: str, num_sequences: int = 1, length: int = 200, guidance_scale: float = 1.0):
|
| 60 |
+
"""Generate DNA sequences with cell-type specific patterns"""
|
| 61 |
+
sequences = []
|
| 62 |
+
|
| 63 |
+
for _ in range(num_sequences):
|
| 64 |
+
# Base generation
|
| 65 |
+
sequence = ''.join(random.choice(['A', 'T', 'C', 'G']) for _ in range(length))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
# Add cell-type specific patterns
|
| 68 |
+
if cell_type == "K562":
|
| 69 |
+
# Add GC-rich regions for K562
|
| 70 |
+
for i in range(0, length-8, 50):
|
| 71 |
+
sequence = sequence[:i] + 'GCGCGCGC' + sequence[i+8:]
|
| 72 |
+
elif cell_type == "GM12878":
|
| 73 |
+
# Add AT-rich regions for GM12878
|
| 74 |
+
for i in range(10, length-8, 60):
|
| 75 |
+
sequence = sequence[:i] + 'ATATATAT' + sequence[i+8:]
|
| 76 |
+
elif cell_type == "HepG2":
|
| 77 |
+
# Add mixed patterns for HepG2
|
| 78 |
+
for i in range(20, length-12, 70):
|
| 79 |
+
sequence = sequence[:i] + 'GCGATCGATCGC' + sequence[i+12:]
|
| 80 |
|
| 81 |
+
sequences.append(sequence)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
return sequences[0] if num_sequences == 1 else sequences
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
# Initialize model
|
| 86 |
+
model = DNAModel()
|
| 87 |
+
|
| 88 |
+
# Main application class
|
| 89 |
+
class DNADiffusionApp:
|
| 90 |
+
"""Main application with GPU-accelerated functions"""
|
| 91 |
|
| 92 |
def __init__(self):
|
| 93 |
self.current_sequence = ""
|
| 94 |
self.current_analysis = {}
|
| 95 |
+
self.generation_count = 0
|
| 96 |
+
|
| 97 |
+
@spaces.GPU(duration=60)
|
| 98 |
+
def generate_with_gpu(self, cell_type: str, guidance_scale: float = 1.0, use_enhanced: bool = True):
|
| 99 |
+
"""GPU-accelerated sequence generation"""
|
| 100 |
+
logger.info(f"Generating sequence on GPU for cell type: {cell_type}")
|
| 101 |
|
|
|
|
|
|
|
| 102 |
try:
|
| 103 |
+
# Simulate GPU computation time
|
| 104 |
+
time.sleep(2) # Simulated GPU processing
|
| 105 |
|
| 106 |
+
# Generate sequence
|
| 107 |
+
sequence = model.generate(cell_type, length=200, guidance_scale=guidance_scale)
|
| 108 |
+
|
| 109 |
+
# If enhanced mode, do additional processing
|
| 110 |
+
if use_enhanced:
|
| 111 |
+
sequence = self.enhance_sequence(sequence, cell_type)
|
| 112 |
+
|
| 113 |
+
self.generation_count += 1
|
| 114 |
+
logger.info(f"Successfully generated sequence #{self.generation_count}")
|
|
|
|
| 115 |
|
| 116 |
+
return sequence
|
| 117 |
|
| 118 |
except Exception as e:
|
| 119 |
+
logger.error(f"GPU generation failed: {e}")
|
| 120 |
+
raise
|
| 121 |
+
|
| 122 |
+
def enhance_sequence(self, sequence: str, cell_type: str) -> str:
|
| 123 |
+
"""Enhance sequence with additional patterns"""
|
| 124 |
+
# Add enhancer sequences based on cell type
|
| 125 |
+
enhancers = {
|
| 126 |
+
"K562": "GGGACTTTCC", # NF-κB binding site
|
| 127 |
+
"GM12878": "TGACGTCA", # CREB binding site
|
| 128 |
+
"HepG2": "TGTTGGTGG" # HNF4 binding site
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
if cell_type in enhancers:
|
| 132 |
+
# Insert enhancer at a reasonable position
|
| 133 |
+
pos = len(sequence) // 4
|
| 134 |
+
enhancer = enhancers[cell_type]
|
| 135 |
+
sequence = sequence[:pos] + enhancer + sequence[pos+len(enhancer):]
|
| 136 |
+
|
| 137 |
+
return sequence
|
| 138 |
|
| 139 |
def analyze_sequence(self, sequence: str) -> Dict[str, Any]:
|
| 140 |
+
"""Analyze DNA sequence properties"""
|
| 141 |
+
if not sequence:
|
| 142 |
+
return {}
|
| 143 |
+
|
| 144 |
+
# GC content
|
| 145 |
gc_count = sequence.count('G') + sequence.count('C')
|
| 146 |
+
gc_content = (gc_count / len(sequence)) * 100
|
| 147 |
|
| 148 |
+
# Melting temperature
|
| 149 |
if len(sequence) < 14:
|
| 150 |
tm = 4 * (sequence.count('G') + sequence.count('C')) + 2 * (sequence.count('A') + sequence.count('T'))
|
| 151 |
else:
|
| 152 |
tm = 81.5 + 0.41 * gc_content - 675 / len(sequence)
|
| 153 |
|
| 154 |
# Find restriction sites
|
| 155 |
+
restriction_sites = {}
|
| 156 |
enzymes = {
|
| 157 |
'EcoRI': 'GAATTC',
|
| 158 |
'BamHI': 'GGATCC',
|
| 159 |
+
'HindIII': 'AAGCTT',
|
| 160 |
+
'PstI': 'CTGCAG',
|
| 161 |
+
'XbaI': 'TCTAGA'
|
| 162 |
}
|
| 163 |
|
| 164 |
for enzyme, pattern in enzymes.items():
|
|
|
|
| 167 |
if sequence[i:i+len(pattern)] == pattern:
|
| 168 |
positions.append(i)
|
| 169 |
if positions:
|
| 170 |
+
restriction_sites[enzyme] = positions
|
| 171 |
+
|
| 172 |
+
# Translate to protein
|
| 173 |
+
protein = self.translate_to_protein(sequence)
|
| 174 |
|
| 175 |
return {
|
| 176 |
+
'length': len(sequence),
|
| 177 |
'gc_content': round(gc_content, 1),
|
| 178 |
'melting_temp': round(tm, 1),
|
| 179 |
+
'restriction_sites': restriction_sites,
|
| 180 |
+
'protein_length': len(protein),
|
| 181 |
+
'protein_sequence': protein[:50] + '...' if len(protein) > 50 else protein
|
| 182 |
}
|
| 183 |
|
| 184 |
+
def translate_to_protein(self, dna_sequence: str) -> str:
|
| 185 |
+
"""Translate DNA to protein sequence"""
|
| 186 |
+
protein = []
|
| 187 |
+
|
| 188 |
+
# Find start codon
|
| 189 |
+
start_pos = dna_sequence.find('ATG')
|
| 190 |
+
if start_pos == -1:
|
| 191 |
+
start_pos = 0
|
| 192 |
+
|
| 193 |
+
# Translate from start position
|
| 194 |
+
for i in range(start_pos, len(dna_sequence) - 2, 3):
|
| 195 |
+
codon = dna_sequence[i:i+3]
|
| 196 |
+
if len(codon) == 3:
|
| 197 |
+
amino_acid = CODON_TABLE.get(codon, 'X')
|
| 198 |
+
if amino_acid == '*': # Stop codon
|
| 199 |
+
break
|
| 200 |
+
protein.append(amino_acid)
|
| 201 |
+
|
| 202 |
+
return ''.join(protein)
|
| 203 |
+
|
| 204 |
+
@spaces.GPU(duration=30)
|
| 205 |
+
def batch_generate(self, cell_types: List[str], count: int = 5):
|
| 206 |
+
"""Generate multiple sequences with GPU acceleration"""
|
| 207 |
+
logger.info(f"Batch generating {count} sequences")
|
| 208 |
+
|
| 209 |
+
results = []
|
| 210 |
+
for i in range(count):
|
| 211 |
+
cell_type = cell_types[i % len(cell_types)]
|
| 212 |
+
sequence = model.generate(cell_type)
|
| 213 |
+
analysis = self.analyze_sequence(sequence)
|
| 214 |
+
|
| 215 |
+
results.append({
|
| 216 |
+
'id': f'SEQ_{i+1:03d}',
|
| 217 |
+
'cell_type': cell_type,
|
| 218 |
+
'sequence': sequence,
|
| 219 |
+
'analysis': analysis
|
| 220 |
+
})
|
| 221 |
|
| 222 |
+
return results
|
|
|
|
| 223 |
|
| 224 |
# Create app instance
|
| 225 |
+
app = DNADiffusionApp()
|
| 226 |
|
| 227 |
+
# Create the Gradio interface
|
| 228 |
+
def create_demo():
|
| 229 |
+
"""Create the Gradio interface"""
|
| 230 |
|
| 231 |
# Custom CSS
|
| 232 |
css = """
|
| 233 |
.gradio-container {
|
| 234 |
+
font-family: 'Arial', sans-serif;
|
| 235 |
+
}
|
| 236 |
+
.sequence-box {
|
| 237 |
+
font-family: 'Courier New', monospace;
|
| 238 |
+
background-color: #f0f0f0;
|
| 239 |
+
padding: 10px;
|
| 240 |
+
border-radius: 5px;
|
| 241 |
}
|
| 242 |
"""
|
| 243 |
|
| 244 |
+
with gr.Blocks(css=css, title="DNA-Diffusion") as demo:
|
| 245 |
+
# Header
|
| 246 |
+
gr.Markdown(
|
| 247 |
+
"""
|
| 248 |
+
# 🧬 DNA-Diffusion: AI-Powered Sequence Generation
|
| 249 |
+
|
| 250 |
+
Generate cell-type specific DNA sequences using advanced AI models.
|
| 251 |
+
This app uses GPU acceleration for optimal performance.
|
| 252 |
+
"""
|
| 253 |
+
)
|
| 254 |
|
| 255 |
+
# GPU status indicator
|
| 256 |
+
gpu_status = gr.Markdown(
|
| 257 |
+
f"🖥️ **GPU Status**: {'✅ Available' if SPACES_AVAILABLE else '❌ Not Available (CPU mode)'}"
|
| 258 |
+
)
|
| 259 |
+
|
| 260 |
+
with gr.Tabs():
|
| 261 |
+
# Tab 1: Single Sequence Generation
|
| 262 |
+
with gr.TabItem("🎯 Single Generation"):
|
| 263 |
+
with gr.Row():
|
| 264 |
+
with gr.Column(scale=1):
|
| 265 |
+
cell_type = gr.Dropdown(
|
| 266 |
+
choices=["K562", "GM12878", "HepG2"],
|
| 267 |
+
value="K562",
|
| 268 |
+
label="Cell Type",
|
| 269 |
+
info="Select the cell type for sequence generation"
|
| 270 |
+
)
|
| 271 |
+
|
| 272 |
+
guidance_scale = gr.Slider(
|
| 273 |
+
minimum=1.0,
|
| 274 |
+
maximum=10.0,
|
| 275 |
+
value=1.0,
|
| 276 |
+
step=0.5,
|
| 277 |
+
label="Guidance Scale",
|
| 278 |
+
info="Higher values create more cell-type specific patterns"
|
| 279 |
+
)
|
| 280 |
+
|
| 281 |
+
enhanced_mode = gr.Checkbox(
|
| 282 |
+
value=True,
|
| 283 |
+
label="Enhanced Mode",
|
| 284 |
+
info="Add cell-type specific enhancer sequences"
|
| 285 |
+
)
|
| 286 |
+
|
| 287 |
+
generate_btn = gr.Button(
|
| 288 |
+
"🚀 Generate Sequence",
|
| 289 |
+
variant="primary",
|
| 290 |
+
size="lg"
|
| 291 |
+
)
|
| 292 |
+
|
| 293 |
+
with gr.Column(scale=2):
|
| 294 |
+
sequence_output = gr.Textbox(
|
| 295 |
+
label="Generated DNA Sequence",
|
| 296 |
+
lines=5,
|
| 297 |
+
max_lines=10,
|
| 298 |
+
elem_classes="sequence-box"
|
| 299 |
+
)
|
| 300 |
+
|
| 301 |
+
analysis_output = gr.JSON(
|
| 302 |
+
label="Sequence Analysis"
|
| 303 |
+
)
|
| 304 |
|
| 305 |
+
# Examples
|
| 306 |
+
gr.Examples(
|
| 307 |
+
examples=[
|
| 308 |
+
["K562", 1.0, True],
|
| 309 |
+
["GM12878", 5.0, True],
|
| 310 |
+
["HepG2", 3.0, False]
|
| 311 |
+
],
|
| 312 |
+
inputs=[cell_type, guidance_scale, enhanced_mode]
|
| 313 |
+
)
|
| 314 |
+
|
| 315 |
+
# Tab 2: Batch Generation
|
| 316 |
+
with gr.TabItem("📦 Batch Generation"):
|
| 317 |
+
with gr.Row():
|
| 318 |
+
with gr.Column():
|
| 319 |
+
batch_cell_types = gr.CheckboxGroup(
|
| 320 |
+
choices=["K562", "GM12878", "HepG2"],
|
| 321 |
+
value=["K562", "GM12878"],
|
| 322 |
+
label="Cell Types for Batch"
|
| 323 |
+
)
|
| 324 |
+
|
| 325 |
+
batch_count = gr.Slider(
|
| 326 |
+
minimum=1,
|
| 327 |
+
maximum=10,
|
| 328 |
+
value=5,
|
| 329 |
+
step=1,
|
| 330 |
+
label="Number of Sequences"
|
| 331 |
+
)
|
| 332 |
+
|
| 333 |
+
batch_generate_btn = gr.Button(
|
| 334 |
+
"🚀 Generate Batch",
|
| 335 |
+
variant="primary"
|
| 336 |
+
)
|
| 337 |
+
|
| 338 |
+
with gr.Column():
|
| 339 |
+
batch_output = gr.Dataframe(
|
| 340 |
+
headers=["ID", "Cell Type", "Length", "GC%", "Tm(°C)"],
|
| 341 |
+
label="Batch Results"
|
| 342 |
+
)
|
| 343 |
+
|
| 344 |
+
download_btn = gr.Button("💾 Download Results")
|
| 345 |
|
| 346 |
+
# Tab 3: Analysis Tools
|
| 347 |
+
with gr.TabItem("🔬 Analysis Tools"):
|
| 348 |
+
gr.Markdown("### Paste or generate a sequence to analyze")
|
| 349 |
|
| 350 |
+
analysis_input = gr.Textbox(
|
| 351 |
+
label="DNA Sequence",
|
| 352 |
+
placeholder="Paste your DNA sequence here...",
|
| 353 |
+
lines=3
|
| 354 |
)
|
| 355 |
+
|
| 356 |
+
analyze_btn = gr.Button("🔍 Analyze")
|
| 357 |
+
|
| 358 |
+
with gr.Row():
|
| 359 |
+
gc_plot = gr.Plot(label="GC Content Distribution")
|
| 360 |
+
restriction_plot = gr.Plot(label="Restriction Sites")
|
| 361 |
+
|
| 362 |
+
detailed_analysis = gr.JSON(label="Detailed Analysis")
|
| 363 |
|
| 364 |
+
# Status bar
|
| 365 |
+
status_text = gr.Textbox(
|
| 366 |
+
label="Status",
|
| 367 |
+
value="Ready to generate sequences...",
|
| 368 |
+
interactive=False
|
| 369 |
+
)
|
|
|
|
|
|
|
|
|
|
| 370 |
|
| 371 |
# Event handlers
|
| 372 |
+
def generate_single(cell_type, guidance_scale, enhanced):
|
| 373 |
+
"""Handle single sequence generation"""
|
| 374 |
+
try:
|
| 375 |
+
status_text.value = "🔄 Generating sequence on GPU..."
|
| 376 |
+
|
| 377 |
+
# Generate sequence using GPU
|
| 378 |
+
sequence = app.generate_with_gpu(cell_type, guidance_scale, enhanced)
|
| 379 |
+
|
| 380 |
+
# Analyze the sequence
|
| 381 |
+
analysis = app.analyze_sequence(sequence)
|
| 382 |
+
|
| 383 |
+
status_text.value = f"✅ Successfully generated sequence for {cell_type}"
|
| 384 |
+
return sequence, analysis, status_text.value
|
| 385 |
+
|
| 386 |
+
except Exception as e:
|
| 387 |
+
error_msg = f"❌ Error: {str(e)}"
|
| 388 |
+
logger.error(error_msg)
|
| 389 |
+
return "", {}, error_msg
|
| 390 |
+
|
| 391 |
+
def generate_batch(cell_types, count):
|
| 392 |
+
"""Handle batch generation"""
|
| 393 |
+
if not cell_types:
|
| 394 |
+
return None, "❌ Please select at least one cell type"
|
| 395 |
|
| 396 |
+
try:
|
| 397 |
+
status_text.value = "🔄 Generating batch on GPU..."
|
| 398 |
+
|
| 399 |
+
# Generate batch
|
| 400 |
+
results = app.batch_generate(cell_types, count)
|
| 401 |
+
|
| 402 |
+
# Format for dataframe
|
| 403 |
+
df_data = []
|
| 404 |
+
for r in results:
|
| 405 |
+
df_data.append([
|
| 406 |
+
r['id'],
|
| 407 |
+
r['cell_type'],
|
| 408 |
+
r['analysis']['length'],
|
| 409 |
+
r['analysis']['gc_content'],
|
| 410 |
+
r['analysis']['melting_temp']
|
| 411 |
+
])
|
| 412 |
+
|
| 413 |
+
status_text.value = f"✅ Generated {len(results)} sequences"
|
| 414 |
+
return df_data, status_text.value
|
| 415 |
+
|
| 416 |
+
except Exception as e:
|
| 417 |
+
error_msg = f"❌ Error: {str(e)}"
|
| 418 |
+
logger.error(error_msg)
|
| 419 |
+
return None, error_msg
|
| 420 |
+
|
| 421 |
+
def analyze_sequence(sequence):
|
| 422 |
+
"""Analyze a given sequence"""
|
| 423 |
+
if not sequence:
|
| 424 |
+
return None, None, {}
|
| 425 |
+
|
| 426 |
+
analysis = app.analyze_sequence(sequence.upper())
|
| 427 |
+
|
| 428 |
+
# Create GC content plot
|
| 429 |
+
import matplotlib.pyplot as plt
|
| 430 |
|
| 431 |
+
fig1, ax1 = plt.subplots(figsize=(6, 4))
|
| 432 |
+
gc = analysis['gc_content']
|
| 433 |
+
at = 100 - gc
|
| 434 |
+
ax1.bar(['GC%', 'AT%'], [gc, at], color=['#4CAF50', '#2196F3'])
|
| 435 |
+
ax1.set_ylabel('Percentage')
|
| 436 |
+
ax1.set_title('Nucleotide Composition')
|
| 437 |
+
|
| 438 |
+
# Create restriction site plot
|
| 439 |
+
fig2, ax2 = plt.subplots(figsize=(8, 3))
|
| 440 |
+
if analysis['restriction_sites']:
|
| 441 |
+
y_pos = 0
|
| 442 |
+
colors = plt.cm.tab10(range(len(analysis['restriction_sites'])))
|
| 443 |
+
|
| 444 |
+
for (enzyme, positions), color in zip(analysis['restriction_sites'].items(), colors):
|
| 445 |
+
for pos in positions:
|
| 446 |
+
ax2.vlines(pos, y_pos - 0.4, y_pos + 0.4, color=color, linewidth=3)
|
| 447 |
+
ax2.text(-10, y_pos, enzyme, ha='right', va='center')
|
| 448 |
+
y_pos += 1
|
| 449 |
+
|
| 450 |
+
ax2.set_xlim(-50, len(sequence))
|
| 451 |
+
ax2.set_ylim(-0.5, len(analysis['restriction_sites']) - 0.5)
|
| 452 |
+
ax2.set_xlabel('Position (bp)')
|
| 453 |
+
ax2.set_title('Restriction Enzyme Sites')
|
| 454 |
+
ax2.grid(axis='x', alpha=0.3)
|
| 455 |
+
else:
|
| 456 |
+
ax2.text(0.5, 0.5, 'No restriction sites found',
|
| 457 |
+
ha='center', va='center', transform=ax2.transAxes)
|
| 458 |
+
|
| 459 |
+
return fig1, fig2, analysis
|
| 460 |
|
| 461 |
+
# Connect event handlers
|
| 462 |
generate_btn.click(
|
| 463 |
+
fn=generate_single,
|
| 464 |
+
inputs=[cell_type, guidance_scale, enhanced_mode],
|
| 465 |
+
outputs=[sequence_output, analysis_output, status_text]
|
| 466 |
)
|
| 467 |
|
| 468 |
+
batch_generate_btn.click(
|
| 469 |
+
fn=generate_batch,
|
| 470 |
+
inputs=[batch_cell_types, batch_count],
|
| 471 |
+
outputs=[batch_output, status_text]
|
| 472 |
+
)
|
| 473 |
|
| 474 |
+
analyze_btn.click(
|
| 475 |
+
fn=analyze_sequence,
|
| 476 |
+
inputs=[analysis_input],
|
| 477 |
+
outputs=[gc_plot, restriction_plot, detailed_analysis]
|
| 478 |
+
)
|
| 479 |
|
| 480 |
+
# Auto-analyze generated sequences
|
| 481 |
+
sequence_output.change(
|
| 482 |
+
fn=lambda seq: app.analyze_sequence(seq) if seq else {},
|
| 483 |
+
inputs=[sequence_output],
|
| 484 |
+
outputs=[analysis_output]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 485 |
)
|
| 486 |
|
| 487 |
return demo
|
| 488 |
|
| 489 |
+
# Main launch function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 490 |
if __name__ == "__main__":
|
| 491 |
+
# Print startup info
|
| 492 |
+
logger.info("=" * 50)
|
| 493 |
+
logger.info("DNA-Diffusion App Starting...")
|
| 494 |
+
logger.info(f"GPU Available: {SPACES_AVAILABLE}")
|
| 495 |
+
logger.info(f"Environment: {'Hugging Face Spaces' if os.getenv('SPACE_ID') else 'Local'}")
|
| 496 |
+
logger.info("=" * 50)
|
| 497 |
+
|
| 498 |
+
# Create and launch the demo
|
| 499 |
+
demo = create_demo()
|
| 500 |
+
|
| 501 |
+
# Launch with appropriate settings
|
| 502 |
+
if os.getenv("SPACE_ID"):
|
| 503 |
+
# Running on Hugging Face Spaces
|
| 504 |
+
demo.launch(
|
| 505 |
+
server_name="0.0.0.0",
|
| 506 |
+
server_port=7860,
|
| 507 |
+
share=False, # Don't set share=True on Spaces
|
| 508 |
+
show_error=True
|
| 509 |
+
)
|
| 510 |
+
else:
|
| 511 |
+
# Local development
|
| 512 |
+
demo.launch(
|
| 513 |
+
share=True,
|
| 514 |
+
show_error=True,
|
| 515 |
+
debug=True
|
| 516 |
+
)
|