Kevin676 commited on
Commit
6b8a2b8
1 Parent(s): 439afdd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -0
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from TTS.api import TTS
2
+ tts = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts", progress_bar=False, gpu=True)
3
+ from scipy.io import wavfile
4
+ import noisereduce as nr
5
+ import whisper
6
+ model = whisper.load_model("small")
7
+ import gradio as gr
8
+ import openai
9
+
10
+ mes1 = [
11
+ {"role": "system", "content": "You are a TOEFL examiner. Help me improve my oral Englsih and give me feedback. Replace the Arabic numerals with the corresponding English words in your response."}
12
+ ]
13
+
14
+ mes2 = [
15
+ {"role": "system", "content": "You are a mental health therapist. Your name is Tina. Replace the Arabic numerals with the corresponding English words in your response."}
16
+ ]
17
+
18
+ mes3 = [
19
+ {"role": "system", "content": "You are my personal assistant. Your name is Alice. Replace the Arabic numerals with the corresponding English words in your response."}
20
+ ]
21
+
22
+ res = []
23
+
24
+ def transcribe(apikey, upload, audio, choice1):
25
+
26
+ openai.api_key = apikey
27
+
28
+ # time.sleep(3)
29
+ # load audio and pad/trim it to fit 30 seconds
30
+ audio = whisper.load_audio(audio)
31
+ audio = whisper.pad_or_trim(audio)
32
+
33
+ # make log-Mel spectrogram and move to the same device as the model
34
+ mel = whisper.log_mel_spectrogram(audio).to(model.device)
35
+
36
+ # detect the spoken language
37
+ _, probs = model.detect_language(mel)
38
+ print(f"Detected language: {max(probs, key=probs.get)}")
39
+
40
+ # decode the audio
41
+ options = whisper.DecodingOptions()
42
+ result = whisper.decode(model, mel, options)
43
+ res.append(result.text)
44
+
45
+ if choice1 == "TOEFL":
46
+ messages = mes1
47
+ elif choice1 == "Therapist":
48
+ messages = mes2
49
+ elif choice1 == "Alice":
50
+ messages = mes3
51
+
52
+ # chatgpt
53
+ n = len(res)
54
+ content = res[n-1]
55
+ messages.append({"role": "user", "content": content})
56
+
57
+ completion = openai.ChatCompletion.create(
58
+ model = "gpt-3.5-turbo",
59
+ messages = messages
60
+ )
61
+
62
+ chat_response = completion.choices[0].message.content
63
+
64
+ messages.append({"role": "assistant", "content": chat_response})
65
+
66
+ tts.tts_to_file(chat_response, speaker_wav = upload, language="en", file_path="output.wav")
67
+
68
+ audio_in = "output.wav"
69
+
70
+ rate, data = wavfile.read(audio_in)
71
+
72
+ reduced_noise = nr.reduce_noise(y=data, sr=rate, prop_decrease= 0.85, stationary=True)
73
+ #reduced_noise = nr.reduce_noise(y = data, sr=rate, prop_decrease= 0.85)
74
+ #reduced_noise = nr.reduce_noise(y = data, sr=rate, thresh_n_mult_nonstationary=2, stationary=False)
75
+
76
+ wavfile.write("audio1.wav", rate, reduced_noise)
77
+
78
+ return [result.text, chat_response, "audio1.wav"]
79
+
80
+ output_1 = gr.Textbox(label="Speech to Text")
81
+ output_2 = gr.Textbox(label="ChatGPT Output")
82
+ output_3 = gr.Audio(label="Audio")
83
+
84
+ gr.Interface(
85
+ title = '🥳💬💕 - TalktoAI,随时随地,谈天说地!',
86
+ theme="huggingface",
87
+ description = "🤖 - 让有人文关怀的AI造福每一个人!AI向善,文明璀璨!TalktoAI,Enable the future!",
88
+ fn=transcribe,
89
+ inputs=[
90
+ gr.Textbox(lines=1, label = "请填写您的OpenAI_API_key"),
91
+ gr.inputs.Audio(source="upload", label = "请上传您喜欢的声音", type="filepath"),
92
+ gr.inputs.Audio(source="microphone", type="filepath"),
93
+ gr.Radio(["TOEFL", "Therapist", "Alice"], label="TOEFL Examiner, Therapist Tina, or Assistant Alice?"),
94
+ ],
95
+ outputs=[
96
+ output_1, output_2, output_3
97
+ ],
98
+ ).launch()