cduss commited on
Commit
454ac7d
·
1 Parent(s): d4c9987

wip that will break everything again

Browse files
Files changed (2) hide show
  1. app.py +35 -1
  2. engine.py +13 -40
app.py CHANGED
@@ -1432,13 +1432,47 @@ def build_app() -> gr.Blocks:
1432
 
1433
  def main() -> None:
1434
  """Launch the Visual Novel Gradio app."""
 
 
 
1435
  logger.info(f"=== Visual Novel App Startup ===")
1436
- logger.info("Using HuggingFace CDN URLs for assets - no static file mounting needed!")
 
 
 
 
1437
 
1438
  # Build Gradio app
1439
  logger.info("Building Gradio app...")
1440
  demo = build_app()
1441
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1442
  # Launch with SSR disabled
1443
  logger.info("Launching app...")
1444
  demo.launch(
 
1432
 
1433
  def main() -> None:
1434
  """Launch the Visual Novel Gradio app."""
1435
+ from fastapi.responses import FileResponse
1436
+ from fastapi import HTTPException
1437
+
1438
  logger.info(f"=== Visual Novel App Startup ===")
1439
+
1440
+ # Get absolute paths for static directories
1441
+ script_dir = os.path.dirname(os.path.abspath(__file__))
1442
+ assets_dir = os.path.join(script_dir, "assets")
1443
+ logger.info(f"Assets directory: {assets_dir}")
1444
 
1445
  # Build Gradio app
1446
  logger.info("Building Gradio app...")
1447
  demo = build_app()
1448
 
1449
+ # Enable queue to access the FastAPI app
1450
+ logger.info("Enabling queue...")
1451
+ demo.queue()
1452
+
1453
+ # Add custom route for serving assets
1454
+ logger.info("Adding custom asset route handler...")
1455
+
1456
+ @demo.app.get("/assets/{category}/{filename:path}")
1457
+ async def serve_asset(category: str, filename: str):
1458
+ """Serve asset files with proper MIME types."""
1459
+ import mimetypes
1460
+
1461
+ filepath = os.path.join(assets_dir, category, filename)
1462
+ logger.info(f"Asset request: /assets/{category}/{filename} -> {filepath}")
1463
+
1464
+ if not os.path.exists(filepath):
1465
+ logger.error(f"File not found: {filepath}")
1466
+ raise HTTPException(status_code=404, detail="File not found")
1467
+
1468
+ # Get MIME type
1469
+ mime_type, _ = mimetypes.guess_type(filepath)
1470
+ logger.info(f"Serving {filepath} as {mime_type}")
1471
+
1472
+ return FileResponse(filepath, media_type=mime_type)
1473
+
1474
+ logger.info("Custom asset route registered!")
1475
+
1476
  # Launch with SSR disabled
1477
  logger.info("Launching app...")
1478
  demo.launch(
engine.py CHANGED
@@ -21,53 +21,26 @@ POSITION_OFFSETS = {
21
  }
22
 
23
 
24
- # Cache for base64 encoded assets to avoid re-encoding
25
- _asset_cache = {}
26
-
27
- def _file_to_data_url(filepath: str) -> str:
28
- """Convert a file to a base64 data URL."""
29
- if filepath in _asset_cache:
30
- return _asset_cache[filepath]
31
-
32
- import base64
33
- import mimetypes
34
-
35
- # Guess MIME type from file extension
36
- mime_type, _ = mimetypes.guess_type(filepath)
37
- if mime_type is None:
38
- mime_type = "application/octet-stream"
39
-
40
- try:
41
- with open(filepath, "rb") as f:
42
- data = base64.b64encode(f.read()).decode('utf-8')
43
- data_url = f"data:{mime_type};base64,{data}"
44
- _asset_cache[filepath] = data_url
45
- logger.info(f"Encoded {filepath} as data URL ({len(data_url)} chars)")
46
- return data_url
47
- except Exception as e:
48
- logger.error(f"Failed to encode {filepath}: {e}")
49
- return TEST_IMAGE_URL # Fallback to test image
50
-
51
- # Asset helper functions
52
  def background_asset(filename: str) -> str:
53
- """Get the URL for a background image - base64 encoded."""
54
- script_dir = os.path.dirname(os.path.abspath(__file__))
55
- filepath = os.path.join(script_dir, "assets", "backgrounds", filename)
56
- return _file_to_data_url(filepath)
57
 
58
 
59
  def sprite_asset(filename: str) -> str:
60
- """Get the URL for a sprite image - base64 encoded."""
61
- script_dir = os.path.dirname(os.path.abspath(__file__))
62
- filepath = os.path.join(script_dir, "assets", "sprites", filename)
63
- return _file_to_data_url(filepath)
64
 
65
 
66
  def audio_asset(filename: str) -> str:
67
- """Get the URL for an audio file - base64 encoded."""
68
- script_dir = os.path.dirname(os.path.abspath(__file__))
69
- filepath = os.path.join(script_dir, "assets", "audio", filename)
70
- return _file_to_data_url(filepath)
71
 
72
 
73
  def create_sprite_data_url(bg_color: str = "#fef3c7", border_color: str = "#ea580c") -> str:
 
21
  }
22
 
23
 
24
+ # Asset helper functions - using custom route handler
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  def background_asset(filename: str) -> str:
26
+ """Get the URL for a background image."""
27
+ url = f"/assets/backgrounds/{filename}"
28
+ logger.info(f"background_asset({filename}) -> {url}")
29
+ return url
30
 
31
 
32
  def sprite_asset(filename: str) -> str:
33
+ """Get the URL for a sprite image."""
34
+ url = f"/assets/sprites/{filename}"
35
+ logger.info(f"sprite_asset({filename}) -> {url}")
36
+ return url
37
 
38
 
39
  def audio_asset(filename: str) -> str:
40
+ """Get the URL for an audio file."""
41
+ url = f"/assets/audio/{filename}"
42
+ logger.info(f"audio_asset({filename}) -> {url}")
43
+ return url
44
 
45
 
46
  def create_sprite_data_url(bg_color: str = "#fef3c7", border_color: str = "#ea580c") -> str: