J Z commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -148,19 +148,30 @@ async def stream_audio(filename: str):
|
|
| 148 |
|
| 149 |
@app.post("/api/chat")
|
| 150 |
async def chat(request: Request):
|
|
|
|
|
|
|
|
|
|
| 151 |
body = await request.json()
|
| 152 |
user_text = body.get("text", "").strip()
|
| 153 |
if not user_text: raise HTTPException(400, "No text")
|
| 154 |
|
| 155 |
logger.info(f"[CHAT] User: {user_text}")
|
| 156 |
|
|
|
|
|
|
|
| 157 |
from llm import generate_darwin_response
|
| 158 |
llm_result = generate_darwin_response(user_text)
|
| 159 |
reply_text = llm_result['text']
|
|
|
|
| 160 |
logger.info(f"[CHAT] Darwin: {reply_text}")
|
|
|
|
| 161 |
|
|
|
|
| 162 |
from tts import generate_audio
|
| 163 |
audio_path = generate_audio(reply_text)
|
|
|
|
|
|
|
|
|
|
| 164 |
audio_url = None
|
| 165 |
audio_duration = 0
|
| 166 |
if audio_path and os.path.exists(audio_path):
|
|
@@ -172,6 +183,7 @@ async def chat(request: Request):
|
|
| 172 |
except Exception:
|
| 173 |
audio_duration = 3.0
|
| 174 |
|
|
|
|
| 175 |
lipsync_video_url = None
|
| 176 |
if FLOAT_ENABLED and audio_path and os.path.exists(audio_path):
|
| 177 |
try:
|
|
@@ -179,15 +191,20 @@ async def chat(request: Request):
|
|
| 179 |
lipsync = get_lipsync()
|
| 180 |
if lipsync.ready:
|
| 181 |
logger.info("[CHAT] Running FLOAT lipsync...")
|
|
|
|
| 182 |
lp = lipsync.generate(audio_path)
|
|
|
|
|
|
|
| 183 |
if lp and os.path.exists(lp):
|
| 184 |
lipsync_video_url = f"/api/stream/{os.path.basename(lp)}"
|
| 185 |
-
logger.info(f"[CHAT] Lipsync: {lipsync_video_url}")
|
| 186 |
else:
|
| 187 |
logger.info("[CHAT] FLOAT not ready")
|
| 188 |
except Exception as e:
|
| 189 |
logger.error(f"[CHAT] Lipsync failed: {e}", exc_info=True)
|
| 190 |
|
|
|
|
|
|
|
|
|
|
| 191 |
return JSONResponse({
|
| 192 |
"text": reply_text,
|
| 193 |
"emotion": llm_result.get('emotion','neutral'),
|
|
|
|
| 148 |
|
| 149 |
@app.post("/api/chat")
|
| 150 |
async def chat(request: Request):
|
| 151 |
+
import time as _time
|
| 152 |
+
t0 = _time.time()
|
| 153 |
+
|
| 154 |
body = await request.json()
|
| 155 |
user_text = body.get("text", "").strip()
|
| 156 |
if not user_text: raise HTTPException(400, "No text")
|
| 157 |
|
| 158 |
logger.info(f"[CHAT] User: {user_text}")
|
| 159 |
|
| 160 |
+
# 1. LLM
|
| 161 |
+
t1 = _time.time()
|
| 162 |
from llm import generate_darwin_response
|
| 163 |
llm_result = generate_darwin_response(user_text)
|
| 164 |
reply_text = llm_result['text']
|
| 165 |
+
t2 = _time.time()
|
| 166 |
logger.info(f"[CHAT] Darwin: {reply_text}")
|
| 167 |
+
logger.info(f"[TIMING] LLM: {t2-t1:.2f}s")
|
| 168 |
|
| 169 |
+
# 2. TTS
|
| 170 |
from tts import generate_audio
|
| 171 |
audio_path = generate_audio(reply_text)
|
| 172 |
+
t3 = _time.time()
|
| 173 |
+
logger.info(f"[TIMING] TTS: {t3-t2:.2f}s")
|
| 174 |
+
|
| 175 |
audio_url = None
|
| 176 |
audio_duration = 0
|
| 177 |
if audio_path and os.path.exists(audio_path):
|
|
|
|
| 183 |
except Exception:
|
| 184 |
audio_duration = 3.0
|
| 185 |
|
| 186 |
+
# 3. Lipsync
|
| 187 |
lipsync_video_url = None
|
| 188 |
if FLOAT_ENABLED and audio_path and os.path.exists(audio_path):
|
| 189 |
try:
|
|
|
|
| 191 |
lipsync = get_lipsync()
|
| 192 |
if lipsync.ready:
|
| 193 |
logger.info("[CHAT] Running FLOAT lipsync...")
|
| 194 |
+
t4 = _time.time()
|
| 195 |
lp = lipsync.generate(audio_path)
|
| 196 |
+
t5 = _time.time()
|
| 197 |
+
logger.info(f"[TIMING] FLOAT total: {t5-t4:.2f}s")
|
| 198 |
if lp and os.path.exists(lp):
|
| 199 |
lipsync_video_url = f"/api/stream/{os.path.basename(lp)}"
|
|
|
|
| 200 |
else:
|
| 201 |
logger.info("[CHAT] FLOAT not ready")
|
| 202 |
except Exception as e:
|
| 203 |
logger.error(f"[CHAT] Lipsync failed: {e}", exc_info=True)
|
| 204 |
|
| 205 |
+
t_end = _time.time()
|
| 206 |
+
logger.info(f"[TIMING] TOTAL request: {t_end-t0:.2f}s")
|
| 207 |
+
|
| 208 |
return JSONResponse({
|
| 209 |
"text": reply_text,
|
| 210 |
"emotion": llm_result.get('emotion','neutral'),
|