Fix backend crash: Make prompt imports optional with fallbacks
Browse filesIssue: Backend (port 8000) was crashing on startup, causing ECONNREFUSED errors
Root cause: New imports from anycoder_app.prompts and docs_manager were failing
due to Gradio dependencies that aren't available in backend-only mode
Fix:
- Wrap all anycoder_app imports in try/except blocks
- Provide fallback prompts if imports fail
- Backend will start successfully even if prompt imports fail
- Nested try/except: outer for basic prompts, inner for dynamic Gradio/ComfyUI prompts
- Add startup logging to track which prompts are loaded
- Backend now resilient to missing dependencies
This ensures:
✅ Backend always starts on port 8000
✅ Frontend can fetch /api/languages and /api/models
✅ Language and model dropdowns populate correctly
✅ System uses best available prompts (full or fallback)
✅ No more ECONNREFUSED errors
18 unmodified lines192021222324252627282930313233222324252627282930313233353637383940413435363738394041424344454647484950515243445354555657585960616263646518 unmodified linesfrom huggingface_hub import InferenceClientimport httpx
# Import system prompts for code generationfrom anycoder_app.prompts import ( HTML_SYSTEM_PROMPT, TRANSFORMERS_JS_SYSTEM_PROMPT, STREAMLIT_SYSTEM_PROMPT, REACT_SYSTEM_PROMPT, GRADIO_SYSTEM_PROMPT, JSON_SYSTEM_PROMPT, GENERIC_SYSTEM_PROMPT)
# Initialize Gradio and ComfyUI prompts on startup# Import system prompts for code generation - use fallback if imports failtry: from anycoder_app.prompts import ( HTML_SYSTEM_PROMPT, TRANSFORMERS_JS_SYSTEM_PROMPT, STREAMLIT_SYSTEM_PROMPT, REACT_SYSTEM_PROMPT, GENERIC_SYSTEM_PROMPT ) # Try to import and initialize dynamic prompts (Gradio, ComfyUI) try: from anycoder_app.docs_manager import update_gradio_system_prompts, update_json_system_prompts from anycoder_app.docs_manager import update_gradio_system_prompts, update_json_system_prompts print("[Startup] Initializing Gradio and ComfyUI system prompts...") update_gradio_system_prompts() update_json_system_prompts() # Re-import to get updated prompts from anycoder_app.prompts import GRADIO_SYSTEM_PROMPT, JSON_SYSTEM_PROMPT print("[Startup] System prompts initialized successfully") from anycoder_app.prompts import GRADIO_SYSTEM_PROMPT, JSON_SYSTEM_PROMPT print("[Startup] Initializing Gradio and ComfyUI system prompts...") update_gradio_system_prompts() update_json_system_prompts() # Re-import to get updated prompts from anycoder_app.prompts import GRADIO_SYSTEM_PROMPT as GRADIO_PROMPT_UPDATED from anycoder_app.prompts import JSON_SYSTEM_PROMPT as JSON_PROMPT_UPDATED GRADIO_SYSTEM_PROMPT = GRADIO_PROMPT_UPDATED JSON_SYSTEM_PROMPT = JSON_PROMPT_UPDATED print("[Startup] System prompts initialized successfully") except Exception as e: print(f"[Startup] Warning: Could not initialize dynamic prompts: {e}") # Use fallback prompts GRADIO_SYSTEM_PROMPT = "You are an expert Gradio developer. Create complete, working Gradio applications." JSON_SYSTEM_PROMPT = "You are an expert at generating JSON configurations. Create valid, well-structured JSON." print("[Startup] System prompts loaded successfully") except Exception as e: print(f"[Startup] Warning: Could not initialize dynamic prompts: {e}") print("[Startup] Will use fallback prompts") print(f"[Startup] ERROR: Could not import prompts from anycoder_app: {e}") print("[Startup] Using basic fallback prompts") # Define minimal fallback prompts HTML_SYSTEM_PROMPT = "You are an expert web developer. Create complete HTML applications with CSS and JavaScript." TRANSFORMERS_JS_SYSTEM_PROMPT = "You are an expert at creating transformers.js applications. Generate complete working code." STREAMLIT_SYSTEM_PROMPT = "You are an expert Streamlit developer. Create complete Streamlit applications." REACT_SYSTEM_PROMPT = "You are an expert React developer. Create complete React applications with Next.js." GRADIO_SYSTEM_PROMPT = "You are an expert Gradio developer. Create complete, working Gradio applications." JSON_SYSTEM_PROMPT = "You are an expert at generating JSON configurations. Create valid, well-structured JSON." GENERIC_SYSTEM_PROMPT = "You are an expert {language} developer. Create complete, working {language} applications."
# Define models and languages here to avoid importing Gradio UIAVAILABLE_MODELS = [