Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,79 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
gr.Markdown("# Test Interface")
|
| 16 |
-
with gr.Row():
|
| 17 |
-
input_box = gr.Textbox(label="Input")
|
| 18 |
-
output_box = gr.Textbox(label="Output")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import pickle
|
| 4 |
+
import subprocess
|
| 5 |
+
from predictor import Predictor
|
| 6 |
+
from tensorflow.keras.models import load_model
|
| 7 |
+
from ml_simplified_tree import maximum_likelihood
|
| 8 |
|
| 9 |
+
# --------- Load Models & Assets ---------
|
| 10 |
+
# Boundary-aware model
|
| 11 |
+
boundary_model = Predictor("best_boundary_aware_model.pth")
|
| 12 |
|
| 13 |
+
# Keras model
|
| 14 |
+
keras_model = load_model("best_model.keras")
|
| 15 |
+
with open("kmer_to_index.pkl", "rb") as f:
|
| 16 |
+
kmer_to_index = pickle.load(f)
|
| 17 |
|
| 18 |
+
# Utility function for keras model
|
| 19 |
+
def predict_with_keras(sequence):
|
| 20 |
+
kmers = [sequence[i:i+6] for i in range(len(sequence)-5)]
|
| 21 |
+
indices = [kmer_to_index.get(kmer, 0) for kmer in kmers]
|
| 22 |
+
input_arr = torch.tensor([indices])
|
| 23 |
+
prediction = keras_model.predict(input_arr)
|
| 24 |
+
return str(prediction)
|
| 25 |
|
| 26 |
+
# MAFFT + IQTree pipeline
|
| 27 |
+
def run_mafft_and_iqtree():
|
| 28 |
+
try:
|
| 29 |
+
subprocess.run(["mafft", "--auto", "f_gene_sequences_aligned.fasta"], check=True)
|
| 30 |
+
subprocess.run(["iqtree", "-s", "f_gene_sequences.phy.treefile", "-m", "GTR"], check=True)
|
| 31 |
+
return "MAFFT and IQTree executed successfully."
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return f"Error: {str(e)}"
|
| 34 |
|
| 35 |
+
# Maximum Likelihood Tree
|
| 36 |
+
def run_maximum_likelihood():
|
| 37 |
+
try:
|
| 38 |
+
result = maximum_likelihood("f gene clean dataset.csv")
|
| 39 |
+
return result
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return f"Error: {str(e)}"
|
| 42 |
|
| 43 |
+
# --------- Gradio Interface ---------
|
| 44 |
+
def boundary_model_predict(sequence):
|
| 45 |
+
return boundary_model.predict(sequence)
|
| 46 |
|
| 47 |
+
gr_interface = gr.TabbedInterface(
|
| 48 |
+
[
|
| 49 |
+
gr.Interface(
|
| 50 |
+
fn=boundary_model_predict,
|
| 51 |
+
inputs=gr.Textbox(label="DNA Sequence"),
|
| 52 |
+
outputs=gr.Textbox(label="Boundary Model Prediction"),
|
| 53 |
+
title="Boundary-Aware Model"
|
| 54 |
+
),
|
| 55 |
+
gr.Interface(
|
| 56 |
+
fn=predict_with_keras,
|
| 57 |
+
inputs=gr.Textbox(label="DNA Sequence"),
|
| 58 |
+
outputs=gr.Textbox(label="Keras Model Prediction"),
|
| 59 |
+
title="Keras Model"
|
| 60 |
+
),
|
| 61 |
+
gr.Interface(
|
| 62 |
+
fn=run_mafft_and_iqtree,
|
| 63 |
+
inputs=[],
|
| 64 |
+
outputs="text",
|
| 65 |
+
title="MAFFT + IQTree Runner"
|
| 66 |
+
),
|
| 67 |
+
gr.Interface(
|
| 68 |
+
fn=run_maximum_likelihood,
|
| 69 |
+
inputs=[],
|
| 70 |
+
outputs="text",
|
| 71 |
+
title="Simplified ML Tree Generator"
|
| 72 |
+
)
|
| 73 |
+
],
|
| 74 |
+
tab_names=["Boundary Model", "Keras Model", "MAFFT + IQTree", "ML Tree"]
|
| 75 |
+
)
|
| 76 |
|
| 77 |
+
# --------- Launch ---------
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
gr_interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|