Skip to content

Instantly share code, notes, and snippets.

@thebwt
Created October 4, 2023 03:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thebwt/003f58a4454876c4e706b7d5875b6fbb to your computer and use it in GitHub Desktop.
Save thebwt/003f58a4454876c4e706b7d5875b6fbb to your computer and use it in GitHub Desktop.
Whisper middleware api
from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
import requests
import openai
import tempfile
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["https://foundry.thebwt.com"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post("/transcribe/")
async def transcribe_audio(audio: UploadFile = File(...)):
audio_data = await audio.read()
# Create a temporary file and write the audio data to it
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_file:
temp_file.write(audio_data)
temp_file.seek(0) # Important: reset file pointer to the beginning
transcript = openai.Audio.transcribe("whisper-1", temp_file)
print(transcript)
return transcript
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment