Spaces:
Paused
Paused
File size: 2,285 Bytes
325b343 85f8df7 c7920e9 325b343 e66d4f0 325b343 85f8df7 325b343 85f8df7 325b343 85f8df7 c7920e9 325b343 bea4dbf 325b343 c7920e9 85f8df7 c7920e9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | 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
) |