File size: 1,437 Bytes
ac156d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Hugging Face Space entry point (Gradio SDK on ZeroGPU hardware).

ZeroGPU expects the Space to be served by Gradio's own launch() and at least one @spaces.GPU function.
So Gradio owns port 7860, and the Cortex FastAPI routes are inserted ahead of Gradio's own routes,
which makes "/" serve the Cortex UI.
"""
import os
import threading

import gradio as gr
import spaces

from app.bootstrap import STATUS, start_background
from app.server import app as api, housekeeping


@spaces.GPU(duration=30)
def gpu_embed(texts):
    """Embeds document chunks; registered for ZeroGPU (runs on CPU outside ZeroGPU)."""
    from core.embeddings import LocalEmbeddings
    return LocalEmbeddings().embed_documents(texts)


with gr.Blocks(title="Cortex status") as demo:
    gr.Markdown("## Cortex status\nThe Cortex app is served at [/](/).")
    out = gr.JSON()
    gr.Button("Refresh").click(lambda: STATUS, outputs=out)

if __name__ == "__main__":
    start_background()
    threading.Thread(target=housekeeping, daemon=True).start()
    # ssr_mode=False: on Spaces, Gradio SSR puts a Node server in front of 7860, which would hide the Cortex routes.
    demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", "7860")), ssr_mode=False, prevent_thread_lock=True)
    # Cortex routes take priority over Gradio's (the Gradio page itself is not used).
    demo.app.router.routes[0:0] = list(api.router.routes)
    demo.block_thread()