Gram_Sewa_Mitra / app.py
rameshmoorthy's picture
Upload 2 files
7f709cf verified
Raw History Blame Contribute Delete
2.02 kB
import html
import json
from pathlib import Path
import gradio as gr
import spaces
# Satisfy ZeroGPU startup check
@spaces.GPU
def _init_zero_gpu():
return True
_init_zero_gpu()
BASE = Path(__file__).parent
SCHEMES = json.loads((BASE / "scheme_data.json").read_text(encoding="utf-8"))
QUESTIONS = json.loads((BASE / "questions.json").read_text(encoding="utf-8"))
AVATARS = json.loads((BASE / "avatars.json").read_text(encoding="utf-8"))
def build_page() -> str:
"""Return the full, self-contained HTML page with the JSON data embedded."""
raw_html = (BASE / "app.html").read_text(encoding="utf-8")
payload = {"schemes": SCHEMES, "questions": QUESTIONS, "avatars": AVATARS}
# "</" is escaped so the JSON can never close the <script> tag early.
json_str = json.dumps(payload, ensure_ascii=False).replace("</", "<\\/")
return raw_html.replace(
"<!-- __DATA_SCRIPT__ -->",
f'<script id="app-data" type="application/json">{json_str}</script>',
)
def load_interface() -> str:
# gr.HTML does NOT execute <script> tags, which is why the avatars never
# appeared. Putting the page in an iframe (srcdoc) lets its JavaScript run.
# allow="microphone" is required for speech recognition inside the iframe.
srcdoc = html.escape(build_page(), quote=True)
return (
f'<iframe srcdoc="{srcdoc}" allow="microphone; autoplay" '
'style="width:100%;height:100vh;min-height:900px;border:0;display:block;"></iframe>'
)
CSS = """
.gradio-container{padding:0 !important;max-width:100% !important;}
.gradio-container .prose, .gradio-container .block{padding:0 !important;border:0 !important;box-shadow:none !important;background:transparent !important;}
footer{display:none !important;}
"""
with gr.Blocks(
title="Gram Seva Mitra – Suthukeny High School",
theme=gr.themes.Soft(primary_hue="green", secondary_hue="orange"),
css=CSS,
) as demo:
gr.HTML(load_interface())
if __name__ == "__main__":
demo.launch(ssr_mode=False)