OrbitMC commited on
Commit
d3e8e7f
Β·
verified Β·
1 Parent(s): 720384c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -29
app.py CHANGED
@@ -42,11 +42,15 @@ WHISPER_DIR = CACHE_DIR / "whisper"
42
  for _d in (MODEL_DIR, ANIM_DIR, WHISPER_DIR):
43
  _d.mkdir(parents=True, exist_ok=True)
44
 
45
- VRM_REPO = os.environ.get("OC_VRM_REPO", "OpenCompanion/assets")
46
- VRM_FILE = os.environ.get("OC_VRM_FILE", "Ani.vrm")
47
- VRMA_ZIP = os.environ.get("OC_VRMA_ZIP", "all_vrma.zip")
48
- LLM_MODEL = os.environ.get("OC_LLM_MODEL", "openbmb/MiniCPM5-1B")
49
- WHISPER_SZ = os.environ.get("OC_WHISPER", "base")
 
 
 
 
50
 
51
  # ── Model globals ──────────────────────────────────────────────────────────
52
  _llm = _tokenizer = _whisper = None
@@ -54,26 +58,79 @@ _llm = _tokenizer = _whisper = None
54
  # ── Asset download ─────────────────────────────────────────────────────────
55
  def _ensure_vrm() -> Path:
56
  dest = MODEL_DIR / VRM_FILE
57
- if dest.exists(): return dest
58
- log.info(f"Downloading VRM {VRM_REPO}/{VRM_FILE}")
59
- try:
60
- p = hf_hub_download(repo_id=VRM_REPO, filename=VRM_FILE, local_dir=str(MODEL_DIR))
61
- return Path(p)
62
- except Exception as e:
63
- log.warning(f"VRM download failed: {e}")
64
  return dest
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  def _ensure_vrma_zip() -> Path:
67
  dest = ANIM_DIR / VRMA_ZIP
68
- if dest.exists(): return dest
69
- log.info(f"Downloading VRMA zip {VRM_REPO}/{VRMA_ZIP}")
70
- try:
71
- p = hf_hub_download(repo_id=VRM_REPO, filename=VRMA_ZIP, local_dir=str(ANIM_DIR))
72
- return Path(p)
73
- except Exception as e:
74
- log.warning(f"VRMA zip download failed: {e}")
75
  return dest
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  def _get_anim_names(zip_path: Path) -> list:
78
  if not zip_path.exists(): return []
79
  try:
@@ -94,7 +151,7 @@ ANIM_LIST = ', '.join(ANIM_NAMES) if ANIM_NAMES else 'neutral2.vrma'
94
  log.info(f"Found {len(ANIM_NAMES)} VRMAs: {ANIM_LIST[:120]}...")
95
 
96
  VRM_SERVER_PATH = f"/file={_vrm_path}" if _vrm_path.exists() else ""
97
- VRMA_SERVER_PATH = f"/file={_vrma_path}" if _vrma_path.exists() else ""
98
 
99
  # ── System prompt ──────────────────────────────────────────────────────────
100
  SYSTEM_PROMPT = """\
@@ -1602,7 +1659,6 @@ THINK_JS = """
1602
  with gr.Blocks(
1603
  title="Open Companion",
1604
  analytics_enabled=False,
1605
- css=CUSTOM_CSS,
1606
  ) as demo:
1607
 
1608
  history_state = gr.State([])
