aiotedu commited on
Commit
1281f9c
·
0 Parent(s):

Duplicate from aiotedu/aiotedu

Browse files
Files changed (4) hide show
  1. .gitattributes +34 -0
  2. README.md +13 -0
  3. app.py +130 -0
  4. requirements.txt +3 -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: Aiotedu
3
+ emoji: 🐠
4
+ colorFrom: green
5
+ colorTo: indigo
6
+ sdk: gradio
7
+ sdk_version: 3.27.0
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: aiotedu/aiotedu
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import gradio as gr
3
+ # print("loading asr and tts success!")
4
+ import soundfile
5
+ import azure.cognitiveservices.speech as speechsdk
6
+ import openai
7
+ import os
8
+ openai.api_key = os.environ.get("OPENAI_API_KEY")
9
+ speech_key = os.environ.get("SPEECH_KEY")
10
+
11
+ def ms_tts(text, filename):
12
+ speech_config = speechsdk.SpeechConfig(subscription=speech_key, region='eastus')
13
+ audio_config = speechsdk.audio.AudioOutputConfig(filename = filename)
14
+
15
+ # The language of the voice that speaks.
16
+ speech_config.speech_synthesis_voice_name='zh-CN-XiaochenNeural'
17
+
18
+ speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)
19
+
20
+ speech_synthesis_result = speech_synthesizer.speak_text_async(text).get()
21
+
22
+ def ms_asr(filename):
23
+ # This example requires environment variables named "SPEECH_KEY" and "SPEECH_REGION"
24
+ speech_config = speechsdk.SpeechConfig(subscription=speech_key, region="eastus")
25
+ speech_config.speech_recognition_language="zh-CN"
26
+
27
+ audio_config = speechsdk.audio.AudioConfig(filename=filename)
28
+ speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
29
+
30
+ # print("Speak into your microphone.")
31
+ speech_recognition_result = speech_recognizer.recognize_once_async().get()
32
+
33
+ if speech_recognition_result.reason == speechsdk.ResultReason.RecognizedSpeech:
34
+ print("Recognized: {}".format(speech_recognition_result.text))
35
+ elif speech_recognition_result.reason == speechsdk.ResultReason.NoMatch:
36
+ print("No speech could be recognized: {}".format(speech_recognition_result.no_match_details))
37
+ elif speech_recognition_result.reason == speechsdk.ResultReason.Canceled:
38
+ cancellation_details = speech_recognition_result.cancellation_details
39
+ print("Speech Recognition canceled: {}".format(cancellation_details.reason))
40
+ if cancellation_details.reason == speechsdk.CancellationReason.Error:
41
+ print("Error details: {}".format(cancellation_details.error_details))
42
+ print("Did you set the speech resource key and region values?")
43
+
44
+ return speech_recognition_result.text
45
+
46
+ class Conversation:
47
+ def __init__(self, prompt, num_of_round):
48
+ self.prompt = prompt
49
+ self.num_of_round = num_of_round
50
+ self.messages = []
51
+ self.messages.append({"role": "system", "content": self.prompt})
52
+
53
+ def ask(self, question):
54
+ try:
55
+ self.messages.append( {"role": "user", "content": question})
56
+ response = openai.ChatCompletion.create(
57
+ model="gpt-3.5-turbo",
58
+ messages=self.messages,
59
+ temperature=0.5,
60
+ max_tokens=2048,
61
+ top_p=1,
62
+ )
63
+ except Exception as e:
64
+ print(e)
65
+ return e
66
+
67
+ message = response["choices"][0]["message"]["content"]
68
+ self.messages.append({"role": "assistant", "content": message})
69
+
70
+ if len(self.messages) > self.num_of_round*2 + 1:
71
+ del self.messages[1:3]
72
+ return message
73
+
74
+
75
+ # prompt = """Say English"""
76
+ prompt = """You are an conversation bot. You can speak English and Chinese. You were developed by the BOE AIOT CTO organization."""
77
+
78
+ conv = Conversation(prompt, 5)
79
+
80
+ def predict(input, history=[]):
81
+ history.append(input)
82
+ response = conv.ask(input)
83
+ history.append(response)
84
+ responses = [(u,b) for u,b in zip(history[::2], history[1::2])]
85
+ return response, responses, history
86
+
87
+ def main(audio, history=[]):
88
+
89
+ s,y = audio
90
+
91
+ print(s)
92
+ assert s in [48000, 16000]
93
+ if s == 48000: # Optional resample to 16000
94
+ y = (y / max(np.max(y), 1) * 32767)[::3].astype("int16")
95
+ soundfile.write("./input.wav",y,16000)
96
+
97
+ wav_res = ms_asr("./input.wav")
98
+ print("You said : ", wav_res)
99
+ # res = requests.post(url='http://10.10.239.164:8088',
100
+ # headers={"Content-Type": "application/json"},
101
+ # json={"prompt":wav_res,"history": historyList.hlist})
102
+
103
+ answer, his_list, history = predict(wav_res, history)
104
+
105
+ # print("answer: ", answer)
106
+ # print("history: ", history)
107
+ # if len(history)>=10:
108
+ # historyList.hlist = history[1:]
109
+ # else:
110
+ # historyList.hlist = history
111
+
112
+
113
+ ms_tts(text=answer, filename="./output.wav")
114
+ path1="./output.wav"
115
+ # print("historyList.hlist: ", historyList.hlist)
116
+ return his_list, history, path1
117
+
118
+
119
+ with gr.Blocks() as demo:
120
+ state = gr.State([])
121
+ with gr.Row():
122
+ with gr.Column(scale=4):
123
+ txt = gr.Chatbot(label="ChatBox")
124
+ out_voice = gr.Audio(label="audio")
125
+ with gr.Column(scale=4):
126
+ mic = gr.Mic(label="input")
127
+ button = gr.Button("Generate")
128
+ button.click(main, [mic, state], [txt, state, out_voice])
129
+ # demo.queue().launch(server_name = "0.0.0.0", server_port=8080)
130
+ demo.queue().launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ openai
2
+ soundfile
3
+ azure-cognitiveservices-speech