Adding Dockerfile app.py
Browse files- Dockerfile +7 -3
- app.py +10 -4
Dockerfile
CHANGED
|
@@ -20,9 +20,13 @@ RUN pip install --no-cache-dir -r requirements.txt
|
|
| 20 |
# Copy application code
|
| 21 |
COPY . .
|
| 22 |
|
| 23 |
-
# Expose port
|
| 24 |
EXPOSE 7860
|
| 25 |
|
| 26 |
-
#
|
| 27 |
ENV PYTHONUNBUFFERED=1
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
# Copy application code
|
| 21 |
COPY . .
|
| 22 |
|
| 23 |
+
# Expose port (HuggingFace Spaces uses 7860)
|
| 24 |
EXPOSE 7860
|
| 25 |
|
| 26 |
+
# Set environment variables
|
| 27 |
ENV PYTHONUNBUFFERED=1
|
| 28 |
+
ENV PORT=7860
|
| 29 |
+
|
| 30 |
+
# Run with uvicorn directly (required for HuggingFace Spaces)
|
| 31 |
+
# Use PORT environment variable if provided, otherwise default to 7860
|
| 32 |
+
CMD uvicorn app:app --host 0.0.0.0 --port ${PORT:-7860} --log-level info
|
app.py
CHANGED
|
@@ -55,15 +55,19 @@ async def startup_event():
|
|
| 55 |
logger.info("β
Models loaded successfully!")
|
| 56 |
except Exception as e:
|
| 57 |
logger.error(f"β Failed to load models: {e}", exc_info=True)
|
| 58 |
-
raise
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
@app.get("/health")
|
| 61 |
async def health_check():
|
| 62 |
"""Health check endpoint"""
|
|
|
|
| 63 |
return {
|
| 64 |
"status": "healthy",
|
| 65 |
"models_loaded": detector is not None,
|
| 66 |
-
"timestamp":
|
| 67 |
}
|
| 68 |
|
| 69 |
@app.post("/analyze")
|
|
@@ -146,10 +150,12 @@ async def root():
|
|
| 146 |
|
| 147 |
if __name__ == "__main__":
|
| 148 |
import uvicorn
|
| 149 |
-
|
|
|
|
|
|
|
| 150 |
uvicorn.run(
|
| 151 |
app,
|
| 152 |
host="0.0.0.0",
|
| 153 |
-
port=
|
| 154 |
log_level="info"
|
| 155 |
)
|
|
|
|
| 55 |
logger.info("β
Models loaded successfully!")
|
| 56 |
except Exception as e:
|
| 57 |
logger.error(f"β Failed to load models: {e}", exc_info=True)
|
| 58 |
+
# Don't raise - allow app to start and return 503 on analyze requests
|
| 59 |
+
# This allows HuggingFace Spaces to show the app is running
|
| 60 |
+
logger.warning("β οΈ App will start but models are not loaded. Analyze requests will return 503.")
|
| 61 |
+
detector = None
|
| 62 |
|
| 63 |
@app.get("/health")
|
| 64 |
async def health_check():
|
| 65 |
"""Health check endpoint"""
|
| 66 |
+
from datetime import datetime
|
| 67 |
return {
|
| 68 |
"status": "healthy",
|
| 69 |
"models_loaded": detector is not None,
|
| 70 |
+
"timestamp": datetime.utcnow().isoformat() + "Z"
|
| 71 |
}
|
| 72 |
|
| 73 |
@app.post("/analyze")
|
|
|
|
| 150 |
|
| 151 |
if __name__ == "__main__":
|
| 152 |
import uvicorn
|
| 153 |
+
# Read port from environment variable (HuggingFace Spaces provides this)
|
| 154 |
+
port = int(os.environ.get("PORT", 7860))
|
| 155 |
+
logger.info(f"π Starting SLAQ Stutter Detector API on port {port}...")
|
| 156 |
uvicorn.run(
|
| 157 |
app,
|
| 158 |
host="0.0.0.0",
|
| 159 |
+
port=port,
|
| 160 |
log_level="info"
|
| 161 |
)
|