Entry Point Conflict Resolution
Problem Resolved
Issue: Both app.py and main.py were attempting to launch Gradio on port 7860, causing port conflicts.
Root Cause:
app.pyhas aif __name__ == "__main__":block that launches Gradiomain.pywas also trying to create and launch its own Gradio instance- Both tried to bind to port 7860 simultaneously
Solution Implemented
Single Entry Point Architecture
For Hugging Face Spaces Deployment:
main.pyis the only entry point (configured inREADME.mdasapp_file: main.py)main.pyimports the interface fromapp.pywithout triggering its launchmain.pyhandles the launch configuration
For Local Development/Testing:
app.pycan still be run directly (python app.py)- When run directly,
app.pylaunches with its own configuration - This allows developers to test the app independently
How It Works
Import Behavior in Python
When main.py imports from app.py:
from app import create_mobile_optimized_interface
- β
All top-level code in
app.pyexecutes (orchestrator initialization, etc.) - β
The function
create_mobile_optimized_interface()becomes available - β The
if __name__ == "__main__":block does NOT execute (because__name__!="__main__"when imported)
Current Structure
app.py:
- Defines: create_mobile_optimized_interface() β Returns (demo, components)
- Has: if __name__ == "__main__": launch code β Only runs when app.py executed directly
main.py:
- Imports: create_mobile_optimized_interface() from app.py
- Calls: demo, components = create_mobile_optimized_interface()
- Launches: demo.launch() with HF Spaces configuration
File Roles
app.py
- Purpose: Core application interface and logic
- When imported: Provides interface creation function
- When executed directly: Launches app for local testing
- Launch configuration: Local-optimized (with share link for external access when not on HF Spaces)
main.py
- Purpose: HF Spaces entry point
- When executed: Imports from app.py and launches
- Launch configuration: HF Spaces-optimized (detects environment, adjusts share setting)
- HF Spaces requirement: Must be specified as
app_file: main.pyin README.md
Configuration
Hugging Face Spaces Setup
In README.md:
sdk: gradio
app_file: main.py # β This tells HF Spaces to use main.py
Environment Detection
Both files automatically detect if running on HF Spaces:
try:
from spaces import GPU
is_hf_spaces = True
except ImportError:
is_hf_spaces = False
On HF Spaces:
share=False(HF Spaces provides its own URL)- No warnings about share being unsupported
Locally:
share=True(creates Gradio public link for external access)
Benefits
- β No Port Conflicts: Only one Gradio instance launches
- β Clean Separation: Interface logic (app.py) vs. Launch logic (main.py)
- β Flexible Testing: Can test app.py independently
- β HF Spaces Compatible: Follows HF Spaces deployment patterns
- β Environment Aware: Automatically adjusts configuration based on deployment environment
Verification
Check That Only One Instance Launches
When running on HF Spaces:
# Should see only one launch message:
"β
Application ready for launch"
Check Port Binding
# Should only see one process on port 7860
netstat -ano | findstr :7860 # Windows
lsof -i :7860 # Linux/Mac
Check Logs
When main.py is entry point (HF Spaces):
π Starting AI Research Assistant MVP
Creating interface from app.py (all handlers already initialized)...
β Interface created with all API endpoints and handlers
β Detected Hugging Face Spaces - using built-in public URL
β
Application ready for launch
When app.py is executed directly (local):
STARTING APP (direct execution)
β Interface created
LAUNCHING GRADIO APP
Creating public Gradio share link for external access... (if not on HF Spaces)
Troubleshooting
Issue: Port Still in Use
Check:
- Only one entry point is being executed
- No other Python processes using port 7860
- Previous Gradio instance was properly terminated
Solution:
# Kill any existing Python processes
taskkill /IM python.exe /F # Windows
pkill -f python # Linux/Mac
Issue: app.py Launching When Imported
Check: Verify if __name__ == "__main__": block is present and correctly indented
Solution: This should never happen - Python's import mechanism prevents it. If it does, check for syntax errors or circular imports.
Issue: API Endpoints Not Available
Check:
show_api=Trueis set in launch configuration- API endpoints are registered in app.py (before import)
- Both files use the same demo instance
Solution: The demo instance is created in app.py and used by main.py, so endpoints should be available. Verify logs show "β API endpoint registered" messages.
Summary
β
Conflict resolved: Single entry point architecture
β
Port conflict eliminated: Only main.py launches Gradio
β
Environment aware: Automatically detects HF Spaces vs. local
β
Flexible: Can still run app.py directly for testing
β
Production ready: Follows HF Spaces deployment best practices