|
| 1 | +from importlib.metadata import version |
1 | 2 | from pathlib import Path
|
2 | 3 |
|
3 | 4 | import gradio as gr
|
| 5 | +import requests |
| 6 | +from theflow.settings import settings |
| 7 | + |
| 8 | + |
| 9 | +def get_remote_doc(url): |
| 10 | + try: |
| 11 | + res = requests.get(url) |
| 12 | + return res.text |
| 13 | + except Exception as e: |
| 14 | + print(f"Failed to fetch document from {url}: {e}") |
| 15 | + return "" |
| 16 | + |
| 17 | + |
| 18 | +def get_changelogs(version): |
| 19 | + release_url = f"https://api.github.com/repos/Cinnamon/kotaemon/releases/{version}" |
| 20 | + try: |
| 21 | + res = requests.get(release_url).json() |
| 22 | + changelogs = res.get("body", "") |
| 23 | + |
| 24 | + return changelogs |
| 25 | + except Exception as e: |
| 26 | + print(f"Failed to fetch changelogs from {release_url}: {e}") |
| 27 | + return "" |
4 | 28 |
|
5 | 29 |
|
6 | 30 | class HelpPage:
|
7 | 31 | def __init__(self, app):
|
8 | 32 | self._app = app
|
9 |
| - self.md_dir = Path(__file__).parent.parent / "assets" / "md" |
10 |
| - self.doc_dir = Path(__file__).parents[4] / "docs" |
| 33 | + self.doc_dir = Path(settings.KH_DOC_DIR) |
| 34 | + self.remote_content_url = "https://raw.githubusercontent.com/Cinnamon/kotaemon" |
11 | 35 |
|
12 |
| - with gr.Accordion("About"): |
13 |
| - with (self.md_dir / "about.md").open(encoding="utf-8") as fi: |
14 |
| - gr.Markdown(fi.read()) |
| 36 | + self.app_version = None |
| 37 | + try: |
| 38 | + # Caution: This might produce the wrong version |
| 39 | + # https://stackoverflow.com/a/59533071 |
| 40 | + self.app_version = version("kotaemon_app") |
| 41 | + except Exception as e: |
| 42 | + print(f"Failed to get app version: {e}") |
15 | 43 |
|
16 |
| - with gr.Accordion("User Guide"): |
17 |
| - with (self.doc_dir / "usage.md").open(encoding="utf-8") as fi: |
18 |
| - gr.Markdown(fi.read()) |
| 44 | + about_md_dir = self.doc_dir / "about.md" |
| 45 | + if about_md_dir.exists(): |
| 46 | + with (self.doc_dir / "about.md").open(encoding="utf-8") as fi: |
| 47 | + about_md = fi.read() |
| 48 | + else: # fetch from remote |
| 49 | + about_md = get_remote_doc( |
| 50 | + f"{self.remote_content_url}/v{self.app_version}/docs/about.md" |
| 51 | + ) |
| 52 | + if about_md: |
| 53 | + with gr.Accordion("About"): |
| 54 | + gr.Markdown(about_md) |
19 | 55 |
|
20 |
| - with gr.Accordion("Changelogs"): |
21 |
| - gr.Markdown(self.get_changelogs()) |
| 56 | + user_guide_md_dir = self.doc_dir / "usage.md" |
| 57 | + if user_guide_md_dir.exists(): |
| 58 | + with (self.doc_dir / "usage.md").open(encoding="utf-8") as fi: |
| 59 | + user_guide_md = fi.read() |
| 60 | + else: # fetch from remote |
| 61 | + user_guide_md = get_remote_doc( |
| 62 | + f"{self.remote_content_url}/v{self.app_version}/docs/usage.md" |
| 63 | + ) |
| 64 | + if user_guide_md: |
| 65 | + with gr.Accordion("User Guide"): |
| 66 | + gr.Markdown(user_guide_md) |
22 | 67 |
|
23 |
| - def get_changelogs(self): |
24 |
| - with (self.md_dir / "changelogs.md").open(encoding="utf-8") as fi: |
25 |
| - return fi.read() |
| 68 | + if self.app_version: |
| 69 | + changelogs = get_changelogs("tags/v" + self.app_version) |
| 70 | + if changelogs: |
| 71 | + with gr.Accordion(f"Changelogs (v{self.app_version})"): |
| 72 | + gr.Markdown(changelogs) |
0 commit comments