Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,7 @@ 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')
|
|
@@ -176,6 +177,19 @@ class DNADiffusionApp:
|
|
| 176 |
|
| 177 |
app = DNADiffusionApp()
|
| 178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
# HTML for 3D Viewer
|
| 180 |
HTML_3D_VIEWER = """
|
| 181 |
<!DOCTYPE html>
|
|
@@ -466,25 +480,41 @@ def create_demo():
|
|
| 466 |
css = """
|
| 467 |
.gradio-container {
|
| 468 |
font-family: 'Arial', sans-serif;
|
|
|
|
| 469 |
}
|
| 470 |
.sequence-box {
|
| 471 |
font-family: 'Courier New', monospace;
|
| 472 |
-
background-color: #
|
| 473 |
-
|
| 474 |
-
|
|
|
|
|
|
|
| 475 |
}
|
| 476 |
iframe {
|
| 477 |
border: none;
|
| 478 |
border-radius: 10px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 479 |
}
|
| 480 |
"""
|
| 481 |
|
| 482 |
-
with gr.Blocks(css=css, title="DNA-Diffusion
|
| 483 |
gr.Markdown(
|
| 484 |
"""
|
| 485 |
-
# 🧬 DNA-Diffusion: AI-Powered
|
|
|
|
|
|
|
| 486 |
|
| 487 |
-
|
| 488 |
"""
|
| 489 |
)
|
| 490 |
|
|
@@ -545,8 +575,9 @@ def create_demo():
|
|
| 545 |
gr.Markdown("The 3D viewer shows the double helix structure of your generated DNA sequence.")
|
| 546 |
|
| 547 |
# HTML component for 3D viewer
|
|
|
|
| 548 |
viewer_html = gr.HTML(
|
| 549 |
-
value=f'<iframe src="data:text/html;charset=utf-8,{
|
| 550 |
label="3D Molecular Viewer"
|
| 551 |
)
|
| 552 |
|
|
@@ -556,7 +587,27 @@ def create_demo():
|
|
| 556 |
variant="secondary"
|
| 557 |
)
|
| 558 |
|
| 559 |
-
# Tab 3:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 560 |
with gr.TabItem("📦 Batch Generation"):
|
| 561 |
with gr.Row():
|
| 562 |
with gr.Column():
|
|
@@ -609,7 +660,7 @@ def create_demo():
|
|
| 609 |
|
| 610 |
def update_3d_viewer(sequence):
|
| 611 |
if not sequence:
|
| 612 |
-
return
|
| 613 |
|
| 614 |
# Create HTML with embedded sequence data
|
| 615 |
html_with_sequence = HTML_3D_VIEWER.replace(
|
|
@@ -617,9 +668,9 @@ def create_demo():
|
|
| 617 |
f"updateSequence('{sequence}');\n window.addEventListener('message'"
|
| 618 |
)
|
| 619 |
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
|
| 624 |
def generate_batch(cell_types, count):
|
| 625 |
if not cell_types:
|
|
@@ -675,6 +726,24 @@ def create_demo():
|
|
| 675 |
inputs=[batch_cell_types, batch_count],
|
| 676 |
outputs=[batch_output, status_text]
|
| 677 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 678 |
|
| 679 |
return demo
|
| 680 |
|
|
|
|
| 12 |
import random
|
| 13 |
import numpy as np
|
| 14 |
from datetime import datetime
|
| 15 |
+
import urllib.parse
|
| 16 |
|
| 17 |
# Configure logging
|
| 18 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
|
|
| 177 |
|
| 178 |
app = DNADiffusionApp()
|
| 179 |
|
| 180 |
+
def load_html_file(filename):
|
| 181 |
+
"""Safely load and prepare HTML file for iframe embedding"""
|
| 182 |
+
try:
|
| 183 |
+
with open(filename, 'r', encoding='utf-8') as f:
|
| 184 |
+
content = f.read()
|
| 185 |
+
# Properly encode content for data URI
|
| 186 |
+
encoded_content = urllib.parse.quote(content, safe='')
|
| 187 |
+
return f'<iframe src="data:text/html;charset=utf-8,{encoded_content}" width="100%" height="800px" style="border: none; border-radius: 10px;"></iframe>'
|
| 188 |
+
except FileNotFoundError:
|
| 189 |
+
return f'<div style="padding: 40px; text-align: center; color: #ff6b6b; background: #2a2a2a; border-radius: 10px;"><h3>⚠️ File Not Found</h3><p>{filename} not found in the current directory.</p></div>'
|
| 190 |
+
except Exception as e:
|
| 191 |
+
return f'<div style="padding: 40px; text-align: center; color: #ff6b6b; background: #2a2a2a; border-radius: 10px;"><h3>❌ Error Loading File</h3><p>{str(e)}</p></div>'
|
| 192 |
+
|
| 193 |
# HTML for 3D Viewer
|
| 194 |
HTML_3D_VIEWER = """
|
| 195 |
<!DOCTYPE html>
|
|
|
|
| 480 |
css = """
|
| 481 |
.gradio-container {
|
| 482 |
font-family: 'Arial', sans-serif;
|
| 483 |
+
background: linear-gradient(135deg, #0a0a0a 0%, #1a1a1a 100%);
|
| 484 |
}
|
| 485 |
.sequence-box {
|
| 486 |
font-family: 'Courier New', monospace;
|
| 487 |
+
background-color: #1e1e1e;
|
| 488 |
+
color: #00ff88;
|
| 489 |
+
padding: 15px;
|
| 490 |
+
border-radius: 8px;
|
| 491 |
+
border: 1px solid #00ff88;
|
| 492 |
}
|
| 493 |
iframe {
|
| 494 |
border: none;
|
| 495 |
border-radius: 10px;
|
| 496 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 497 |
+
}
|
| 498 |
+
.gr-button-primary {
|
| 499 |
+
background: linear-gradient(135deg, #00ff88, #0088ff) !important;
|
| 500 |
+
border: none !important;
|
| 501 |
+
color: #000 !important;
|
| 502 |
+
}
|
| 503 |
+
.gr-button-secondary {
|
| 504 |
+
background: linear-gradient(135deg, #ff6b6b, #ff9f40) !important;
|
| 505 |
+
border: none !important;
|
| 506 |
+
color: #fff !important;
|
| 507 |
}
|
| 508 |
"""
|
| 509 |
|
| 510 |
+
with gr.Blocks(css=css, title="DNA-Diffusion Suite", theme=gr.themes.Soft()) as demo:
|
| 511 |
gr.Markdown(
|
| 512 |
"""
|
| 513 |
+
# 🧬 DNA-Diffusion Suite: AI-Powered Molecular Engineering
|
| 514 |
+
|
| 515 |
+
Generate, visualize, and gamify DNA sequences with advanced AI models!
|
| 516 |
|
| 517 |
+
[](https://huggingface.co/spaces)
|
| 518 |
"""
|
| 519 |
)
|
| 520 |
|
|
|
|
| 575 |
gr.Markdown("The 3D viewer shows the double helix structure of your generated DNA sequence.")
|
| 576 |
|
| 577 |
# HTML component for 3D viewer
|
| 578 |
+
encoded_viewer = urllib.parse.quote(HTML_3D_VIEWER, safe='')
|
| 579 |
viewer_html = gr.HTML(
|
| 580 |
+
value=f'<iframe src="data:text/html;charset=utf-8,{encoded_viewer}" width="100%" height="600px"></iframe>',
|
| 581 |
label="3D Molecular Viewer"
|
| 582 |
)
|
| 583 |
|
|
|
|
| 587 |
variant="secondary"
|
| 588 |
)
|
| 589 |
|
| 590 |
+
# Tab 3: DNA Casino (if external HTML exists)
|
| 591 |
+
with gr.TabItem("🎰 DNA Casino"):
|
| 592 |
+
gr.Markdown("### DNA Slot Machine Game")
|
| 593 |
+
gr.Markdown("Experience DNA generation as a casino game!")
|
| 594 |
+
|
| 595 |
+
# Load external HTML file using helper function
|
| 596 |
+
casino_html = gr.HTML(
|
| 597 |
+
value=load_html_file('dna-slot-machine.html'),
|
| 598 |
+
label="DNA Casino Game"
|
| 599 |
+
)
|
| 600 |
+
|
| 601 |
+
# Button to capture sequence from casino
|
| 602 |
+
with gr.Row():
|
| 603 |
+
casino_sequence = gr.Textbox(
|
| 604 |
+
label="Casino Generated Sequence",
|
| 605 |
+
placeholder="Sequence from casino will appear here",
|
| 606 |
+
interactive=False
|
| 607 |
+
)
|
| 608 |
+
analyze_casino_btn = gr.Button("🔬 Analyze Casino Sequence", variant="secondary")
|
| 609 |
+
|
| 610 |
+
# Tab 4: Batch Generation
|
| 611 |
with gr.TabItem("📦 Batch Generation"):
|
| 612 |
with gr.Row():
|
| 613 |
with gr.Column():
|
|
|
|
| 660 |
|
| 661 |
def update_3d_viewer(sequence):
|
| 662 |
if not sequence:
|
| 663 |
+
return viewer_html.value
|
| 664 |
|
| 665 |
# Create HTML with embedded sequence data
|
| 666 |
html_with_sequence = HTML_3D_VIEWER.replace(
|
|
|
|
| 668 |
f"updateSequence('{sequence}');\n window.addEventListener('message'"
|
| 669 |
)
|
| 670 |
|
| 671 |
+
# Properly encode content for data URI
|
| 672 |
+
encoded_content = urllib.parse.quote(html_with_sequence, safe='')
|
| 673 |
+
return f'<iframe src="data:text/html;charset=utf-8,{encoded_content}" width="100%" height="600px"></iframe>'
|
| 674 |
|
| 675 |
def generate_batch(cell_types, count):
|
| 676 |
if not cell_types:
|
|
|
|
| 726 |
inputs=[batch_cell_types, batch_count],
|
| 727 |
outputs=[batch_output, status_text]
|
| 728 |
)
|
| 729 |
+
|
| 730 |
+
# Casino sequence analysis
|
| 731 |
+
def analyze_casino_sequence(seq):
|
| 732 |
+
if not seq:
|
| 733 |
+
return {}, "❌ No sequence to analyze"
|
| 734 |
+
|
| 735 |
+
analysis = app.analyze_sequence(seq)
|
| 736 |
+
return analysis, "✅ Casino sequence analyzed"
|
| 737 |
+
|
| 738 |
+
try:
|
| 739 |
+
analyze_casino_btn.click(
|
| 740 |
+
fn=analyze_casino_sequence,
|
| 741 |
+
inputs=[casino_sequence],
|
| 742 |
+
outputs=[analysis_output, status_text]
|
| 743 |
+
)
|
| 744 |
+
except NameError:
|
| 745 |
+
# Casino tab components may not be defined if HTML file is missing
|
| 746 |
+
pass
|
| 747 |
|
| 748 |
return demo
|
| 749 |
|