Download app.py from Nra/LA_8: direct link, hf CLI and curl.
- Browser
- Download file 1.71 kB
-
https://huggingface.co/spaces/Nra/LA_8/resolve/main/app.py
- Command line
-
hf download hf://spaces/Nra/LA_8/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Nra/LA_8/resolve/main/app.py
1.71 kB
| import os | |
| import json | |
| import uvicorn | |
| from fastapi import FastAPI | |
| from fastapi.staticfiles import StaticFiles | |
| # --- Part 1: Automatic File List Generation --- | |
| # This code runs ONCE when the application starts. | |
| DATA_DIR = "data" | |
| OUTPUT_FILE = "filelist.json" | |
| def generate_file_list(): | |
| """Scans the data directory and creates a fresh filelist.json.""" | |
| print("--- Running automatic file scan ---") | |
| if not os.path.exists(DATA_DIR): | |
| print(f"Warning: The '{DATA_DIR}' directory was not found. Creating an empty list.") | |
| filenames = [] | |
| else: | |
| filenames = [f for f in os.listdir(DATA_DIR) if f.endswith(".html")] | |
| # In a Hugging Face Space, we need to make sure we write to a writable directory. | |
| # The root directory is writable. | |
| with open(OUTPUT_FILE, 'w') as f: | |
| json.dump(filenames, f) | |
| print(f"Successfully created '{OUTPUT_FILE}' with {len(filenames)} files.") | |
| # Run the function on startup to generate the file list | |
| generate_file_list() | |
| # --- Part 2: The FastAPI Web Server --- | |
| # Create the main application instance | |
| app = FastAPI() | |
| # This "mounts" the static file server. It serves all files from the current | |
| # directory ('.') and makes index.html the default page. | |
| app.mount("/", StaticFiles(directory=".", html=True), name="static") | |
| # --- Part 3: The Runner --- | |
| # This is the crucial part that fixes the error. It explicitly starts the server. | |
| if __name__ == "__main__": | |
| # Uvicorn is the server that runs our FastAPI application. | |
| # host="0.0.0.0" makes it accessible within the Hugging Face environment. | |
| # port=7860 is the standard port Hugging Face Spaces expects. | |
| uvicorn.run(app, host="0.0.0.0", port=7860) |