Skip to content

Commit

Permalink
initial commit with extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
str-anger committed Jun 6, 2024
1 parent 0ca0d50 commit 89acd10
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/pull_request.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Static Analysis and Tests

on:
push:
branches: [main, feat/*]
pull_request:
branches: [main, feat/*]
workflow_dispatch:

jobs:
extensions_test:
strategy:
matrix:
python-version: ["3.10"]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Run extensions smoke tests
run: >
sh tests/mock.sh
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,6 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

# project specific
.aqueduct-extension-venv/
49 changes: 49 additions & 0 deletions py-wolfram-alpha/manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: "Wolfram alpha solution extension"
description: >
This extension solves mathematical problems
using Wolfram|Alpha cloud engine. To run the
extension, please obtain an App ID at
https://developer.wolframalpha.com/access
authors: [email protected]
aqueduct_url: https://localhost:8000/

constants:
wolfram_app_id: "KEY HERE"

actions:
-
name: solve as text
description: >
solve an equation and return a text answer
# there is a trick, which allows to
# customise execution environments by having
# a venv inside a extension folder, and running
# a venv-specific
# python binary from venv folder. Details:
# https://stackoverflow.com/a/54482177
#
# This thick allows to avoid `aqueductcore`
# dependency on client library.
#
# Extension may cache some data and libraries
# for it's further usage, or, alternatively,
# it may drop and recreate
# its environment on each run.
script: >
$python solve_alpha.py
# arguments of the action
parameters:
-
name: equation
description: equation to solve
data_type: str
-
name: experiment
description: experiment to store result
data_type: experiment
-
name: result_file
description: file name to store result
data_type: str
2 changes: 2 additions & 0 deletions py-wolfram-alpha/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests>=2.0.0
pyaqueduct==0.0.8
70 changes: 70 additions & 0 deletions py-wolfram-alpha/solve_alpha.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import os
import urllib.parse
import requests
from xml.etree import ElementTree

from tempfile import TemporaryDirectory
from pathlib import Path

from pyaqueduct import API
from pyaqueduct.experiment import Experiment


def solve_with_wolfram_alpha(app_id: str, question: str) -> str:
question_encoded = urllib.parse.quote_plus(question)
url = f"http://api.wolframalpha.com/v2/query?input={question_encoded}&appid={app_id}"
response = requests.get(url)
tree = ElementTree.fromstring(response.content)

result = []
for node in tree.findall("./pod[@id='Solution']/subpod/plaintext"):
if node.text:
result.append(node.text)
for node in tree.findall("./pod[@id='ComplexSolution']/subpod/plaintext"):
if node.text:
result.append(node.text)
return "\n".join(sorted(result))


def save_to_aqueduct(
content: str,
aqueduct_url: str,
aqueduct_key: str,
experiment_id: str,
filename: str) -> None:
"""Saves content string as a file in aqueduct
content (str): string to save
aqueduct_url (str): URL of the aqueduct instance
aqueduct_key (str): access key (if needed) to access the instance
experiment_id (str):
ID of the experiment where the file will be saved
filename (str):
name of the resulting file
"""
api = API(url=aqueduct_url, timeout=10)
exp = api.get_experiment(experiment_id)
with TemporaryDirectory() as directory:
fullname = Path(directory) / filename
with open(fullname, "w") as file:
file.write(content)
exp.upload_file(str(fullname))


if __name__ == "__main__":

app_id = os.environ.get("wolfram_app_id", "")
problem = os.environ.get("equation", "")

aq_url = os.environ.get("aqueduct_url", "")
aq_key = os.environ.get("aqueduct_key", "")

experiment_id = os.environ.get("experiment", "")
filename = os.environ.get("result_file", "")

solution = solve_with_wolfram_alpha(app_id, problem)
save_to_aqueduct(
f"{problem}:\n{solution}",
aq_url, aq_key,
experiment_id, filename)
print(solution)
9 changes: 9 additions & 0 deletions sh-example/dummy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
echo var1=$var1
echo var2=$var2
echo var3=$var3
echo var4=$var4
# echo flattens strings, so use printf
printf "var5=$var5\n"
echo var6=$var6
echo var7=$var7
echo dummykey=$dummykey
79 changes: 79 additions & 0 deletions sh-example/manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: "Dummy extension"
description: >
This extension prints environment variables
passed to it. No requests to Aqueduct sent.
authors: [email protected]
aqueduct_url: http://localhost:8000/

# global variables for all actions of the extension
constants:
dummykey: dummyvalue

actions:
- name: echo
description: "Print values to stdout"
script: >
sh dummy.sh
# arguments of the action
parameters:
- name: var1
description: variable 1
data_type: str
default_value: 1
- name: var2
description: variable 2
data_type: int
display_name: some display name
- name: var3
description: variable 3
data_type: float
- name: var4
description: variable 4
data_type: experiment
- name: var5
description: variable 5 multiline
data_type: textarea
- name: var6
description: boolean variable
data_type: bool
default_value: true
- name: var7
description: select / combobox
data_type: select
default_value: "string three"
options: [ string1, string2, string three, string4 ]

- name: echo_stderr
description: "Print values to stdout"
script: >
>&2 sh dummy.sh; exit 13
# arguments of the action
parameters:
- name: var1
description: variable 1
data_type: str
default_value: 1
- name: var2
description: variable 2
data_type: int
display_name: some display name
- name: var3
description: variable 3
data_type: float
- name: var4
description: variable 4
data_type: experiment
- name: var5
description: variable 5 multiline
data_type: textarea
- name: var6
description: boolean variable
data_type: bool
default_value: true
- name: var7
description: select / combobox
data_type: select
default_value: "string three"
options: [ string1, string2, string three, string4 ]

0 comments on commit 89acd10

Please sign in to comment.