Spaces:
Running
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
- backend_api.py +39 -21
|
@@ -19,29 +19,47 @@ import os
|
|
| 19 |
from huggingface_hub import InferenceClient
|
| 20 |
import httpx
|
| 21 |
|
| 22 |
-
# Import system prompts for code generation
|
| 23 |
-
from anycoder_app.prompts import (
|
| 24 |
-
HTML_SYSTEM_PROMPT,
|
| 25 |
-
TRANSFORMERS_JS_SYSTEM_PROMPT,
|
| 26 |
-
STREAMLIT_SYSTEM_PROMPT,
|
| 27 |
-
REACT_SYSTEM_PROMPT,
|
| 28 |
-
GRADIO_SYSTEM_PROMPT,
|
| 29 |
-
JSON_SYSTEM_PROMPT,
|
| 30 |
-
GENERIC_SYSTEM_PROMPT
|
| 31 |
-
)
|
| 32 |
-
|
| 33 |
-
# Initialize Gradio and ComfyUI prompts on startup
|
| 34 |
try:
|
| 35 |
-
from anycoder_app.
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
-
print(f"[Startup]
|
| 44 |
-
print("[Startup]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# Define models and languages here to avoid importing Gradio UI
|
| 47 |
AVAILABLE_MODELS = [
|
|
|
|
| 19 |
from huggingface_hub import InferenceClient
|
| 20 |
import httpx
|
| 21 |
|
| 22 |
+
# Import system prompts for code generation - use fallback if imports fail
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
try:
|
| 24 |
+
from anycoder_app.prompts import (
|
| 25 |
+
HTML_SYSTEM_PROMPT,
|
| 26 |
+
TRANSFORMERS_JS_SYSTEM_PROMPT,
|
| 27 |
+
STREAMLIT_SYSTEM_PROMPT,
|
| 28 |
+
REACT_SYSTEM_PROMPT,
|
| 29 |
+
GENERIC_SYSTEM_PROMPT
|
| 30 |
+
)
|
| 31 |
+
# Try to import and initialize dynamic prompts (Gradio, ComfyUI)
|
| 32 |
+
try:
|
| 33 |
+
from anycoder_app.docs_manager import update_gradio_system_prompts, update_json_system_prompts
|
| 34 |
+
from anycoder_app.prompts import GRADIO_SYSTEM_PROMPT, JSON_SYSTEM_PROMPT
|
| 35 |
+
print("[Startup] Initializing Gradio and ComfyUI system prompts...")
|
| 36 |
+
update_gradio_system_prompts()
|
| 37 |
+
update_json_system_prompts()
|
| 38 |
+
# Re-import to get updated prompts
|
| 39 |
+
from anycoder_app.prompts import GRADIO_SYSTEM_PROMPT as GRADIO_PROMPT_UPDATED
|
| 40 |
+
from anycoder_app.prompts import JSON_SYSTEM_PROMPT as JSON_PROMPT_UPDATED
|
| 41 |
+
GRADIO_SYSTEM_PROMPT = GRADIO_PROMPT_UPDATED
|
| 42 |
+
JSON_SYSTEM_PROMPT = JSON_PROMPT_UPDATED
|
| 43 |
+
print("[Startup] System prompts initialized successfully")
|
| 44 |
+
except Exception as e:
|
| 45 |
+
print(f"[Startup] Warning: Could not initialize dynamic prompts: {e}")
|
| 46 |
+
# Use fallback prompts
|
| 47 |
+
GRADIO_SYSTEM_PROMPT = "You are an expert Gradio developer. Create complete, working Gradio applications."
|
| 48 |
+
JSON_SYSTEM_PROMPT = "You are an expert at generating JSON configurations. Create valid, well-structured JSON."
|
| 49 |
+
|
| 50 |
+
print("[Startup] System prompts loaded successfully")
|
| 51 |
+
|
| 52 |
except Exception as e:
|
| 53 |
+
print(f"[Startup] ERROR: Could not import prompts from anycoder_app: {e}")
|
| 54 |
+
print("[Startup] Using basic fallback prompts")
|
| 55 |
+
# Define minimal fallback prompts
|
| 56 |
+
HTML_SYSTEM_PROMPT = "You are an expert web developer. Create complete HTML applications with CSS and JavaScript."
|
| 57 |
+
TRANSFORMERS_JS_SYSTEM_PROMPT = "You are an expert at creating transformers.js applications. Generate complete working code."
|
| 58 |
+
STREAMLIT_SYSTEM_PROMPT = "You are an expert Streamlit developer. Create complete Streamlit applications."
|
| 59 |
+
REACT_SYSTEM_PROMPT = "You are an expert React developer. Create complete React applications with Next.js."
|
| 60 |
+
GRADIO_SYSTEM_PROMPT = "You are an expert Gradio developer. Create complete, working Gradio applications."
|
| 61 |
+
JSON_SYSTEM_PROMPT = "You are an expert at generating JSON configurations. Create valid, well-structured JSON."
|
| 62 |
+
GENERIC_SYSTEM_PROMPT = "You are an expert {language} developer. Create complete, working {language} applications."
|
| 63 |
|
| 64 |
# Define models and languages here to avoid importing Gradio UI
|
| 65 |
AVAILABLE_MODELS = [
|