allcited-api / app.py
suvendu-2006
feat: add 10-minute public self-ping daemon to keep space awake
238f8b2
Raw History Blame Contribute Delete
2.93 kB
import os
# Disable Gradio SSR mode to avoid Node.js sub-process port collisions
os.environ["GRADIO_SSR_MODE"] = "False"
os.environ.pop("GRADIO_NODE_SERVER_PORT", None)
os.environ.setdefault("ALLOW_LOCAL_ORIGINS_IN_PROD", "true")
import spaces
import gradio as gr
import uvicorn
import threading
import time
import urllib.request
from app.main import app
def _keep_alive_loop():
"""Background keep-alive thread pinging public endpoint every 10 minutes to prevent idle sleep."""
time.sleep(30)
url = "https://subh-007-allcited-api.hf.space/v1/readyz"
while True:
try:
req = urllib.request.Request(url, headers={"User-Agent": "Allcited-KeepAlive-Internal/1.0"})
with urllib.request.urlopen(req, timeout=15) as resp:
print(f"[keep-alive] Self-ping status {resp.status}")
except Exception as e:
print(f"[keep-alive] Self-ping note: {e}")
time.sleep(600)
threading.Thread(target=_keep_alive_loop, daemon=True, name="hf-space-keep-alive").start()
@spaces.GPU(duration=30)
def gpu_inference_function(text: str) -> str:
"""ZeroGPU event binding hook for Hugging Face Space analyzer."""
return f"Allcited ZeroGPU ready: {text}"
# Send ZeroGPU startup report on FastAPI startup
@app.on_event("startup")
def on_startup_zerogpu():
try:
from spaces.zero import client as zero_client
from spaces.config import Config as ZeroConfig
if getattr(ZeroConfig, "zero_gpu", False):
zero_client.startup_report()
print("๐Ÿš€ ZeroGPU startup report transmitted via FastAPI startup hook!")
except Exception as e:
print(f"ZeroGPU startup hook: {e}")
# Throwaway Gradio launch on unused port to satisfy spaces.one_launch hook
try:
dummy_block = gr.Interface(fn=gpu_inference_function, inputs="text", outputs="text")
dummy_block.launch(prevent_thread_lock=True, server_port=7865, quiet=True)
dummy_block.close()
print("๐Ÿš€ ZeroGPU one_launch hook triggered successfully!")
except Exception as e:
print(f"ZeroGPU throwaway launch note: {e}")
with gr.Blocks(title="Allcited API") as demo:
gr.Markdown("# Allcited API Server\n\nFastAPI backend with grounded citations.")
txt_input = gr.Textbox(label="Status Input", value="ready")
txt_output = gr.Textbox(label="Status Output")
btn = gr.Button("Ping GPU")
btn.click(fn=gpu_inference_function, inputs=txt_input, outputs=txt_output)
# Mount Gradio interface at root so HF Space iframe renders, while FastAPI routes take priority
app = gr.mount_gradio_app(app, demo, path="/")
if __name__ == "__main__":
port = int(os.environ.get("PORT", "7860"))
if os.environ.get("GRADIO_SSR_MODE", "").lower() in ("false", "0") and port == 7861:
port = 7860
print(f"Starting server on port {port} (SSR={os.environ.get('GRADIO_SSR_MODE')})")
uvicorn.run(app, host="0.0.0.0", port=port)