Spaces:
Running
on
Zero
Running
on
Zero
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# --- Imports ---
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import pipeline
|
4 |
+
import pandas as pd
|
5 |
+
import os
|
6 |
+
|
7 |
+
# --- Load Model ---
|
8 |
+
pipe = pipeline(model="InstaDeepAI/ChatNT", trust_remote_code=True)
|
9 |
+
|
10 |
+
# --- Logs ---
|
11 |
+
log_file = "logs.txt"
|
12 |
+
|
13 |
+
class Log:
|
14 |
+
def __init__(self, log_file):
|
15 |
+
self.log_file = log_file
|
16 |
+
|
17 |
+
def __call__(self):
|
18 |
+
if not os.path.exists(self.log_file):
|
19 |
+
return ""
|
20 |
+
with open(self.log_file, "r") as f:
|
21 |
+
return f.read()
|
22 |
+
|
23 |
+
# --- Main Function ---
|
24 |
+
def run_chatnt(input_file, custom_question):
|
25 |
+
with open(log_file, "a") as log:
|
26 |
+
log.write("Request started\n")
|
27 |
+
|
28 |
+
if not custom_question or custom_question.strip() == "":
|
29 |
+
return pd.DataFrame(), None
|
30 |
+
|
31 |
+
# Read DNA sequences
|
32 |
+
dna_sequences = []
|
33 |
+
if input_file is not None:
|
34 |
+
with open(input_file.name, "r") as f:
|
35 |
+
lines = f.readlines()
|
36 |
+
for line in lines:
|
37 |
+
if line.startswith(">"):
|
38 |
+
continue
|
39 |
+
dna_sequences.append(line.strip())
|
40 |
+
|
41 |
+
if not dna_sequences:
|
42 |
+
return pd.DataFrame(), None
|
43 |
+
|
44 |
+
# Build prompt
|
45 |
+
english_sequence = custom_question + " <DNA>"
|
46 |
+
|
47 |
+
# Call model
|
48 |
+
output = pipe(
|
49 |
+
inputs={
|
50 |
+
"english_sequence": english_sequence,
|
51 |
+
"dna_sequences": dna_sequences
|
52 |
+
}
|
53 |
+
)
|
54 |
+
|
55 |
+
# Wrap output
|
56 |
+
results = []
|
57 |
+
if isinstance(output, list):
|
58 |
+
for item in output:
|
59 |
+
results.append({"Result": item})
|
60 |
+
else:
|
61 |
+
results.append({"Result": output})
|
62 |
+
|
63 |
+
df = pd.DataFrame(results)
|
64 |
+
output_file = "output.csv"
|
65 |
+
df.to_csv(output_file, index=False)
|
66 |
+
|
67 |
+
with open(log_file, "a") as log:
|
68 |
+
log.write("Request finished\n")
|
69 |
+
|
70 |
+
return df, output_file
|
71 |
+
|
72 |
+
# --- Gradio Interface ---
|
73 |
+
css = """
|
74 |
+
.gradio-container { font-family: sans-serif; }
|
75 |
+
.gr-button { color: white; border-color: black; background: black; }
|
76 |
+
footer { display: none !important; }
|
77 |
+
"""
|
78 |
+
|
79 |
+
with gr.Blocks(css=css) as demo:
|
80 |
+
gr.Markdown("# 🧬 ChatNT — DNA Sequence Query Assistant")
|
81 |
+
|
82 |
+
with gr.Row():
|
83 |
+
with gr.Column(scale=1):
|
84 |
+
input_file = gr.File(
|
85 |
+
label="Upload DNA Sequence File (.fasta or .txt)",
|
86 |
+
file_types=[".fasta", ".fa", ".txt"]
|
87 |
+
)
|
88 |
+
custom_question = gr.Textbox(
|
89 |
+
label="English Question (required)",
|
90 |
+
placeholder="e.g., Does this sequence contain a donor splice site?"
|
91 |
+
)
|
92 |
+
|
93 |
+
submit_btn = gr.Button("Run Query", variant="primary")
|
94 |
+
|
95 |
+
with gr.Column(scale=2):
|
96 |
+
output_df = gr.DataFrame(
|
97 |
+
label="Results",
|
98 |
+
headers=["Result"]
|
99 |
+
)
|
100 |
+
output_file = gr.File(label="Download Results (CSV)")
|
101 |
+
|
102 |
+
submit_btn.click(
|
103 |
+
run_chatnt,
|
104 |
+
inputs=[input_file, custom_question],
|
105 |
+
outputs=[output_df, output_file],
|
106 |
+
)
|
107 |
+
|
108 |
+
gr.Markdown("""
|
109 |
+
**Note:** Your question **must** include the `<DNA>` token if needed for multiple sequences.
|
110 |
+
""")
|
111 |
+
|
112 |
+
with gr.Accordion("Logs", open=True):
|
113 |
+
log_display = Log(log_file)
|
114 |
+
gr.Markdown(log_display)
|
115 |
+
|
116 |
+
# --- Launch ---
|
117 |
+
if __name__ == "__main__":
|
118 |
+
demo.queue()
|
119 |
+
demo.launch(debug=True, show_error=True)
|