Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
from fastapi import FastAPI
|
| 4 |
+
from fastapi.staticfiles import StaticFiles
|
| 5 |
+
|
| 6 |
+
# --- Part 1: Automatic File List Generation ---
|
| 7 |
+
# This code runs ONLY ONCE when the application starts up.
|
| 8 |
+
|
| 9 |
+
DATA_DIR = "data"
|
| 10 |
+
OUTPUT_FILE = "filelist.json"
|
| 11 |
+
|
| 12 |
+
def generate_file_list():
|
| 13 |
+
"""
|
| 14 |
+
Scans the data directory for .html files and saves the list to a JSON file.
|
| 15 |
+
This function is the "automatic" part.
|
| 16 |
+
"""
|
| 17 |
+
print("--- Running automatic file scan ---")
|
| 18 |
+
if not os.path.exists(DATA_DIR):
|
| 19 |
+
print(f"Warning: The '{DATA_DIR}' directory was not found. Creating an empty list.")
|
| 20 |
+
filenames = []
|
| 21 |
+
else:
|
| 22 |
+
filenames = [f for f in os.listdir(DATA_DIR) if f.endswith(".html")]
|
| 23 |
+
|
| 24 |
+
with open(OUTPUT_FILE, 'w') as f:
|
| 25 |
+
json.dump(filenames, f)
|
| 26 |
+
|
| 27 |
+
print(f"Successfully created '{OUTPUT_FILE}' with {len(filenames)} files.")
|
| 28 |
+
|
| 29 |
+
# *** Run the function now to generate the file list ***
|
| 30 |
+
generate_file_list()
|
| 31 |
+
|
| 32 |
+
# --- Part 2: The Web Server ---
|
| 33 |
+
# This part serves your files to the user.
|
| 34 |
+
|
| 35 |
+
app = FastAPI()
|
| 36 |
+
|
| 37 |
+
# This is the magic command. It tells FastAPI to serve all files
|
| 38 |
+
# from the current directory ('.'). The 'html=True' part automatically
|
| 39 |
+
# makes it serve index.html as the main page.
|
| 40 |
+
app.mount("/", StaticFiles(directory=".", html=True), name="static")
|
| 41 |
+
|
| 42 |
+
# When Hugging Face runs this app.py, it needs to know how to start the server.
|
| 43 |
+
# The 'app:app' string points to the FastAPI instance named 'app' in the file 'app.py'.
|
| 44 |
+
# You don't need to run this command yourself; Hugging Face does it for you.
|
| 45 |
+
# To run locally, you would use: uvicorn app:app --reload
|