test-demoprep / app.py
mikeboone's picture
chore: drop stale dataset_first reporting from build_info
194f181
Raw
History Blame Contribute Delete
3.35 kB
"""
Hugging Face Spaces entry point for DemoPrep.
Secrets to set in HF Spaces β†’ Settings β†’ Repository Secrets:
SUPABASE_URL Supabase project URL
SUPABASE_ANON_KEY Supabase anonymous key
OPENAI_API_KEY OpenAI API key (primary LLM)
GOOGLE_API_KEY Google API key (optional, for Gemini models)
SLACK_BOT_TOKEN Slack bot token for outbound deployment notifications
SLACK_DEPLOYMENT_CHANNEL_ID
Slack channel ID for deployment notifications
All other credentials (Snowflake, ThoughtSpot) are stored in Supabase
admin settings and loaded at runtime β€” no extra HF secrets needed.
"""
import os
import sys
from pathlib import Path
# Ensure repo root is on the path
sys.path.insert(0, str(Path(__file__).parent))
# Patch gradio_client bug: schema traversal crashes when schema is a bool
# (happens when a component has additionalProperties: true in its JSON schema).
# Both get_type() and _json_schema_to_python_type() must guard against non-dict input.
import gradio_client.utils as _gcu
_orig_get_type = _gcu.get_type
_gcu.get_type = lambda schema: _orig_get_type(schema) if isinstance(schema, dict) else "any"
_orig_j2p = _gcu._json_schema_to_python_type
_gcu._json_schema_to_python_type = lambda schema, defs=None: "any" if not isinstance(schema, dict) else _orig_j2p(schema, defs)
os.makedirs("results", exist_ok=True)
os.makedirs("demo_logs", exist_ok=True)
from demoprep_app.controllers.chat import authenticate_user, create_chat_interface
app = create_chat_interface()
def _git_commit() -> str:
try:
import subprocess
return subprocess.check_output(
["git", "rev-parse", "--short", "HEAD"],
cwd=Path(__file__).parent,
text=True,
stderr=subprocess.DEVNULL,
).strip()
except Exception:
return os.getenv("GIT_COMMIT", "unknown")
def build_info():
pipeline_ok = False
pipeline_error = ""
try:
from demoprep_app.pipeline.build_demo import build_demo
pipeline_ok = callable(build_demo)
except Exception as exc:
pipeline_error = str(exc)
return {
"app": "demoprep",
"commit": _git_commit(),
"pipeline_ok": pipeline_ok,
"pipeline_import_error": pipeline_error,
}
import gradio.routes as _gr_routes
_orig_create_app = _gr_routes.App.create_app
def _create_app_with_build_info(blocks, app_kwargs=None, auth_dependency=None):
server_app = _orig_create_app(
blocks,
app_kwargs=app_kwargs,
auth_dependency=auth_dependency,
)
if not getattr(server_app.state, "_demoprep_build_info_route", False):
server_app.add_api_route(
"/build-info",
build_info,
methods=["GET"],
include_in_schema=False,
)
server_app.state._demoprep_build_info_route = True
return server_app
_gr_routes.App.create_app = staticmethod(_create_app_with_build_info)
app.queue(
default_concurrency_limit=10,
api_open=False,
)
# Bypass Gradio localhost accessibility check (httpx 0.28 compatibility)
import gradio.networking as _gn
_gn.url_ok = lambda url: True
app.launch(
server_name="0.0.0.0",
server_port=7860,
share=False,
inbrowser=False,
auth=authenticate_user,
show_api=False,
)