Spaces:
Paused
Paused
Download app.py from kphfrepo/httpBucket: direct link, hf CLI and curl.
- Browser
- Download file 2.29 kB
-
https://huggingface.co/spaces/kphfrepo/httpBucket/resolve/main/app.py
- Command line
-
hf download hf://spaces/kphfrepo/httpBucket/app.py
-
curl -L -o app.py https://huggingface.co/spaces/kphfrepo/httpBucket/resolve/main/app.py
2.29 kB
| import os | |
| import gc | |
| import gradio as gr | |
| import uvicorn | |
| from fastapi import FastAPI, Request | |
| from fastapi.staticfiles import StaticFiles | |
| from fastapi.responses import FileResponse | |
| app = FastAPI() | |
| WORKSPACE_DATA = os.path.abspath("/bucket_data") | |
| # --- HIGH-PERFORMANCE DIRECT ROUTE FOR VIDEOS --- | |
| # @app.get("/bucket_data/{file_path:path}") | |
| # async def stream_video_direct(file_path: str): | |
| # full_path = os.path.join(WORKSPACE_DATA, file_path) | |
| # if not os.path.exists(full_path): | |
| # return {"error": "File not found"}, 404 | |
| # # FileResponse handles chunking, range requests (206), and closing | |
| # # file handles properly when a connection drops out. | |
| # response = FileResponse( | |
| # full_path, | |
| # media_type="video/mp4", | |
| # headers={"Accept-Ranges": "bytes"} | |
| # ) | |
| # # Force clean up unmanaged allocations periodically | |
| # if os.getpid() % 5 == 0: | |
| # gc.collect() | |
| # return response | |
| # 1. Mount Static Files FIRST | |
| if os.path.exists(WORKSPACE_DATA): | |
| print(f"🎉 SUCCESS: Found workspace storage directory at: {WORKSPACE_DATA}") | |
| else: | |
| print(f"⚠️ WARNING: {WORKSPACE_DATA} not found. Creating local folder fallback.") | |
| os.makedirs(WORKSPACE_DATA, exist_ok=True) | |
| # Important: Keep html=False so it doesn't try to look for index.html files | |
| app.mount("/bucket_data", StaticFiles(directory=WORKSPACE_DATA, html=False), name="data_bucket") | |
| # 2. Gradio Interface | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# App Component Running") | |
| gr.Markdown(f"Serving files from workspace: `{WORKSPACE_DATA}` over HTTP endpoint `/bucket_data/`") | |
| # 3. FIX: Bind Gradio safely | |
| # Pass the FastAPI app directly inside standard Gradio launching, OR if using mount, | |
| # ensure allowed_paths points explicitly to the absolute folder string. | |
| app = gr.mount_gradio_app(app, demo, path="/", allowed_paths=[WORKSPACE_DATA]) | |
| if __name__ == "__main__": | |
| # Hugging Face Spaces automatically passes port 7860, make sure uvicorn matches it strictly | |
| uvicorn.run( | |
| app, | |
| host="0.0.0.0", | |
| port=7860, | |
| limit_concurrency=300, # Tells Uvicorn to hold up to 500 open sockets concurrently | |
| timeout_keep_alive=15 # Keeps the pipes warm while switching chunks | |
| ) |