Spaces:
Sleeping
Sleeping
File size: 2,582 Bytes
3eb720d 77507c2 3eb720d 5f20f16 77507c2 3eb720d 77507c2 3eb720d 77507c2 aaa6bb4 77507c2 69a84ff 3eb720d 77507c2 3eb720d 77507c2 | 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 64 65 66 67 68 69 70 71 72 73 74 75 76 | """
Hugging Face Spaces Entry Point (ZeroGPU + Gradio SDK)
Serves FastAPI Backend + Static Vite React Frontend All-in-One
"""
import os
import sys
# Ensure backend directory is in Python path
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
backend_dir = os.path.join(ROOT_DIR, "backend")
if backend_dir not in sys.path:
sys.path.insert(0, backend_dir)
# Hotfix for huggingface_hub >= 0.25 removing HfFolder needed by Gradio 4
try:
import huggingface_hub
if not hasattr(huggingface_hub, "HfFolder"):
class HfFolder:
@classmethod
def get_token(cls):
try:
return huggingface_hub.get_token()
except Exception:
return None
@classmethod
def save_token(cls, token):
try:
return huggingface_hub.save_token(token)
except Exception:
pass
@classmethod
def delete_token(cls):
try:
return huggingface_hub.delete_token()
except Exception:
pass
huggingface_hub.HfFolder = HfFolder
except Exception:
pass
import gradio as gr
from main import app # FastAPI instance with SPA & API routes
# Hugging Face ZeroGPU compatibility handler
try:
import spaces
@spaces.GPU
def gpu_check(prompt: str = "") -> str:
"""ZeroGPU validator function to satisfy Hugging Face ZeroGPU startup scanner."""
return f"Perps AI ZeroGPU Active: {prompt}"
except Exception:
def gpu_check(prompt: str = "") -> str:
return f"Perps AI Online: {prompt}"
with gr.Blocks(title="Perps Terminal Engine") as demo:
gr.Markdown("# 🚀 Perps Institutional Quant Terminal")
gr.Markdown("Frontend live di root `/`. Buka tab langsung untuk tampilan full screen.")
txt_in = gr.Textbox(value="Health Check", label="AI Co-Pilot Status")
txt_out = gr.Textbox(label="Status")
btn = gr.Button("Check AI Engine Status")
btn.click(fn=gpu_check, inputs=txt_in, outputs=txt_out)
demo.load(fn=gpu_check, inputs=txt_in, outputs=txt_out)
# Mount Gradio at /gradio so root '/' serves the React SPA
try:
app = gr.mount_gradio_app(app, demo, path="/gradio", ssr_mode=False)
except TypeError:
app = gr.mount_gradio_app(app, demo, path="/gradio")
if __name__ == "__main__":
import uvicorn
# Hugging Face Spaces strictly requires port 7860
port = int(os.environ.get("PORT", 7860))
uvicorn.run("app:app", host="0.0.0.0", port=port)
|