Spaces:
Runtime error
Runtime error
Duplicate from hwberry2/WhisperDemo
Browse filesCo-authored-by: Bill Berry <hwberry2@users.noreply.huggingface.co>
- .gitattributes +34 -0
- README.md +13 -0
- app.py +115 -0
- requirements.txt +2 -0
.gitattributes
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: WhisperDemo
|
| 3 |
+
emoji: 🌖
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: gray
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 3.20.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
duplicated_from: hwberry2/WhisperDemo
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import openai
|
| 4 |
+
from gtts import gTTS # Google Text To Speech
|
| 5 |
+
|
| 6 |
+
# load the api key
|
| 7 |
+
openai.api_key = os.environ["OPEN_AI_KEY"]
|
| 8 |
+
|
| 9 |
+
# takes an audio file from the microphone
|
| 10 |
+
# submits the raw audio to OpenAI for
|
| 11 |
+
# Speech to Text Translation
|
| 12 |
+
# input from Microphone Component
|
| 13 |
+
# output to User Input - Textbox Component
|
| 14 |
+
def transcribe(audio):
|
| 15 |
+
audio_file = open(audio, "rb")
|
| 16 |
+
# Call the transcribe method with the file-like object
|
| 17 |
+
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
| 18 |
+
|
| 19 |
+
return transcript["text"]
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# Create a Gradio App using Blocks
|
| 24 |
+
with gr.Blocks() as demo:
|
| 25 |
+
gr.Markdown(
|
| 26 |
+
"""
|
| 27 |
+
# Welcome to the Virtual Therapist Chat Bot!
|
| 28 |
+
"""
|
| 29 |
+
)
|
| 30 |
+
with gr.Accordion("Click for Instructions:"):
|
| 31 |
+
gr.Markdown(
|
| 32 |
+
"""
|
| 33 |
+
* Tell the therapist your problems, by recording your query.
|
| 34 |
+
* Submit your query, and follow the chat or listen to the Therapists advice.
|
| 35 |
+
* When you are ready to respond, clear your last recording and resubmit.
|
| 36 |
+
note: Transcribe Audio does not work on iOS
|
| 37 |
+
""")
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# First message as instructions to OpenAI
|
| 41 |
+
# Establishes a State object to create a
|
| 42 |
+
# unique state for each user and on reload
|
| 43 |
+
messages = gr.State(value=[{"role": "system", "content": "You are a therapist. Respond in less than 5 sentences."}])
|
| 44 |
+
|
| 45 |
+
# Takes the users transcribed audio as a string
|
| 46 |
+
# Takes the messages list as a reference
|
| 47 |
+
# Sends the ongoing chat log to OpenAI
|
| 48 |
+
# input from User Input - Textbox Component
|
| 49 |
+
# output to Chat Log - Textbox Component
|
| 50 |
+
def botResponse(user_input, messages):
|
| 51 |
+
# adds the user input to the ongoing chat log
|
| 52 |
+
# and submits the log to OpenAI
|
| 53 |
+
messages.append({"role": "user", "content": user_input})
|
| 54 |
+
response = openai.ChatCompletion.create(
|
| 55 |
+
model="gpt-3.5-turbo-0301",
|
| 56 |
+
messages=messages
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# Parse the response from OpenAI and store
|
| 60 |
+
# it in the chat log
|
| 61 |
+
system_message = response["choices"][0]["message"]["content"]
|
| 62 |
+
messages.append({"role": "assistant", "content": system_message})
|
| 63 |
+
|
| 64 |
+
# Process the messages list to get the
|
| 65 |
+
# chat log into a string. Exclude the
|
| 66 |
+
# System responses from the string
|
| 67 |
+
chat_transcript = ""
|
| 68 |
+
for message in messages:
|
| 69 |
+
if (message["role"] != "system"):
|
| 70 |
+
chat_transcript += message["role"] + ": " + message["content"] + "\n\n"
|
| 71 |
+
|
| 72 |
+
return chat_transcript
|
| 73 |
+
|
| 74 |
+
# Gets the last message in the
|
| 75 |
+
# chat log and uses GTTS to
|
| 76 |
+
# convert the last response into
|
| 77 |
+
# an audio file. Returns a path to
|
| 78 |
+
# the converted text as an mp3 file
|
| 79 |
+
# input from messages as a reference
|
| 80 |
+
# output to GPT Voice - Audio Component
|
| 81 |
+
def giveVoice(messages):
|
| 82 |
+
bot_message=messages[-1]
|
| 83 |
+
|
| 84 |
+
myobj = gTTS(text=bot_message["content"])
|
| 85 |
+
myobj.save("temp.mp3")
|
| 86 |
+
|
| 87 |
+
dir = os.getcwd()
|
| 88 |
+
new_path = os.path.join(dir, "temp.mp3")
|
| 89 |
+
|
| 90 |
+
return new_path
|
| 91 |
+
|
| 92 |
+
# Creates the Gradio interface objects
|
| 93 |
+
# The submit button triggers a cascade of
|
| 94 |
+
# events that each engage a different
|
| 95 |
+
# component as input/output
|
| 96 |
+
with gr.Row():
|
| 97 |
+
with gr.Column(scale=1):
|
| 98 |
+
user_audio = gr.Audio(source="microphone", type="filepath", label="Input Phrase")
|
| 99 |
+
submit_btn = gr.Button(value="Transcribe Audio")
|
| 100 |
+
submit_btn2 = gr.Button(value="Submit Text")
|
| 101 |
+
gpt_voice = gr.Audio(label="Therapists Advice")
|
| 102 |
+
with gr.Column(scale=2):
|
| 103 |
+
user_transcript = gr.Text(label="Audio Translation", interactive=False)
|
| 104 |
+
user_text = gr.Text(label="Text Input")
|
| 105 |
+
gpt_transcript = gr.Text(label="Chat Transcript")
|
| 106 |
+
submit_btn.click(transcribe, user_audio, user_transcript)
|
| 107 |
+
submit_btn2.click(botResponse, [user_text, messages], gpt_transcript)
|
| 108 |
+
user_transcript.change(botResponse, [user_transcript, messages], gpt_transcript)
|
| 109 |
+
gpt_transcript.change(giveVoice, messages, gpt_voice)
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
# creates a local web server
|
| 113 |
+
# if share=True creates a public
|
| 114 |
+
# demo on huggingface.co
|
| 115 |
+
demo.launch(share=False)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai
|
| 2 |
+
gtts
|