Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,10 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import requests
|
| 3 |
import tempfile
|
|
|
|
| 4 |
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 5 |
from fastapi.responses import JSONResponse
|
| 6 |
|
| 7 |
-
HF_SPACE_URL = "https://rafaaa2105-crisper-whisper.hf.space/run/predict"
|
| 8 |
-
|
| 9 |
app = FastAPI()
|
|
|
|
| 10 |
|
| 11 |
def parse_transcript(text: str):
|
| 12 |
lines = text.strip().split("\n")
|
|
@@ -27,21 +25,20 @@ def parse_transcript(text: str):
|
|
| 27 |
return chunks
|
| 28 |
|
| 29 |
@app.post("/speech2text")
|
| 30 |
-
async def
|
| 31 |
if not file.filename.endswith((".mp3", ".wav")):
|
| 32 |
-
raise HTTPException(status_code=400, detail="Only .mp3 or .wav files are supported")
|
|
|
|
| 33 |
try:
|
| 34 |
with tempfile.NamedTemporaryFile(delete=False, suffix=file.filename[-4:]) as tmp:
|
| 35 |
tmp.write(await file.read())
|
| 36 |
tmp_path = tmp.name
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
"data": [None, "transcribe"]
|
| 41 |
-
}, files={"file": files["file"]})
|
| 42 |
|
| 43 |
if response.status_code != 200:
|
| 44 |
-
raise HTTPException(status_code=500, detail="Failed to get response from
|
| 45 |
|
| 46 |
output_text = response.json().get("data", [""])[0]
|
| 47 |
chunks = parse_transcript(output_text)
|
|
|
|
|
|
|
|
|
|
| 1 |
import tempfile
|
| 2 |
+
import requests
|
| 3 |
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 4 |
from fastapi.responses import JSONResponse
|
| 5 |
|
|
|
|
|
|
|
| 6 |
app = FastAPI()
|
| 7 |
+
CRISPER_SPACE_API = "https://rafaaa2105-crisper-whisper.hf.space/run/predict_1"
|
| 8 |
|
| 9 |
def parse_transcript(text: str):
|
| 10 |
lines = text.strip().split("\n")
|
|
|
|
| 25 |
return chunks
|
| 26 |
|
| 27 |
@app.post("/speech2text")
|
| 28 |
+
async def speech2text(file: UploadFile = File(...)):
|
| 29 |
if not file.filename.endswith((".mp3", ".wav")):
|
| 30 |
+
raise HTTPException(status_code=400, detail="Only .mp3 or .wav files are supported.")
|
| 31 |
+
|
| 32 |
try:
|
| 33 |
with tempfile.NamedTemporaryFile(delete=False, suffix=file.filename[-4:]) as tmp:
|
| 34 |
tmp.write(await file.read())
|
| 35 |
tmp_path = tmp.name
|
| 36 |
|
| 37 |
+
with open(tmp_path, "rb") as f:
|
| 38 |
+
response = requests.post(CRISPER_SPACE_API, files={"data": f}, json={"data": [None, "transcribe"]})
|
|
|
|
|
|
|
| 39 |
|
| 40 |
if response.status_code != 200:
|
| 41 |
+
raise HTTPException(status_code=500, detail="Failed to get response from upstream Space")
|
| 42 |
|
| 43 |
output_text = response.json().get("data", [""])[0]
|
| 44 |
chunks = parse_transcript(output_text)
|