chibs abidlabs HF Staff commited on
Commit
a42a13f
·
0 Parent(s):

Duplicate from abidlabs/audiotest4

Browse files

Co-authored-by: Abubakar Abid <[email protected]>

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