cduss commited on
Commit
2919798
·
1 Parent(s): c2f5fdd

fix mime types and stuff

Browse files
Files changed (2) hide show
  1. app.py +34 -28
  2. engine.py +9 -6
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
- """Generate JavaScript to dynamically load the DXL script from static files."""
707
- import time
708
- timestamp = int(time.time())
 
 
 
 
 
 
 
 
 
 
 
 
709
  return f"""
710
  () => {{
 
711
  const script = document.createElement('script');
712
  script.type = 'module';
713
- script.src = '/web/dxl_webserial.js?v={timestamp}';
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 with FastAPI for static file serving."""
1418
- # Create FastAPI app
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 and mount Gradio app
1429
  gradio_app = build_app()
1430
- fastapi_app = gr.mount_gradio_app(fastapi_app, gradio_app, path="/")
1431
 
1432
- # Launch with proper shutdown handling
1433
- import uvicorn
1434
- try:
1435
- uvicorn.run(
1436
- fastapi_app,
1437
- host="0.0.0.0",
1438
- port=7860,
1439
- log_level="debug",
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 URL to a background image in the assets directory."""
21
- return os.path.join("user-assets", "backgrounds", filename)
 
22
 
23
 
24
  def sprite_asset(filename: str) -> str:
25
- """Get the URL to a sprite image in the assets directory."""
26
- return os.path.join("user-assets", "sprites", filename)
 
27
 
28
 
29
  def audio_asset(filename: str) -> str:
30
- """Get the URL to an audio file in the assets directory."""
31
- return os.path.join("user-assets", "audio", filename)
 
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: