Skip to content

Instantly share code, notes, and snippets.

@masafumimori
Last active November 13, 2023 16:28
Show Gist options
  • Save masafumimori/a44d1fdb7ab823baef4e2dfe48745d82 to your computer and use it in GitHub Desktop.
Save masafumimori/a44d1fdb7ab823baef4e2dfe48745d82 to your computer and use it in GitHub Desktop.
This is a simple implementation of transcription with OpenAI Whisper API and Gradio.
import gradio as gr
from openai import OpenAI
import os
client = OpenAI(
api_key=os.getenv("OPENAI_API_KEY"),
)
DEFAULT_PROMPT = (
"Hello, welcome to my lecture." # To make sure transcription has punctuation
)
def transcribe(audio_file, prompt):
prompt = prompt if prompt else DEFAULT_PROMPT
try:
with open(audio_file, "rb") as file:
response = client.audio.transcriptions.create(
file=file,
model="whisper-1",
prompt=prompt,
temperature=0.2,
language="en", # ja for Japanese
response_format="text",
)
return response
except Exception as e:
return f"An error occurred: {str(e)}"
# Gradio interface
iface = gr.Interface(
fn=transcribe,
inputs=[
gr.Audio(sources=["upload"], type="filepath"),
gr.Textbox(label="Whisper Model Prompt"),
],
outputs="text",
)
# Run the Gradio app
iface.launch(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment