Clearwave48 commited on
Commit
f964d49
·
verified ·
1 Parent(s): 0a46798

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +25 -6
main.py CHANGED
@@ -6,16 +6,23 @@ No Gradio, no routing conflicts.
6
 
7
  import os
8
  import json
9
- import base64
10
  import tempfile
11
  import logging
12
- import time
13
  import requests
14
  import numpy as np
 
 
15
  from fastapi import FastAPI, Request
16
  from fastapi.responses import StreamingResponse, JSONResponse
17
  from fastapi.middleware.cors import CORSMiddleware
18
 
 
 
 
 
 
 
 
19
  logging.basicConfig(level=logging.INFO)
20
  logger = logging.getLogger(__name__)
21
 
@@ -86,8 +93,20 @@ def run_pipeline(audio_path, src_lang="auto", tgt_lang="te",
86
  yield {"status": "processing", "step": 5, "message": "Step 5/5 — Summarizing..."}
87
  summary = translator.summarize(transcript)
88
 
89
- with open(clean1, "rb") as f:
90
- enhanced_b64 = base64.b64encode(f.read()).decode("utf-8")
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
  yield {
93
  "status": "done",
@@ -96,7 +115,7 @@ def run_pipeline(audio_path, src_lang="auto", tgt_lang="te",
96
  "transcript": transcript,
97
  "translation": translation,
98
  "summary": summary,
99
- "enhancedAudio": enhanced_b64,
100
  "stats": {
101
  "language": detected_lang.upper(),
102
  "noise_method": stats.get("noise_method", "noisereduce"),
@@ -185,4 +204,4 @@ async def process_url(request: Request):
185
  generate(),
186
  media_type="text/event-stream",
187
  headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
188
- )
 
6
 
7
  import os
8
  import json
 
9
  import tempfile
10
  import logging
 
11
  import requests
12
  import numpy as np
13
+ import cloudinary
14
+ import cloudinary.uploader
15
  from fastapi import FastAPI, Request
16
  from fastapi.responses import StreamingResponse, JSONResponse
17
  from fastapi.middleware.cors import CORSMiddleware
18
 
19
+ # Cloudinary config — set these in your HF Space secrets
20
+ cloudinary.config(
21
+ cloud_name = os.environ.get("CLOUD_NAME"),
22
+ api_key = os.environ.get("API_KEY"),
23
+ api_secret = os.environ.get("API_SECRET"),
24
+ )
25
+
26
  logging.basicConfig(level=logging.INFO)
27
  logger = logging.getLogger(__name__)
28
 
 
93
  yield {"status": "processing", "step": 5, "message": "Step 5/5 — Summarizing..."}
94
  summary = translator.summarize(transcript)
95
 
96
+ # Upload enhanced audio to Cloudinary — returns a URL instead of base64.
97
+ # This keeps the done SSE event tiny (~200 bytes) instead of ~700KB,
98
+ # which was causing the JSON to be split across 85+ TCP chunks.
99
+ try:
100
+ upload_result = cloudinary.uploader.upload(
101
+ clean1,
102
+ resource_type = "video", # Cloudinary uses "video" for audio
103
+ folder = "clearwave_enhanced",
104
+ )
105
+ enhanced_url = upload_result["secure_url"]
106
+ logger.info(f"Enhanced audio uploaded: {enhanced_url}")
107
+ except Exception as e:
108
+ logger.error(f"Cloudinary upload failed: {e}")
109
+ enhanced_url = None
110
 
111
  yield {
112
  "status": "done",
 
115
  "transcript": transcript,
116
  "translation": translation,
117
  "summary": summary,
118
+ "enhancedAudio": enhanced_url,
119
  "stats": {
120
  "language": detected_lang.upper(),
121
  "noise_method": stats.get("noise_method", "noisereduce"),
 
204
  generate(),
205
  media_type="text/event-stream",
206
  headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
207
+ )