From a386c7e974cc35e9bfd80d9fe680a1c4c313d43b Mon Sep 17 00:00:00 2001 From: "hy.li" Date: Mon, 4 Sep 2023 23:30:27 +0800 Subject: [PATCH 1/7] playwright version mmdc --- README.md | 24 + config/config.yaml | 4 + metagpt/config.py | 1 + metagpt/utils/common.py | 2 +- metagpt/utils/index.html | 2212 +++++++++++++++++++++++++++ metagpt/utils/mermaid.py | 3 + metagpt/utils/mermaid_playwright.py | 199 +++ 7 files changed, 2444 insertions(+), 1 deletion(-) create mode 100644 metagpt/utils/index.html create mode 100644 metagpt/utils/mermaid_playwright.py diff --git a/README.md b/README.md index 84dafa46b..adc9d8cea 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,30 @@ # Step 3: Clone the repository to your local machine, and install it. - if `python setup.py install` fails with error `[Errno 13] Permission denied: '/usr/local/lib/python3.11/dist-packages/test-easy-install-13129.write-test'`, try instead running `python setup.py install --user` +- To convert Mermaid charts to SVG, PNG, and PDF formats. In addition to the Node.js version of Mermaid-CLI, you now have the option to use Python version Playwright for this task. + +- **Install Playwright** + +```bash +pip install playwright +``` + +- **Install the Required Browsers** + +to support PDF conversion, had better install Chrominum. + +```bash +playwright install --with-deps chromium +``` + +- **modify `config.yaml`** + +uncomment MERMAID_ENGINE from config.yaml and change it to `playwright` + +```yaml +MERMAID_ENGINE: playwright +``` + ### Installation by Docker ```bash diff --git a/config/config.yaml b/config/config.yaml index 274cdf469..ec89a9932 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -75,3 +75,7 @@ SD_T2I_API: "/sdapi/v1/txt2img" ### for Research MODEL_FOR_RESEARCHER_SUMMARY: gpt-3.5-turbo MODEL_FOR_RESEARCHER_REPORT: gpt-3.5-turbo-16k + +### choose the engine for mermaid conversion, +# default is nodejs, you can change it to playwright +# MERMAID_ENGINE: nodejs \ No newline at end of file diff --git a/metagpt/config.py b/metagpt/config.py index 76c6563cb..b51c81862 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -82,6 +82,7 @@ class Config(metaclass=Singleton): self.calc_usage = self._get("CALC_USAGE", True) self.model_for_researcher_summary = self._get("MODEL_FOR_RESEARCHER_SUMMARY") self.model_for_researcher_report = self._get("MODEL_FOR_RESEARCHER_REPORT") + self.mermaid_engine = self._get("MERMAID_ENGINE", 'nodejs') def _init_with_config_files_and_env(self, configs: dict, yaml_file): """Load from config/key.yaml, config/config.yaml, and env in decreasing order of priority""" diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index 7f090cf63..2e214685c 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -187,7 +187,7 @@ class CodeParser: else: logger.error(f"{pattern} not match following text:") logger.error(text) - raise Exception + # raise Exception return code @classmethod diff --git a/metagpt/utils/index.html b/metagpt/utils/index.html new file mode 100644 index 000000000..0ac6d9a74 --- /dev/null +++ b/metagpt/utils/index.html @@ -0,0 +1,2212 @@ + + + + + + + +
+ + + diff --git a/metagpt/utils/mermaid.py b/metagpt/utils/mermaid.py index 24aabe8ae..f395b43b2 100644 --- a/metagpt/utils/mermaid.py +++ b/metagpt/utils/mermaid.py @@ -14,6 +14,7 @@ from metagpt.logs import logger from metagpt.utils.common import check_cmd_exists + def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height=2048) -> int: """suffix: png/svg/pdf @@ -56,6 +57,8 @@ def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height subprocess.run([CONFIG.mmdc, "-i", str(tmp), "-o", output_file, "-w", str(width), "-H", str(height)]) return 0 +if CONFIG.mermaid_engine.lower() == "playwright": + from metagpt.utils.mermaid_playwright import mermaid_to_file MMC1 = """classDiagram class Main { diff --git a/metagpt/utils/mermaid_playwright.py b/metagpt/utils/mermaid_playwright.py new file mode 100644 index 000000000..aa04e70eb --- /dev/null +++ b/metagpt/utils/mermaid_playwright.py @@ -0,0 +1,199 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/9/4 16:12 +@Author : Steven Lee +@File : mermaid_playwright.py +""" +import os +import asyncio +from metagpt.config import CONFIG +from metagpt.const import PROJECT_ROOT +from metagpt.logs import logger + +from urllib.parse import urljoin +from playwright.async_api import async_playwright +import nest_asyncio + +__dirname = os.path.dirname(os.path.abspath(__file__)) + + +def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height=2048, output_formats=['png', 'svg', 'pdf']) -> int: + """ + Converts the given Mermaid code to various output formats and saves them to files. + + Args: + mermaid_code (str): The Mermaid code to convert. + output_file_without_suffix (str): The output file name without the file extension. + width (int, optional): The width of the output image in pixels. Defaults to 2048. + height (int, optional): The height of the output image in pixels. Defaults to 2048. + output_formats (list[str], optional): The list of output formats to generate. Defaults to ['png', 'svg', 'pdf']. + + Returns: + int: Returns 1 if the conversion and saving were successful, -1 otherwise. + """ + + async def mermaid_to_file0(mermaid_code, output_file_without_suffix, width=2048, height=2048, output_formats=['png', 'svg', 'pdf'])-> int: + + async with async_playwright() as p: + browser = await p.chromium.launch() + device_scale_factor = 1.0 + + context = await browser.new_context( + viewport={'width': width, 'height': height}, + device_scale_factor=device_scale_factor, + ) + page = await context.new_page() + + async def console_message(msg): + print(msg.text) + page.on('console', console_message) + + try: + await page.set_viewport_size({'width': width, 'height': height}) + + mermaid_html_path = os.path.abspath( + os.path.join(__dirname, 'index.html')) + mermaid_html_url = urljoin('file:', mermaid_html_path) + await page.goto(mermaid_html_url) + await page.wait_for_load_state("networkidle") + + await page.wait_for_selector("div#container", state="attached") + mermaid_config = {} + background_color = "#ffffff" + my_css = "" + await page.evaluate(f'document.body.style.background = "{background_color}";') + + metadata = await page.evaluate('''async ([definition, mermaidConfig, myCSS, backgroundColor]) => { + const { mermaid, zenuml } = globalThis; + await mermaid.registerExternalDiagrams([zenuml]); + mermaid.initialize({ startOnLoad: false, ...mermaidConfig }); + const { svg } = await mermaid.render('my-svg', definition, document.getElementById('container')); + document.getElementById('container').innerHTML = svg; + const svgElement = document.querySelector('svg'); + svgElement.style.backgroundColor = backgroundColor; + + if (myCSS) { + const style = document.createElementNS('http://www.w3.org/2000/svg', 'style'); + style.appendChild(document.createTextNode(myCSS)); + svgElement.appendChild(style); + } + + let title = null; + let desc = null; + + if (svgElement.firstChild instanceof SVGTitleElement) { + title = svgElement.firstChild.textContent; + } + + for (const svgNode of svgElement.children) { + if (svgNode instanceof SVGDescElement) { + desc = svgNode.textContent; + break; + } + } + + return { + title, + desc + }; + }''', [mermaid_code, mermaid_config, my_css, background_color]) + + if 'svg' in output_formats : + svg_xml = await page.evaluate('''() => { + const svg = document.querySelector('svg'); + const xmlSerializer = new XMLSerializer(); + return xmlSerializer.serializeToString(svg); + }''') + # result[f'{output_file_without_suffix}.svg'] = svg_xml + with open(f'{output_file_without_suffix}.svg', 'wb') as f: + f.write(svg_xml.encode('utf-8')) + + if 'png' in output_formats: + clip = await page.evaluate('''() => { + const svg = document.querySelector('svg'); + const rect = svg.getBoundingClientRect(); + return { + x: Math.floor(rect.left), + y: Math.floor(rect.top), + width: Math.ceil(rect.width), + height: Math.ceil(rect.height) + }; + }''') + await page.set_viewport_size({'width': clip['x'] + clip['width'], 'height': clip['y'] + clip['height']}) + screenshot = await page.screenshot(clip=clip, omit_background=True, scale='device') + with open(f'{output_file_without_suffix}.png', 'wb') as f: + f.write(screenshot) + if 'pdf' in output_formats: + pdf_data = await page.pdf(scale=device_scale_factor) + with open(f'{output_file_without_suffix}.pdf', 'wb') as f: + f.write(pdf_data) + return 1 + except Exception as e: + logger.error(e) + return -1 + finally: + await browser.close() + with open(f"{output_file_without_suffix}.mmd", "w", encoding="utf-8") as f: + f.write(mermaid_code) + nest_asyncio.apply() + loop = asyncio.new_event_loop() + result = loop.run_until_complete(mermaid_to_file0(mermaid_code, output_file_without_suffix, width, height, output_formats)) + loop.close() + return result + +MMC1 = """classDiagram + class Main { + -SearchEngine search_engine + +main() str + } + class SearchEngine { + -Index index + -Ranking ranking + -Summary summary + +search(query: str) str + } + class Index { + -KnowledgeBase knowledge_base + +create_index(data: dict) + +query_index(query: str) list + } + class Ranking { + +rank_results(results: list) list + } + class Summary { + +summarize_results(results: list) str + } + class KnowledgeBase { + +update(data: dict) + +fetch_data(query: str) dict + } + Main --> SearchEngine + SearchEngine --> Index + SearchEngine --> Ranking + SearchEngine --> Summary + Index --> KnowledgeBase""" + +MMC2 = """sequenceDiagram + participant M as Main + participant SE as SearchEngine + participant I as Index + participant R as Ranking + participant S as Summary + participant KB as KnowledgeBase + M->>SE: search(query) + SE->>I: query_index(query) + I->>KB: fetch_data(query) + KB-->>I: return data + I-->>SE: return results + SE->>R: rank_results(results) + R-->>SE: return ranked_results + SE->>S: summarize_results(ranked_results) + S-->>SE: return summary + SE-->>M: return summary""" + + +if __name__ == "__main__": + # logger.info(print_members(print_members)) + mermaid_to_file(MMC1, PROJECT_ROOT / "MMC1") + mermaid_to_file(MMC2, PROJECT_ROOT / "MMC2") From 10ce3ed70245462f50fcb23eeee7708b637d65ad Mon Sep 17 00:00:00 2001 From: "hy.li" Date: Mon, 11 Sep 2023 16:59:41 +0800 Subject: [PATCH 2/7] more options to convert mermaid --- README.md | 69 ++++++++-- config/config.yaml | 2 +- metagpt/utils/mermaid.py | 73 ++++++---- metagpt/utils/mmdc_ink.py | 51 +++++++ ...rmaid_playwright.py => mmdc_playwright.py} | 119 ++++------------ metagpt/utils/mmdc_pyppeteer.py | 129 ++++++++++++++++++ 6 files changed, 312 insertions(+), 131 deletions(-) create mode 100644 metagpt/utils/mmdc_ink.py rename metagpt/utils/{mermaid_playwright.py => mmdc_playwright.py} (63%) create mode 100644 metagpt/utils/mmdc_pyppeteer.py diff --git a/README.md b/README.md index 864d56c53..a380ce1a8 100644 --- a/README.md +++ b/README.md @@ -81,29 +81,68 @@ # Step 3: Clone the repository to your local machine, and install it. - if `python setup.py install` fails with error `[Errno 13] Permission denied: '/usr/local/lib/python3.11/dist-packages/test-easy-install-13129.write-test'`, try instead running `python setup.py install --user` -- To convert Mermaid charts to SVG, PNG, and PDF formats. In addition to the Node.js version of Mermaid-CLI, you now have the option to use Python version Playwright for this task. +- To convert Mermaid charts to SVG, PNG, and PDF formats. In addition to the Node.js version of Mermaid-CLI, you now have the option to use Python version Playwright, pyppeteer or mermaid.ink for this task. -- **Install Playwright** + - Playwright + - **Install Playwright** -```bash -pip install playwright -``` + ```bash + pip install playwright + ``` -- **Install the Required Browsers** + - **Install the Required Browsers** -to support PDF conversion, had better install Chrominum. + to support PDF conversion, had better install Chrominum. -```bash -playwright install --with-deps chromium -``` + ```bash + playwright install --with-deps chromium + ``` -- **modify `config.yaml`** + - **modify `config.yaml`** -uncomment MERMAID_ENGINE from config.yaml and change it to `playwright` + uncomment MERMAID_ENGINE from config.yaml and change it to `playwright` -```yaml -MERMAID_ENGINE: playwright -``` + ```yaml + MERMAID_ENGINE: playwright + ``` + + - pyppeteer + - **Install pyppeteer** + + ```bash + pip install pyppeteer + ``` + + - **Install the Required Browsers** + + ```bash + pyppeteer-install + ``` + + pyppeteer alow you use already installed browsers, if you do not want to run the above command, please set the following envirment + + ```bash + export PUPPETEER_EXECUTABLE_PATH = /path/to/your/chromium or edge or chrome + ``` + + - **modify `config.yaml`** + + uncomment MERMAID_ENGINE from config.yaml and change it to `pyppeteer` + + ```yaml + MERMAID_ENGINE: pyppeteer + ``` + + - mermaid.ink + - **modify `config.yaml`** + + uncomment MERMAID_ENGINE from config.yaml and change it to `ink` + + ```yaml + MERMAID_ENGINE: ink + ``` + + Note: this method does not support pdf export. ### Installation by Docker diff --git a/config/config.yaml b/config/config.yaml index 40d37451a..179985a6f 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -78,5 +78,5 @@ MODEL_FOR_RESEARCHER_SUMMARY: gpt-3.5-turbo MODEL_FOR_RESEARCHER_REPORT: gpt-3.5-turbo-16k ### choose the engine for mermaid conversion, -# default is nodejs, you can change it to playwright +# default is nodejs, you can change it to playwright,pyppeteer or ink # MERMAID_ENGINE: nodejs \ No newline at end of file diff --git a/metagpt/utils/mermaid.py b/metagpt/utils/mermaid.py index f395b43b2..713f49601 100644 --- a/metagpt/utils/mermaid.py +++ b/metagpt/utils/mermaid.py @@ -12,6 +12,7 @@ from metagpt.config import CONFIG from metagpt.const import PROJECT_ROOT from metagpt.logs import logger from metagpt.utils.common import check_cmd_exists +import os @@ -31,34 +32,58 @@ def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height if check_cmd_exists("mmdc") != 0: logger.warning("RUN `npm install -g @mermaid-js/mermaid-cli` to install mmdc") return -1 + engine = CONFIG.mermaid_engine.lower() - for suffix in ["pdf", "svg", "png"]: - output_file = f"{output_file_without_suffix}.{suffix}" - # Call the `mmdc` command to convert the Mermaid code to a PNG - logger.info(f"Generating {output_file}..") + if engine == "nodejs": + for suffix in ["pdf", "svg", "png"]: + output_file = f"{output_file_without_suffix}.{suffix}" + # Call the `mmdc` command to convert the Mermaid code to a PNG + logger.info(f"Generating {output_file}..") - if CONFIG.puppeteer_config: - subprocess.run( - [ - CONFIG.mmdc, - "-p", - CONFIG.puppeteer_config, - "-i", - str(tmp), - "-o", - output_file, - "-w", - str(width), - "-H", - str(height), - ] - ) - else: - subprocess.run([CONFIG.mmdc, "-i", str(tmp), "-o", output_file, "-w", str(width), "-H", str(height)]) + if CONFIG.puppeteer_config: + subprocess.run( + [ + CONFIG.mmdc, + "-p", + CONFIG.puppeteer_config, + "-i", + str(tmp), + "-o", + output_file, + "-w", + str(width), + "-H", + str(height), + ] + ) + else: + subprocess.run([CONFIG.mmdc, "-i", str(tmp), "-o", output_file, "-w", str(width), "-H", str(height)]) + else: + if engine not in ['playwright', 'puppeteer', 'ink']: + logger.warning(f"Unsupported mermaid engine: {engine}") + return -1 + __dirname = os.path.dirname(os.path.abspath(__file__)) + module_path = os.path.join(__dirname, f'mmdc_{engine}.py') + import sys + # 构建命令行参数 + command = [ + sys.executable, + module_path, + "-i",mermaid_code, + "-o",output_file_without_suffix + ] + + # 执行命令 + try: + result = subprocess.run(command, text=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + logger.info(result.stdout) + if result.stderr: + logger.error(result.stderr) + except subprocess.CalledProcessError as e: + logger.error(f"Command execution failed with return code {e.returncode}") + logger.error(e.output) return 0 -if CONFIG.mermaid_engine.lower() == "playwright": - from metagpt.utils.mermaid_playwright import mermaid_to_file MMC1 = """classDiagram class Main { diff --git a/metagpt/utils/mmdc_ink.py b/metagpt/utils/mmdc_ink.py new file mode 100644 index 000000000..ce50b11cd --- /dev/null +++ b/metagpt/utils/mmdc_ink.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/7/4 10:53 +@Author : alexanderwu, imjohndoe +@File : mermaid.py +""" + +import requests +import base64 +import os + +import click +@click.command() +@click.version_option() +@click.option("-i","--mermaid_code", type=str, help="mermaid code") +@click.option("-o","--output_file_without_suffix", type=str, help="output filename without suffix") +def mermaid_to_file(mermaid_code, output_file_without_suffix): + """suffix: png/svg + :param mermaid_code: mermaid code + :param output_file_without_suffix: output filename without suffix + :return: 0 if succeed, -1 if failed + """ + print('Starting mermaid_to_file command of mermaid.ink...') + + encoded_string = base64.b64encode(mermaid_code.encode()).decode() + + dir_name = os.path.dirname(output_file_without_suffix) + if dir_name and not os.path.exists(dir_name): + os.makedirs(dir_name) + with open(f"{output_file_without_suffix}.mmd", "w", encoding="utf-8") as f: + f.write(mermaid_code) + + for suffix in ["svg", "png"]: + output_file = f"{output_file_without_suffix}.{suffix}" + path_type = "svg" if suffix == "svg" else "img" + url = f"https://mermaid.ink/{path_type}/{encoded_string}" + response = requests.get(url) + + if response.status_code == 200: + with open(output_file, 'wb') as f: + f.write(response.content) + print(f"Generating {output_file}..") + else: + print(f"Failed to retrieve {suffix}") + return -1 + + return 0 + +if __name__ == "__main__": + mermaid_to_file() \ No newline at end of file diff --git a/metagpt/utils/mermaid_playwright.py b/metagpt/utils/mmdc_playwright.py similarity index 63% rename from metagpt/utils/mermaid_playwright.py rename to metagpt/utils/mmdc_playwright.py index aa04e70eb..d5d6b898e 100644 --- a/metagpt/utils/mermaid_playwright.py +++ b/metagpt/utils/mmdc_playwright.py @@ -3,22 +3,24 @@ """ @Time : 2023/9/4 16:12 @Author : Steven Lee -@File : mermaid_playwright.py +@File : mmdc_playwright.py """ import os import asyncio -from metagpt.config import CONFIG -from metagpt.const import PROJECT_ROOT -from metagpt.logs import logger - +import click from urllib.parse import urljoin + from playwright.async_api import async_playwright -import nest_asyncio -__dirname = os.path.dirname(os.path.abspath(__file__)) +@click.command() +@click.version_option() +@click.option("-i","--mermaid_code", type=str, help="mermaid code") +@click.option("-o","--output_file_without_suffix", type=str, help="output filename without suffix") +@click.option("--width",type=int,help="width",default=2048) +@click.option("--height",type=int,help="height",default=2048) +def mermaid_to_file(mermaid_code, output_file_without_suffix, width, height): - -def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height=2048, output_formats=['png', 'svg', 'pdf']) -> int: + """ Converts the given Mermaid code to various output formats and saves them to files. @@ -27,18 +29,18 @@ def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height output_file_without_suffix (str): The output file name without the file extension. width (int, optional): The width of the output image in pixels. Defaults to 2048. height (int, optional): The height of the output image in pixels. Defaults to 2048. - output_formats (list[str], optional): The list of output formats to generate. Defaults to ['png', 'svg', 'pdf']. Returns: int: Returns 1 if the conversion and saving were successful, -1 otherwise. """ - async def mermaid_to_file0(mermaid_code, output_file_without_suffix, width=2048, height=2048, output_formats=['png', 'svg', 'pdf'])-> int: + __dirname = os.path.dirname(os.path.abspath(__file__)) + + async def mermaid_to_file0(mermaid_code, output_file_without_suffix, width=2048, height=2048, suffixes=['png', 'svg', 'pdf'])-> int: async with async_playwright() as p: browser = await p.chromium.launch() device_scale_factor = 1.0 - context = await browser.new_context( viewport={'width': width, 'height': height}, device_scale_factor=device_scale_factor, @@ -79,37 +81,19 @@ def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height svgElement.appendChild(style); } - let title = null; - let desc = null; - - if (svgElement.firstChild instanceof SVGTitleElement) { - title = svgElement.firstChild.textContent; - } - - for (const svgNode of svgElement.children) { - if (svgNode instanceof SVGDescElement) { - desc = svgNode.textContent; - break; - } - } - - return { - title, - desc - }; }''', [mermaid_code, mermaid_config, my_css, background_color]) - if 'svg' in output_formats : + if 'svg' in suffixes : svg_xml = await page.evaluate('''() => { const svg = document.querySelector('svg'); const xmlSerializer = new XMLSerializer(); return xmlSerializer.serializeToString(svg); }''') - # result[f'{output_file_without_suffix}.svg'] = svg_xml + print(f"Generating {output_file_without_suffix}.svg..") with open(f'{output_file_without_suffix}.svg', 'wb') as f: f.write(svg_xml.encode('utf-8')) - if 'png' in output_formats: + if 'png' in suffixes: clip = await page.evaluate('''() => { const svg = document.querySelector('svg'); const rect = svg.getBoundingClientRect(); @@ -122,78 +106,31 @@ def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height }''') await page.set_viewport_size({'width': clip['x'] + clip['width'], 'height': clip['y'] + clip['height']}) screenshot = await page.screenshot(clip=clip, omit_background=True, scale='device') + print(f"Generating {output_file_without_suffix}.png..") with open(f'{output_file_without_suffix}.png', 'wb') as f: f.write(screenshot) - if 'pdf' in output_formats: + if 'pdf' in suffixes: pdf_data = await page.pdf(scale=device_scale_factor) + print(f"Generating {output_file_without_suffix}.pdf..") with open(f'{output_file_without_suffix}.pdf', 'wb') as f: f.write(pdf_data) return 1 except Exception as e: - logger.error(e) + print(e) return -1 finally: await browser.close() + dir_name = os.path.dirname(output_file_without_suffix) + if dir_name and not os.path.exists(dir_name): + os.makedirs(dir_name) with open(f"{output_file_without_suffix}.mmd", "w", encoding="utf-8") as f: f.write(mermaid_code) - nest_asyncio.apply() + suffixes = ['png', 'svg', 'pdf'] loop = asyncio.new_event_loop() - result = loop.run_until_complete(mermaid_to_file0(mermaid_code, output_file_without_suffix, width, height, output_formats)) + result = loop.run_until_complete(mermaid_to_file0(mermaid_code, output_file_without_suffix, width, height, suffixes)) loop.close() return result -MMC1 = """classDiagram - class Main { - -SearchEngine search_engine - +main() str - } - class SearchEngine { - -Index index - -Ranking ranking - -Summary summary - +search(query: str) str - } - class Index { - -KnowledgeBase knowledge_base - +create_index(data: dict) - +query_index(query: str) list - } - class Ranking { - +rank_results(results: list) list - } - class Summary { - +summarize_results(results: list) str - } - class KnowledgeBase { - +update(data: dict) - +fetch_data(query: str) dict - } - Main --> SearchEngine - SearchEngine --> Index - SearchEngine --> Ranking - SearchEngine --> Summary - Index --> KnowledgeBase""" - -MMC2 = """sequenceDiagram - participant M as Main - participant SE as SearchEngine - participant I as Index - participant R as Ranking - participant S as Summary - participant KB as KnowledgeBase - M->>SE: search(query) - SE->>I: query_index(query) - I->>KB: fetch_data(query) - KB-->>I: return data - I-->>SE: return results - SE->>R: rank_results(results) - R-->>SE: return ranked_results - SE->>S: summarize_results(ranked_results) - S-->>SE: return summary - SE-->>M: return summary""" - - if __name__ == "__main__": - # logger.info(print_members(print_members)) - mermaid_to_file(MMC1, PROJECT_ROOT / "MMC1") - mermaid_to_file(MMC2, PROJECT_ROOT / "MMC2") + mermaid_to_file() + diff --git a/metagpt/utils/mmdc_pyppeteer.py b/metagpt/utils/mmdc_pyppeteer.py new file mode 100644 index 000000000..e6986bc76 --- /dev/null +++ b/metagpt/utils/mmdc_pyppeteer.py @@ -0,0 +1,129 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/9/4 16:12 +@Author : Steven Lee +@File : mmdc_pyppeteer.py +""" +import asyncio +import click +import os +from urllib.parse import urljoin +import sys +from pyppeteer import launch + +@click.command() +@click.version_option() +@click.option("-i","--mermaid_code", type=str, help="mermaid code") +@click.option("-o","--output_file_without_suffix", type=str, help="output filename without suffix") +@click.option("--width",type=int,help="width",default=2048) +@click.option("--height",type=int,help="height",default=2048) +def mermaid_to_file(mermaid_code, output_file_without_suffix, width, height): + """ + Converts the given Mermaid code to various output formats and saves them to files. + + Args: + mermaid_code (str): The Mermaid code to convert. + output_file_without_suffix (str): The output file name without the file extension. + width (int, optional): The width of the output image in pixels. Defaults to 2048. + height (int, optional): The height of the output image in pixels. Defaults to 2048. + + Returns: + int: Returns 1 if the conversion and saving were successful, -1 otherwise. + """ + + async def mermaid_to_file0(mermaid_code, output_file_without_suffix, width=2048, height=2048, suffixes=['png', 'svg', 'pdf'])-> int: + __dirname = os.path.dirname(os.path.abspath(__file__)) + browser = await launch(headless=True, + executablePath=os.getenv('PUPPETEER_EXECUTABLE_PATH',"/opt/homebrew/bin/chromium"), + args=['--disable-extensions',"--no-sandbox"] + ) + page = await browser.newPage() + device_scale_factor = 1.0 + + async def console_message(msg): + print(msg.text) + page.on('console', console_message) + + try: + await page.setViewport(viewport={'width': width, 'height': height, 'deviceScaleFactor': device_scale_factor}) + + mermaid_html_path = os.path.abspath( + os.path.join(__dirname, 'index.html')) + mermaid_html_url = urljoin('file:', mermaid_html_path) + await page.goto(mermaid_html_url) + + await page.querySelector("div#container") + mermaid_config = {} + background_color = "#ffffff" + my_css = "" + await page.evaluate(f'document.body.style.background = "{background_color}";') + + metadata = await page.evaluate('''async ([definition, mermaidConfig, myCSS, backgroundColor]) => { + const { mermaid, zenuml } = globalThis; + await mermaid.registerExternalDiagrams([zenuml]); + mermaid.initialize({ startOnLoad: false, ...mermaidConfig }); + const { svg } = await mermaid.render('my-svg', definition, document.getElementById('container')); + document.getElementById('container').innerHTML = svg; + const svgElement = document.querySelector('svg'); + svgElement.style.backgroundColor = backgroundColor; + + if (myCSS) { + const style = document.createElementNS('http://www.w3.org/2000/svg', 'style'); + style.appendChild(document.createTextNode(myCSS)); + svgElement.appendChild(style); + } + }''', [mermaid_code, mermaid_config, my_css, background_color]) + + if 'svg' in suffixes : + svg_xml = await page.evaluate('''() => { + const svg = document.querySelector('svg'); + const xmlSerializer = new XMLSerializer(); + return xmlSerializer.serializeToString(svg); + }''') + print(f"Generating {output_file_without_suffix}.svg..") + with open(f'{output_file_without_suffix}.svg', 'wb') as f: + f.write(svg_xml.encode('utf-8')) + + if 'png' in suffixes: + clip = await page.evaluate('''() => { + const svg = document.querySelector('svg'); + const rect = svg.getBoundingClientRect(); + return { + x: Math.floor(rect.left), + y: Math.floor(rect.top), + width: Math.ceil(rect.width), + height: Math.ceil(rect.height) + }; + }''') + await page.setViewport({'width': clip['x'] + clip['width'], 'height': clip['y'] + clip['height'], 'deviceScaleFactor': device_scale_factor}) + screenshot = await page.screenshot(clip=clip, omit_background=True, scale='device') + print(f"Generating {output_file_without_suffix}.png..") + with open(f'{output_file_without_suffix}.png', 'wb') as f: + f.write(screenshot) + if 'pdf' in suffixes: + pdf_data = await page.pdf(scale=device_scale_factor) + print(f"Generating {output_file_without_suffix}.pdf..") + with open(f'{output_file_without_suffix}.pdf', 'wb') as f: + f.write(pdf_data) + return 1 + except Exception as e: + print(e) + return -1 + finally: + await browser.close() + + + suffixes = ['png', 'svg', 'pdf'] + dir_name = os.path.dirname(output_file_without_suffix) + if dir_name and not os.path.exists(dir_name): + os.makedirs(dir_name) + with open(f"{output_file_without_suffix}.mmd", "w", encoding="utf-8") as f: + f.write(mermaid_code) + loop = asyncio.new_event_loop() + result = loop.run_until_complete(mermaid_to_file0(mermaid_code, output_file_without_suffix, width, height,suffixes)) + loop.close() + return result + +if __name__ == "__main__": + mermaid_to_file() From a3d1c3362f81980b45915d784ad808481338d7d1 Mon Sep 17 00:00:00 2001 From: Steven Lee Date: Tue, 12 Sep 2023 07:23:07 +0000 Subject: [PATCH 3/7] bug fix --- metagpt/utils/mermaid.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/metagpt/utils/mermaid.py b/metagpt/utils/mermaid.py index 713f49601..b13199a93 100644 --- a/metagpt/utils/mermaid.py +++ b/metagpt/utils/mermaid.py @@ -13,8 +13,7 @@ from metagpt.const import PROJECT_ROOT from metagpt.logs import logger from metagpt.utils.common import check_cmd_exists import os - - +import sys def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height=2048) -> int: """suffix: png/svg/pdf @@ -28,13 +27,12 @@ def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height # Write the Mermaid code to a temporary file tmp = Path(f"{output_file_without_suffix}.mmd") tmp.write_text(mermaid_code, encoding="utf-8") - - if check_cmd_exists("mmdc") != 0: - logger.warning("RUN `npm install -g @mermaid-js/mermaid-cli` to install mmdc") - return -1 engine = CONFIG.mermaid_engine.lower() - if engine == "nodejs": + if check_cmd_exists("mmdc") != 0: + logger.warning("RUN `npm install -g @mermaid-js/mermaid-cli` to install mmdc") + return -1 + for suffix in ["pdf", "svg", "png"]: output_file = f"{output_file_without_suffix}.{suffix}" # Call the `mmdc` command to convert the Mermaid code to a PNG @@ -59,12 +57,12 @@ def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height else: subprocess.run([CONFIG.mmdc, "-i", str(tmp), "-o", output_file, "-w", str(width), "-H", str(height)]) else: - if engine not in ['playwright', 'puppeteer', 'ink']: + if engine not in ['playwright', 'pyppeteer', 'ink']: logger.warning(f"Unsupported mermaid engine: {engine}") return -1 __dirname = os.path.dirname(os.path.abspath(__file__)) module_path = os.path.join(__dirname, f'mmdc_{engine}.py') - import sys + # 构建命令行参数 command = [ sys.executable, From 95391ca32d2a5c2381b6eb20ff6bafbb33e41b58 Mon Sep 17 00:00:00 2001 From: Steven Lee Date: Tue, 12 Sep 2023 08:44:50 +0000 Subject: [PATCH 4/7] bug fix --- README.md | 16 +++++++++------- metagpt/utils/common.py | 1 + metagpt/utils/mmdc_pyppeteer.py | 13 +++++++++---- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 5faf5d9de..5e67483d6 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ # Step 3: Clone the repository to your local machine, and install it. - **Install the Required Browsers** - to support PDF conversion, had better install Chrominum. + to support PDF conversion, please install Chrominum. ```bash playwright install --with-deps chromium @@ -114,18 +114,20 @@ # Step 3: Clone the repository to your local machine, and install it. pip install pyppeteer ``` - - **Install the Required Browsers** + - **Use your own Browsers** - ```bash - pyppeteer-install - ``` - - pyppeteer alow you use already installed browsers, if you do not want to run the above command, please set the following envirment + pyppeteer alow you use installed browsers, please set the following envirment ```bash export PUPPETEER_EXECUTABLE_PATH = /path/to/your/chromium or edge or chrome ``` + please do not use this command to install browser, it is too old + + ```bash + pyppeteer-install + ``` + - **modify `config.yaml`** uncomment MERMAID_ENGINE from config.yaml and change it to `pyppeteer` diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index 99038dc64..5f94de066 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -192,6 +192,7 @@ class CodeParser: logger.error(f"{pattern} not match following text:") logger.error(text) # raise Exception + return "" return code @classmethod diff --git a/metagpt/utils/mmdc_pyppeteer.py b/metagpt/utils/mmdc_pyppeteer.py index e6986bc76..f3e00d053 100644 --- a/metagpt/utils/mmdc_pyppeteer.py +++ b/metagpt/utils/mmdc_pyppeteer.py @@ -34,10 +34,15 @@ def mermaid_to_file(mermaid_code, output_file_without_suffix, width, height): async def mermaid_to_file0(mermaid_code, output_file_without_suffix, width=2048, height=2048, suffixes=['png', 'svg', 'pdf'])-> int: __dirname = os.path.dirname(os.path.abspath(__file__)) - browser = await launch(headless=True, - executablePath=os.getenv('PUPPETEER_EXECUTABLE_PATH',"/opt/homebrew/bin/chromium"), - args=['--disable-extensions',"--no-sandbox"] - ) + executablePath = os.getenv('PUPPETEER_EXECUTABLE_PATH',"") + if executablePath: + browser = await launch(headless=True, + executablePath=executablePath, + args=['--disable-extensions',"--no-sandbox"] + ) + else: + print("Please set the environment variable:PUPPETEER_EXECUTABLE_PATH.") + return -1 page = await browser.newPage() device_scale_factor = 1.0 From 69ea116d1a265b4b9c0e112832392d5decd7aab1 Mon Sep 17 00:00:00 2001 From: "hy.li" Date: Wed, 13 Sep 2023 12:41:38 +0800 Subject: [PATCH 5/7] change to async --- metagpt/actions/design_api.py | 18 +-- metagpt/utils/mermaid.py | 71 ++++++------ metagpt/utils/mmdc_ink.py | 52 ++++----- metagpt/utils/mmdc_playwright.py | 179 +++++++++++++----------------- metagpt/utils/mmdc_pyppeteer.py | 182 ++++++++++++++----------------- 5 files changed, 225 insertions(+), 277 deletions(-) diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index abd1f9d4c..4d17e4f5e 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -103,23 +103,23 @@ class WriteDesign(Action): pass # Folder does not exist, but we don't care workspace.mkdir(parents=True, exist_ok=True) - def _save_prd(self, docs_path, resources_path, prd): + async def _save_prd(self, docs_path, resources_path, prd): prd_file = docs_path / 'prd.md' quadrant_chart = CodeParser.parse_code(block="Competitive Quadrant Chart", text=prd) - mermaid_to_file(quadrant_chart, resources_path / 'competitive_analysis') + await mermaid_to_file(quadrant_chart, resources_path / 'competitive_analysis') logger.info(f"Saving PRD to {prd_file}") prd_file.write_text(prd) - def _save_system_design(self, docs_path, resources_path, content): + async def _save_system_design(self, docs_path, resources_path, content): data_api_design = CodeParser.parse_code(block="Data structures and interface definitions", text=content) seq_flow = CodeParser.parse_code(block="Program call flow", text=content) - mermaid_to_file(data_api_design, resources_path / 'data_api_design') - mermaid_to_file(seq_flow, resources_path / 'seq_flow') + await mermaid_to_file(data_api_design, resources_path / 'data_api_design') + await mermaid_to_file(seq_flow, resources_path / 'seq_flow') system_design_file = docs_path / 'system_design.md' logger.info(f"Saving System Designs to {system_design_file}") system_design_file.write_text(content) - def _save(self, context, system_design): + async def _save(self, context, system_design): if isinstance(system_design, ActionOutput): content = system_design.content ws_name = CodeParser.parse_str(block="Python package name", text=content) @@ -132,13 +132,13 @@ class WriteDesign(Action): resources_path = workspace / 'resources' docs_path.mkdir(parents=True, exist_ok=True) resources_path.mkdir(parents=True, exist_ok=True) - self._save_prd(docs_path, resources_path, context[-1].content) - self._save_system_design(docs_path, resources_path, content) + await self._save_prd(docs_path, resources_path, context[-1].content) + await self._save_system_design(docs_path, resources_path, content) async def run(self, context): prompt = PROMPT_TEMPLATE.format(context=context, format_example=FORMAT_EXAMPLE) # system_design = await self._aask(prompt) system_design = await self._aask_v1(prompt, "system_design", OUTPUT_MAPPING) - self._save(context, system_design) + await self._save(context, system_design) return system_design \ No newline at end of file diff --git a/metagpt/utils/mermaid.py b/metagpt/utils/mermaid.py index b13199a93..d2cce3965 100644 --- a/metagpt/utils/mermaid.py +++ b/metagpt/utils/mermaid.py @@ -2,9 +2,10 @@ # -*- coding: utf-8 -*- """ @Time : 2023/7/4 10:53 -@Author : alexanderwu +@Author : alexanderwu alitrack @File : mermaid.py """ +import asyncio import subprocess from pathlib import Path @@ -15,18 +16,22 @@ from metagpt.utils.common import check_cmd_exists import os import sys -def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height=2048) -> int: +async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height=2048) -> int: """suffix: png/svg/pdf :param mermaid_code: mermaid code :param output_file_without_suffix: output filename :param width: :param height: - :return: 0 if succed, -1 if failed + :return: 0 if succeed, -1 if failed """ # Write the Mermaid code to a temporary file + dir_name = os.path.dirname(output_file_without_suffix) + if dir_name and not os.path.exists(dir_name): + os.makedirs(dir_name) tmp = Path(f"{output_file_without_suffix}.mmd") tmp.write_text(mermaid_code, encoding="utf-8") + engine = CONFIG.mermaid_engine.lower() if engine == "nodejs": if check_cmd_exists("mmdc") != 0: @@ -39,8 +44,7 @@ def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height logger.info(f"Generating {output_file}..") if CONFIG.puppeteer_config: - subprocess.run( - [ + commands =[ CONFIG.mmdc, "-p", CONFIG.puppeteer_config, @@ -53,33 +57,32 @@ def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height "-H", str(height), ] - ) else: - subprocess.run([CONFIG.mmdc, "-i", str(tmp), "-o", output_file, "-w", str(width), "-H", str(height)]) - else: - if engine not in ['playwright', 'pyppeteer', 'ink']: - logger.warning(f"Unsupported mermaid engine: {engine}") - return -1 - __dirname = os.path.dirname(os.path.abspath(__file__)) - module_path = os.path.join(__dirname, f'mmdc_{engine}.py') - - # 构建命令行参数 - command = [ - sys.executable, - module_path, - "-i",mermaid_code, - "-o",output_file_without_suffix - ] + commands =[CONFIG.mmdc, "-i", str(tmp), "-o", output_file, "-w", str(width), "-H", str(height)] + process = await asyncio.create_subprocess_exec( + *commands, + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE + ) - # 执行命令 - try: - result = subprocess.run(command, text=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - logger.info(result.stdout) - if result.stderr: - logger.error(result.stderr) - except subprocess.CalledProcessError as e: - logger.error(f"Command execution failed with return code {e.returncode}") - logger.error(e.output) + stdout, stderr = await process.communicate() + if stdout: + logger.info(stdout.decode()) + if stderr: + logger.error(stderr.decode()) + else: + + if engine =='playwright': + from metagpt.utils.mmdc_playwright import mermaid_to_file + return await mermaid_to_file(mermaid_code, output_file_without_suffix, width, height) + elif engine =='pyppeteer': + from metagpt.utils.mmdc_pyppeteer import mermaid_to_file + return await mermaid_to_file(mermaid_code, output_file_without_suffix, width, height) + elif engine =='ink': + from metagpt.utils.mmdc_ink import mermaid_to_file + return await mermaid_to_file(mermaid_code, output_file_without_suffix) + else: + logger.warning(f"Unsupported mermaid engine: {engine}") return 0 @@ -134,7 +137,9 @@ MMC2 = """sequenceDiagram SE-->>M: return summary""" + if __name__ == "__main__": - # logger.info(print_members(print_members)) - mermaid_to_file(MMC1, PROJECT_ROOT / "tmp/1.png") - mermaid_to_file(MMC2, PROJECT_ROOT / "tmp/2.png") + loop = asyncio.new_event_loop() + result = loop.run_until_complete(mermaid_to_file(MMC1, PROJECT_ROOT / f"{CONFIG.mermaid_engine}/1")) + result = loop.run_until_complete(mermaid_to_file(MMC2, PROJECT_ROOT / f"{CONFIG.mermaid_engine}/1")) + loop.close() diff --git a/metagpt/utils/mmdc_ink.py b/metagpt/utils/mmdc_ink.py index ce50b11cd..3d91cde9d 100644 --- a/metagpt/utils/mmdc_ink.py +++ b/metagpt/utils/mmdc_ink.py @@ -1,51 +1,41 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- """ -@Time : 2023/7/4 10:53 -@Author : alexanderwu, imjohndoe +@Time : 2023/9/4 16:12 +@Author : alitrack @File : mermaid.py """ - -import requests import base64 import os -import click -@click.command() -@click.version_option() -@click.option("-i","--mermaid_code", type=str, help="mermaid code") -@click.option("-o","--output_file_without_suffix", type=str, help="output filename without suffix") -def mermaid_to_file(mermaid_code, output_file_without_suffix): +from aiohttp import ClientSession,ClientError +from metagpt.logs import logger + + +async def mermaid_to_file(mermaid_code, output_file_without_suffix): """suffix: png/svg :param mermaid_code: mermaid code :param output_file_without_suffix: output filename without suffix :return: 0 if succeed, -1 if failed """ - print('Starting mermaid_to_file command of mermaid.ink...') - encoded_string = base64.b64encode(mermaid_code.encode()).decode() - dir_name = os.path.dirname(output_file_without_suffix) - if dir_name and not os.path.exists(dir_name): - os.makedirs(dir_name) - with open(f"{output_file_without_suffix}.mmd", "w", encoding="utf-8") as f: - f.write(mermaid_code) - for suffix in ["svg", "png"]: output_file = f"{output_file_without_suffix}.{suffix}" path_type = "svg" if suffix == "svg" else "img" url = f"https://mermaid.ink/{path_type}/{encoded_string}" - response = requests.get(url) - - if response.status_code == 200: - with open(output_file, 'wb') as f: - f.write(response.content) - print(f"Generating {output_file}..") - else: - print(f"Failed to retrieve {suffix}") - return -1 - + async with ClientSession() as session: + try: + async with session.get(url) as response: + if response.status == 200: + text = await response.content.read() + with open(output_file, 'wb') as f: + f.write(text) + logger.info(f"Generating {output_file}..") + else: + logger.error(f"Failed to generate {output_file}") + return -1 + except ClientError as e: + logger.error(f"network error: {e}") + return -1 return 0 - -if __name__ == "__main__": - mermaid_to_file() \ No newline at end of file diff --git a/metagpt/utils/mmdc_playwright.py b/metagpt/utils/mmdc_playwright.py index d5d6b898e..bdbfd82ff 100644 --- a/metagpt/utils/mmdc_playwright.py +++ b/metagpt/utils/mmdc_playwright.py @@ -5,22 +5,13 @@ @Author : Steven Lee @File : mmdc_playwright.py """ + import os -import asyncio -import click from urllib.parse import urljoin - from playwright.async_api import async_playwright +from metagpt.logs import logger -@click.command() -@click.version_option() -@click.option("-i","--mermaid_code", type=str, help="mermaid code") -@click.option("-o","--output_file_without_suffix", type=str, help="output filename without suffix") -@click.option("--width",type=int,help="width",default=2048) -@click.option("--height",type=int,help="height",default=2048) -def mermaid_to_file(mermaid_code, output_file_without_suffix, width, height): - - +async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height=2048)-> int: """ Converts the given Mermaid code to various output formats and saves them to files. @@ -33,104 +24,88 @@ def mermaid_to_file(mermaid_code, output_file_without_suffix, width, height): Returns: int: Returns 1 if the conversion and saving were successful, -1 otherwise. """ - + suffixes=['png', 'svg', 'pdf'] __dirname = os.path.dirname(os.path.abspath(__file__)) - async def mermaid_to_file0(mermaid_code, output_file_without_suffix, width=2048, height=2048, suffixes=['png', 'svg', 'pdf'])-> int: + async with async_playwright() as p: + browser = await p.chromium.launch() + device_scale_factor = 1.0 + context = await browser.new_context( + viewport={'width': width, 'height': height}, + device_scale_factor=device_scale_factor, + ) + page = await context.new_page() - async with async_playwright() as p: - browser = await p.chromium.launch() - device_scale_factor = 1.0 - context = await browser.new_context( - viewport={'width': width, 'height': height}, - device_scale_factor=device_scale_factor, - ) - page = await context.new_page() + async def console_message(msg): + logger.info(msg.text) + page.on('console', console_message) - async def console_message(msg): - print(msg.text) - page.on('console', console_message) + try: + await page.set_viewport_size({'width': width, 'height': height}) - try: - await page.set_viewport_size({'width': width, 'height': height}) + mermaid_html_path = os.path.abspath( + os.path.join(__dirname, 'index.html')) + mermaid_html_url = urljoin('file:', mermaid_html_path) + await page.goto(mermaid_html_url) + await page.wait_for_load_state("networkidle") - mermaid_html_path = os.path.abspath( - os.path.join(__dirname, 'index.html')) - mermaid_html_url = urljoin('file:', mermaid_html_path) - await page.goto(mermaid_html_url) - await page.wait_for_load_state("networkidle") + await page.wait_for_selector("div#container", state="attached") + mermaid_config = {} + background_color = "#ffffff" + my_css = "" + await page.evaluate(f'document.body.style.background = "{background_color}";') - await page.wait_for_selector("div#container", state="attached") - mermaid_config = {} - background_color = "#ffffff" - my_css = "" - await page.evaluate(f'document.body.style.background = "{background_color}";') + metadata = await page.evaluate('''async ([definition, mermaidConfig, myCSS, backgroundColor]) => { + const { mermaid, zenuml } = globalThis; + await mermaid.registerExternalDiagrams([zenuml]); + mermaid.initialize({ startOnLoad: false, ...mermaidConfig }); + const { svg } = await mermaid.render('my-svg', definition, document.getElementById('container')); + document.getElementById('container').innerHTML = svg; + const svgElement = document.querySelector('svg'); + svgElement.style.backgroundColor = backgroundColor; - metadata = await page.evaluate('''async ([definition, mermaidConfig, myCSS, backgroundColor]) => { - const { mermaid, zenuml } = globalThis; - await mermaid.registerExternalDiagrams([zenuml]); - mermaid.initialize({ startOnLoad: false, ...mermaidConfig }); - const { svg } = await mermaid.render('my-svg', definition, document.getElementById('container')); - document.getElementById('container').innerHTML = svg; - const svgElement = document.querySelector('svg'); - svgElement.style.backgroundColor = backgroundColor; + if (myCSS) { + const style = document.createElementNS('http://www.w3.org/2000/svg', 'style'); + style.appendChild(document.createTextNode(myCSS)); + svgElement.appendChild(style); + } - if (myCSS) { - const style = document.createElementNS('http://www.w3.org/2000/svg', 'style'); - style.appendChild(document.createTextNode(myCSS)); - svgElement.appendChild(style); - } + }''', [mermaid_code, mermaid_config, my_css, background_color]) - }''', [mermaid_code, mermaid_config, my_css, background_color]) - - if 'svg' in suffixes : - svg_xml = await page.evaluate('''() => { - const svg = document.querySelector('svg'); - const xmlSerializer = new XMLSerializer(); - return xmlSerializer.serializeToString(svg); - }''') - print(f"Generating {output_file_without_suffix}.svg..") - with open(f'{output_file_without_suffix}.svg', 'wb') as f: - f.write(svg_xml.encode('utf-8')) - - if 'png' in suffixes: - clip = await page.evaluate('''() => { - const svg = document.querySelector('svg'); - const rect = svg.getBoundingClientRect(); - return { - x: Math.floor(rect.left), - y: Math.floor(rect.top), - width: Math.ceil(rect.width), - height: Math.ceil(rect.height) - }; - }''') - await page.set_viewport_size({'width': clip['x'] + clip['width'], 'height': clip['y'] + clip['height']}) - screenshot = await page.screenshot(clip=clip, omit_background=True, scale='device') - print(f"Generating {output_file_without_suffix}.png..") - with open(f'{output_file_without_suffix}.png', 'wb') as f: - f.write(screenshot) - if 'pdf' in suffixes: - pdf_data = await page.pdf(scale=device_scale_factor) - print(f"Generating {output_file_without_suffix}.pdf..") - with open(f'{output_file_without_suffix}.pdf', 'wb') as f: - f.write(pdf_data) - return 1 - except Exception as e: - print(e) - return -1 - finally: - await browser.close() - dir_name = os.path.dirname(output_file_without_suffix) - if dir_name and not os.path.exists(dir_name): - os.makedirs(dir_name) - with open(f"{output_file_without_suffix}.mmd", "w", encoding="utf-8") as f: - f.write(mermaid_code) - suffixes = ['png', 'svg', 'pdf'] - loop = asyncio.new_event_loop() - result = loop.run_until_complete(mermaid_to_file0(mermaid_code, output_file_without_suffix, width, height, suffixes)) - loop.close() - return result - -if __name__ == "__main__": - mermaid_to_file() + if 'svg' in suffixes : + svg_xml = await page.evaluate('''() => { + const svg = document.querySelector('svg'); + const xmlSerializer = new XMLSerializer(); + return xmlSerializer.serializeToString(svg); + }''') + logger.info(f"Generating {output_file_without_suffix}.svg..") + with open(f'{output_file_without_suffix}.svg', 'wb') as f: + f.write(svg_xml.encode('utf-8')) + if 'png' in suffixes: + clip = await page.evaluate('''() => { + const svg = document.querySelector('svg'); + const rect = svg.getBoundingClientRect(); + return { + x: Math.floor(rect.left), + y: Math.floor(rect.top), + width: Math.ceil(rect.width), + height: Math.ceil(rect.height) + }; + }''') + await page.set_viewport_size({'width': clip['x'] + clip['width'], 'height': clip['y'] + clip['height']}) + screenshot = await page.screenshot(clip=clip, omit_background=True, scale='device') + logger.info(f"Generating {output_file_without_suffix}.png..") + with open(f'{output_file_without_suffix}.png', 'wb') as f: + f.write(screenshot) + if 'pdf' in suffixes: + pdf_data = await page.pdf(scale=device_scale_factor) + logger.info(f"Generating {output_file_without_suffix}.pdf..") + with open(f'{output_file_without_suffix}.pdf', 'wb') as f: + f.write(pdf_data) + return 0 + except Exception as e: + logger.error(e) + return -1 + finally: + await browser.close() diff --git a/metagpt/utils/mmdc_pyppeteer.py b/metagpt/utils/mmdc_pyppeteer.py index f3e00d053..56367236f 100644 --- a/metagpt/utils/mmdc_pyppeteer.py +++ b/metagpt/utils/mmdc_pyppeteer.py @@ -2,23 +2,15 @@ # -*- coding: utf-8 -*- """ @Time : 2023/9/4 16:12 -@Author : Steven Lee +@Author : alitrack @File : mmdc_pyppeteer.py """ -import asyncio -import click import os from urllib.parse import urljoin -import sys from pyppeteer import launch +from metagpt.logs import logger -@click.command() -@click.version_option() -@click.option("-i","--mermaid_code", type=str, help="mermaid code") -@click.option("-o","--output_file_without_suffix", type=str, help="output filename without suffix") -@click.option("--width",type=int,help="width",default=2048) -@click.option("--height",type=int,help="height",default=2048) -def mermaid_to_file(mermaid_code, output_file_without_suffix, width, height): +async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height=2048)-> int: """ Converts the given Mermaid code to various output formats and saves them to files. @@ -31,104 +23,90 @@ def mermaid_to_file(mermaid_code, output_file_without_suffix, width, height): Returns: int: Returns 1 if the conversion and saving were successful, -1 otherwise. """ + suffixes = ['png', 'svg', 'pdf'] + __dirname = os.path.dirname(os.path.abspath(__file__)) - async def mermaid_to_file0(mermaid_code, output_file_without_suffix, width=2048, height=2048, suffixes=['png', 'svg', 'pdf'])-> int: - __dirname = os.path.dirname(os.path.abspath(__file__)) - executablePath = os.getenv('PUPPETEER_EXECUTABLE_PATH',"") - if executablePath: - browser = await launch(headless=True, - executablePath=executablePath, - args=['--disable-extensions',"--no-sandbox"] - ) - else: - print("Please set the environment variable:PUPPETEER_EXECUTABLE_PATH.") - return -1 - page = await browser.newPage() - device_scale_factor = 1.0 + executablePath = os.getenv('PUPPETEER_EXECUTABLE_PATH',"") + if executablePath: + browser = await launch(headless=True, + executablePath=executablePath, + args=['--disable-extensions',"--no-sandbox"] + ) + else: + logger.error("Please set the environment variable:PUPPETEER_EXECUTABLE_PATH.") + return -1 + page = await browser.newPage() + device_scale_factor = 1.0 - async def console_message(msg): - print(msg.text) - page.on('console', console_message) + async def console_message(msg): + logger.info(msg.text) + page.on('console', console_message) - try: - await page.setViewport(viewport={'width': width, 'height': height, 'deviceScaleFactor': device_scale_factor}) + try: + await page.setViewport(viewport={'width': width, 'height': height, 'deviceScaleFactor': device_scale_factor}) - mermaid_html_path = os.path.abspath( - os.path.join(__dirname, 'index.html')) - mermaid_html_url = urljoin('file:', mermaid_html_path) - await page.goto(mermaid_html_url) + mermaid_html_path = os.path.abspath( + os.path.join(__dirname, 'index.html')) + mermaid_html_url = urljoin('file:', mermaid_html_path) + await page.goto(mermaid_html_url) - await page.querySelector("div#container") - mermaid_config = {} - background_color = "#ffffff" - my_css = "" - await page.evaluate(f'document.body.style.background = "{background_color}";') + await page.querySelector("div#container") + mermaid_config = {} + background_color = "#ffffff" + my_css = "" + await page.evaluate(f'document.body.style.background = "{background_color}";') - metadata = await page.evaluate('''async ([definition, mermaidConfig, myCSS, backgroundColor]) => { - const { mermaid, zenuml } = globalThis; - await mermaid.registerExternalDiagrams([zenuml]); - mermaid.initialize({ startOnLoad: false, ...mermaidConfig }); - const { svg } = await mermaid.render('my-svg', definition, document.getElementById('container')); - document.getElementById('container').innerHTML = svg; - const svgElement = document.querySelector('svg'); - svgElement.style.backgroundColor = backgroundColor; + metadata = await page.evaluate('''async ([definition, mermaidConfig, myCSS, backgroundColor]) => { + const { mermaid, zenuml } = globalThis; + await mermaid.registerExternalDiagrams([zenuml]); + mermaid.initialize({ startOnLoad: false, ...mermaidConfig }); + const { svg } = await mermaid.render('my-svg', definition, document.getElementById('container')); + document.getElementById('container').innerHTML = svg; + const svgElement = document.querySelector('svg'); + svgElement.style.backgroundColor = backgroundColor; - if (myCSS) { - const style = document.createElementNS('http://www.w3.org/2000/svg', 'style'); - style.appendChild(document.createTextNode(myCSS)); - svgElement.appendChild(style); - } - }''', [mermaid_code, mermaid_config, my_css, background_color]) + if (myCSS) { + const style = document.createElementNS('http://www.w3.org/2000/svg', 'style'); + style.appendChild(document.createTextNode(myCSS)); + svgElement.appendChild(style); + } + }''', [mermaid_code, mermaid_config, my_css, background_color]) - if 'svg' in suffixes : - svg_xml = await page.evaluate('''() => { - const svg = document.querySelector('svg'); - const xmlSerializer = new XMLSerializer(); - return xmlSerializer.serializeToString(svg); - }''') - print(f"Generating {output_file_without_suffix}.svg..") - with open(f'{output_file_without_suffix}.svg', 'wb') as f: - f.write(svg_xml.encode('utf-8')) + if 'svg' in suffixes : + svg_xml = await page.evaluate('''() => { + const svg = document.querySelector('svg'); + const xmlSerializer = new XMLSerializer(); + return xmlSerializer.serializeToString(svg); + }''') + logger.info(f"Generating {output_file_without_suffix}.svg..") + with open(f'{output_file_without_suffix}.svg', 'wb') as f: + f.write(svg_xml.encode('utf-8')) - if 'png' in suffixes: - clip = await page.evaluate('''() => { - const svg = document.querySelector('svg'); - const rect = svg.getBoundingClientRect(); - return { - x: Math.floor(rect.left), - y: Math.floor(rect.top), - width: Math.ceil(rect.width), - height: Math.ceil(rect.height) - }; - }''') - await page.setViewport({'width': clip['x'] + clip['width'], 'height': clip['y'] + clip['height'], 'deviceScaleFactor': device_scale_factor}) - screenshot = await page.screenshot(clip=clip, omit_background=True, scale='device') - print(f"Generating {output_file_without_suffix}.png..") - with open(f'{output_file_without_suffix}.png', 'wb') as f: - f.write(screenshot) - if 'pdf' in suffixes: - pdf_data = await page.pdf(scale=device_scale_factor) - print(f"Generating {output_file_without_suffix}.pdf..") - with open(f'{output_file_without_suffix}.pdf', 'wb') as f: - f.write(pdf_data) - return 1 - except Exception as e: - print(e) - return -1 - finally: - await browser.close() - + if 'png' in suffixes: + clip = await page.evaluate('''() => { + const svg = document.querySelector('svg'); + const rect = svg.getBoundingClientRect(); + return { + x: Math.floor(rect.left), + y: Math.floor(rect.top), + width: Math.ceil(rect.width), + height: Math.ceil(rect.height) + }; + }''') + await page.setViewport({'width': clip['x'] + clip['width'], 'height': clip['y'] + clip['height'], 'deviceScaleFactor': device_scale_factor}) + screenshot = await page.screenshot(clip=clip, omit_background=True, scale='device') + logger.info(f"Generating {output_file_without_suffix}.png..") + with open(f'{output_file_without_suffix}.png', 'wb') as f: + f.write(screenshot) + if 'pdf' in suffixes: + pdf_data = await page.pdf(scale=device_scale_factor) + logger.info(f"Generating {output_file_without_suffix}.pdf..") + with open(f'{output_file_without_suffix}.pdf', 'wb') as f: + f.write(pdf_data) + return 0 + except Exception as e: + logger.error(e) + return -1 + finally: + await browser.close() - suffixes = ['png', 'svg', 'pdf'] - dir_name = os.path.dirname(output_file_without_suffix) - if dir_name and not os.path.exists(dir_name): - os.makedirs(dir_name) - with open(f"{output_file_without_suffix}.mmd", "w", encoding="utf-8") as f: - f.write(mermaid_code) - loop = asyncio.new_event_loop() - result = loop.run_until_complete(mermaid_to_file0(mermaid_code, output_file_without_suffix, width, height,suffixes)) - loop.close() - return result - -if __name__ == "__main__": - mermaid_to_file() From e947ce5fea6d543141eb3146ef534609ef124886 Mon Sep 17 00:00:00 2001 From: "hy.li" Date: Wed, 13 Sep 2023 14:39:51 +0800 Subject: [PATCH 6/7] use config for PYPPETEER_EXECUTABLE_PATH --- config/config.yaml | 5 ++++- metagpt/config.py | 1 + metagpt/utils/mmdc_pyppeteer.py | 9 +++++---- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/config/config.yaml b/config/config.yaml index 179985a6f..93301fcf2 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -79,4 +79,7 @@ MODEL_FOR_RESEARCHER_REPORT: gpt-3.5-turbo-16k ### choose the engine for mermaid conversion, # default is nodejs, you can change it to playwright,pyppeteer or ink -# MERMAID_ENGINE: nodejs \ No newline at end of file +# MERMAID_ENGINE: nodejs + +### browser path for pyppeteer engine, support Chrome, Chromium,MS Edge +#PYPPETEER_EXECUTABLE_PATH: "/usr/bin/google-chrome-stable" \ No newline at end of file diff --git a/metagpt/config.py b/metagpt/config.py index 9260ae605..b4e0fe7fa 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -84,6 +84,7 @@ class Config(metaclass=Singleton): self.model_for_researcher_summary = self._get("MODEL_FOR_RESEARCHER_SUMMARY") self.model_for_researcher_report = self._get("MODEL_FOR_RESEARCHER_REPORT") self.mermaid_engine = self._get("MERMAID_ENGINE", 'nodejs') + self.pyppeteer_executable_path = self._get("PYPPETEER_EXECUTABLE_PATH", '') def _init_with_config_files_and_env(self, configs: dict, yaml_file): """Load from config/key.yaml, config/config.yaml, and env in decreasing order of priority""" diff --git a/metagpt/utils/mmdc_pyppeteer.py b/metagpt/utils/mmdc_pyppeteer.py index 56367236f..7ec30fd12 100644 --- a/metagpt/utils/mmdc_pyppeteer.py +++ b/metagpt/utils/mmdc_pyppeteer.py @@ -9,6 +9,7 @@ import os from urllib.parse import urljoin from pyppeteer import launch from metagpt.logs import logger +from metagpt.config import CONFIG async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height=2048)-> int: """ @@ -26,14 +27,14 @@ async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, suffixes = ['png', 'svg', 'pdf'] __dirname = os.path.dirname(os.path.abspath(__file__)) - executablePath = os.getenv('PUPPETEER_EXECUTABLE_PATH',"") - if executablePath: + + if CONFIG.pyppeteer_executable_path: browser = await launch(headless=True, - executablePath=executablePath, + executablePath=CONFIG.pyppeteer_executable_path, args=['--disable-extensions',"--no-sandbox"] ) else: - logger.error("Please set the environment variable:PUPPETEER_EXECUTABLE_PATH.") + logger.error("Please set the environment variable:PYPPETEER_EXECUTABLE_PATH.") return -1 page = await browser.newPage() device_scale_factor = 1.0 From 53d5a1e86efa827bedfe45a50b885a6af9798a0e Mon Sep 17 00:00:00 2001 From: "hy.li" Date: Fri, 15 Sep 2023 13:03:21 +0800 Subject: [PATCH 7/7] extras_require: pyppeteer --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index a88f9de92..f9ae768e6 100644 --- a/setup.py +++ b/setup.py @@ -47,6 +47,7 @@ setup( "selenium": ["selenium>4", "webdriver_manager", "beautifulsoup4"], "search-google": ["google-api-python-client==2.94.0"], "search-ddg": ["duckduckgo-search==3.8.5"], + "pyppeteer": ["pyppeteer>=1.0.2"], }, cmdclass={ "install_mermaid": InstallMermaidCLI,