import numpy as np import gradio as gr # print("loading asr and tts success!") import soundfile import azure.cognitiveservices.speech as speechsdk import openai import os openai.api_key = os.environ.get("OPENAI_API_KEY") speech_key = os.environ.get("SPEECH_KEY") def ms_tts(text, filename): speech_config = speechsdk.SpeechConfig(subscription=speech_key, region='eastasia') audio_config = speechsdk.audio.AudioOutputConfig(filename = filename) # The language of the voice that speaks. # speech_config.speech_synthesis_voice_name='zh-CN-XiaochenNeural' speech_config.speech_synthesis_voice_name='zh-CN-XiaomengNeural' speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config) speech_synthesis_result = speech_synthesizer.speak_text_async(text).get() def ms_asr(filename): # This example requires environment variables named "SPEECH_KEY" and "SPEECH_REGION" speech_config = speechsdk.SpeechConfig(subscription=speech_key, region="eastus") speech_config.speech_recognition_language="zh-CN" audio_config = speechsdk.audio.AudioConfig(filename=filename) speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config) # print("Speak into your microphone.") speech_recognition_result = speech_recognizer.recognize_once_async().get() if speech_recognition_result.reason == speechsdk.ResultReason.RecognizedSpeech: print("Recognized: {}".format(speech_recognition_result.text)) elif speech_recognition_result.reason == speechsdk.ResultReason.NoMatch: print("No speech could be recognized: {}".format(speech_recognition_result.no_match_details)) elif speech_recognition_result.reason == speechsdk.ResultReason.Canceled: cancellation_details = speech_recognition_result.cancellation_details print("Speech Recognition canceled: {}".format(cancellation_details.reason)) if cancellation_details.reason == speechsdk.CancellationReason.Error: print("Error details: {}".format(cancellation_details.error_details)) print("Did you set the speech resource key and region values?") return speech_recognition_result.text class Conversation: def __init__(self, prompt, num_of_round): self.prompt = prompt self.num_of_round = num_of_round self.messages = [] self.messages.append({"role": "system", "content": self.prompt}) def ask(self, question): try: self.messages.append( {"role": "user", "content": question}) response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=self.messages, # temperature=0.5, temperature=0, max_tokens=2048, top_p=1, ) except Exception as e: print(e) return e message = response["choices"][0]["message"]["content"] self.messages.append({"role": "assistant", "content": message}) if len(self.messages) > self.num_of_round*2 + 1: del self.messages[1:3] return message # prompt = """Say English""" # prompt = """You are an conversation bot. You can speak English and Chinese. You were developed by the AIOT CTO organization.""" # prompt = """You are an conversation bot. You can speak English and Chinese. You were developed by the AIOT CTO organization. # 旨在帮助用户回答与金融相关的问题。 # 说明 # - 主要回答与金融相关的问题。 # - 可回答一般性问题。 # - 如果你不确定答案,你可以说“我不知道”或“我不确定" # 金融的定义如下: # - 金融是以货币本身为经营目的,通过货币融通使货币增值的经济活动,包括以银行为中心的间接投融资和以投资银行为中心的直接投融资两种形式。 # - 金融的内容概括为货币的发行与回笼,存款的吸收与付出,贷款的发放与回收,金银、外汇的买卖,有价证券的发行与转让,保险、信托、国内、国际的货币结算等。 # - 从事金融活动的机构主要有银行、信托投资公司、保险公司、证券公司、投资基金,还有信用合作社、财务公司、金融资产管理公司、邮政储蓄机构、金融租赁公司以及证券、金银、外汇交易所等。 # """ # prompt = """You are an conversation bot. You can speak English and Chinese. You were developed by the AIOT CTO organization. # 旨在帮助用户回答与知识产权相关的问题。 # 说明 # - 主要回答与知识产权相关的问题。 # - 可回答一般性问题。 # - 如果你不确定答案,你可以说“我不知道”或“我不确定" # 知识产权的定义如下: # - 知识产权是指人们就其智力劳动成果所依法享有的专有权利,通常是国家赋予创造者对其智力成果在一定时期内享有的专有权或独占权。其本质上是一种无形财产权,它的客体是智力成果或者知识产品。知识产权属于民事权利,受国家法律保护。 # - 知识产权包括: # 1. 专利 # (1)发明专利:审查严格、含金量高,发明分为产品发明(如机器、仪器、设备和用具等)和方法发明(制造方法)两大类 # (2)实用新型专利:实用新型是指对产品的形状、构造或者其结合所提出的适于实用的新的技术方案。低成本、研制周期短。 # (3)外观专利:即视觉的新事物,是企业的无形资产 # 2. 软件著作权:计算机软件著作权是指软件的开发者或者其他权利人依据有关著作权法律的规定,对于软件作品所享有的各项专有权利。分为软件著作权个人登记和企业登记,如果企业要申报双软认证或高新技术企业认定,需保证著作权是企业登记的状态。 # 3. 作品著作权(版权):指文学、艺术、科学作品的作者对其作品享有的权利(包括财产权、人身权)。版权是知识产权的一种类型,它是由自然科学、社会科学以及文学、音乐、戏剧、绘画、雕塑、摄影、图片和电影摄影等方面的作品组成。 # 4. 集成电路布图设计:指集成电路中至少有一个是有源元件的两个以上元件和部分或者全部互连线路的三维配置,或者为制造集成电路而准备的上述三维配置。集成电路布图设计实质上是一种图形设计,但它并非是工业品外观设计,不能适用专利法保护。从专利的的取得程序,专利申请审批的时间过长,成本较高,不利于技术的推广和应用。 # 5. 商标:商标是用来区别一个经营者的品牌或服务和其他经营者的商品或服务的标记。我国商标法规定,经商标局核准注册的商标,包括商品商标、服务商标和集体商标、证明商标,商标注册人享有商标专用权,受法律保护,如果是驰名商标,将会获得跨类别的商标专用权法律保护。 # """ prompt = """You are an educational conversation bot. Your name is Xiaomeng. You can speak English and Chinese. You were developed by the AIOT CTO organization. 你是一个教育对话机器人。你叫小萌。你可以说英文和中文。你是由智能物联首席技术官组织开发的。 你的任务包括: - 英语口语对话 - 知识问答 - 讲故事 知识问答任务要求: - 回答必须保证是正确的 - 回答需要采用逐步推理的方式 - 如果你不确定答案,你可以说“我不知道”或“我不确定" 合规要求: - 用户为年龄大于3岁且小于15岁的儿童 - 知识问答任务中回答的内容和形式必须适合年龄大于3岁且小于15岁的儿童 - 英语口语对话任务中回答的内容和形式必须适合大于3岁且小于15岁的儿童 - 讲故事任务中回答的内容和形式必须适合年龄大于3岁且小于15岁的儿童 - 全部对话的内容和形式必须适合年龄大于3岁且小于15岁的儿童 - 需要对全部对话进行分析,当检测到用户不是年龄大于3岁且小于15岁的儿童时,回答”我猜和我对话的一定是一位机灵可爱的小朋友^_^ I guess I'm talking to a clever and cute kid ^_^” 语言要求: - 只支持用中文或英文回答 - 当用户用中文提问时,用中文回答 - 当用户用英文提问时,用英文回答 - 当用户用中文和英文混合语言提问时,可以用中文回答,也可以用英文回答,还可以用中文和英文混合语言回答 - 当用户用其它语言提问时,回答“抱歉,我只说中文或英文!Sorry, I only speak Chinese or English!” - 当用户要求用中文回答时,用中文回答 - 当用户要求用英文回答时,用英文回答 - 当用户要求回答的语言既不是中文也不是英文时,回答“抱歉,我只说中文或英文!Sorry, I only speak Chinese or English!” 回答长度要求: - 知识问答任务中回答长度小于50字 - 英语口语对话任务中回答长度小于20字 - 讲故事任务中回答长度小于100字 """ conv = Conversation(prompt, 20) def predict(input, history=[]): history.append(input) response = conv.ask(input) history.append(response) responses = [(u,b) for u,b in zip(history[::2], history[1::2])] return response, responses, history def main(audio, history=[]): s,y = audio print(s) assert s in [48000, 16000] if s == 48000: # Optional resample to 16000 y = (y / max(np.max(y), 1) * 32767)[::3].astype("int16") soundfile.write("./input.wav",y,16000) # wav_res = ms_asr("./input.wav") wav_res = "hello!" print("You said : ", wav_res) # res = requests.post(url='http://10.10.239.164:8088', # headers={"Content-Type": "application/json"}, # json={"prompt":wav_res,"history": historyList.hlist}) answer, his_list, history = predict(wav_res, history) print("answer: ", answer) # print("history: ", history) # if len(history)>=10: # historyList.hlist = history[1:] # else: # historyList.hlist = history # ms_tts(text=answer, filename="./output.wav") path1="./output.wav" print("historyList.hlist: ", historyList.hlist) return his_list, history, path1 with gr.Blocks() as demo: state = gr.State([]) with gr.Row(): with gr.Column(scale=4): txt = gr.Chatbot(label="ChatBox") out_voice = gr.Audio(label="audio") with gr.Column(scale=4): mic = gr.Mic(label="input") button = gr.Button("Generate") button.click(main, [mic, state], [txt, state, out_voice]) # demo.queue().launch(server_name = "0.0.0.0", server_port=8080) demo.queue().launch()