Spaces:
Sleeping
Sleeping
A newer version of the Gradio SDK is available: 6.12.0
STT Service Logging Conflict Fix
Problem
The STT service at https://huggingface.co/spaces/pgits/stt-gpu-service was experiencing logging conflicts when starting up:
ValueError: I/O operation on closed file
ValueError: Unable to configure formatter 'default'
This occurred due to stdout/stderr conflicts between:
- ZeroGPU environment logging
- uvicorn server logging
- Gradio interface logging
- MCP server stdio protocol
Solution Implemented
1. ZeroGPU-Compatible Logging Setup
- Custom formatter:
[STT-{level}] {message}format to avoid conflicts - Safe stream handling: Uses
sys.__stdout__instead of potentially closed streams - Handler cleanup: Removes existing handlers before adding new ones
- Library silencing: Reduces verbosity from transformers, torch, gradio, uvicorn
2. Fallback Safety Mechanism
- Safe logging function:
safe_log()that falls back to print statements - Error handling: Graceful degradation if logging configuration fails
- Dual protocol support: Maintains both Gradio and MCP functionality
3. MCP Server Error Handling
- Thread isolation: MCP server runs in separate thread with own event loop
- Graceful failures: Service continues if MCP server fails to start
- Stream protection: MCP stdio protocol doesn't interfere with Gradio
4. Enhanced Startup Process
- Warning suppression: Filters out UserWarning and FutureWarning
- Configuration staging: Logging setup happens before other imports
- Error recovery: Gradio continues even if MCP fails
Code Changes Made
Key Functions Added:
setup_zerogpu_logging()- ZeroGPU-compatible logging configurationsafe_log()- Robust logging with print fallback- Enhanced
start_mcp_server_thread()- Better error handling - Improved
run_mcp_server()- Exception handling for stdio conflicts
Environment Variables:
GRADIO_SERVER_NAME=0.0.0.0GRADIO_SERVER_PORT=7860
Launch Parameters:
quiet=True- Reduces Gradio logging noiseenable_queue=True- Better request handlingmax_threads=4- ZeroGPU thread limit
Testing
- β Logging setup logic verified
- β Safe logging fallback tested
- β Stream isolation confirmed
- β Dual server mode compatibility
Expected Results
- Clean startup - No more "I/O operation on closed file" errors
- Dual protocol support - Both Gradio UI and MCP API available
- Graceful degradation - Service continues if one component fails
- ZeroGPU compatibility - Optimized for HuggingFace Pro environment
Deployment
The fixed service should now start cleanly in the ZeroGPU environment with both:
- Gradio Interface: Available on port 7860 for web UI
- MCP Server: Available on stdio protocol for API integration
Monitoring
Check logs for these success indicators:
[STT-INFO] π Starting STT service with dual protocol support...[STT-INFO] β MCP Server: Available on stdio protocol[STT-INFO] β Gradio Interface: Starting on port 7860...