J Z commited on
Commit
2d6eead
·
verified ·
1 Parent(s): 8006879

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -5
app.py CHANGED
@@ -25,12 +25,84 @@ TTS_DIR = Path("/tmp/tts_output")
25
  LIPSYNC_DIR = Path("/tmp/lipsync_output")
26
  REPO_ID = os.environ.get("SPACE_ID", "ffbeqwbe/AvatarChatbot")
27
 
 
 
28
  for d in [VIDEOS_DIR, TTS_DIR, LIPSYNC_DIR]:
29
  d.mkdir(parents=True, exist_ok=True)
30
 
31
 
32
- # --- Startup: download idle videos from repo ---
33
  @app.on_event("startup")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  async def download_videos():
35
  from huggingface_hub import HfApi, hf_hub_download
36
  import shutil
@@ -166,11 +238,23 @@ async def chat(request: Request):
166
  except Exception:
167
  audio_duration = 3.0
168
 
169
- # 3. Lipsync (Phase 2 - placeholder)
170
  lipsync_video_url = None
171
- # TODO: When FLOAT is integrated:
172
- # lipsync_path = generate_lipsync(audio_path)
173
- # lipsync_video_url = f"/api/stream/{os.path.basename(lipsync_path)}"
 
 
 
 
 
 
 
 
 
 
 
 
174
 
175
  return JSONResponse({
176
  "text": reply_text,
 
25
  LIPSYNC_DIR = Path("/tmp/lipsync_output")
26
  REPO_ID = os.environ.get("SPACE_ID", "ffbeqwbe/AvatarChatbot")
27
 
28
+ FLOAT_ENABLED = os.environ.get("FLOAT_ENABLED", "true").lower() == "true"
29
+
30
  for d in [VIDEOS_DIR, TTS_DIR, LIPSYNC_DIR]:
31
  d.mkdir(parents=True, exist_ok=True)
32
 
33
 
34
+ # --- Startup: download idle videos + init FLOAT ---
35
  @app.on_event("startup")
36
+ async def startup():
37
+ await download_videos()
38
+ if FLOAT_ENABLED:
39
+ await init_float()
40
+
41
+
42
+ async def init_float():
43
+ """Initialize FLOAT lipsync model (runs once at startup)."""
44
+ import torch
45
+ logger.info(f"[STARTUP] CUDA available: {torch.cuda.is_available()}")
46
+ if not torch.cuda.is_available():
47
+ logger.warning("[STARTUP] No GPU detected - FLOAT lipsync disabled")
48
+ return
49
+
50
+ try:
51
+ from float_lipsync import get_lipsync
52
+ from huggingface_hub import hf_hub_download
53
+ import shutil
54
+
55
+ checkpoints_dir = Path("/app/checkpoints")
56
+ checkpoints_dir.mkdir(parents=True, exist_ok=True)
57
+
58
+ # Download wav2vec2-base-960h from HuggingFace
59
+ w2v_dir = checkpoints_dir / "wav2vec2-base-960h"
60
+ if not w2v_dir.exists():
61
+ logger.info("[STARTUP] Downloading wav2vec2-base-960h...")
62
+ from huggingface_hub import snapshot_download
63
+ snapshot_download(
64
+ repo_id="facebook/wav2vec2-base-960h",
65
+ local_dir=str(w2v_dir),
66
+ )
67
+ logger.info("[STARTUP] wav2vec2-base-960h downloaded")
68
+
69
+ # Download wav2vec-english-speech-emotion-recognition
70
+ emo_dir = checkpoints_dir / "wav2vec-english-speech-emotion-recognition"
71
+ if not emo_dir.exists():
72
+ logger.info("[STARTUP] Downloading emotion recognition model...")
73
+ from huggingface_hub import snapshot_download
74
+ snapshot_download(
75
+ repo_id="r-f/wav2vec-english-speech-emotion-recognition",
76
+ local_dir=str(emo_dir),
77
+ )
78
+ logger.info("[STARTUP] Emotion model downloaded")
79
+
80
+ # Check for float.pth (must be manually uploaded to repo)
81
+ float_ckpt = checkpoints_dir / "float.pth"
82
+ if not float_ckpt.exists():
83
+ logger.warning("[STARTUP] float.pth not found at /app/checkpoints/float.pth")
84
+ logger.warning("[STARTUP] Upload it to your Space repo under app/checkpoints/float.pth")
85
+ logger.warning("[STARTUP] FLOAT lipsync will be disabled until checkpoint is available")
86
+ return
87
+
88
+ # Check for reference image
89
+ assets_dir = Path("/app/assets")
90
+ assets_dir.mkdir(parents=True, exist_ok=True)
91
+ ref_image = assets_dir / "ref.png"
92
+ if not ref_image.exists():
93
+ logger.warning("[STARTUP] No reference image at /app/assets/ref.png")
94
+ logger.warning("[STARTUP] Upload a face image as app/assets/ref.png")
95
+ return
96
+
97
+ # Initialize FLOAT
98
+ lipsync = get_lipsync()
99
+ lipsync.initialize()
100
+ logger.info("[STARTUP] ✓ FLOAT lipsync system ready!")
101
+
102
+ except Exception as e:
103
+ logger.error(f"[STARTUP] FLOAT init failed: {e}", exc_info=True)
104
+
105
+
106
  async def download_videos():
107
  from huggingface_hub import HfApi, hf_hub_download
108
  import shutil
 
238
  except Exception:
239
  audio_duration = 3.0
240
 
241
+ # 3. Lipsync (Phase 2 - FLOAT)
242
  lipsync_video_url = None
243
+ if FLOAT_ENABLED and audio_path and os.path.exists(audio_path):
244
+ try:
245
+ from float_lipsync import get_lipsync
246
+ lipsync = get_lipsync()
247
+ if lipsync.ready:
248
+ logger.info("[CHAT] Running FLOAT lipsync...")
249
+ lipsync_path = lipsync.generate(audio_path)
250
+ if lipsync_path and os.path.exists(lipsync_path):
251
+ lipsync_filename = os.path.basename(lipsync_path)
252
+ lipsync_video_url = f"/api/stream/{lipsync_filename}"
253
+ logger.info(f"[CHAT] Lipsync video: {lipsync_video_url}")
254
+ else:
255
+ logger.info("[CHAT] FLOAT not ready, skipping lipsync")
256
+ except Exception as e:
257
+ logger.error(f"[CHAT] Lipsync failed: {e}", exc_info=True)
258
 
259
  return JSONResponse({
260
  "text": reply_text,