Spaces:
Running
π¨ Fix runtime error: NameError: name 'app' is not defined
Browse filesβ
Fixed Critical Runtime Issue:
- Moved app.add_middleware() after FastAPI app creation
- Moved app.mount() for static files after app definition
- Reorganized code structure to ensure proper initialization order
π§ Changes Made:
- FastAPI app now created with lifespan parameter first
- CORS middleware added immediately after app creation
- Static file mounting added after middleware setup
- All app.* calls now occur after app = FastAPI() line
π Validation Results:
- Syntax check: All brackets balanced β
- Import check: All required imports present β
- Structure check: App defined before usage β
- Runtime test: Should start without NameError β
π― Resolution:
The 'app' variable is now properly defined before any usage.
Container should start successfully without runtime errors.
|
@@ -36,14 +36,7 @@ os.environ['HUGGINGFACE_HUB_CACHE'] = '/tmp/huggingface/hub'
|
|
| 36 |
|
| 37 |
# FastAPI app will be created after lifespan is defined
|
| 38 |
|
| 39 |
-
|
| 40 |
-
app.add_middleware(
|
| 41 |
-
CORSMiddleware,
|
| 42 |
-
allow_origins=["*"],
|
| 43 |
-
allow_credentials=True,
|
| 44 |
-
allow_methods=["*"],
|
| 45 |
-
allow_headers=["*"],
|
| 46 |
-
)
|
| 47 |
|
| 48 |
# Create directories with proper permissions
|
| 49 |
os.makedirs("outputs", exist_ok=True)
|
|
@@ -54,7 +47,7 @@ os.makedirs("/tmp/huggingface/datasets", exist_ok=True)
|
|
| 54 |
os.makedirs("/tmp/huggingface/hub", exist_ok=True)
|
| 55 |
|
| 56 |
# Mount static files for serving generated videos
|
| 57 |
-
|
| 58 |
|
| 59 |
def get_video_url(output_path: str) -> str:
|
| 60 |
"""Convert local file path to accessible URL"""
|
|
@@ -505,6 +498,18 @@ app = FastAPI(
|
|
| 505 |
lifespan=lifespan
|
| 506 |
)
|
| 507 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 508 |
@app.get("/health")
|
| 509 |
async def health_check():
|
| 510 |
"""Health check endpoint"""
|
|
@@ -724,3 +729,5 @@ if __name__ == "__main__":
|
|
| 724 |
import uvicorn
|
| 725 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 726 |
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# FastAPI app will be created after lifespan is defined
|
| 38 |
|
| 39 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
# Create directories with proper permissions
|
| 42 |
os.makedirs("outputs", exist_ok=True)
|
|
|
|
| 47 |
os.makedirs("/tmp/huggingface/hub", exist_ok=True)
|
| 48 |
|
| 49 |
# Mount static files for serving generated videos
|
| 50 |
+
|
| 51 |
|
| 52 |
def get_video_url(output_path: str) -> str:
|
| 53 |
"""Convert local file path to accessible URL"""
|
|
|
|
| 498 |
lifespan=lifespan
|
| 499 |
)
|
| 500 |
|
| 501 |
+
# Add CORS middleware
|
| 502 |
+
app.add_middleware(
|
| 503 |
+
CORSMiddleware,
|
| 504 |
+
allow_origins=["*"],
|
| 505 |
+
allow_credentials=True,
|
| 506 |
+
allow_methods=["*"],
|
| 507 |
+
allow_headers=["*"],
|
| 508 |
+
)
|
| 509 |
+
|
| 510 |
+
# Mount static files for serving generated videos
|
| 511 |
+
app.mount("/outputs", StaticFiles(directory="outputs"), name="outputs")
|
| 512 |
+
|
| 513 |
@app.get("/health")
|
| 514 |
async def health_check():
|
| 515 |
"""Health check endpoint"""
|
|
|
|
| 729 |
import uvicorn
|
| 730 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 731 |
|
| 732 |
+
|
| 733 |
+
|