add google application credentials

This commit is contained in:
wugren
2023-09-22 13:38:44 +08:00
parent b8345eccaf
commit b195e6a6a4
2 changed files with 39 additions and 14 deletions
+19 -2
View File
@@ -3,9 +3,11 @@ import os
import asyncio import asyncio
from asyncio import Queue from asyncio import Queue
import logging import logging
from typing import Optional
from google.cloud import texttospeech from google.cloud import texttospeech
from .storage import AIStorage
from .compute_task import ComputeTask, ComputeTaskResult, ComputeTaskState, ComputeTaskType from .compute_task import ComputeTask, ComputeTaskResult, ComputeTaskState, ComputeTaskType
from .compute_node import ComputeNode from .compute_node import ComputeNode
@@ -31,8 +33,7 @@ class GoogleTextToSpeechNode(ComputeNode):
super().__init__() super().__init__()
self.node_id = "google_text_to_speech_node" self.node_id = "google_text_to_speech_node"
self.task_queue = Queue() self.task_queue = Queue()
self.client: Optional[texttospeech.TextToSpeechClient] = None
self.client = texttospeech.TextToSpeechClient()
self.language_list = { self.language_list = {
"cnm-CN": { "cnm-CN": {
@@ -86,6 +87,14 @@ class GoogleTextToSpeechNode(ComputeNode):
} }
self.start() self.start()
def init(self):
user_config = AIStorage.get_instance().get_user_config()
google_application_credentials = user_config.get_value("google_application_credentials")
if google_application_credentials is None:
raise Exception("google_application_credentials is None!")
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = google_application_credentials
self.client = texttospeech.TextToSpeechClient()
def start(self): def start(self):
async def _run_task_loop(): async def _run_task_loop():
while True: while True:
@@ -161,3 +170,11 @@ class GoogleTextToSpeechNode(ComputeNode):
def is_local(self) -> bool: def is_local(self) -> bool:
return False return False
def declare_user_config(self):
if os.getenv("GOOGLE_APPLICATION_CREDENTIALS") is None:
user_config = AIStorage.get_instance().get_user_config()
user_config.add_user_config("google_application_credentials",
"google application credentials, please visit:https://cloud.google.com/text-to-speech/docs/before-you-begin",
False,
None)
+20 -12
View File
@@ -59,6 +59,9 @@ class AIOS_Shell:
user_config.add_user_config("shell.current","last opened target and topic",True,"default@Jarvis") user_config.add_user_config("shell.current","last opened target and topic",True,"default@Jarvis")
proxy.declare_user_config() proxy.declare_user_config()
google_text_to_speech = GoogleTextToSpeechNode.get_instance()
google_text_to_speech.declare_user_config()
async def _handle_no_target_msg(self,bus:AIBus,msg:AgentMsg) -> bool: async def _handle_no_target_msg(self,bus:AIBus,msg:AgentMsg) -> bool:
target_id = msg.target.split(".")[0] target_id = msg.target.split(".")[0]
@@ -97,8 +100,13 @@ class AIOS_Shell:
return False return False
ComputeKernel.get_instance().add_compute_node(open_ai_node) ComputeKernel.get_instance().add_compute_node(open_ai_node)
google_text_to_speech_node = GoogleTextToSpeechNode.get_instance() try:
ComputeKernel.get_instance().add_compute_node(google_text_to_speech_node) google_text_to_speech_node = GoogleTextToSpeechNode.get_instance()
google_text_to_speech_node.init()
ComputeKernel.get_instance().add_compute_node(google_text_to_speech_node)
except Exception as e:
logger.error(f"google text to speech node initial failed! {e}")
return False
llama_ai_node = LocalLlama_ComputeNode() llama_ai_node = LocalLlama_ComputeNode()
await llama_ai_node.start() await llama_ai_node.start()
@@ -116,7 +124,7 @@ class AIOS_Shell:
contact_config_path =os.path.abspath(f"{user_data_dir}/contacts.toml") contact_config_path =os.path.abspath(f"{user_data_dir}/contacts.toml")
cm = ContactManager.get_instance(contact_config_path) cm = ContactManager.get_instance(contact_config_path)
cm.load_data() cm.load_data()
tunnels_config_path = os.path.abspath(f"{user_data_dir}/etc/tunnels.cfg.toml") tunnels_config_path = os.path.abspath(f"{user_data_dir}/etc/tunnels.cfg.toml")
tunnel_config = None tunnel_config = None
try: try:
@@ -125,10 +133,10 @@ class AIOS_Shell:
await AgentTunnel.load_all_tunnels_from_config(tunnel_config) await AgentTunnel.load_all_tunnels_from_config(tunnel_config)
except Exception as e: except Exception as e:
logger.warning(f"load tunnels config from {tunnels_config_path} failed!") logger.warning(f"load tunnels config from {tunnels_config_path} failed!")
KnowledgePipline.get_instance().initial() KnowledgePipline.get_instance().initial()
return True return True
def get_version(self) -> str: def get_version(self) -> str:
return "0.5.1" return "0.5.1"
@@ -169,10 +177,10 @@ class AIOS_Shell:
user_input = await try_get_input(f"{key} : {item.desc}") user_input = await try_get_input(f"{key} : {item.desc}")
if user_input is None: if user_input is None:
return None return None
tunnel_config[key] = user_input
return tunnel_config tunnel_config[key] = user_input
return tunnel_config
async def append_tunnel_config(self,tunnel_config): async def append_tunnel_config(self,tunnel_config):
user_data_dir = AIStorage.get_instance().get_myai_dir() user_data_dir = AIStorage.get_instance().get_myai_dir()
@@ -235,7 +243,7 @@ class AIOS_Shell:
prompt.messages.append({"role": "user", "content":" ".join(args[1:])}) prompt.messages.append({"role": "user", "content":" ".join(args[1:])})
result = await KnowledgeBase().query_prompt(prompt) result = await KnowledgeBase().query_prompt(prompt)
print_formatted_text(result.as_str()) print_formatted_text(result.as_str())
async def call_func(self,func_name, args): async def call_func(self,func_name, args):
match func_name: match func_name:
case 'send': case 'send':
@@ -252,7 +260,7 @@ class AIOS_Shell:
key = args[0] key = args[0]
config_item = AIStorage.get_instance().get_user_config().get_config_item(key) config_item = AIStorage.get_instance().get_user_config().get_config_item(key)
old_value = AIStorage.get_instance().get_user_config().get_value(key) old_value = AIStorage.get_instance().get_user_config().get_value(key)
if config_item is not None: if config_item is not None:
value = await session.prompt_async(f"{key} : {config_item.desc} \nCurrent : {old_value}\nPlease input new value:",style=shell_style) value = await session.prompt_async(f"{key} : {config_item.desc} \nCurrent : {old_value}\nPlease input new value:",style=shell_style)
AIStorage.get_instance().get_user_config().set_value(key,value) AIStorage.get_instance().get_user_config().set_value(key,value)
@@ -459,7 +467,7 @@ async def main():
'/history $num $offset', '/history $num $offset',
'/login $username', '/login $username',
'/connect $target', '/connect $target',
'/knowledge add email | dir', '/knowledge add email | dir',
'/knowledge journal [$topn]', '/knowledge journal [$topn]',
'/knowledge query $query' '/knowledge query $query'
'/set_config $key', '/set_config $key',