maylinejix commited on
Commit
e7cf803
·
verified ·
1 Parent(s): 93457f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -0
app.py CHANGED
@@ -83,6 +83,43 @@ async def transcribe(
83
  finally:
84
  os.unlink(tmp_path)
85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  @app.get("/health")
87
  async def health():
88
  return {"status": "ok", "model": "whisper-base"}
 
83
  finally:
84
  os.unlink(tmp_path)
85
 
86
+ @app.post("/transcribe-url")
87
+ async def transcribe_url(
88
+ url: str = Form(...),
89
+ language: str = Form(default="auto")
90
+ ):
91
+ try:
92
+ async with httpx.AsyncClient(timeout=60) as client:
93
+ r = await client.get(url)
94
+ r.raise_for_status()
95
+ except Exception as e:
96
+ return JSONResponse({"error": f"Failed to download: {str(e)}"}, status_code=400)
97
+
98
+ ext = os.path.splitext(url.split("?")[0])[-1] or ".mp3"
99
+ with tempfile.NamedTemporaryFile(suffix=ext, delete=False) as tmp:
100
+ tmp.write(r.content)
101
+ tmp_path = tmp.name
102
+
103
+ try:
104
+ lang = language if language != "auto" else None
105
+ result = model.transcribe(tmp_path, language=lang)
106
+ return JSONResponse({
107
+ "text": result["text"].strip(),
108
+ "language": result.get("language", "unknown"),
109
+ "segments": [
110
+ {
111
+ "start": round(s["start"], 2),
112
+ "end": round(s["end"], 2),
113
+ "text": s["text"].strip()
114
+ }
115
+ for s in result.get("segments", [])
116
+ ]
117
+ })
118
+ except Exception as e:
119
+ return JSONResponse({"error": str(e)}, status_code=500)
120
+ finally:
121
+ os.unlink(tmp_path)
122
+
123
  @app.get("/health")
124
  async def health():
125
  return {"status": "ok", "model": "whisper-base"}