Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,19 +4,48 @@ import numpy as np
|
|
| 4 |
import json
|
| 5 |
from typing import Optional, List, Dict, Tuple
|
| 6 |
import logging
|
| 7 |
-
|
| 8 |
|
| 9 |
# Configure logging
|
| 10 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 11 |
logger = logging.getLogger(__name__)
|
| 12 |
|
| 13 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
try:
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
except Exception as e:
|
|
|
|
| 18 |
logger.error(f"Failed to initialize predictor: {e}")
|
| 19 |
-
predictor = None
|
| 20 |
|
| 21 |
def predict_gene_regions(sequence: str,
|
| 22 |
ground_truth_labels: Optional[str] = None,
|
|
@@ -32,7 +61,13 @@ def predict_gene_regions(sequence: str,
|
|
| 32 |
"""
|
| 33 |
|
| 34 |
if predictor is None:
|
| 35 |
-
error_msg = "❌ Model not loaded
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
return error_msg, "", ""
|
| 37 |
|
| 38 |
# Input validation
|
|
@@ -146,13 +181,22 @@ def load_example_sequence():
|
|
| 146 |
|
| 147 |
# Create the Gradio interface
|
| 148 |
def create_interface():
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
-
gr.Markdown("""
|
| 152 |
-
# 🧬
|
| 153 |
|
| 154 |
This tool predicts gene regions in DNA sequences using a boundary-aware deep learning model.
|
| 155 |
The model identifies start and end positions of genes, along with confidence scores and detailed analysis.
|
|
|
|
|
|
|
| 156 |
""")
|
| 157 |
|
| 158 |
with gr.Row():
|
|
@@ -247,8 +291,10 @@ def create_interface():
|
|
| 247 |
|
| 248 |
return interface
|
| 249 |
|
| 250 |
-
#
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
|
|
|
|
|
|
|
|
| 4 |
import json
|
| 5 |
from typing import Optional, List, Dict, Tuple
|
| 6 |
import logging
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
# Configure logging
|
| 10 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 11 |
logger = logging.getLogger(__name__)
|
| 12 |
|
| 13 |
+
# Debug info for Hugging Face Spaces
|
| 14 |
+
print("=== Hugging Face Spaces Debug Info ===")
|
| 15 |
+
print(f"Current working directory: {os.getcwd()}")
|
| 16 |
+
print(f"Files in current directory: {os.listdir('.')}")
|
| 17 |
+
print(f"Python path: {os.environ.get('PYTHONPATH', 'Not set')}")
|
| 18 |
+
print("==========================================")
|
| 19 |
+
|
| 20 |
+
# Initialize the predictor globally with error handling
|
| 21 |
+
predictor = None
|
| 22 |
try:
|
| 23 |
+
print("Attempting to import predictor...")
|
| 24 |
+
from predictor import GenePredictor
|
| 25 |
+
print("✅ Predictor module imported successfully")
|
| 26 |
+
|
| 27 |
+
# Check if model file exists
|
| 28 |
+
model_path = 'best_boundary_aware_model.pth'
|
| 29 |
+
if os.path.exists(model_path):
|
| 30 |
+
print(f"✅ Model file found: {model_path}")
|
| 31 |
+
predictor = GenePredictor(model_path=model_path)
|
| 32 |
+
print("✅ Gene predictor initialized successfully")
|
| 33 |
+
logger.info("Gene predictor initialized successfully")
|
| 34 |
+
else:
|
| 35 |
+
print(f"❌ Model file not found: {model_path}")
|
| 36 |
+
print(f"Available files: {[f for f in os.listdir('.') if f.endswith('.pth')]}")
|
| 37 |
+
raise FileNotFoundError(f"Model file {model_path} not found")
|
| 38 |
+
|
| 39 |
+
except ImportError as e:
|
| 40 |
+
print(f"❌ Import Error: {e}")
|
| 41 |
+
print("Make sure predictor.py is in the same directory as app.py")
|
| 42 |
+
logger.error(f"Failed to import predictor: {e}")
|
| 43 |
+
except FileNotFoundError as e:
|
| 44 |
+
print(f"❌ File Error: {e}")
|
| 45 |
+
logger.error(f"Model file not found: {e}")
|
| 46 |
except Exception as e:
|
| 47 |
+
print(f"❌ Unexpected error during initialization: {e}")
|
| 48 |
logger.error(f"Failed to initialize predictor: {e}")
|
|
|
|
| 49 |
|
| 50 |
def predict_gene_regions(sequence: str,
|
| 51 |
ground_truth_labels: Optional[str] = None,
|
|
|
|
| 61 |
"""
|
| 62 |
|
| 63 |
if predictor is None:
|
| 64 |
+
error_msg = """❌ **Model not loaded**
|
| 65 |
+
|
| 66 |
+
Please check:
|
| 67 |
+
1. predictor.py file exists
|
| 68 |
+
2. best_boundary_aware_model.pth file exists
|
| 69 |
+
3. All dependencies are installed
|
| 70 |
+
4. Check the Hugging Face Spaces logs for specific errors"""
|
| 71 |
return error_msg, "", ""
|
| 72 |
|
| 73 |
# Input validation
|
|
|
|
| 181 |
|
| 182 |
# Create the Gradio interface
|
| 183 |
def create_interface():
|
| 184 |
+
# Show setup status
|
| 185 |
+
status_message = "✅ Model loaded successfully!" if predictor else "❌ Model failed to load - check setup"
|
| 186 |
+
|
| 187 |
+
with gr.Blocks(
|
| 188 |
+
title="Gene Prediction Tool",
|
| 189 |
+
theme=gr.themes.Soft(),
|
| 190 |
+
css="footer {visibility: hidden}" # Hide Gradio footer
|
| 191 |
+
) as interface:
|
| 192 |
|
| 193 |
+
gr.Markdown(f"""
|
| 194 |
+
# 🧬 Gene Prediction Tool
|
| 195 |
|
| 196 |
This tool predicts gene regions in DNA sequences using a boundary-aware deep learning model.
|
| 197 |
The model identifies start and end positions of genes, along with confidence scores and detailed analysis.
|
| 198 |
+
|
| 199 |
+
**Status:** {status_message}
|
| 200 |
""")
|
| 201 |
|
| 202 |
with gr.Row():
|
|
|
|
| 291 |
|
| 292 |
return interface
|
| 293 |
|
| 294 |
+
# Create and launch the interface
|
| 295 |
+
# Note: On Hugging Face Spaces, we don't need to call .launch() explicitly
|
| 296 |
+
# The interface will be launched automatically
|
| 297 |
+
interface = create_interface()
|
| 298 |
+
|
| 299 |
+
# For local development, uncomment the line below:
|
| 300 |
+
# interface.launch()
|