transformer.js / api.py

This commit is contained in:
fred 2024-02-22 12:48:26 +01:00
parent 8fc9f46aac
commit 5df64847ea
2 changed files with 31 additions and 0 deletions

View File

@ -0,0 +1,30 @@
#!/bin/python3
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import JSONResponse
import whisper
app = FastAPI()
whisper = whisper.load_model("medium.en")
@app.post("/speechToText")
async def speech_to_text(file: UploadFile = File(...)):
try:
# Save the uploaded audio file locally
audio_path = "temp_audio.wav"
with open(audio_path, "wb") as audio_file:
audio_file.write(file.file.read())
# Use whisper to convert speech to text
text = whisper.transcribe(audio_path,language="en")['text']
# Return the transcribed text
return text
except Exception as e:
return JSONResponse(content={"error": str(e)}, status_code=500)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=9000)

View File

@ -28,4 +28,5 @@ mv ./rooms/young_hacker.sh ./rooms/start.sh
<3<3<3
https://github.com/xenova/whisper-web