diff --git a/__init__.py b/__init__.py index 3ff3cf4..72c39b7 100644 --- a/__init__.py +++ b/__init__.py @@ -13,7 +13,7 @@ import tempfile from os.path import join, isfile - +from typing import Optional import requests from ovos_backend_client.api import WolframAlphaApi as _WA from ovos_bus_client import Message @@ -21,20 +21,19 @@ from ovos_config import Configuration from ovos_plugin_manager.templates.solvers import QuestionSolver from ovos_utils import classproperty -from ovos_utils.gui import can_use_gui from ovos_utils.process_utils import RuntimeRequirements from ovos_workshop.decorators import intent_handler from ovos_workshop.skills.common_query_skill import CommonQuerySkill, CQSMatchLevel class WolframAlphaApi(_WA): - def get_image(self, query): + def get_image(self, query: str, units: Optional[str] = None): """ query assured to be in self.default_lang return path/url to a single image to acompany spoken_answer """ # TODO - extend backend-client method for picture - units = Configuration().get("system_unit", "metric") + units = units or Configuration().get("system_unit", "metric") url = 'http://api.wolframalpha.com/v1/simple' params = {"appid": self.credentials["wolfram"], "i": query, @@ -51,7 +50,7 @@ def get_image(self, query): class WolframAlphaSolver(QuestionSolver): priority = 25 - enable_cache = True + enable_cache = False enable_tx = True def __init__(self, config=None): @@ -123,7 +122,7 @@ def get_data(self, query, context=None): query assured to be in self.default_lang return a dict response """ - units = Configuration().get("system_unit", "metric") + units = context.get("units") or Configuration().get("system_unit", "metric") return self.api.full_results(query, units=units) # image api (simple) @@ -132,7 +131,8 @@ def get_image(self, query, context=None): query assured to be in self.default_lang return path/url to a single image to acompany spoken_answer """ - return self.api.get_image(query) + units = context.get("units") or Configuration().get("system_unit", "metric") + return self.api.get_image(query, units) # spoken answers api (spoken) def get_spoken_answer(self, query, context): @@ -140,7 +140,7 @@ def get_spoken_answer(self, query, context): query assured to be in self.default_lang return a single sentence text response """ - units = Configuration().get("system_unit", "metric") + units = context.get("units") or Configuration().get("system_unit", "metric") answer = self.api.spoken(query, units=units) bad_answers = ["no spoken result available", "wolfram alpha did not understand your input"] @@ -158,7 +158,6 @@ def get_expanded_answer(self, query, context=None): "summary": "speak this", "img": "optional/path/or/url } - """ data = self.get_data(query, context) # these are returned in spoken answer or otherwise unwanted @@ -213,12 +212,12 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.session_results = {} # session_id: {} self.wolfie = WolframAlphaSolver({ - "units": self.config_core['system_unit'], "appid": self.settings.get("api_key") }) @classproperty def runtime_requirements(self): + """this skill requires internet""" return RuntimeRequirements(internet_before_load=True, network_before_load=True, gui_before_load=False, @@ -236,8 +235,10 @@ def handle_search(self, message: Message): sess = SessionManager.get(message) self.session_results[sess.session_id] = {"phrase": query, "image": None, + "lang": sess.lang, + "system_unit": sess.system_unit, "spoken_answer": ""} - response = self.ask_the_wolf(query) + response = self.ask_the_wolf(query, sess.lang, sess.system_unit) if response: self.session_results[sess.session_id]["spoken_answer"] = response self.speak(response) @@ -254,9 +255,11 @@ def CQS_match_query_phrase(self, phrase: str): sess = SessionManager.get() self.session_results[sess.session_id] = {"phrase": phrase, "image": None, + "lang": sess.lang, + "system_unit": sess.system_unit, "spoken_answer": None} - response = self.ask_the_wolf(phrase, sess.lang) + response = self.ask_the_wolf(phrase, sess.lang, sess.system_unit) if response: self.session_results[sess.session_id]["spoken_answer"] = response self.log.debug(f"WolframAlpha response: {response}") @@ -265,10 +268,20 @@ def CQS_match_query_phrase(self, phrase: str): def CQS_action(self, phrase: str, data: dict): """ If selected show gui """ - self.display_wolfie() + # generate image for the query after skill was selected for speed + image = self.wolfie.visual_answer(phrase, context=data) + self.gui["wolfram_image"] = image or f"{self.root_dir}/res/logo.png" + # scrollable full result page + self.gui.show_page("wolf", override_idle=45) # wolfram integration - def ask_the_wolf(self, query: str, lang: str = None): + def ask_the_wolf(self, query: str, + lang: Optional[str] = None, + units: Optional[str] = None): + units = units or self.system_unit + if units != "metric": + units = "nonmetric" # what wolfram api expects + lang = lang or self.lang if lang.startswith("en"): self.log.debug(f"skipping auto translation for wolfram alpha, " @@ -278,25 +291,8 @@ def ask_the_wolf(self, query: str, lang: str = None): self.log.info(f"enabling auto translation for wolfram alpha, " f"{lang} is not supported internally") WolframAlphaSolver.enable_tx = True - return self.wolfie.spoken_answer(query, context={"lang": lang}) - - def display_wolfie(self): - if not can_use_gui(self.bus): - return - - # generate image for the last query this session made - # only after skill was selected for speed - sess = SessionManager.get() - res = self.session_results.get(sess.session_id) - if not res or not res["spoken_response"]: - return - - image = res.get("image") or self.wolfie.visual_answer(res["phrase"], - context={"lang": self.lang}) - if image: - self.gui["wolfram_image"] = image - # scrollable full result page - self.gui.show_page(join(self.root_dir, "ui", "wolf"), override_idle=45) + return self.wolfie.spoken_answer(query, + context={"lang": lang, "units": units}) def stop_session(self, sess): if sess.session_id in self.session_results: @@ -308,7 +304,12 @@ def stop_session(self, sess): d = WolframAlphaSkill(bus=FakeBus(), skill_id="fake.wolf") - print(d.ask_the_wolf("what is the speed of light")) + print(d.ask_the_wolf("what is the speed of light", units="nonmetric")) # SI units regardless + # The speed of light has a value of about 300 million meters per second + print(d.ask_the_wolf("how tall is the eiffel tower", units="metric")) + print(d.ask_the_wolf("how tall is the eiffel tower", units="nonmetric")) + # The total height of the Eiffel Tower is 330 meters + # The total height of the Eiffel Tower is about 1083 feet exit() diff --git a/ui/wolf.qml b/qt5/wolf.qml similarity index 100% rename from ui/wolf.qml rename to qt5/wolf.qml diff --git a/requirements.txt b/requirements.txt index fff01f5..0ead123 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ ovos-translate-server-plugin ovos-config>=0.0.12 ovos-utils~=0.0, >=0.0.38 -ovos_workshop~=0.0, >=0.0.15 +ovos_workshop~=0.0, >=0.0.16a25 ovos-plugin-manager \ No newline at end of file diff --git a/ui/logo.png b/res/logo.png similarity index 100% rename from ui/logo.png rename to res/logo.png diff --git a/scripts/prepare_skillstore.py b/scripts/prepare_skillstore.py index 6dc728a..11d6d3f 100644 --- a/scripts/prepare_skillstore.py +++ b/scripts/prepare_skillstore.py @@ -19,7 +19,7 @@ base_dir = dirname(dirname(__file__)) desktop_dir = join(base_dir, "res", "desktop") -android_ui = join(base_dir, "ui", "+android") +android_ui = join(base_dir, "qt5", "+android") makedirs(desktop_dir, exist_ok=True) readme = join(base_dir, "README.md") diff --git a/setup.py b/setup.py index b0067ef..04fddc5 100755 --- a/setup.py +++ b/setup.py @@ -31,7 +31,7 @@ def get_requirements(requirements_filename: str): def find_resource_files(): - resource_base_dirs = ("locale", "ui", "vocab", "dialog", "regex", "skill") + resource_base_dirs = ("locale", "qt5", "res") base_dir = path.dirname(__file__) package_data = ["*.json"] for res in resource_base_dirs: