diff --git a/selfContainedDemo.ipynb b/selfContainedDemo.ipynb index 1146e9c221cf944d30dcb5e9ce12780c8c523783..e5d0d28e175aaa47ecfbdc0e31569eaf15f37e91 100644 --- a/selfContainedDemo.ipynb +++ b/selfContainedDemo.ipynb @@ -20,9 +20,6 @@ "from glob import glob\n", "import sys\n", "import os\n", - "TENET_PATH = \"/opt/dashboards/TetrasMARS/tenet/\"\n", - "sys.path.insert(0, os.path.abspath(TENET_PATH))\n", - "import tenet\n", "from IPython.display import HTML,IFrame\n", "import ipywidgets\n", "import dash_bootstrap_components as dbc\n", @@ -31,6 +28,15 @@ "from dash.dependencies import Input, Output, State\n", "import base64\n", "\n", + "AMR_BATCH_PATH = \"/opt/dashboards/TetrasMARS/amrbatch/\"\n", + "sys.path.insert(0, os.path.abspath(AMR_BATCH_PATH))\n", + "import amrbatch\n", + "\n", + "TENET_PATH = \"/opt/dashboards/TetrasMARS/tenet/\"\n", + "sys.path.insert(0, os.path.abspath(TENET_PATH))\n", + "import tenet\n", + "\n", + "AMR_MODEL_PATH=\"/opt/dashboards/TetrasMARS/corpus/cm-tool/amrModel/model_parse_xfm_bart_large-v0_1_0\"\n", "MEDIA_PATH = \"/opt/dashboards/media/17/\"\n", "MEDIA_URL = \"https://unsel.tetras-lab.io/dashboard/17/media/\"\n", "ROOT_PATH = \"/opt/dashboards/TetrasMARS/tetras-mars-demo/\"\n", @@ -52,19 +58,14 @@ "metadata": {}, "outputs": [], "source": [ - "stog = amrlib.load_stog_model(model_dir=\"/opt/dashboards/TetrasMARS/corpus/cm-tool/amrModel/model_parse_xfm_bart_large-v0_1_0\") \n", + "#stog = amrlib.load_stog_model(model_dir=\"/opt/dashboards/TetrasMARS/corpus/cm-tool/amrModel/model_parse_xfm_bart_large-v0_1_0\") \n", " \n", - "uuidStr = str(uuid.uuid4())\n", - "uuidDirPath = \"/opt/data/tmp/demo-tetras-mars/\"+uuidStr+'/'\n", - "os.mkdir(uuidDirPath)\n", - "prefixPath = uuidDirPath+\"file\"\n", - "penmanPath = prefixPath+\".amr.penman\"\n", - "svgPath = prefixPath+\".amr.svg\"\n", - "ttlFilePath = uuidDirPath+onto_prefix+\"-0/\"+onto_prefix+\"_factoid.ttl\"\n", - "webvowlFileName = ttlFilePath.split('/')[-1].replace('ttl','json')\n", - "webvowlFilepath = WEBVOWL_PATH+uuidStr+'_'+webvowlFileName\n", - "uuidZipPath = MEDIA_PATH+uuidStr # without the .zip extention\n", - "uuidZipUrl = MEDIA_URL+uuidStr+\".zip\"" + "\n", + "\n", + "#prefixPath = uuidDirPath+\"file\"\n", + "#penmanPath = prefixPath+\".amr.penman\"\n", + "#svgPath = prefixPath+\".amr.svg\"\n", + "#ttlFilePath = uuidDirPath+onto_prefix+\"-0/\"+onto_prefix+\"_factoid.ttl\"\n" ] }, { @@ -82,6 +83,8 @@ " stog_result = stog.parse_sents([clean_sting(string)], add_metadata=True) \n", " return stog_result[0] \n", "\n", + "\n", + "\n", "def show_svg(path):\n", " display(SVG(filename=path))\n", " \n", @@ -90,7 +93,7 @@ " penmanStr = '# ::id '+uuidStr+'\\n'+penmanStr\n", " return penmanStr\n", "\n", - "def owl2vowl(ttlFilePath, uuid='', importList=[]):\n", + "def owl2vowl(ttlFilePath, webvowlFileName, webvowlFilePath, uuid='', importList=[]):\n", " # Run java parser\n", " if importList == []:\n", " cmd = ['java', '-jar', owl2vowlPath,\n", @@ -104,7 +107,7 @@ " p.stdout.flush()\n", " if p.returncode != 0:\n", " print(\"Error in owl2vowl: \\n\\n\"+p.stdout.read().decode())\n", - " os.rename(webvowlFileName, webvowlFilepath)\n", + " os.rename(webvowlFileName, webvowlFilePath)\n", " \n", "def localImage2htmlImg(imgPath):\n", " with open(imgPath, \"rb\") as image_file:\n", @@ -123,36 +126,33 @@ "outputs": [], "source": [ "def processStr(input):\n", - " penmanStr = string2amr(input,stog)\n", - " format = 'svg'\n", - " penmanStr = add_id_in_penman_if_needed(penmanStr,uuidStr)\n", - " penmanFile = open(penmanPath,\"w\")\n", - " penmanFile.write(penmanStr)\n", - " penmanFile.close()\n", - " plot = AMRPlot(uuidDirPath+\"/file.amr\", format) \n", - " plot.build_from_graph(penmanStr)\n", - " plot.graph.render()\n", - " amrldWorkPenmanFilepath = AMRLD_PATH+\"/wk/\"+uuidStr+\".amr.penman\"\n", - " amrldWorkNtFilepath = AMRLD_PATH+\"/wk/\"+uuidStr+\".amr.nt\"\n", - "\n", - " amrNtPath = prefixPath+\".amr.nt\" \n", - " amrTtlPath = prefixPath+\".amr.ttl\" \n", - " os.chdir(AMRLD_PATH)\n", - "\n", - " amrld_process = [\"python3\", \"amr_to_rdf.py\", \n", - " \"-i\", penmanPath, \n", - " \"-o\", amrTtlPath,\n", - " \"-f\", \"ttl\" ]\n", - " subprocess.run(amrld_process) \n", - "\n", - " # Besoin de se mettre dans le répertoire tenet jusqu'à résolution du ticket https://gitlab.tetras-libre.fr/tetras-mars/tenet/-/issues/133\n", - " os.chdir(TENET_PATH+'tenet/')\n", - " factoids = tenet.create_ontology_from_amrld_file(amrTtlPath,\n", - " onto_prefix=onto_prefix, # \"https://tenet.tetras-libre.fr/demo/\",\n", - " out_file_path=uuidDirPath+\"factoid.ttl\",\n", - " technical_dir_path=uuidDirPath)\n", - " webvowlFilepath = owl2vowl(ttlFilePath,uuid=uuidStr)\n", - " shutil.make_archive(uuidZipPath, 'zip', uuidDirPath)" + " # Define usefull variable and paths based on a uuid\n", + " uuidStr = str(uuid.uuid4())\n", + " uuidDirPath = \"/opt/data/tmp/demo-tetras-mars/\"+uuidStr+'/'\n", + " os.mkdir(uuidDirPath)\n", + " fullOntoPath = uuidDirPath+'full-ontology.ttl'\n", + " ontoBySentencePath = uuidDirPath+'onto-by-sentence/'\n", + " webvowlFileName = fullOntoPath.split('/')[-1].replace('ttl','json')\n", + " webvowlFilePath = WEBVOWL_PATH+uuidStr+'_'+webvowlFileName\n", + " uuidZipPath = MEDIA_PATH+uuidStr # without the .zip extention\n", + " uuidZipUrl = MEDIA_URL+uuidStr+\".zip\"\n", + " \n", + " # Generate an AMR graph by sentence in a subfolder (with companion files such as images of the graphs)\n", + " amr_graph_list = amrbatch.parse_document_string_to_produce_amr_graph(\n", + " input, None, amr_model_path=AMR_MODEL_PATH, output_dirpath=uuidDirPath, \n", + " amrld_serialization=True)\n", + " \n", + " # Construct ontologies from each AMR graph plus a \"full\" one that is the union\n", + " factoids = tenet.create_ontology_from_amrld_dir(\n", + " uuidDirPath,\n", + " onto_prefix=\"http://ontologies\",\n", + " out_file_path=fullOntoPath,\n", + " technical_dir_path=ontoBySentencePath)\n", + " \n", + " # Create a zip file so the user can download all generated files\n", + " owl2vowl(fullOntoPath, webvowlFileName, webvowlFilePath, uuid=uuidStr)\n", + " shutil.make_archive(uuidZipPath, 'zip', uuidDirPath)\n", + " return uuidDirPath, uuidZipUrl, webvowlFilePath" ] }, { @@ -220,19 +220,23 @@ "##################################################################################################\n", "\n", "app.layout = html.Div([\n", - " \"Enter an english text and click on the button bellow to construct an ontology. You can then download the results as a zip file.\",\n", + " \"Enter an english text and click on the button bellow to construct an ontology.\",\n", + " html.Br(),\n", + " \" You can then browse the results online or download them as a zip file.\",\n", + " html.Br(),\n", " html.Br(),\n", - " \"Please reload the page before entering another text.\",\n", " dcc.Textarea(\n", " id='textarea-state',\n", - " value='Jupiter is a gas giant.',\n", - " style={'width': '100%', 'height': 100},\n", + " value='Jupiter is a gas giant. Earth is a rock planet.',\n", + " style={'width': '80%', 'height': 100},\n", " ),\n", + " html.Br(), html.Br(),\n", " html.Button('Construct AMR graphs and ontology', id='textarea-state-button', n_clicks=0),\n", - " #html.Button('Download result as zip', id='download-zip-button', n_clicks=0),\n", + " html.Br(),\n", " html.A(children=\"\", href='', target=\"_blank\",id=\"download-link\"),\n", " html.Br(),\n", - " dcc.Loading(html.Div(id='my-output'), color='#5A8264'),\n", + " html.Br(),\n", + " dcc.Loading(html.Div(id='my-output'), color='#A85431', style={\"verticalAlign\":\"top\"}),\n", "])\n", "\n", "@app.callback(\n", @@ -246,19 +250,35 @@ ")\n", "def update_output(n_clicks, value):\n", " if n_clicks > 0:\n", - " processStr(value)\n", + " uuidDirPath, uuidZipUrl, webvowlFilePath = processStr(value)\n", " #show_svg(svgPath) \n", - " #display(IFrame('''https://unsel.tetras-lab.io/webvowl/#{}\">'''.format(webvowlFilepath.replace(\"/opt/webvowl/\",\"\").replace(\".json\",\"\")),800,1200))\n", - " return [[\n", - " \"Browse ontology (You can click a class to see its instances in the right panel, they are not shown on the graph.) :\",\n", - " html.Br(),\n", - " html.Iframe(src='''https://unsel.tetras-lab.io/webvowl/#{}'''.format(webvowlFilepath.replace(\"/opt/webvowl/\",\"\").replace(\".json\",\"\")),style={\"height\": \"800px\", \"width\": \"100%\"}),\n", - " html.Br(),\n", - " \"AMR graph :\",\n", + " #display(IFrame('''https://unsel.tetras-lab.io/webvowl/#{}\">'''.format(webvowlFilePath.replace(\"/opt/webvowl/\",\"\").replace(\".json\",\"\")),800,1200))\n", + " return [html.Div(\n", + " dbc.Accordion(\n", + " [\n", + " dbc.AccordionItem(\n", + " [\n", + " \"You can click a class to see its instances in the right panel, they are not shown on the graph.\",\n", " html.Br(),\n", - " localImage2htmlImg(svgPath)\n", + " html.Iframe(src='''https://unsel.tetras-lab.io/webvowl/#{}'''.format(webvowlFilePath.replace(\"/opt/webvowl/\",\"\").replace(\".json\",\"\")),style={\"height\": \"800px\", \"width\": \"100%\"}),\n", " ],\n", - " \"Download Zip File\", uuidZipUrl\n", + " title=\"Browse ontology\",\n", + " )\n", + " ] \n", + " #+ [dbc.AccordionItem(\n", + " # [\n", + " # localImage2htmlImg(svgPath)\n", + " # ],\n", + " # title=\"AMR Graph\",\n", + " # )\n", + " # \n", + " # for svgPath in glob(uuidDirPath+\"document-*/*.svg\")\n", + " # ]\n", + " # \n", + " # ,\n", + " )\n", + "),\n", + " \"Download Zip File\", uuidZipUrl\n", " ]\n", "\n", "#@app.callback(\n", @@ -272,7 +292,8 @@ " \n", "\n", " \n", - "app.run_server(mode=\"inline\", \n", + "app.run_server(mode=\"inline\",\n", + " #mode=\"external\",\n", " host=host, port=port, proxy=proxy, height=2000)\n", "##################################################################################################\n" ] @@ -280,7 +301,7 @@ { "cell_type": "code", "execution_count": null, - "id": "4cbaf48e-3467-45b0-b931-20a758b79895", + "id": "b1817beb-04b7-4e6f-9b82-675de76cf8af", "metadata": {}, "outputs": [], "source": []