Update app.py
Browse files
app.py
CHANGED
|
@@ -4,17 +4,32 @@ import openai
|
|
| 4 |
import whisper
|
| 5 |
from gtts import gTTS
|
| 6 |
from IPython.display import Audio
|
|
|
|
| 7 |
model = whisper.load_model("base")
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
res = []
|
| 12 |
|
| 13 |
question = []
|
| 14 |
|
| 15 |
-
def transcribe(audio):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
#time.sleep(3)
|
| 18 |
# load audio and pad/trim it to fit 30 seconds
|
| 19 |
audio = whisper.load_audio(audio)
|
| 20 |
audio = whisper.pad_or_trim(audio)
|
|
@@ -31,6 +46,13 @@ def transcribe(audio):
|
|
| 31 |
result = whisper.decode(model, mel, options)
|
| 32 |
res.append(result.text)
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# chatgpt
|
| 35 |
n = len(res)
|
| 36 |
content = res[n-1]
|
|
@@ -43,10 +65,12 @@ def transcribe(audio):
|
|
| 43 |
|
| 44 |
chat_response = completion.choices[0].message.content
|
| 45 |
|
| 46 |
-
messages.append({"role": "assistant", "content": chat_response})
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
|
| 51 |
tts.save('1.wav')
|
| 52 |
sound_file = '1.wav'
|
|
@@ -61,11 +85,15 @@ output_3 = gr.Audio(label="Audio")
|
|
| 61 |
|
| 62 |
gr.Interface(
|
| 63 |
title = 'TalktoAI,随时随地,谈天说地!',
|
|
|
|
| 64 |
fn=transcribe,
|
| 65 |
inputs=[
|
| 66 |
-
gr.
|
|
|
|
|
|
|
|
|
|
| 67 |
],
|
| 68 |
outputs=[
|
| 69 |
output_1, output_2, output_3
|
| 70 |
],
|
| 71 |
-
|
|
|
|
| 4 |
import whisper
|
| 5 |
from gtts import gTTS
|
| 6 |
from IPython.display import Audio
|
| 7 |
+
|
| 8 |
model = whisper.load_model("base")
|
| 9 |
|
| 10 |
+
mes1 = [
|
| 11 |
+
{"role": "system", "content": "You are a TOEFL examiner who will ask me questions in a TOEFL test."}
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
mes2 = [
|
| 15 |
+
{"role": "system", "content": "You are a mental health therapist. Your name is Tina."}
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
mes3 = [
|
| 19 |
+
{"role": "system", "content": "You are my personal assistant. Your name is Alice."}
|
| 20 |
+
]
|
| 21 |
|
| 22 |
res = []
|
| 23 |
|
| 24 |
question = []
|
| 25 |
|
| 26 |
+
def transcribe(apikey, audio, choice1, choice2):
|
| 27 |
+
|
| 28 |
+
#global messages
|
| 29 |
+
|
| 30 |
+
openai.api_key = apikey
|
| 31 |
|
| 32 |
+
# time.sleep(3)
|
| 33 |
# load audio and pad/trim it to fit 30 seconds
|
| 34 |
audio = whisper.load_audio(audio)
|
| 35 |
audio = whisper.pad_or_trim(audio)
|
|
|
|
| 46 |
result = whisper.decode(model, mel, options)
|
| 47 |
res.append(result.text)
|
| 48 |
|
| 49 |
+
if choice1 == "TOEFL":
|
| 50 |
+
messages = mes1
|
| 51 |
+
elif choice1 == "Therapist":
|
| 52 |
+
messages = mes2
|
| 53 |
+
else:
|
| 54 |
+
messages = mes3
|
| 55 |
+
|
| 56 |
# chatgpt
|
| 57 |
n = len(res)
|
| 58 |
content = res[n-1]
|
|
|
|
| 65 |
|
| 66 |
chat_response = completion.choices[0].message.content
|
| 67 |
|
| 68 |
+
messages.append({"role": "assistant", "content": chat_response})
|
| 69 |
|
| 70 |
+
if choice2 == "Chinese":
|
| 71 |
+
tts = gTTS(chat_response, lang='zh-CN')
|
| 72 |
+
elif choice2 == "English":
|
| 73 |
+
tts = gTTS(chat_response, lang='en', tld='us')
|
| 74 |
|
| 75 |
tts.save('1.wav')
|
| 76 |
sound_file = '1.wav'
|
|
|
|
| 85 |
|
| 86 |
gr.Interface(
|
| 87 |
title = 'TalktoAI,随时随地,谈天说地!',
|
| 88 |
+
description = "让具有人文关怀的AI造福每一个人!科技向善,AI璀璨!",
|
| 89 |
fn=transcribe,
|
| 90 |
inputs=[
|
| 91 |
+
gr.Textbox(lines=1, label = "请填写您的OpenAI_API_key"),
|
| 92 |
+
gr.inputs.Audio(source="microphone", type="filepath"),
|
| 93 |
+
gr.Radio(["TOEFL", "Therapist", "Alice"], label="TOEFL Examiner, Therapist Tina, or Assistant Alice?"),
|
| 94 |
+
gr.Radio(["Chinese", "English"], label="Chinese or English?")
|
| 95 |
],
|
| 96 |
outputs=[
|
| 97 |
output_1, output_2, output_3
|
| 98 |
],
|
| 99 |
+
).launch()
|