habulaj commited on
Commit
fb9ecbd
Β·
verified Β·
1 Parent(s): e00e8a8

Update routers/editor.py

Browse files
Files changed (1) hide show
  1. routers/editor.py +26 -4
routers/editor.py CHANGED
@@ -122,6 +122,28 @@ EASING_TYPES = [
122
  # editorAgent.ts's stream-reading helper on the frontend, which expects the
123
  # exact same shape.
124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  def _cleanup_temp_files(*paths: Optional[str]) -> None:
126
  """Shared cleanup for every temp file this router creates β€” dedups via
127
  `set()` since a couple of these paths can legitimately be the same file
@@ -396,7 +418,7 @@ async def editor_chat(request: EditorChatRequest):
396
  save_session(request.chat_id, chat.metadata)
397
  _cleanup_temp_files(context_path)
398
 
399
- return StreamingResponse(body_stream(), media_type="text/plain; charset=utf-8")
400
 
401
 
402
  # ── Smart video cut (Fase 2) ────────────────────────────────────────────────
@@ -539,7 +561,7 @@ async def editor_smart_cut(request: SmartCutRequest):
539
  async def empty_body():
540
  yield '{"segments": []}\n' + "I couldn't find any speech in this clip to analyze."
541
 
542
- return StreamingResponse(empty_body(), media_type="text/plain; charset=utf-8")
543
 
544
  print(f"πŸ•’ [SMART-CUT] TranscriΓ§Γ£o em tempo absoluto ({len(transcript_blocks)} blocos):")
545
  for b in transcript_blocks:
@@ -589,7 +611,7 @@ async def editor_smart_cut(request: SmartCutRequest):
589
  finally:
590
  _cleanup_temp_files(original_media_path, context_path)
591
 
592
- return StreamingResponse(body_stream(), media_type="text/plain; charset=utf-8")
593
 
594
 
595
  # ── Media analysis (analyze_media) ─────────────────────────────────────────
@@ -740,4 +762,4 @@ async def editor_analyze_media(
740
  finally:
741
  _cleanup_temp_files(upload_path, gemini_filepath, context_path)
742
 
743
- return StreamingResponse(body_stream(), media_type="text/plain; charset=utf-8")
 
122
  # editorAgent.ts's stream-reading helper on the frontend, which expects the
123
  # exact same shape.
124
 
125
+ # Headers asking any reverse proxy in front of this app (Hugging Face
126
+ # Spaces' Docker gateway, in particular) to NOT buffer the response until
127
+ # it's complete. `StreamingResponse` already streams correctly out of
128
+ # uvicorn on its own (confirmed: no compression/buffering middleware in
129
+ # main.py) β€” but a hop between here and the browser can still coalesce
130
+ # everything into one delivery despite that, which reads exactly like "no
131
+ # streaming" client-side even though the server sent it live.
132
+ # `X-Accel-Buffering: no` is the standard nginx-family opt-out (harmless to
133
+ # send even if the fronting proxy isn't nginx-based); Cache-Control/
134
+ # Connection reinforce the same intent for any other caching layer. If a
135
+ # proxy hop still buffers despite these, that's a platform-level behavior no
136
+ # response header can override β€” `curl -N <url>` straight against the
137
+ # deployed endpoint (bypassing the browser entirely) is the way to tell
138
+ # "backend isn't streaming" apart from "something downstream is buffering
139
+ # it anyway".
140
+ _NO_BUFFER_HEADERS = {
141
+ "X-Accel-Buffering": "no",
142
+ "Cache-Control": "no-cache, no-transform",
143
+ "Connection": "keep-alive",
144
+ }
145
+
146
+
147
  def _cleanup_temp_files(*paths: Optional[str]) -> None:
148
  """Shared cleanup for every temp file this router creates β€” dedups via
149
  `set()` since a couple of these paths can legitimately be the same file
 
418
  save_session(request.chat_id, chat.metadata)
419
  _cleanup_temp_files(context_path)
420
 
421
+ return StreamingResponse(body_stream(), media_type="text/plain; charset=utf-8", headers=_NO_BUFFER_HEADERS)
422
 
423
 
424
  # ── Smart video cut (Fase 2) ────────────────────────────────────────────────
 
561
  async def empty_body():
562
  yield '{"segments": []}\n' + "I couldn't find any speech in this clip to analyze."
563
 
564
+ return StreamingResponse(empty_body(), media_type="text/plain; charset=utf-8", headers=_NO_BUFFER_HEADERS)
565
 
566
  print(f"πŸ•’ [SMART-CUT] TranscriΓ§Γ£o em tempo absoluto ({len(transcript_blocks)} blocos):")
567
  for b in transcript_blocks:
 
611
  finally:
612
  _cleanup_temp_files(original_media_path, context_path)
613
 
614
+ return StreamingResponse(body_stream(), media_type="text/plain; charset=utf-8", headers=_NO_BUFFER_HEADERS)
615
 
616
 
617
  # ── Media analysis (analyze_media) ─────────────────────────────────────────
 
762
  finally:
763
  _cleanup_temp_files(upload_path, gemini_filepath, context_path)
764
 
765
+ return StreamingResponse(body_stream(), media_type="text/plain; charset=utf-8", headers=_NO_BUFFER_HEADERS)