-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
55 lines (40 loc) · 1.67 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import os
import sys
import gradio as gr
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
openai_key = os.getenv("OPENAI_KEY")
if openai_key == "<YOUR_OPENAI_KEY>":
openai_key = ""
if openai_key == "":
sys.exit("Please Provide Your OpenAI API Key")
def transcript(audio, model, response_type):
try:
client = OpenAI(api_key=openai_key)
print(audio)
audio_file = open(audio, "rb")
transcriptions = client.audio.transcriptions.create(
model=model,
file=audio_file,
response_format=response_type
)
except Exception as error:
print(str(error))
raise gr.Error("An error occurred while generating speech. Please check your API key and come back try again.")
return transcriptions
def upload_file(files):
print(files)
with gr.Blocks() as demo:
gr.Markdown("# <center> OpenAI Speed To Text API with Gradio </center>")
with gr.Row(variant="panel"):
model = gr.Dropdown(choices=["whisper-1"], label="Model", value="whisper-1")
response_type = gr.Dropdown(choices=["json", "text", "srt", "verbose_json", "vtt"], label="Response Type",
value="text")
with gr.Row():
audio = gr.Audio(sources=["microphone"], type="filepath", streaming=True)
file = gr.UploadButton(file_types=[".mp3", ".wav"], label="Select File", type="filepath")
output_text = gr.Text(label="Output Text")
audio.stop_recording(fn=transcript, inputs=[audio, model, response_type], outputs=output_text, api_name=False)
file.upload(fn=transcript, inputs=[file, model, response_type], outputs=output_text)
demo.launch()