Created
October 4, 2023 03:31
-
-
Save thebwt/003f58a4454876c4e706b7d5875b6fbb to your computer and use it in GitHub Desktop.
Whisper middleware api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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