Spaces:
Runtime error
Runtime error
Update app.py (#10)
Browse files- Update app.py (43eb70f73fd8688348085853d9672fb0ec306e74)
Co-authored-by: Yanis Adel <[email protected]>
app.py
CHANGED
|
@@ -22,61 +22,55 @@ class Log:
|
|
| 22 |
|
| 23 |
# --- Main Function ---
|
| 24 |
@spaces.GPU
|
| 25 |
-
def run_chatnt(
|
| 26 |
with open(log_file, "a") as log:
|
| 27 |
log.write("Request started\n\n")
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
lines =
|
| 36 |
sequence = ""
|
| 37 |
for line in lines:
|
| 38 |
line = line.strip()
|
| 39 |
-
if not line:
|
| 40 |
continue
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
if sequence:
|
| 48 |
-
dna_sequences.append(sequence)
|
| 49 |
-
|
| 50 |
-
if not dna_sequences:
|
| 51 |
-
return "No DNA sequences found in the input."
|
| 52 |
|
| 53 |
with open(log_file, "a") as log:
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# Build prompt
|
| 58 |
-
num_sequences = len(dna_sequences)
|
| 59 |
num_placeholders = custom_question.count("<DNA>")
|
| 60 |
|
| 61 |
-
if
|
|
|
|
|
|
|
| 62 |
if num_placeholders == 0:
|
| 63 |
-
|
| 64 |
elif num_placeholders == 1:
|
| 65 |
english_sequence = custom_question
|
| 66 |
else:
|
| 67 |
-
return "
|
| 68 |
-
elif num_sequences > 1:
|
| 69 |
-
if num_placeholders != num_sequences:
|
| 70 |
-
return f"You provided {num_sequences} DNA sequences but only {num_placeholders} <DNA> placeholders. Please specify one <DNA> for each sequence."
|
| 71 |
-
english_sequence = custom_question
|
| 72 |
-
else:
|
| 73 |
-
return "No DNA sequences detected."
|
| 74 |
-
|
| 75 |
-
with open(log_file, "a") as log:
|
| 76 |
-
log.write(f"Initial user question : {custom_question}\n")
|
| 77 |
-
log.write(f"Full english prompt : {english_sequence}\n")
|
| 78 |
|
| 79 |
with open(log_file, "a") as log:
|
|
|
|
|
|
|
| 80 |
log.write("Calling model\n")
|
| 81 |
|
| 82 |
output = pipe(
|
|
@@ -86,8 +80,11 @@ def run_chatnt(fasta_text, custom_question):
|
|
| 86 |
}
|
| 87 |
)
|
| 88 |
|
|
|
|
|
|
|
|
|
|
| 89 |
with open(log_file, "a") as log:
|
| 90 |
-
log.write(f"Output
|
| 91 |
|
| 92 |
return output
|
| 93 |
|
|
@@ -103,14 +100,20 @@ with gr.Blocks(css=css) as demo:
|
|
| 103 |
|
| 104 |
with gr.Row():
|
| 105 |
with gr.Column(scale=1):
|
| 106 |
-
|
| 107 |
-
label="Paste your DNA
|
| 108 |
-
placeholder="
|
| 109 |
-
lines=
|
| 110 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
custom_question = gr.Textbox(
|
| 112 |
label="English Question",
|
| 113 |
-
placeholder="e.g., Does this sequence contain a donor splice site?
|
| 114 |
)
|
| 115 |
|
| 116 |
submit_btn = gr.Button("Run Query", variant="primary")
|
|
@@ -120,14 +123,15 @@ with gr.Blocks(css=css) as demo:
|
|
| 120 |
|
| 121 |
submit_btn.click(
|
| 122 |
run_chatnt,
|
| 123 |
-
inputs=[
|
| 124 |
outputs=output,
|
| 125 |
)
|
| 126 |
|
| 127 |
gr.Markdown("""
|
| 128 |
-
**Note:**
|
| 129 |
-
|
| 130 |
-
|
|
|
|
| 131 |
""")
|
| 132 |
|
| 133 |
with gr.Accordion("Logs", open=True):
|
|
|
|
| 22 |
|
| 23 |
# --- Main Function ---
|
| 24 |
@spaces.GPU
|
| 25 |
+
def run_chatnt(dna_text, fasta_file, custom_question):
|
| 26 |
with open(log_file, "a") as log:
|
| 27 |
log.write("Request started\n\n")
|
| 28 |
|
| 29 |
+
# Read DNA sequence from text field or file
|
| 30 |
+
dna_sequence = ""
|
| 31 |
+
if dna_text and dna_text.strip():
|
| 32 |
+
dna_sequence = dna_text.strip().replace("\n", "")
|
| 33 |
+
elif fasta_file is not None:
|
| 34 |
+
file_content = fasta_file.read().decode("utf-8")
|
| 35 |
+
lines = file_content.splitlines()
|
| 36 |
sequence = ""
|
| 37 |
for line in lines:
|
| 38 |
line = line.strip()
|
| 39 |
+
if not line or line.startswith(">"):
|
| 40 |
continue
|
| 41 |
+
sequence += line
|
| 42 |
+
dna_sequence = sequence
|
| 43 |
+
|
| 44 |
+
dna_sequences = []
|
| 45 |
+
if dna_sequence:
|
| 46 |
+
dna_sequences.append(dna_sequence)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
with open(log_file, "a") as log:
|
| 49 |
+
log.write(f"DNA sequences found: {dna_sequences}\n")
|
| 50 |
+
|
| 51 |
+
# Check DNA sequences count
|
| 52 |
+
if len(dna_sequences) > 1:
|
| 53 |
+
return "You must use only one DNA sequence."
|
| 54 |
+
|
| 55 |
+
if not custom_question or custom_question.strip() == "":
|
| 56 |
+
return "Please provide a question."
|
| 57 |
|
| 58 |
# Build prompt
|
|
|
|
| 59 |
num_placeholders = custom_question.count("<DNA>")
|
| 60 |
|
| 61 |
+
if len(dna_sequences) == 0:
|
| 62 |
+
english_sequence = custom_question
|
| 63 |
+
else:
|
| 64 |
if num_placeholders == 0:
|
| 65 |
+
return "Your question must include the <DNA> token at the position where the DNA sequence should be inserted."
|
| 66 |
elif num_placeholders == 1:
|
| 67 |
english_sequence = custom_question
|
| 68 |
else:
|
| 69 |
+
return "You can only provide one DNA sequence, so you must use exactly one <DNA> placeholder."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
with open(log_file, "a") as log:
|
| 72 |
+
log.write(f"Initial user question: {custom_question}\n")
|
| 73 |
+
log.write(f"Full english prompt: {english_sequence}\n")
|
| 74 |
log.write("Calling model\n")
|
| 75 |
|
| 76 |
output = pipe(
|
|
|
|
| 80 |
}
|
| 81 |
)
|
| 82 |
|
| 83 |
+
if len(dna_sequences) == 0:
|
| 84 |
+
return f"{output}\n\nNote: Careful, you did not provide any DNA sequence."
|
| 85 |
+
|
| 86 |
with open(log_file, "a") as log:
|
| 87 |
+
log.write(f"Output: {output}\n")
|
| 88 |
|
| 89 |
return output
|
| 90 |
|
|
|
|
| 100 |
|
| 101 |
with gr.Row():
|
| 102 |
with gr.Column(scale=1):
|
| 103 |
+
dna_text = gr.Textbox(
|
| 104 |
+
label="Paste your DNA sequence",
|
| 105 |
+
placeholder="ATGCATGCATGC...",
|
| 106 |
+
lines=4
|
| 107 |
)
|
| 108 |
+
|
| 109 |
+
fasta_file = gr.File(
|
| 110 |
+
label="Or upload your FASTA file",
|
| 111 |
+
file_types=[".fasta", ".fa", ".txt"]
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
custom_question = gr.Textbox(
|
| 115 |
label="English Question",
|
| 116 |
+
placeholder="e.g., Does this sequence <DNA> contain a donor splice site?"
|
| 117 |
)
|
| 118 |
|
| 119 |
submit_btn = gr.Button("Run Query", variant="primary")
|
|
|
|
| 123 |
|
| 124 |
submit_btn.click(
|
| 125 |
run_chatnt,
|
| 126 |
+
inputs=[dna_text, fasta_file, custom_question],
|
| 127 |
outputs=output,
|
| 128 |
)
|
| 129 |
|
| 130 |
gr.Markdown("""
|
| 131 |
+
**Note:**
|
| 132 |
+
✅ You must use **exactly one DNA sequence** (either paste it or upload a file).
|
| 133 |
+
✅ Your question must include the `<DNA>` token **exactly once** at the position where the DNA will be inserted.
|
| 134 |
+
Example: *"Does this sequence <DNA> contain a donor splice site?"*
|
| 135 |
""")
|
| 136 |
|
| 137 |
with gr.Accordion("Logs", open=True):
|