@@ -1701,11 +1757,14 @@ async def tts_route(
1701
 
1702
 
1703
  # ── Entry point ──────────────────────────────────────────────
1704
- if __name__ == "__main__":
1705
- demo.launch(
1706
- server_name = "0.0.0.0",
1707
- server_port = int(os.environ.get("PORT", 7860)),
1708
- share = False,
1709
- show_error = True,
1710
- allowed_paths = [str(CACHE_DIR)],
1711
- )
 
 
 
 
42
  for _d in (MODEL_DIR, ANIM_DIR, WHISPER_DIR):
43
  _d.mkdir(parents=True, exist_ok=True)
44
 
45
+ VRM_FILE = os.environ.get("OC_VRM_FILE", "Ani.vrm")
46
+ VRMA_ZIP = os.environ.get("OC_VRMA_ZIP", "all_vrma.zip")
47
+ LLM_MODEL = os.environ.get("OC_LLM_MODEL", "openbmb/MiniCPM5-1B")
48
+ WHISPER_SZ = os.environ.get("OC_WHISPER", "base")
49
+
50
+ # Auto-detect HF Space repo from SPACE_ID env var (set automatically on HF Spaces).
51
+ # Fallback order: OC_VRM_REPO env β†’ SPACE_ID β†’ None (skip HF download)
52
+ _SPACE_ID = os.environ.get("SPACE_ID", "") # e.g. "MyUser/open-companion"
53
+ VRM_REPO = os.environ.get("OC_VRM_REPO", _SPACE_ID if _SPACE_ID else "")
54
 
55
  # ── Model globals ──────────────────────────────────────────────────────────
56
  _llm = _tokenizer = _whisper = None
 
58
  # ── Asset download ─────────────────────────────────────────────────────────
59
  def _ensure_vrm() -> Path:
60
  dest = MODEL_DIR / VRM_FILE
61
+ if dest.exists():
62
+ log.info(f"VRM already cached: {dest}")
 
 
 
 
 
63
  return dest
64
 
65
+ # 1. Check local /app directory (HF Spaces places repo files there)
66
+ local_candidates = [
67
+ Path("/app") / VRM_FILE,
68
+ Path(".") / VRM_FILE,
69
+ ]
70
+ for lp in local_candidates:
71
+ if lp.exists():
72
+ log.info(f"VRM found locally at {lp}, symlinking/copying to cache")
73
+ import shutil
74
+ shutil.copy2(str(lp), str(dest))
75
+ return dest
76
+
77
+ # 2. Download from HF repo
78
+ if VRM_REPO:
79
+ log.info(f"Downloading VRM from {VRM_REPO}/{VRM_FILE}")
80
+ try:
81
+ p = hf_hub_download(repo_id=VRM_REPO, filename=VRM_FILE,
82
+ local_dir=str(MODEL_DIR), repo_type="space")
83
+ return Path(p)
84
+ except Exception as e:
85
+ log.warning(f"Space repo VRM download failed: {e}")
86
+ # Try as model/dataset repo
87
+ try:
88
+ p = hf_hub_download(repo_id=VRM_REPO, filename=VRM_FILE,
89
+ local_dir=str(MODEL_DIR))
90
+ return Path(p)
91
+ except Exception as e:
92
+ log.warning(f"VRM download failed: {e}")
93
+
94
+ log.warning("VRM not found β€” browser will use CDN fallback if available")
95
+ return dest
96
+
97
  def _ensure_vrma_zip() -> Path:
98
  dest = ANIM_DIR / VRMA_ZIP
99
+ if dest.exists():
100
+ log.info(f"VRMA zip already cached: {dest}")
 
 
 
 
 
101
  return dest
102
 
103
+ # 1. Check local /app directory
104
+ local_candidates = [
105
+ Path("/app") / VRMA_ZIP,
106
+ Path(".") / VRMA_ZIP,
107
+ ]
108
+ for lp in local_candidates:
109
+ if lp.exists():
110
+ log.info(f"VRMA zip found locally at {lp}")
111
+ import shutil
112
+ shutil.copy2(str(lp), str(dest))
113
+ return dest
114
+
115
+ # 2. Download from HF repo
116
+ if VRM_REPO:
117
+ log.info(f"Downloading VRMA zip from {VRM_REPO}/{VRMA_ZIP}")
118
+ try:
119
+ p = hf_hub_download(repo_id=VRM_REPO, filename=VRMA_ZIP,
120
+ local_dir=str(ANIM_DIR), repo_type="space")
121
+ return Path(p)
122
+ except Exception as e:
123
+ log.warning(f"Space repo VRMA download failed: {e}")
124
+ try:
125
+ p = hf_hub_download(repo_id=VRM_REPO, filename=VRMA_ZIP,
126
+ local_dir=str(ANIM_DIR))
127
+ return Path(p)
128
+ except Exception as e:
129
+ log.warning(f"VRMA zip download failed: {e}")
130
+
131
+ log.warning("VRMA zip not found β€” browser will fetch from GitHub CDN fallback")
132
+ return dest
133
+
134
  def _get_anim_names(zip_path: Path) -> list:
135
  if not zip_path.exists(): return []
136
  try:
 
151
  log.info(f"Found {len(ANIM_NAMES)} VRMAs: {ANIM_LIST[:120]}...")
152
 
153
  VRM_SERVER_PATH = f"/file={_vrm_path}" if _vrm_path.exists() else ""
154
+ VRMA_SERVER_PATH = f"/file={_vrma_path}" if _vrma_path.exists() else "" # ← fixed: was _vrm_path
155
 
156
  # ── System prompt ──────────────────────────────────────────────────────────
157
  SYSTEM_PROMPT = """\
 
1659
  with gr.Blocks(
1660
  title="Open Companion",
1661
  analytics_enabled=False,
 
1662
  ) as demo:
1663
 
1664
  history_state = gr.State([])
 
1757
 
1758
 
1759
  # ── Entry point ──────────────────────────────────────────────
1760
+ # ── Entry point ──────────────────────────────────────────────
1761
+ # Called unconditionally so HF Spaces (which imports the module) also applies css.
1762
+ # css= is the Gradio 6.0+ way; it was moved from gr.Blocks() to launch().
1763
+ demo.launch(
1764
+ server_name = "0.0.0.0",
1765
+ server_port = int(os.environ.get("PORT", 7860)),
1766
+ share = False,
1767
+ show_error = True,
1768
+ allowed_paths = [str(CACHE_DIR)],
1769
+ css = CUSTOM_CSS,
1770
+ )