yangheng commited on
Commit
a7bffc3
·
1 Parent(s): e26d066
Files changed (2) hide show
  1. app.py +85 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import multiprocessing
2
+ import gradio as gr
3
+ import torch
4
+ from omnigenome import OmniGenomeModelForRNADesign # Assuming this is where the model class is defined
5
+ import RNA # ViennaRNA library for RNA structure plotting
6
+ import tempfile # For handling temporary files
7
+ import os # For file operations
8
+
9
+ # Initialize the model for RNA design
10
+ model = OmniGenomeModelForRNADesign(model_path="anonymous8/OmniGenome-186M")
11
+ model.to("cuda" if torch.cuda.is_available() else "cpu")
12
+
13
+
14
+ # RNA Design function with structure plotting
15
+ def design_rna(target_structure):
16
+ if not 0 < len(target_structure) <= 50:
17
+ return "The online demo only supports RNA structures with 1 to 100 characters.", None
18
+
19
+ # Run the genetic algorithm to design RNA sequences
20
+ best_sequences = model.run_rna_design(
21
+ structure=target_structure.strip(),
22
+ mutation_ratio=0.5,
23
+ num_population=50,
24
+ num_generation=100
25
+ )
26
+
27
+ # Select the best sequence (assuming it's the first one)
28
+ best_sequence = best_sequences[0]
29
+
30
+ # Generate the RNA secondary structure plot
31
+ plot_path = plot_rna_structure(best_sequence, target_structure)
32
+
33
+ return best_sequence, plot_path
34
+
35
+
36
+ # Function to plot RNA structure and return the path to the SVG image
37
+ def plot_rna_structure(sequence, structure):
38
+ # Create a temporary file to save the SVG plot
39
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".svg") as tmpfile:
40
+ plot_path = tmpfile.name
41
+
42
+ # Plot RNA structure using ViennaRNA
43
+ RNA.svg_rna_plot(sequence, structure, plot_path)
44
+
45
+ return plot_path
46
+
47
+
48
+ # Launch the app
49
+ if __name__ == "__main__":
50
+ multiprocessing.set_start_method('spawn', force=True)
51
+
52
+ # Gradio Interface with vertical layout
53
+ with gr.Blocks() as iface:
54
+ gr.Markdown("# RNA Design with OmniGenome")
55
+ gr.Markdown(
56
+ "Enter a target RNA secondary structure to generate a designed RNA sequence and visualize its structure. "
57
+ "Please note that the online demo only supports RNA structures with 1 to 50 bases due to computational resource shortage."
58
+ "For larger structures, please run the model locally."
59
+ )
60
+ gr.Markdown("""
61
+ ### Example RNA Structures:
62
+ - `(((((......)))))`
63
+ - `((((((.((((....))))))).)))..........`
64
+ - `((....)).((....))`
65
+ - `.(((((((((((...)))))....)))))).`
66
+ - `..((((((((.....))))((((.....))))))))..`
67
+ """)
68
+
69
+ with gr.Column():
70
+ target_structure_input = gr.Textbox(
71
+ label="Target RNA Secondary Structure",
72
+ placeholder="Enter RNA structure here, e.g., (((((......)))))"
73
+ )
74
+ output_sequence = gr.Textbox(label="Designed RNA Sequence")
75
+ output_plot = gr.Image(type="filepath", label="RNA Structure Plot")
76
+
77
+ # Defining the function call on input
78
+ submit_button = gr.Button("Submit")
79
+ submit_button.click(
80
+ design_rna,
81
+ inputs=target_structure_input,
82
+ outputs=[output_sequence, output_plot]
83
+ )
84
+
85
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ omnigenome
2
+ transformers
3
+ gradio