Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
transcriber_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-base.en")
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def voice_commands(api_key, audio):
|
| 10 |
+
if api_key == '':
|
| 11 |
+
output= gr.Textbox("*** Please provide API Key ***")
|
| 12 |
+
else:
|
| 13 |
+
try:
|
| 14 |
+
sr, y = audio
|
| 15 |
+
y = y.astype(np.float32)
|
| 16 |
+
y /= np.max(np.abs(y))
|
| 17 |
+
prompt = transcriber_pipe({"sampling_rate": sr, "raw": y})["text"]
|
| 18 |
+
client = OpenAI(api_key=api_key)
|
| 19 |
+
messages = [{"role": "user", "content": prompt}]
|
| 20 |
+
model = "gpt-3.5-turbo-1106"
|
| 21 |
+
response = client.chat.completions.create(
|
| 22 |
+
model=model,
|
| 23 |
+
messages=messages,
|
| 24 |
+
temperature=0
|
| 25 |
+
)
|
| 26 |
+
output = response.choices[0].message.content
|
| 27 |
+
except :
|
| 28 |
+
output = gr.Textbox(" *** Please check your API-Key and try again ***")
|
| 29 |
+
return output
|
| 30 |
+
|
| 31 |
+
with gr.Blocks() as demo:
|
| 32 |
+
gr.Markdown(
|
| 33 |
+
"""
|
| 34 |
+
# Voice Commands to prompt Chatgpt using Whisper.
|
| 35 |
+
# OpenAI's Whisper is used to translate voice to text, to prompt ChatGPT.
|
| 36 |
+
<img src = "https://upload.wikimedia.org/wikipedia/commons/4/4d/OpenAI_Logo.svg" width=300px>
|
| 37 |
+
""")
|
| 38 |
+
|
| 39 |
+
gr.Interface(
|
| 40 |
+
voice_commands,
|
| 41 |
+
[
|
| 42 |
+
gr.Textbox(type = 'password',label="Enter your API-Key", placeholder="API-Key", lines=1),
|
| 43 |
+
gr.Audio(sources=["microphone"])
|
| 44 |
+
|
| 45 |
+
],
|
| 46 |
+
[
|
| 47 |
+
"text"
|
| 48 |
+
]
|
| 49 |
+
)
|
| 50 |
+
demo.launch()
|