Skip to content
Snippets Groups Projects
Commit 55a431f7 authored by David Rouquet's avatar David Rouquet
Browse files

Add instructions and cosmetic stuff

parent 7d1e5dac
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id:0d91640d-23ea-4079-b765-2eea030926c5 tags:
``` python
import importlib.util
import re
import amrlib
from amrlib.graph_processing.amr_plot import AMRPlot
import uuid
from IPython.display import SVG, display
import os
import shutil
import subprocess
from subprocess import Popen, PIPE, STDOUT
from glob import glob
import sys
import os
TENET_PATH = "/opt/dashboards/TetrasMARS/tenet/"
sys.path.insert(0, os.path.abspath(TENET_PATH))
import tenet
from IPython.display import HTML,IFrame
import ipywidgets
import dash_bootstrap_components as dbc
from dash import dcc, html, Input, Output
from jupyter_dash import JupyterDash as Dash
from dash.dependencies import Input, Output, State
import base64
MEDIA_PATH = "/opt/dashboards/media/17/"
MEDIA_URL = "https://unsel.tetras-lab.io/dashboard/17/media/"
ROOT_PATH = "/opt/dashboards/TetrasMARS/tetras-mars-demo/"
AMRLD_PATH = "/opt/dashboards/TetrasMARS/tetras-mars-demo/lib/amrld/"
owl2vowlPath = '/opt/dashboards/tools/owl2vowl_0.3.7/owl2vowl.jar'
WEBVOWL_PATH = '/opt/webvowl/'
onto_prefix="ontologyTarget"
# The following is basically `import tenet`
#spec=importlib.util.spec_from_file_location("tenet",TENET_PATH+'tenet/__init__.py')
#tenet = importlib.util.module_from_spec(spec)
#spec.loader.exec_module(tenet)
```
%% Cell type:code id:295e4aef-bbd8-40f0-8d84-0b8032b7b039 tags:
``` python
stog = amrlib.load_stog_model(model_dir="/opt/dashboards/TetrasMARS/corpus/cm-tool/amrModel/model_parse_xfm_bart_large-v0_1_0")
uuidStr = str(uuid.uuid4())
uuidDirPath = "/opt/data/tmp/demo-tetras-mars/"+uuidStr+'/'
os.mkdir(uuidDirPath)
prefixPath = uuidDirPath+"file"
penmanPath = prefixPath+".amr.penman"
svgPath = prefixPath+".amr.svg"
ttlFilePath = uuidDirPath+onto_prefix+"-0/"+onto_prefix+"_factoid.ttl"
webvowlFileName = ttlFilePath.split('/')[-1].replace('ttl','json')
webvowlFilepath = WEBVOWL_PATH+uuidStr+'_'+webvowlFileName
uuidZipPath = MEDIA_PATH+uuidStr # without the .zip extention
uuidZipUrl = MEDIA_URL+uuidStr+".zip"
```
%% Cell type:code id:a410a6b3-865d-441f-9b83-90a1badae291 tags:
``` python
def clean_sting(string):
""" Sentence cleanup as needed """
return re.sub("(\.)*\\n", "", string)
def string2amr(string,stog):
stog_result = stog.parse_sents([clean_sting(string)], add_metadata=True)
return stog_result[0]
def show_svg(path):
display(SVG(filename=path))
def add_id_in_penman_if_needed(penmanStr,uuidStr):
if not(penmanStr.startswith('# ::id')):
penmanStr = '# ::id '+uuidStr+'\n'+penmanStr
return penmanStr
def owl2vowl(ttlFilePath, uuid='', importList=[]):
# Run java parser
if importList == []:
cmd = ['java', '-jar', owl2vowlPath,
'-file', ttlFilePath]
else:
cmd = ['java', '-jar', owl2vowlPath,
'-file', ttlFilePath,
'-dependencies'] + importList
with Popen(cmd, stdout=PIPE, stderr=STDOUT) as p:
p.wait()
p.stdout.flush()
if p.returncode != 0:
print("Error in owl2vowl: \n\n"+p.stdout.read().decode())
os.rename(webvowlFileName, webvowlFilepath)
def localImage2htmlImg(imgPath):
with open(imgPath, "rb") as image_file:
img_data = base64.b64encode(image_file.read())
img_data = img_data.decode()
img_data = "data:image/svg+xml;base64,{}".format(img_data)
# ...
return html.Img(id="tag_id", src=img_data, width="100%", height="100%", className="img_class")#, alt="my image"
```
%% Cell type:code id:5fd9cf0c-990a-4776-b206-8cc94f87c7be tags:
``` python
def processStr(input):
penmanStr = string2amr(input,stog)
format = 'svg'
penmanStr = add_id_in_penman_if_needed(penmanStr,uuidStr)
penmanFile = open(penmanPath,"w")
penmanFile.write(penmanStr)
penmanFile.close()
plot = AMRPlot(uuidDirPath+"/file.amr", format)
plot.build_from_graph(penmanStr)
plot.graph.render()
amrldWorkPenmanFilepath = AMRLD_PATH+"/wk/"+uuidStr+".amr.penman"
amrldWorkNtFilepath = AMRLD_PATH+"/wk/"+uuidStr+".amr.nt"
amrNtPath = prefixPath+".amr.nt"
amrTtlPath = prefixPath+".amr.ttl"
os.chdir(AMRLD_PATH)
amrld_process = ["python3", "amr_to_rdf.py",
"-i", penmanPath,
"-o", amrTtlPath,
"-f", "ttl" ]
subprocess.run(amrld_process)
# Besoin de se mettre dans le répertoire tenet jusqu'à résolution du ticket https://gitlab.tetras-libre.fr/tetras-mars/tenet/-/issues/133
os.chdir(TENET_PATH+'tenet/')
factoids = tenet.create_ontology_from_amrld_file(amrTtlPath,
onto_prefix=onto_prefix, # "https://tenet.tetras-libre.fr/demo/",
out_file_path=uuidDirPath+"factoid.ttl",
technical_dir_path=uuidDirPath)
webvowlFilepath = owl2vowl(ttlFilePath,uuid=uuidStr)
shutil.make_archive(uuidZipPath, 'zip', uuidDirPath)
```
%% Cell type:code id:0cd10e8b-cf7a-4fd4-b8ac-540fcb943325 tags:
``` python
##################################################################################################
# THE FOLLOWING PART IS SPECIFIC TO TÉTRAS LAB
#
# The _get_tl_config function gets configuration parameters for your
# Tétras Lab instance.
# Those parameters are passed when initialising the Dash app.
##################################################################################################
def _get_tl_config():
import socket, errno, os
# Find a free port
host = "0.0.0.0"
port = 8050
end = 9999
found = False
while not found:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
s.bind((host, port))
found = True
except socket.error as e:
if e.errno == errno.EADDRINUSE:
port = port + 1
if (port > end):
raise "No available APP port"
else:
raise e
if (os.getenv("HOST", None) is not None):
proto = os.getenv("PROTO")
actualhost = os.getenv("JUPYTER_HOST", os.getenv("VOILA_HOST", ""))
localport = os.getenv("PORT", 80)
intermediatehost = os.getenv("HOST", "localhost")
base_path = f"/{actualhost}/app_proxy/{port}/"
proxified= f"{proto}://{intermediatehost}:{localport}{base_path}"
localurl = f"http://{host}:{port}"
proxy = f"{localurl}::{proxified}"
return ((proxified, host, port, proxy, base_path))
return ((f"http://localhost:{port}", host, port, None, "/"))
server_url, host, port, proxy, base_path = _get_tl_config()
app = Dash(
server_url=server_url,
requests_pathname_prefix=base_path,
)
##################################################################################################
##################################################################################################
# THE FOLLOWING PART IS GENERIC (JUPYTER)-DASH CODE FROM https://dash.plotly.com/basic-callbacks
#
# The _get_tl_config function gets configuration parameters for your
# Tétras Lab instance.
# Those parameters are passed when initialising the Dash app.
##################################################################################################
app.layout = html.Div([
"Enter an english text and click on the button bellow to construct an ontology. You can then download the results as a zip file.",
html.Br(),
"Please reload the page before entering another text.",
dcc.Textarea(
id='textarea-state',
value='Jupyter is a gas giant.',
style={'width': '100%', 'height': 200},
value='Jupiter is a gas giant.',
style={'width': '100%', 'height': 100},
),
html.Button('Construct AMR graphs and extract ontology', id='textarea-state-button', n_clicks=0),
html.Button('Construct AMR graphs and ontology', id='textarea-state-button', n_clicks=0),
#html.Button('Download result as zip', id='download-zip-button', n_clicks=0),
html.A(children="", href='', target="_blank",id="download-link"),
dcc.Loading(html.Div(id='my-output'), color='#5A8264')
html.Br(),
dcc.Loading(html.Div(id='my-output'), color='#5A8264'),
])
@app.callback(
#Output('textarea-state-output', 'children'),
Output(component_id='my-output', component_property='children'),
Output(component_id='download-link', component_property='children'),
Output(component_id='download-link', component_property='href'),
Input('textarea-state-button', 'n_clicks'),
State('textarea-state', 'value'),
prevent_initial_call=True,
)
def update_output(n_clicks, value):
if n_clicks > 0:
processStr(value)
#show_svg(svgPath)
#display(IFrame('''https://unsel.tetras-lab.io/webvowl/#{}">'''.format(webvowlFilepath.replace("/opt/webvowl/","").replace(".json","")),800,1200))
return [[
html.Iframe(src='''https://unsel.tetras-lab.io/webvowl/#{}'''.format(webvowlFilepath.replace("/opt/webvowl/","").replace(".json","")),
style={"height": "800px", "width": "100%"}),
"Browse ontology (You can click a class to see its instances in the right panel, they are not shown on the graph.) :",
html.Br(),
html.Iframe(src='''https://unsel.tetras-lab.io/webvowl/#{}'''.format(webvowlFilepath.replace("/opt/webvowl/","").replace(".json","")),style={"height": "800px", "width": "100%"}),
html.Br(),
"AMR graph :",
html.Br(),
localImage2htmlImg(svgPath)
],
"Download Zip File", uuidZipUrl
]
#@app.callback(
# Output("download-zip", "data"),
# Input("download-zip-button", "n_clicks"),
# prevent_initial_call=True,
#)
#def func(n_clicks):
# if n_clicks > 0:
# return dcc.send_file('https://unsel.tetras-lab.io/dashboard/17/media/9f7287d0-e7b2-4328-9137-7a7c44225b68.zip')
app.run_server(mode="inline",
host=host, port=port, proxy=proxy, height=2000)
##################################################################################################
```
%% Cell type:code id:4cbaf48e-3467-45b0-b931-20a758b79895 tags:
``` python
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment