Spaces:
Sleeping
Sleeping
fix mime types and stuff
Browse files
app.py
CHANGED
|
@@ -9,8 +9,6 @@ from typing import Optional
|
|
| 9 |
|
| 10 |
import gradio as gr
|
| 11 |
from fastrtc import WebRTC
|
| 12 |
-
from fastapi import FastAPI
|
| 13 |
-
from fastapi.staticfiles import StaticFiles
|
| 14 |
|
| 15 |
from engine import SceneState, POSITION_OFFSETS, Choice, InputRequest
|
| 16 |
from story import build_sample_story
|
|
@@ -703,16 +701,29 @@ async (currentDevices) => {
|
|
| 703 |
"""
|
| 704 |
|
| 705 |
def load_dxl_script_js() -> str:
|
| 706 |
-
"""
|
| 707 |
-
|
| 708 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 709 |
return f"""
|
| 710 |
() => {{
|
|
|
|
| 711 |
const script = document.createElement('script');
|
| 712 |
script.type = 'module';
|
| 713 |
-
script.
|
| 714 |
-
script.onerror = () => console.error("[DXL] Failed to load motor control script");
|
| 715 |
document.head.appendChild(script);
|
|
|
|
| 716 |
}}
|
| 717 |
"""
|
| 718 |
|
|
@@ -952,6 +963,11 @@ async (pose_data) => {
|
|
| 952 |
|
| 953 |
|
| 954 |
def build_app() -> gr.Blocks:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 955 |
with gr.Blocks(title="Gradio Visual Novel") as demo:
|
| 956 |
gr.HTML(f"<style>{CUSTOM_CSS}</style>", elem_id="vn-styles")
|
| 957 |
story_state = gr.State()
|
|
@@ -1414,33 +1430,23 @@ def build_app() -> gr.Blocks:
|
|
| 1414 |
|
| 1415 |
|
| 1416 |
def main() -> None:
|
| 1417 |
-
"""Launch the Visual Novel Gradio app
|
| 1418 |
-
#
|
| 1419 |
-
fastapi_app = FastAPI()
|
| 1420 |
-
|
| 1421 |
-
# Mount static files for assets and web scripts
|
| 1422 |
script_dir = os.path.dirname(os.path.abspath(__file__))
|
| 1423 |
assets_dir = os.path.join(script_dir, "assets")
|
| 1424 |
web_dir = os.path.join(script_dir, "web")
|
| 1425 |
-
fastapi_app.mount("/user-assets", StaticFiles(directory=assets_dir), name="user-assets")
|
| 1426 |
-
fastapi_app.mount("/web", StaticFiles(directory=web_dir), name="web")
|
| 1427 |
|
| 1428 |
-
# Build
|
| 1429 |
gradio_app = build_app()
|
| 1430 |
-
fastapi_app = gr.mount_gradio_app(fastapi_app, gradio_app, path="/")
|
| 1431 |
|
| 1432 |
-
#
|
| 1433 |
-
|
| 1434 |
-
|
| 1435 |
-
|
| 1436 |
-
|
| 1437 |
-
|
| 1438 |
-
|
| 1439 |
-
|
| 1440 |
-
timeout_graceful_shutdown=1 # Quick shutdown
|
| 1441 |
-
)
|
| 1442 |
-
except KeyboardInterrupt:
|
| 1443 |
-
print("\n[INFO] Server stopped")
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
|
|
|
| 9 |
|
| 10 |
import gradio as gr
|
| 11 |
from fastrtc import WebRTC
|
|
|
|
|
|
|
| 12 |
|
| 13 |
from engine import SceneState, POSITION_OFFSETS, Choice, InputRequest
|
| 14 |
from story import build_sample_story
|
|
|
|
| 701 |
"""
|
| 702 |
|
| 703 |
def load_dxl_script_js() -> str:
|
| 704 |
+
"""Inline the DXL script directly (for HF Spaces compatibility)."""
|
| 705 |
+
# Read the JavaScript file and inline it
|
| 706 |
+
script_dir = os.path.dirname(os.path.abspath(__file__))
|
| 707 |
+
web_dir = os.path.join(script_dir, "web")
|
| 708 |
+
dxl_script_path = os.path.join(web_dir, "dxl_webserial.js")
|
| 709 |
+
|
| 710 |
+
try:
|
| 711 |
+
with open(dxl_script_path, 'r') as f:
|
| 712 |
+
dxl_js_content = f.read()
|
| 713 |
+
except FileNotFoundError:
|
| 714 |
+
dxl_js_content = "console.error('[DXL] Script file not found');"
|
| 715 |
+
|
| 716 |
+
# Escape backticks and ${} in the JavaScript content for safe embedding
|
| 717 |
+
dxl_js_content_escaped = dxl_js_content.replace('\\', '\\\\').replace('`', '\\`').replace('${', '\\${')
|
| 718 |
+
|
| 719 |
return f"""
|
| 720 |
() => {{
|
| 721 |
+
// Inline DXL script for HF Spaces compatibility
|
| 722 |
const script = document.createElement('script');
|
| 723 |
script.type = 'module';
|
| 724 |
+
script.textContent = `{dxl_js_content_escaped}`;
|
|
|
|
| 725 |
document.head.appendChild(script);
|
| 726 |
+
console.log('[DXL] Script loaded inline');
|
| 727 |
}}
|
| 728 |
"""
|
| 729 |
|
|
|
|
| 963 |
|
| 964 |
|
| 965 |
def build_app() -> gr.Blocks:
|
| 966 |
+
# Get absolute paths for static directories
|
| 967 |
+
script_dir = os.path.dirname(os.path.abspath(__file__))
|
| 968 |
+
assets_dir = os.path.join(script_dir, "assets")
|
| 969 |
+
web_dir = os.path.join(script_dir, "web")
|
| 970 |
+
|
| 971 |
with gr.Blocks(title="Gradio Visual Novel") as demo:
|
| 972 |
gr.HTML(f"<style>{CUSTOM_CSS}</style>", elem_id="vn-styles")
|
| 973 |
story_state = gr.State()
|
|
|
|
| 1430 |
|
| 1431 |
|
| 1432 |
def main() -> None:
|
| 1433 |
+
"""Launch the Visual Novel Gradio app."""
|
| 1434 |
+
# Get absolute paths for static directories
|
|
|
|
|
|
|
|
|
|
| 1435 |
script_dir = os.path.dirname(os.path.abspath(__file__))
|
| 1436 |
assets_dir = os.path.join(script_dir, "assets")
|
| 1437 |
web_dir = os.path.join(script_dir, "web")
|
|
|
|
|
|
|
| 1438 |
|
| 1439 |
+
# Build Gradio app
|
| 1440 |
gradio_app = build_app()
|
|
|
|
| 1441 |
|
| 1442 |
+
# For HF Spaces compatibility, we launch directly without FastAPI
|
| 1443 |
+
# Gradio will handle static files through allowed_paths
|
| 1444 |
+
gradio_app.launch(
|
| 1445 |
+
server_name="0.0.0.0",
|
| 1446 |
+
server_port=7860,
|
| 1447 |
+
allowed_paths=[assets_dir, web_dir],
|
| 1448 |
+
show_error=True
|
| 1449 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
engine.py
CHANGED
|
@@ -17,18 +17,21 @@ POSITION_OFFSETS = {
|
|
| 17 |
|
| 18 |
# Asset helper functions
|
| 19 |
def background_asset(filename: str) -> str:
|
| 20 |
-
"""Get the
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
|
| 24 |
def sprite_asset(filename: str) -> str:
|
| 25 |
-
"""Get the
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
|
| 29 |
def audio_asset(filename: str) -> str:
|
| 30 |
-
"""Get the
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
def create_sprite_data_url(bg_color: str = "#fef3c7", border_color: str = "#ea580c") -> str:
|
|
|
|
| 17 |
|
| 18 |
# Asset helper functions
|
| 19 |
def background_asset(filename: str) -> str:
|
| 20 |
+
"""Get the absolute path to a background image in the assets directory."""
|
| 21 |
+
script_dir = os.path.dirname(os.path.abspath(__file__))
|
| 22 |
+
return os.path.join(script_dir, "assets", "backgrounds", filename)
|
| 23 |
|
| 24 |
|
| 25 |
def sprite_asset(filename: str) -> str:
|
| 26 |
+
"""Get the absolute path to a sprite image in the assets directory."""
|
| 27 |
+
script_dir = os.path.dirname(os.path.abspath(__file__))
|
| 28 |
+
return os.path.join(script_dir, "assets", "sprites", filename)
|
| 29 |
|
| 30 |
|
| 31 |
def audio_asset(filename: str) -> str:
|
| 32 |
+
"""Get the absolute path to an audio file in the assets directory."""
|
| 33 |
+
script_dir = os.path.dirname(os.path.abspath(__file__))
|
| 34 |
+
return os.path.join(script_dir, "assets", "audio", filename)
|
| 35 |
|
| 36 |
|
| 37 |
def create_sprite_data_url(bg_color: str = "#fef3c7", border_color: str = "#ea580c") -> str:
|