Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -76,16 +76,23 @@ def parse_fasta(content: str) -> list:
|
|
| 76 |
logger.error(f"Error parsing FASTA: {str(e)}")
|
| 77 |
raise
|
| 78 |
|
| 79 |
-
def predict_sequence(
|
| 80 |
"""Process FASTA input and return formatted predictions"""
|
| 81 |
try:
|
| 82 |
logger.info("Starting prediction process")
|
| 83 |
|
| 84 |
-
if
|
| 85 |
return "Please upload a FASTA file"
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 91 |
logger.info(f"Using device: {device}")
|
|
@@ -147,14 +154,22 @@ Non-human probability: {float(probs[0][0]):0.4f}
|
|
| 147 |
logger.error(f"Unexpected error in predict_sequence: {str(e)}")
|
| 148 |
return f"An unexpected error occurred: {str(e)}"
|
| 149 |
|
| 150 |
-
# Create Gradio interface
|
| 151 |
iface = gr.Interface(
|
| 152 |
fn=predict_sequence,
|
| 153 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
outputs=gr.Textbox(label="Prediction Results", lines=10),
|
| 155 |
title="Virus Host Classifier",
|
| 156 |
-
description="Upload a FASTA file to predict whether a virus sequence is likely to infect human or non-human hosts.
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
cache_examples=True
|
| 159 |
)
|
| 160 |
|
|
|
|
| 76 |
logger.error(f"Error parsing FASTA: {str(e)}")
|
| 77 |
raise
|
| 78 |
|
| 79 |
+
def predict_sequence(fasta_file) -> str:
|
| 80 |
"""Process FASTA input and return formatted predictions"""
|
| 81 |
try:
|
| 82 |
logger.info("Starting prediction process")
|
| 83 |
|
| 84 |
+
if fasta_file is None:
|
| 85 |
return "Please upload a FASTA file"
|
| 86 |
|
| 87 |
+
# Get file content - handle both string and file inputs
|
| 88 |
+
try:
|
| 89 |
+
if isinstance(fasta_file, str):
|
| 90 |
+
content = fasta_file
|
| 91 |
+
else:
|
| 92 |
+
content = fasta_file.name # For Gradio file upload
|
| 93 |
+
except Exception as e:
|
| 94 |
+
logger.error(f"Error reading file: {str(e)}")
|
| 95 |
+
return f"Error reading file: {str(e)}"
|
| 96 |
|
| 97 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 98 |
logger.info(f"Using device: {device}")
|
|
|
|
| 154 |
logger.error(f"Unexpected error in predict_sequence: {str(e)}")
|
| 155 |
return f"An unexpected error occurred: {str(e)}"
|
| 156 |
|
| 157 |
+
# Create Gradio interface with both file upload and text input
|
| 158 |
iface = gr.Interface(
|
| 159 |
fn=predict_sequence,
|
| 160 |
+
inputs=[
|
| 161 |
+
gr.File(label="Upload FASTA file", file_types=[".fasta", ".fa", ".txt"]),
|
| 162 |
+
# Adding a text input as an alternative
|
| 163 |
+
gr.Textbox(label="Or paste FASTA sequence here", lines=10)
|
| 164 |
+
],
|
| 165 |
outputs=gr.Textbox(label="Prediction Results", lines=10),
|
| 166 |
title="Virus Host Classifier",
|
| 167 |
+
description="""Upload a FASTA file or paste your sequence to predict whether a virus sequence is likely to infect human or non-human hosts.
|
| 168 |
+
|
| 169 |
+
Example format:
|
| 170 |
+
>sequence_name
|
| 171 |
+
ATCGATCGATCG...""",
|
| 172 |
+
examples=[["example.fasta", None]],
|
| 173 |
cache_examples=True
|
| 174 |
)
|
| 175 |
|