Quellcode durchsuchen

[voice] add support for wispper

wanggang vor 3 Jahren
Ursprung
Commit
882e6c3576

+ 2 - 2
bridge/bridge.py

@@ -10,7 +10,7 @@ class Bridge(object):
         return bot_factory.create_bot("chatGPT").reply(query, context)
         return bot_factory.create_bot("chatGPT").reply(query, context)
 
 
     def fetch_voice_to_text(self, voiceFile):
     def fetch_voice_to_text(self, voiceFile):
-        return voice_factory.create_voice("google").voiceToText(voiceFile)
+        return voice_factory.create_voice("openai").voiceToText(voiceFile)
 
 
     def fetch_text_to_voice(self, text):
     def fetch_text_to_voice(self, text):
-        return voice_factory.create_voice("google").textToVoice(text)
+        return voice_factory.create_voice("baidu").textToVoice(text)

+ 2 - 2
channel/wechat/wechat_channel.py

@@ -54,14 +54,14 @@ class WechatChannel(Channel):
     def handle_voice(self, msg):
     def handle_voice(self, msg):
         if conf().get('speech_recognition') != True :
         if conf().get('speech_recognition') != True :
             return
             return
-        logger.debug("[WX]receive voice msg: ", msg['FileName'])
+        logger.debug("[WX]receive voice msg: " + msg['FileName'])
         thread_pool.submit(self._do_handle_voice, msg)
         thread_pool.submit(self._do_handle_voice, msg)
 
 
     def _do_handle_voice(self, msg):
     def _do_handle_voice(self, msg):
         fileName = self.tmpFilePath+msg['FileName']
         fileName = self.tmpFilePath+msg['FileName']
         msg.download(fileName)
         msg.download(fileName)
         content = super().build_voice_to_text(fileName)
         content = super().build_voice_to_text(fileName)
-        self._handle_single_msg(msg, content, True)
+        self._handle_single_msg(msg, content, False)
 
 
     def handle_text(self, msg):
     def handle_text(self, msg):
         logger.debug("[WX]receive text msg: " + json.dumps(msg, ensure_ascii=False))
         logger.debug("[WX]receive text msg: " + json.dumps(msg, ensure_ascii=False))

+ 3 - 0
config-template.json

@@ -8,6 +8,9 @@
   "image_create_prefix": ["画", "看", "找"],
   "image_create_prefix": ["画", "看", "找"],
   "conversation_max_tokens": 1000,
   "conversation_max_tokens": 1000,
   "speech_recognition": false,
   "speech_recognition": false,
+  "baidu_app_id": "YOUR BAIDU APP ID",
+  "baidu_api_key": "YOUR BAIDU API KEY",
+  "baidu_secret_key": "YOUR BAIDU SERVICE KEY",
   "character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。",
   "character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。",
   "expires_in_seconds": 3600
   "expires_in_seconds": 3600
 }
 }

+ 22 - 0
voice/baidu/baidu_voice.py

@@ -0,0 +1,22 @@
+
+"""
+baidu voice service
+"""
+from aip import AipSpeech
+from voice.voice import Voice
+from config import conf
+
+class BaiduVoice(Voice):
+    APP_ID = conf().get('baidu_app_id')
+    API_KEY = conf().get('baidu_api_key')
+    SECRET_KEY = conf().get('baidu_secret_key')
+    client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
+    
+    def __init__(self):
+        pass
+
+    def voiceToText(self, voice_file):
+        pass
+
+    def textToVoice(self, text):
+        pass

+ 25 - 0
voice/openai/openai_voice.py

@@ -0,0 +1,25 @@
+
+"""
+google voice service
+"""
+import json
+import openai
+from common.log import logger
+from voice.voice import Voice
+
+
+class OpenaiVoice(Voice):
+    def __init__(self):
+        pass
+
+    def voiceToText(self, voice_file):
+        file = open(voice_file, "rb")
+        reply = openai.Audio.transcribe("whisper-1", file)
+        json_dict = json.loads(reply)
+        text = json_dict['text']
+        logger.info(
+            '[Openai] voiceToText text={} voice file name={}'.format(text, voice_file))
+        return text
+
+    def textToVoice(self, text):
+        pass

+ 6 - 3
voice/voice_factory.py

@@ -8,10 +8,13 @@ def create_voice(voice_type):
     :param voice_type: voice type code
     :param voice_type: voice type code
     :return: voice instance
     :return: voice instance
     """
     """
-    if voice_type == 'xfyun':
-        from voice.xfyun.xfyun_voice import XfyunVoice
-        return XfyunVoice()
+    if voice_type == 'baidu':
+        from voice.baidu.baidu_voice import BaiduVoice
+        return BaiduVoice()
     elif voice_type == 'google':
     elif voice_type == 'google':
         from voice.google.google_voice import GoogleVoice
         from voice.google.google_voice import GoogleVoice
         return GoogleVoice()
         return GoogleVoice()
+    elif voice_type == 'openai':
+        from voice.openai.openai_voice import OpenaiVoice
+        return OpenaiVoice()
     raise RuntimeError
     raise RuntimeError

+ 0 - 35
voice/xfyun/xfyun_voice.py

@@ -1,35 +0,0 @@
-
-"""
-科大讯飞 voice service
-"""
-
-from voice.voice import Voice
-
-# 科大讯飞语音识别
-lfasr_host = 'http://raasr.xfyun.cn/api'
-# 请求的接口名
-api_prepare = '/prepare'
-api_upload = '/upload'
-api_merge = '/merge'
-api_get_progress = '/getProgress'
-api_get_result = '/getResult'
-# 文件分片大小10M
-file_piece_sice = 10485760
-# ——————————————————转写可配置参数————————————————
-# 参数可在官网界面(https://doc.xfyun.cn/rest_api/%E8%AF%AD%E9%9F%B3%E8%BD%AC%E5%86%99.html)查看,根据需求可自行在gene_params方法里添加修改
-# 转写类型
-lfasr_type = 0
-# 是否开启分词
-has_participle = 'false'
-has_seperate = 'true'
-# 多候选词个数
-max_alternatives = 0
-# 子用户标识
-suid = ''
-
-class XfyunVoice(Voice):
-    def __init__(self):
-        pass
-
-    def voiceToText(self, voice_file):
-        pass