jpwsutton commited on
Commit
9db5ca1
·
0 Parent(s):

Deployment

Browse files
.gitattributes ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ *.png filter=lfs diff=lfs merge=lfs -text
37
+ *.jpg filter=lfs diff=lfs merge=lfs -text
.github/pull_request_template.md ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!--
2
+ Thank you for your pull request. Please review and complete the sections below.
3
+ -->
4
+
5
+ ### Description
6
+
7
+ <!-- Provide a description of the change or tick the promote to stable box -->
8
+
9
+ - [ ] Promote QA to Stable
10
+
11
+ ### Checklist
12
+
13
+ <!-- For completed items, change [ ] to [x]. -->
14
+
15
+ - [ ] I have installed pre-commit: `pre-commit install`
16
+ - [ ] All pre-commit checks pass: `pre-commit run`
17
+ - [ ] Dependencies are [compatible with ZeroGPU](https://huggingface.co/docs/hub/en/spaces-zerogpu#supported-versions)
.gitignore ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ #uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ #poetry.lock
109
+
110
+ # pdm
111
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
112
+ #pdm.lock
113
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
114
+ # in version control.
115
+ # https://pdm.fming.dev/latest/usage/project/#working-with-version-control
116
+ .pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
121
+ __pypackages__/
122
+
123
+ # Celery stuff
124
+ celerybeat-schedule
125
+ celerybeat.pid
126
+
127
+ # SageMath parsed files
128
+ *.sage.py
129
+
130
+ # Environments
131
+ .env
132
+ .venv
133
+ env/
134
+ venv/
135
+ ENV/
136
+ env.bak/
137
+ venv.bak/
138
+
139
+ # Spyder project settings
140
+ .spyderproject
141
+ .spyproject
142
+
143
+ # Rope project settings
144
+ .ropeproject
145
+
146
+ # mkdocs documentation
147
+ /site
148
+
149
+ # mypy
150
+ .mypy_cache/
151
+ .dmypy.json
152
+ dmypy.json
153
+
154
+ # Pyre type checker
155
+ .pyre/
156
+
157
+ # pytype static type analyzer
158
+ .pytype/
159
+
160
+ # Cython debug symbols
161
+ cython_debug/
162
+
163
+ # PyCharm
164
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
165
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
166
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
167
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
168
+ #.idea/
169
+
170
+
171
+ # Boilerplate specific ignores
172
+ .gradio/
173
+ .ruff_cache/
.gitlint ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Edit this file as you like.
2
+ #
3
+ # All these sections are optional. Each section with the exception of [general] represents
4
+ # one rule and each key in it is an option for that specific rule.
5
+ #
6
+ # Rules and sections can be referenced by their full name or by id. For example
7
+ # section "[body-max-line-length]" could also be written as "[B1]". Full section names are
8
+ # used in here for clarity.
9
+ #
10
+ # [general]
11
+ # Ignore certain rules, this example uses both full name and id
12
+ # ignore=title-trailing-punctuation, T3
13
+
14
+ [general]
15
+ # You HAVE to add the rule here to enable it, only configuring (such as below)
16
+ # does NOT enable it.
17
+ contrib=contrib-title-conventional-commits
18
+
19
+ [contrib-title-conventional-commits]
20
+ # Specify allowed commit types. For details see: https://www.conventionalcommits.org/
21
+ types = build,chore,ci,docs,feat,fix,perf,refactor,revert,style,test
22
+
23
+ # verbosity should be a value between 1 and 3, the commandline -v flags take precedence over this
24
+ # verbosity = 2
25
+
26
+ # By default gitlint will ignore merge, revert, fixup and squash commits.
27
+ # ignore-merge-commits=true
28
+ # ignore-revert-commits=true
29
+ # ignore-fixup-commits=true
30
+ # ignore-squash-commits=true
31
+
32
+ # Ignore any data send to gitlint via stdin
33
+ # ignore-stdin=true
34
+
35
+ # Fetch additional meta-data from the local repository when manually passing a
36
+ # commit message to gitlint via stdin or --commit-msg. Disabled by default.
37
+ # staged=true
38
+
39
+ # Hard fail when the target commit range is empty. Note that gitlint will
40
+ # already fail by default on invalid commit ranges. This option is specifically
41
+ # to tell gitlint to fail on *valid but empty* commit ranges.
42
+ # Disabled by default.
43
+ # fail-without-commits=true
44
+
45
+ # Enable debug mode (prints more output). Disabled by default.
46
+ # debug=true
47
+
48
+ # Enable community contributed rules
49
+ # See http://jorisroovers.github.io/gitlint/contrib_rules for details
50
+ # contrib=contrib-title-conventional-commits,CC1
51
+
52
+ # Set the extra-path where gitlint will search for user defined rules
53
+ # See http://jorisroovers.github.io/gitlint/user_defined_rules for details
54
+ # extra-path=examples/
55
+
56
+ # This is an example of how to configure the "title-max-length" rule and
57
+ # set the line-length it enforces to 50
58
+ # [title-max-length]
59
+ # line-length=50
60
+
61
+ # Conversely, you can also enforce minimal length of a title with the
62
+ # "title-min-length" rule:
63
+ # [title-min-length]
64
+ # min-length=5
65
+
66
+ # [title-must-not-contain-word]
67
+ # Comma-separated list of words that should not occur in the title. Matching is case
68
+ # insensitive. It's fine if the keyword occurs as part of a larger word (so "WIPING"
69
+ # will not cause a violation, but "WIP: my title" will.
70
+ # words=wip
71
+
72
+ # [title-match-regex]
73
+ # python-style regex that the commit-msg title must match
74
+ # Note that the regex can contradict with other rules if not used correctly
75
+ # (e.g. title-must-not-contain-word).
76
+ # regex=^US[0-9]*
77
+
78
+ # [body-max-line-length]
79
+ # line-length=72
80
+
81
+ # [body-min-length]
82
+ # min-length=5
83
+
84
+ # [body-is-missing]
85
+ # Whether to ignore this rule on merge commits (which typically only have a title)
86
+ # default = True
87
+ # ignore-merge-commits=false
88
+
89
+ # [body-changed-file-mention]
90
+ # List of files that need to be explicitly mentioned in the body when they are changed
91
+ # This is useful for when developers often erroneously edit certain files or git submodules.
92
+ # By specifying this rule, developers can only change the file when they explicitly reference
93
+ # it in the commit message.
94
+ # files=gitlint-core/gitlint/rules.py,README.md
95
+
96
+ # [body-match-regex]
97
+ # python-style regex that the commit-msg body must match.
98
+ # E.g. body must end in My-Commit-Tag: foo
99
+ # regex=My-Commit-Tag: foo$
100
+
101
+ # [author-valid-email]
102
+ # python-style regex that the commit author email address must match.
103
+ # For example, use the following regex if you only want to allow email addresses from foo.com
104
+ # regex=[^@]+@foo.com
105
+
106
+ # [ignore-by-title]
107
+ # Ignore certain rules for commits of which the title matches a regex
108
+ # E.g. Match commit titles that start with "Release"
109
+ # regex=^Release(.*)
110
+
111
+ # Ignore certain rules, you can reference them by their id or by their full name
112
+ # Use 'all' to ignore all rules
113
+ # ignore=T1,body-min-length
114
+
115
+ # [ignore-by-body]
116
+ # Ignore certain rules for commits of which the body has a line that matches a regex
117
+ # E.g. Match bodies that have a line that that contain "release"
118
+ # regex=(.*)release(.*)
119
+ #
120
+ # Ignore certain rules, you can reference them by their id or by their full name
121
+ # Use 'all' to ignore all rules
122
+ # ignore=T1,body-min-length
123
+
124
+ # [ignore-body-lines]
125
+ # Ignore certain lines in a commit body that match a regex.
126
+ # E.g. Ignore all lines that start with 'Co-Authored-By'
127
+ # regex=^Co-Authored-By
128
+
129
+ # [ignore-by-author-name]
130
+ # Ignore certain rules for commits of which the author name matches a regex
131
+ # E.g. Match commits made by dependabot
132
+ # regex=(.*)dependabot(.*)
133
+ #
134
+ # Ignore certain rules, you can reference them by their id or by their full name
135
+ # Use 'all' to ignore all rules
136
+ # ignore=T1,body-min-length
137
+
138
+ # This is a contrib rule - a community contributed rule. These are disabled by default.
139
+ # You need to explicitly enable them one-by-one by adding them to the "contrib" option
140
+ # under [general] section above.
141
+ # [contrib-title-conventional-commits]
142
+ # Specify allowed commit types. For details see: https://www.conventionalcommits.org/
143
+ # types = bugfix,user-story,epic
.pre-commit-config.yaml ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ repos:
2
+ - repo: https://github.com/pre-commit/pre-commit-hooks
3
+ rev: v5.0.0
4
+ hooks:
5
+ - id: check-added-large-files
6
+ args: ['--maxkb=850'] # Set custom size limit (1MB) - id: check-ast
7
+ - id: check-case-conflict
8
+ - id: check-json
9
+ - id: check-merge-conflict
10
+ - id: check-toml
11
+ - id: end-of-file-fixer
12
+ - repo: https://github.com/astral-sh/ruff-pre-commit
13
+ # Ruff version.
14
+ rev: v0.11.8
15
+ hooks:
16
+ - id: ruff
17
+ - repo: https://github.com/pycqa/isort
18
+ rev: 6.0.1
19
+ hooks:
20
+ - id: isort
21
+ args: ["--profile", "black"]
22
+ - repo: https://github.com/asottile/pyupgrade
23
+ rev: v3.19.1
24
+ hooks:
25
+ - id: pyupgrade
26
+ args: ["--py310-plus"]
27
+ - repo: https://github.com/psf/black
28
+ rev: 25.1.0
29
+ hooks:
30
+ - id: black
31
+ args:
32
+ - --line-length=120
33
+ - repo: https://github.com/jorisroovers/gitlint
34
+ rev: v0.19.1
35
+ hooks:
36
+ - id: gitlint
37
+ name: gitlint
38
+ language: python
39
+ entry: gitlint
40
+ args: [--staged, --msg-filename]
41
+ stages: [commit-msg]
42
+ - repo: https://github.com/python-poetry/poetry
43
+ rev: '1.8.0'
44
+ hooks:
45
+ - id: poetry-check
46
+ - id: poetry-lock
47
+ args: [--no-update]
48
+ language_version: "3.10"
49
+ - id: poetry-export
50
+ name: poetry export for base requirements
51
+ args: [-f, requirements.txt, -o, requirements.txt, -n, --only=main, --without-hashes]
DEVELOPMENT.md ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # IBM Research Hugging Face Spaces gradio template
2
+
3
+ This template repository lets you quickly build a [gradio](https://www.gradio.app/) Hugging Face spaces demo for the [ibm-granite org](https://huggingface.co/ibm-granite). It is set up with the requirements, theming and analytics for the ibm-granite org as well as pre-commit hooks and linting configuration to maintain a consistent code standard across all demos.
4
+
5
+ ## 👩‍💻 Introduction
6
+
7
+ To deploy demos to the ibm-granite org on Hugging Face, you will be working with the Research Design Technical Experiences (RDTE) team via this GitHub org. You will not gain access to the ibm-granite Hugging Face org as there are limited seats available. Hence, you will work via the RDTE team (who have write access) to create and deploy demos to Hugging Face.
8
+
9
+ ## 🛠️ Getting started
10
+
11
+ This is the place to start when building gradio demos for IBM Granite. Complete the following steps to get a repository set up and configured for your demo as well as the deployment pipeline to validate and push it to Hugging Face spaces.
12
+
13
+ 1. [Raise an onboarding request](https://github.ibm.com/ibm-huggingface-space-demos/deployment/issues/new?assignees=james-sutton,gwhite&labels=onboarding%2Ctriage&projects=&template=onboarding.yaml&title=%5BOnboarding%5D%3A+). Please fill the templated onboarding request to get a new repository set up for you in this org and to give access to anything else required.
14
+ 2. Once your repository has been created, please either update it with your existing demo if you have one, or have a play with the example and modify it to create your new demo. You'll be working in the `main` branch whilst developing your demo. Your `main` branch is linked to the "QA" instance of your demo in the IBM org on Hugging Face.
15
+ 3. Make sure that you follow this development guide and use the pre-configured pre-commit hooks before every commit and push.
16
+ 4. Once you are happy with your demo and want to get it deployed into production on Hugging Face spaces in the ibm-granite org, open a pull request to merge the `main` branch into the `stable` branch. The RDTE team will validate the demo works well both from a technical and UX standpoint. If your demo needs any custom environment variables or secrets, let the RDTE team know and we will contact you directly to get them added to the Space configuration on Hugging Face.
17
+ 5. Once the Pull request has been approved, you can merge it into the `stable` branch. A deployment will then push your changes to Hugging Face spaces where it will build and become available for use. Initially, both the "QA" and "Production" versions of your demo will be marked as private and only visible to members of the ibm-research org (QA) and ibm-granite org (production) that have logged into Hugging Face. The "QA" version will always remain private in the ibm-research org. However, when the RDTE team are happy to publish the demo to stable, they will mark the "Production" version as public in the ibm-granite org.
18
+
19
+ ### Onboarding Process Summary
20
+
21
+ The following diagram explains the onboarding process. Actions that you, the developer, take are shown in darker blue. Actions that we, the RDTE team, take are shown in lighter blue. The lighter blue steps that have darker borders are automations maintained by the RDTE team, these steps require no manual intervention.
22
+
23
+ ```mermaid
24
+ flowchart TD
25
+ 1[Developer opens onboarding ticket in GHE Org]:::developer-->2
26
+ 2{RDTE team review the request}:::rdte--Request returned with comments-->1
27
+ 2--Approved-->3
28
+ 3[Developer is invited to join the ibm-research org on HF]:::rdte-->4
29
+ 4[New git repository created from template and configured]:::rdte-->5
30
+ 5[QA HF space created in IBM org]:::rdte-->6
31
+ 6[Developers push commits to main branch]:::developer-->7
32
+ 7-->6
33
+ 7[Main branch deployed to QA space in ibm-granite org on HF]:::rdteauto-->8
34
+ 8[Developers open/update PR to merge main branch to stable branch]:::developer-->9
35
+ 9{PR review}:::rdte--PR Approved-->10
36
+ 9--Changes requested-->6
37
+ 10{Prod space exists?}:::rdte-- Yes -->12
38
+ 10-- No -->11
39
+ 11[Prod HF space created in ibm-granite org]:::rdte-->12
40
+ 12[Merge PR]:::developer-->13
41
+ 13[Stable branch deployed to prod space in ibm-granite org on HF]:::rdteauto
42
+ classDef rdte fill:#EDF5FF,stroke:#D0E2FF,color:#000;
43
+ classDef rdteauto fill:#EDF5FF,stroke:#78A9FF,color:#000;
44
+ classDef developer fill:#A6C8FF,stroke:#78A9FF,color:#000;
45
+ ```
46
+
47
+ ## 🛠️ Development guide
48
+
49
+ Further information on developing the code in this repository is provided below.
50
+
51
+ ### Clone your code repository
52
+
53
+ Once you have been notified that your code repository has been created in this org, you can clone it to your local machine and start work.
54
+
55
+ If you just want to play with our template, you're welcome to [use it](https://github.ibm.com/new?template_name=gradio-template&template_owner=ibm-huggingface-space-demos) to create a new code repository in another org. Later, for deployment, you wil need to move your code to the repository created in this org.
56
+
57
+ ### Prerequisites
58
+
59
+ Some things you will need to do on your machine before developing.
60
+
61
+ #### Precommit
62
+
63
+ [Precommit](https://pre-commit.com) is a tool that adds git commit hooks. You will need to [install](https://pre-commit.com/#install) it on your machine and then run within your code repository:
64
+
65
+ ```shell
66
+ pre-commit install
67
+ ```
68
+
69
+ You can manually run pre-commit using the following command:
70
+
71
+ ```shell
72
+ # To run against staged files:
73
+ pre-commit run
74
+
75
+ # If you want to run against staged and unstaged files:
76
+ pre-commit run --all-files
77
+ ```
78
+
79
+ It is important to run the pre-commit hooks and fix any files that fail before you commit and push to the repository as the pull request build will fail any PR that does not adhere to them i.e. the RDTE team will only accept your code for deployment to Hugging Face once it has passed all of the pre-commit checks.
80
+
81
+ #### Poetry
82
+
83
+ [Poetry](https://python-poetry.org/) is a tool for Python packaging, dependency and virtual environment management that is used to manage the development of this project. You will need to install Poetry locally. There are several ways to install it including through the package manager of your operating system, however, the easiest way to install is likely using their installer, as follows:
84
+
85
+ ```shell
86
+ curl -sSL https://install.python-poetry.org | python3 -
87
+ ```
88
+
89
+ You can also use `pip` and `pipx` to install poetry, the details of which are at https://python-poetry.org/docs/
90
+
91
+ Once installed, the project is configured and controlled via the `pyproject.toml` file with the current dependency tree stored in `poetry.lock`. You may also [configure poetry](https://python-poetry.org/docs/configuration/) further if you wish but there is no need to do so as the default options are sufficient. You may, however, wish to change some of the options set in this template:
92
+ | Setting | Notes |
93
+ | ------- | ----- |
94
+ | name | **Update this**, to reflect the name of your demo |
95
+ | version | **Update this**, to reflect the current version of your demo |
96
+ | description | **Update this**, to a short description of your demo |
97
+ | authors | **Update this**, to the list of authors of your demo |
98
+
99
+ ## 🛠️ Install and run locally
100
+
101
+ To get set up ready to run the code in development mode:
102
+
103
+ ```shell
104
+ # add the poetry shell and export plugins (you only need to do this once on your machine)
105
+ poetry self add poetry-plugin-shell
106
+ poetry self add poetry-plugin-export
107
+
108
+ # create and activate a python virtual environment
109
+ poetry shell
110
+ poetry install
111
+
112
+ # run the demo locally (for development with automatic reload)
113
+ gradio src/app.py
114
+ ```
115
+
116
+ ## 📝 Documenting your demo
117
+
118
+ If you would like to write some information/documentation about your demo that is intended for developers or other people that might want to run the demo from scratch, please use the [README.md](README.md) file, leaving the Hugging Face Spaces configuration header in place at the top of the file.
119
+
120
+ ### Hugging face spaces configuration settings
121
+
122
+ Hugging Face allow the configuration of spaces demonstrations via the [README.md](README.md) file in the root of the project. There is a [Spaces Configuration Reference](https://huggingface.co/docs/hub/en/spaces-config-reference) guide that you can use to gain an understanding of the configuration options that can be specified here.
123
+
124
+ The template has a set of initial defaults, similar to these:
125
+
126
+ ```
127
+ ---
128
+ title: Granite 3.0 Chat
129
+ colorFrom: blue
130
+ colorTo: indigo
131
+ sdk: gradio
132
+ sdk_version: 5.9.1
133
+ app_file: src/app.py
134
+ pinned: false
135
+ license: apache-2.0
136
+ short_description: Chat with IBM Granite 3.0
137
+ ---
138
+ ```
139
+
140
+ #### Options
141
+
142
+ The default options specified above:
143
+ | Setting | Notes |
144
+ | ------- | ----- |
145
+ | title | **Update this**, keep this short (recommend max 24 chars), this information is displayed in the centre of the demo description card |
146
+ | emoji | Do not update this, our demos will use a consistent emoji character |
147
+ | colorFrom | Do not update this, used in combination with colorTo to colourize the demo description card |
148
+ | colorTo | see colorFrom |
149
+ | sdk | Do not update this, our Gradio demos will always use the "gradio" setting |
150
+ | sdk_version | Update this if necessary for your demo to function, ideally should be set to the latest gradio version |
151
+ | app_file | Update this if necessary for your demo to function, should be set to the path of the main entry point to the demo |
152
+ | license | Do not update this, our demos are to always be apache-2.0 licensed |
153
+ | short_description | **Update this**, should be set to a few words that describe the demo in a little more detail than the title, this information is displayed in the bottom-right of the demo description card |
154
+
155
+ Other available options:
156
+ | Setting | Notes |
157
+ | ------- | ----- |
158
+ | python_version | You may optionally set this, best advice is to use the default Python version if possible (current default is Python 3.10) |
159
+ | suggested_hardware | Do not use this, unlikely to be required as demos run on ZeroGPU |
160
+ | suggested_storage | Do not use this, our demos do not require storage |
161
+ | app_port | Do not use this, not relevant for gradio demos |
162
+ | base_path | Do not use this, use the app_file setting |
163
+ | fullWidth | Do not use this, our demos will use a consistent default width |
164
+ | header | Do not use this, our demos will use a consistent header |
165
+ | models | Do not use this, let their parsing discover these from our code |
166
+ | datasets | Do not use this, let their parsing discover these from our code |
167
+ | tags | Do not use this, we are not tagging our demos |
168
+ | thumbnail | Do not use this, provides a thumbnail for social sharing of demos |
169
+ | pinned | Do not use this, the RDTE team will change this setting if it's deemed necessary |
170
+ | hf_oauth | Do not use this, we are not using OAuth |
171
+ | hf_oauth_scopes | Do not use this, we are not using OAuth |
172
+ | hf_oauth_expiration_minutes | Do not use this, we are not using OAuth |
173
+ | disable_embedding | Do not use this, leave at the default that allows embedding to take place |
174
+ | startup_duration_timeout | Do not use this, leave at the default 30 minutes |
175
+ | custom_headers | Do not use this, we do not need to add any custom HTTP headers |
176
+ | preload_from_hub | Do not use this, specifying this builds the models and data sets into the container image with the goal of making start up times faster due to not needing to download them each time. However, RDTE testing indicates this setting significantly increases the start up time for our relatively small Granite models |
README.md ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: granite-docling-258M demo
3
+ emoji: 📝
4
+ colorFrom: blue
5
+ colorTo: indigo
6
+ sdk: gradio
7
+ sdk_version: 5.16.1
8
+ app_file: src/app.py
9
+ pinned: false
10
+ license: apache-2.0
11
+ ---
data/images/06236926002285.png ADDED

Git LFS Details

  • SHA256: 2a6aee80400e5b6ae61ac0221fa3369a5e25f0a2dd08928cc1716a59082caab2
  • Pointer size: 130 Bytes
  • Size of remote file: 61.3 kB
data/images/2433.jpg ADDED

Git LFS Details

  • SHA256: cb2edffc09e2df662bedcfc69e560665d8e0ee2d5dbbf9e3da38fd22df6e98a6
  • Pointer size: 129 Bytes
  • Size of remote file: 6 kB
data/images/7666.jpg ADDED

Git LFS Details

  • SHA256: cf017ae80879765c3a07cb033ac6ba7e09a8f6c39c78a3be84bd08279e458286
  • Pointer size: 130 Bytes
  • Size of remote file: 77.6 kB
data/images/87664.png ADDED

Git LFS Details

  • SHA256: cd349148c12e8bc5b16944a2208751eb612140d6a7afba9b189991d2d0bbd19f
  • Pointer size: 130 Bytes
  • Size of remote file: 70.2 kB
data/images/ar_page_0.png ADDED

Git LFS Details

  • SHA256: 22dd5e43d683b1fd0b57b844120a2fa9268b059a06a12f33b2d14f44febeb60a
  • Pointer size: 131 Bytes
  • Size of remote file: 620 kB
data/images/code.jpg ADDED

Git LFS Details

  • SHA256: f12a94ce0603cd33057b41ea49aeb76d294149e3591867cb6dd37f75c9c1e6ca
  • Pointer size: 130 Bytes
  • Size of remote file: 73.3 kB
data/images/image-2.jpg ADDED

Git LFS Details

  • SHA256: ddb9c749499e74b7a2753d5e2907485021ec83e8f87627f9b0a909b095d5b057
  • Pointer size: 130 Bytes
  • Size of remote file: 46.4 kB
data/images/japanse_4_ibm.png ADDED

Git LFS Details

  • SHA256: f6b44e73839134fd363dfe35d8cada812e2085cb5b92f1fb20ffff715d0b61ec
  • Pointer size: 131 Bytes
  • Size of remote file: 354 kB
data/images/lake-zurich-switzerland-view-nature-landscapes-7bbda4-1024.jpg ADDED

Git LFS Details

  • SHA256: 54600a354f6e1384821f45a7a7e22e51e918e95ed45d3d2cc963903a0ce09781
  • Pointer size: 131 Bytes
  • Size of remote file: 152 kB
data/images/new_arxiv.png ADDED

Git LFS Details

  • SHA256: 15e72aca956d9e796788eaa4b0debb9ae988ca7a9637ae3b6df1e6ce671d73d0
  • Pointer size: 131 Bytes
  • Size of remote file: 523 kB
data/images/paper_3.png ADDED

Git LFS Details

  • SHA256: bee89438e58beb702aa6940c002d3ff7e5dfd2bae8e697164e718f2170014d6f
  • Pointer size: 131 Bytes
  • Size of remote file: 431 kB
data/images/zh_page_0.png ADDED

Git LFS Details

  • SHA256: ca064a6500cfe11804151c130d8673acf4cd26ebad94c623d18bee80fb8ff265
  • Pointer size: 131 Bytes
  • Size of remote file: 411 kB
poetry.lock ADDED
The diff for this file is too large to render. See raw diff
 
pyproject.toml ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [tool.poetry]
2
+ name = "huggingface-gradio-template"
3
+ version = "0.1.0"
4
+ description = "A boilerplate template for an IBM Granite Huggingface Spaces Gradio Demo"
5
+ authors = ["James Sutton <james.sutton@uk.ibm.com>"]
6
+ license = "Apache-2.0"
7
+ readme = "README.md"
8
+ package-mode = false
9
+
10
+ [tool.poetry.dependencies]
11
+ python = ">=3.10,<3.11"
12
+ gradio = "5.9.1"
13
+ torch = "2.4.0"
14
+ spaces = "0.30.4"
15
+ transformers = "^4.47.1"
16
+ accelerate = "^1.2.1"
17
+ docling_core = "^2.48.1"
18
+
19
+ [tool.poetry.group.dev.dependencies]
20
+ pre-commit = "^4.0.1"
21
+ git-lint = "^0.1.2"
22
+ ruff = "^0.8.3"
23
+ pytest = "^8.3.4"
24
+
25
+
26
+ [build-system]
27
+ requires = ["poetry-core"]
28
+ build-backend = "poetry.core.masonry.api"
29
+
30
+
31
+ [tool.ruff]
32
+ line-length = 120
33
+
34
+ [tool.ruff.lint]
35
+ select = [
36
+ "E", # pycodestyle
37
+ "F", # pyflakes
38
+ "UP", # pyupgrade
39
+ "D", # pydocstyle
40
+ "I", # isort
41
+ "B", # bugbear
42
+ "ANN", # annotations
43
+ "N", # pep8-naming
44
+ "C4", # Comprehensions
45
+ "DTZ", # DatetimeZ
46
+ "Q", # Quotes
47
+ "SIM", # Simplify
48
+ "RUF", # Ruff
49
+ ]
50
+ ignore = ["D203", "D213"]
51
+ fixable = ["ALL"]
52
+ unfixable = []
53
+
54
+
55
+ [tool.black]
56
+ line-length = 120
57
+
58
+ [tool.ruff.lint.pydocstyle]
59
+ convention = "google"
requirements.txt ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ accelerate==1.2.1 ; python_version >= "3.10" and python_version < "3.11"
2
+ aiofiles==23.2.1 ; python_version >= "3.10" and python_version < "3.11"
3
+ annotated-types==0.7.0 ; python_version >= "3.10" and python_version < "3.11"
4
+ anyio==4.8.0 ; python_version >= "3.10" and python_version < "3.11"
5
+ attrs==25.3.0 ; python_version >= "3.10" and python_version < "3.11"
6
+ certifi==2024.12.14 ; python_version >= "3.10" and python_version < "3.11"
7
+ charset-normalizer==3.4.1 ; python_version >= "3.10" and python_version < "3.11"
8
+ click==8.1.8 ; python_version >= "3.10" and python_version < "3.11"
9
+ colorama==0.4.6 ; python_version >= "3.10" and python_version < "3.11" and platform_system == "Windows"
10
+ docling-core==2.48.1 ; python_version >= "3.10" and python_version < "3.11"
11
+ exceptiongroup==1.2.2 ; python_version >= "3.10" and python_version < "3.11"
12
+ fastapi==0.115.6 ; python_version >= "3.10" and python_version < "3.11"
13
+ ffmpy==0.5.0 ; python_version >= "3.10" and python_version < "3.11"
14
+ filelock==3.16.1 ; python_version >= "3.10" and python_version < "3.11"
15
+ fsspec==2024.12.0 ; python_version >= "3.10" and python_version < "3.11"
16
+ gradio-client==1.5.2 ; python_version >= "3.10" and python_version < "3.11"
17
+ gradio==5.9.1 ; python_version >= "3.10" and python_version < "3.11"
18
+ h11==0.14.0 ; python_version >= "3.10" and python_version < "3.11"
19
+ httpcore==1.0.7 ; python_version >= "3.10" and python_version < "3.11"
20
+ httpx==0.28.1 ; python_version >= "3.10" and python_version < "3.11"
21
+ huggingface-hub==0.27.1 ; python_version >= "3.10" and python_version < "3.11"
22
+ idna==3.10 ; python_version >= "3.10" and python_version < "3.11"
23
+ jinja2==3.1.5 ; python_version >= "3.10" and python_version < "3.11"
24
+ jsonref==1.1.0 ; python_version >= "3.10" and python_version < "3.11"
25
+ jsonschema-specifications==2025.9.1 ; python_version >= "3.10" and python_version < "3.11"
26
+ jsonschema==4.25.1 ; python_version >= "3.10" and python_version < "3.11"
27
+ latex2mathml==3.78.1 ; python_version >= "3.10" and python_version < "3.11"
28
+ markdown-it-py==3.0.0 ; python_version >= "3.10" and python_version < "3.11"
29
+ markupsafe==2.1.5 ; python_version >= "3.10" and python_version < "3.11"
30
+ mdurl==0.1.2 ; python_version >= "3.10" and python_version < "3.11"
31
+ mpmath==1.3.0 ; python_version >= "3.10" and python_version < "3.11"
32
+ networkx==3.4.2 ; python_version >= "3.10" and python_version < "3.11"
33
+ numpy==2.2.1 ; python_version >= "3.10" and python_version < "3.11"
34
+ nvidia-cublas-cu12==12.1.3.1 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.10" and python_version < "3.11"
35
+ nvidia-cuda-cupti-cu12==12.1.105 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.10" and python_version < "3.11"
36
+ nvidia-cuda-nvrtc-cu12==12.1.105 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.10" and python_version < "3.11"
37
+ nvidia-cuda-runtime-cu12==12.1.105 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.10" and python_version < "3.11"
38
+ nvidia-cudnn-cu12==9.1.0.70 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.10" and python_version < "3.11"
39
+ nvidia-cufft-cu12==11.0.2.54 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.10" and python_version < "3.11"
40
+ nvidia-curand-cu12==10.3.2.106 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.10" and python_version < "3.11"
41
+ nvidia-cusolver-cu12==11.4.5.107 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.10" and python_version < "3.11"
42
+ nvidia-cusparse-cu12==12.1.0.106 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.10" and python_version < "3.11"
43
+ nvidia-nccl-cu12==2.20.5 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.10" and python_version < "3.11"
44
+ nvidia-nvjitlink-cu12==12.6.85 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.10" and python_version < "3.11"
45
+ nvidia-nvtx-cu12==12.1.105 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.10" and python_version < "3.11"
46
+ orjson==3.10.13 ; python_version >= "3.10" and python_version < "3.11"
47
+ packaging==24.2 ; python_version >= "3.10" and python_version < "3.11"
48
+ pandas==2.2.3 ; python_version >= "3.10" and python_version < "3.11"
49
+ pillow==11.1.0 ; python_version >= "3.10" and python_version < "3.11"
50
+ psutil==5.9.8 ; python_version >= "3.10" and python_version < "3.11"
51
+ pydantic-core==2.27.2 ; python_version >= "3.10" and python_version < "3.11"
52
+ pydantic==2.10.4 ; python_version >= "3.10" and python_version < "3.11"
53
+ pydub==0.25.1 ; python_version >= "3.10" and python_version < "3.11"
54
+ pygments==2.19.1 ; python_version >= "3.10" and python_version < "3.11"
55
+ python-dateutil==2.9.0.post0 ; python_version >= "3.10" and python_version < "3.11"
56
+ python-multipart==0.0.20 ; python_version >= "3.10" and python_version < "3.11"
57
+ pytz==2024.2 ; python_version >= "3.10" and python_version < "3.11"
58
+ pyyaml==6.0.2 ; python_version >= "3.10" and python_version < "3.11"
59
+ referencing==0.36.2 ; python_version >= "3.10" and python_version < "3.11"
60
+ regex==2024.11.6 ; python_version >= "3.10" and python_version < "3.11"
61
+ requests==2.32.3 ; python_version >= "3.10" and python_version < "3.11"
62
+ rich==13.9.4 ; python_version >= "3.10" and python_version < "3.11"
63
+ rpds-py==0.27.1 ; python_version >= "3.10" and python_version < "3.11"
64
+ ruff==0.8.6 ; python_version >= "3.10" and python_version < "3.11" and sys_platform != "emscripten"
65
+ safehttpx==0.1.6 ; python_version >= "3.10" and python_version < "3.11"
66
+ safetensors==0.5.1 ; python_version >= "3.10" and python_version < "3.11"
67
+ semantic-version==2.10.0 ; python_version >= "3.10" and python_version < "3.11"
68
+ shellingham==1.5.4 ; python_version >= "3.10" and python_version < "3.11"
69
+ six==1.17.0 ; python_version >= "3.10" and python_version < "3.11"
70
+ sniffio==1.3.1 ; python_version >= "3.10" and python_version < "3.11"
71
+ spaces==0.30.4 ; python_version >= "3.10" and python_version < "3.11"
72
+ starlette==0.41.3 ; python_version >= "3.10" and python_version < "3.11"
73
+ sympy==1.13.3 ; python_version >= "3.10" and python_version < "3.11"
74
+ tabulate==0.9.0 ; python_version >= "3.10" and python_version < "3.11"
75
+ tokenizers==0.21.0 ; python_version >= "3.10" and python_version < "3.11"
76
+ tomlkit==0.13.2 ; python_version >= "3.10" and python_version < "3.11"
77
+ torch==2.4.0 ; python_version >= "3.10" and python_version < "3.11"
78
+ tqdm==4.67.1 ; python_version >= "3.10" and python_version < "3.11"
79
+ transformers==4.47.1 ; python_version >= "3.10" and python_version < "3.11"
80
+ triton==3.0.0 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version < "3.11" and python_version >= "3.10"
81
+ typer==0.15.1 ; python_version >= "3.10" and python_version < "3.11"
82
+ typing-extensions==4.12.2 ; python_version >= "3.10" and python_version < "3.11"
83
+ tzdata==2024.2 ; python_version >= "3.10" and python_version < "3.11"
84
+ urllib3==2.3.0 ; python_version >= "3.10" and python_version < "3.11"
85
+ uvicorn==0.34.0 ; python_version >= "3.10" and python_version < "3.11" and sys_platform != "emscripten"
86
+ websockets==14.1 ; python_version >= "3.10" and python_version < "3.11"
src/app.css ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ footer {
2
+ display: none !important;
3
+ }
4
+ .pdf_viewer .top-panel{
5
+ display: none !important;
6
+ }
7
+ .chatbot_view .top-panel {
8
+ display: none !important;
9
+ }
10
+
11
+ .jumping-dots span {
12
+ position: relative;
13
+ bottom: 0px;
14
+ animation: jump 2s infinite;
15
+ }
16
+ .jumping-dots .dot-1{
17
+ animation-delay: 200ms;
18
+ }
19
+ .jumping-dots .dot-2{
20
+ animation-delay: 400ms;
21
+ }
22
+ .jumping-dots .dot-3{
23
+ animation-delay: 600ms;
24
+ }
25
+
26
+ @keyframes jump {
27
+ 0% {bottom: 0px;}
28
+ 20% {bottom: 5px;}
29
+ 40% {bottom: 0px;}
30
+ }
31
+
32
+ .inactive_div {
33
+ pointer-events: none;
34
+ opacity: .5;
35
+ }
36
+
37
+ /*.gallery-container .grid-container {*/
38
+ /* width: 50px;*/
39
+ /*}*/
40
+
41
+ .upload_button {
42
+ margin-top: 10px;
43
+ width: 100%;
44
+ }
45
+
46
+ .upload_button button {
47
+ background-color: #007bff !important;
48
+ color: white !important;
49
+ border: none !important;
50
+ border-radius: 8px !important;
51
+ padding: 12px 20px !important;
52
+ font-size: 14px !important;
53
+ font-weight: 500 !important;
54
+ transition: background-color 0.2s ease !important;
55
+ }
56
+
57
+ .upload_button button:hover {
58
+ background-color: #0056b3 !important;
59
+ }
60
+
61
+ /* Gallery hover effect - targeting Gradio Gallery structure */
62
+ .preview_im_element .gallery-item {
63
+ position: relative !important;
64
+ overflow: hidden !important;
65
+ transition: all 0.3s ease !important;
66
+ }
67
+
68
+ .preview_im_element .gallery-item:hover {
69
+ transform: scale(1.05) !important;
70
+ z-index: 10 !important;
71
+ }
72
+
73
+ /* Semi-transparent grey overlay on hover */
74
+ .preview_im_element .gallery-item::before {
75
+ content: '' !important;
76
+ position: absolute !important;
77
+ top: 0 !important;
78
+ left: 0 !important;
79
+ right: 0 !important;
80
+ bottom: 0 !important;
81
+ background: rgba(128, 128, 128, 0.8) !important;
82
+ opacity: 0 !important;
83
+ transition: opacity 0.3s ease !important;
84
+ z-index: 1 !important;
85
+ pointer-events: none !important;
86
+ }
87
+
88
+ .preview_im_element .gallery-item:hover::before {
89
+ opacity: 1 !important;
90
+ }
91
+
92
+ /* Hide all default captions and buttons */
93
+ .preview_im_element .gallery-item .caption,
94
+ .preview_im_element .gallery-item button,
95
+ .preview_im_element .gallery-item [role="button"] {
96
+ display: none !important;
97
+ opacity: 0 !important;
98
+ visibility: hidden !important;
99
+ }
100
+
101
+ /* Style our custom hover text */
102
+ .preview_im_element .gallery-item .custom-hover-text {
103
+ position: absolute !important;
104
+ top: 50% !important;
105
+ left: 50% !important;
106
+ transform: translate(-50%, -50%) !important;
107
+ color: white !important;
108
+ font-size: 13px !important;
109
+ font-weight: 600 !important;
110
+ text-align: center !important;
111
+ opacity: 0 !important;
112
+ transition: opacity 0.3s ease !important;
113
+ z-index: 2 !important;
114
+ pointer-events: none !important;
115
+ max-width: 85% !important;
116
+ line-height: 1.4 !important;
117
+ background: rgba(0, 0, 0, 0.7) !important;
118
+ padding: 8px 12px !important;
119
+ border-radius: 6px !important;
120
+ text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.8) !important;
121
+ box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3) !important;
122
+ text-transform: uppercase !important;
123
+ letter-spacing: 0.5px !important;
124
+ border: none !important;
125
+ outline: none !important;
126
+ display: block !important;
127
+ }
128
+
129
+ .preview_im_element .gallery-item:hover .custom-hover-text {
130
+ opacity: 1 !important;
131
+ }
src/app.py ADDED
@@ -0,0 +1,598 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Template Demo for IBM Granite Hugging Face spaces."""
2
+
3
+ import html
4
+ import os
5
+ import random
6
+ import re
7
+ import time
8
+ from pathlib import Path
9
+ from threading import Thread
10
+
11
+ import gradio as gr
12
+ import numpy as np
13
+ import spaces
14
+ import torch
15
+ from docling_core.types.doc import DoclingDocument
16
+ from docling_core.types.doc.document import DocTagsDocument
17
+ from PIL import Image, ImageDraw, ImageOps
18
+ from transformers import (
19
+ AutoProcessor,
20
+ Idefics3ForConditionalGeneration,
21
+ TextIteratorStreamer,
22
+ )
23
+
24
+ from themes.research_monochrome import theme
25
+
26
+ dir_ = Path(__file__).parent.parent
27
+
28
+ TITLE = "Granite-docling-258m demo"
29
+
30
+ DESCRIPTION = """
31
+ <p>This experimental demo highlights the capabilities of granite-docling-258M for document conversion,
32
+ showcasing Granite Docling's various features. Explore the sample document excerpts and try the sample
33
+ prompts or enter your own. Keep in mind that AI can occasionally make mistakes.</p>
34
+ """
35
+
36
+ device = torch.device("cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu")
37
+
38
+ SAMPLES_PATH = dir_ / "data" / "images"
39
+
40
+ sample_data = [
41
+ {
42
+ "preview_image": str(SAMPLES_PATH / "new_arxiv.png"),
43
+ "prompts": [
44
+ "Convert this page to docling.",
45
+ "Does the document contain tables?",
46
+ "Can you extract the 2nd section header?",
47
+ "What element is located at <loc_84><loc_403><loc_238><loc_419>",
48
+ "How can effective temperature be computed?",
49
+ "Extract all picture elements on the page.",
50
+ ],
51
+ "image": str(SAMPLES_PATH / "new_arxiv.png"),
52
+ "name": "Doc Conversion",
53
+ "pad": False,
54
+ },
55
+ {
56
+ "preview_image": str(SAMPLES_PATH / "image-2.jpg"),
57
+ "prompts": ["Convert this table to OTSL.", "What is the Net income in 2008?"],
58
+ "image": str(SAMPLES_PATH / "image-2.jpg"),
59
+ "name": "Table Recognition",
60
+ "pad": True,
61
+ },
62
+ {
63
+ "preview_image": str(SAMPLES_PATH / "code.jpg"),
64
+ "prompts": ["Convert code to text."],
65
+ "image": str(SAMPLES_PATH / "code.jpg"),
66
+ "name": "Code Recognition",
67
+ "pad": True,
68
+ },
69
+ {
70
+ "preview_image": str(SAMPLES_PATH / "lake-zurich-switzerland-view-nature-landscapes-7bbda4-1024.jpg"),
71
+ "prompts": ["Describe this image."],
72
+ "image": str(SAMPLES_PATH / "lake-zurich-switzerland-view-nature-landscapes-7bbda4-1024.jpg"),
73
+ "name": "Image Captioning",
74
+ "pad": False,
75
+ },
76
+ {
77
+ "preview_image": str(SAMPLES_PATH / "87664.png"),
78
+ "prompts": ["Convert formula to latex."],
79
+ "image": str(SAMPLES_PATH / "87664.png"),
80
+ "name": "Formula Recognition",
81
+ "pad": True,
82
+ },
83
+ {
84
+ "preview_image": str(SAMPLES_PATH / "06236926002285.png"),
85
+ "prompts": ["Convert chart to OTSL."],
86
+ "image": str(SAMPLES_PATH / "06236926002285.png"),
87
+ "name": "Chart Extraction",
88
+ "pad": False,
89
+ },
90
+ {
91
+ "preview_image": str(SAMPLES_PATH / "ar_page_0.png"),
92
+ "prompts": ["Convert this page to docling."],
93
+ "image": str(SAMPLES_PATH / "ar_page_0.png"),
94
+ "name": "Arabic Conversion",
95
+ "pad": False,
96
+ },
97
+ {
98
+ "preview_image": str(SAMPLES_PATH / "japanse_4_ibm.png"),
99
+ "prompts": ["Convert this page to docling."],
100
+ "image": str(SAMPLES_PATH / "japanse_4_ibm.png"),
101
+ "name": "Japanese Conversion",
102
+ "pad": False,
103
+ },
104
+ {
105
+ "preview_image": str(SAMPLES_PATH / "zh_page_0.png"),
106
+ "prompts": ["Convert this page to docling."],
107
+ "image": str(SAMPLES_PATH / "zh_page_0.png"),
108
+ "name": "Chinese Conversion",
109
+ "pad": False,
110
+ },
111
+ ]
112
+
113
+ # Initialize the model
114
+ model_id = "ibm-granite/granite-docling-258M"
115
+
116
+ if gr.NO_RELOAD:
117
+ processor = AutoProcessor.from_pretrained(model_id, use_auth_token=True)
118
+ model = Idefics3ForConditionalGeneration.from_pretrained(
119
+ model_id, device_map=device, torch_dtype=torch.bfloat16, use_auth_token=True
120
+ )
121
+ if not torch.cuda.is_available():
122
+ model = model.to(device)
123
+
124
+
125
+ def lower_md_headers(md: str) -> str:
126
+ """Convert markdown headers to lower level headers."""
127
+ return re.sub(r"(?:^|\n)##?\s(.+)", lambda m: "\n### " + m.group(1), md)
128
+
129
+
130
+ def add_random_padding(image: Image.Image, min_percent: float = 0.1, max_percent: float = 0.10) -> Image.Image:
131
+ """Add random padding to an image."""
132
+ image = image.convert("RGB")
133
+
134
+ width, height = image.size
135
+
136
+ pad_w_percent = random.uniform(min_percent, max_percent)
137
+ pad_h_percent = random.uniform(min_percent, max_percent)
138
+
139
+ pad_w = int(width * pad_w_percent)
140
+ pad_h = int(height * pad_h_percent)
141
+
142
+ corner_pixel = image.getpixel((0, 0)) # Top-left corner
143
+ padded_image = ImageOps.expand(image, border=(pad_w, pad_h, pad_w, pad_h), fill=corner_pixel)
144
+
145
+ return padded_image
146
+
147
+
148
+ def draw_bounding_boxes(image_path: str, response_text: str, is_doctag_response: bool = False) -> Image.Image:
149
+ """Draw bounding boxes on the image based on loc tags and return the annotated image."""
150
+ try:
151
+ # Load the original image
152
+ image = Image.open(image_path).convert("RGB")
153
+ draw = ImageDraw.Draw(image)
154
+
155
+ # Get image dimensions
156
+ width, height = image.size
157
+
158
+ # Color mapping for different classes (RGB values converted to hex)
159
+ class_colors = {
160
+ "caption": "#FFCC99", # (255, 204, 153)
161
+ "footnote": "#C8C8FF", # (200, 200, 255)
162
+ "formula": "#C0C0C0", # (192, 192, 192)
163
+ "list_item": "#9999FF", # (153, 153, 255)
164
+ "page_footer": "#CCFFCC", # (204, 255, 204)
165
+ "page_header": "#CCFFCC", # (204, 255, 204)
166
+ "picture": "#FFCCA4", # (255, 204, 164)
167
+ "chart": "#FFCCA4", # (255, 204, 164)
168
+ "section_header": "#FF9999", # (255, 153, 153)
169
+ "table": "#FFCCCC", # (255, 204, 204)
170
+ "text": "#FFFF99", # (255, 255, 153)
171
+ "title": "#FF9999", # (255, 153, 153)
172
+ "document_index": "#DCDCDC", # (220, 220, 220)
173
+ "code": "#7D7D7D", # (125, 125, 125)
174
+ "checkbox_selected": "#FFB6C1", # (255, 182, 193)
175
+ "checkbox_unselected": "#FFB6C1", # (255, 182, 193)
176
+ "form": "#C8FFFF", # (200, 255, 255)
177
+ "key_value_region": "#B7410E", # (183, 65, 14)
178
+ "paragraph": "#FFFF99", # (255, 255, 153)
179
+ "reference": "#B0E0E6", # (176, 224, 230)
180
+ "grading_scale": "#FFCCCC", # (255, 204, 204)
181
+ "handwritten_text": "#CCFFCC", # (204, 255, 204)
182
+ "empty_value": "#DCDCDC", # (220, 220, 220)
183
+ }
184
+
185
+ doctag_class_pattern = r"<([^>]+)><loc_(\d+)><loc_(\d+)><loc_(\d+)><loc_(\d+)>[^<]*</[^>]+>"
186
+ doctag_matches = re.findall(doctag_class_pattern, response_text)
187
+
188
+ class_pattern = r"<([^>]+)><loc_(\d+)><loc_(\d+)><loc_(\d+)><loc_(\d+)>"
189
+ class_matches = re.findall(class_pattern, response_text)
190
+ seen_coords = set()
191
+ all_class_matches = []
192
+
193
+ for match in doctag_matches:
194
+ coords = (match[1], match[2], match[3], match[4])
195
+ if coords not in seen_coords:
196
+ seen_coords.add(coords)
197
+ all_class_matches.append(match)
198
+
199
+ for match in class_matches:
200
+ coords = (match[1], match[2], match[3], match[4])
201
+ if coords not in seen_coords:
202
+ seen_coords.add(coords)
203
+ all_class_matches.append(match)
204
+
205
+ loc_only_pattern = r"<loc_(\d+)><loc_(\d+)><loc_(\d+)><loc_(\d+)>"
206
+ loc_only_matches = re.findall(loc_only_pattern, response_text)
207
+
208
+ for class_name, xmin, ymin, xmax, ymax in all_class_matches:
209
+ if is_doctag_response:
210
+ color = class_colors.get(class_name.lower(), None)
211
+ if color is None:
212
+ for key in class_colors:
213
+ if class_name.lower() in key or key in class_name.lower():
214
+ color = class_colors[key]
215
+ break
216
+ if color is None:
217
+ color = "#808080"
218
+ else:
219
+ color = "#E0115F"
220
+
221
+ x1 = int((int(xmin) / 500) * width)
222
+ y1 = int((int(ymin) / 500) * height)
223
+ x2 = int((int(xmax) / 500) * width)
224
+ y2 = int((int(ymax) / 500) * height)
225
+ draw.rectangle([x1, y1, x2, y2], outline=color, width=3)
226
+
227
+ for xmin, ymin, xmax, ymax in loc_only_matches:
228
+ if is_doctag_response:
229
+ continue
230
+ else:
231
+ color = "#808080"
232
+
233
+ x1 = int((int(xmin) / 500) * width)
234
+ y1 = int((int(ymin) / 500) * height)
235
+ x2 = int((int(xmax) / 500) * width)
236
+ y2 = int((int(ymax) / 500) * height)
237
+ draw.rectangle([x1, y1, x2, y2], outline=color, width=3)
238
+
239
+ return image
240
+
241
+ except Exception:
242
+ return Image.open(image_path)
243
+
244
+
245
+ def clean_model_response(text: str) -> str:
246
+ """Clean up model response by removing special tokens and formatting properly."""
247
+ if not text:
248
+ return "No response generated."
249
+ special_tokens = [
250
+ "<|end_of_text|>",
251
+ "<|end|>",
252
+ "<|assistant|>",
253
+ "<|user|>",
254
+ "<|system|>",
255
+ "<pad>",
256
+ "</s>",
257
+ "<s>",
258
+ ]
259
+
260
+ cleaned = text
261
+ for token in special_tokens:
262
+ cleaned = cleaned.replace(token, "")
263
+ cleaned = cleaned.strip()
264
+
265
+ if not cleaned or len(cleaned) == 0:
266
+ return "The model generated a response, but it appears to be empty or contain only special tokens."
267
+ return cleaned
268
+
269
+
270
+ @spaces.GPU()
271
+ def generate_with_model(question: str, image_path: str, apply_padding: bool = False) -> str:
272
+ """Generate answer using the Granite Docling model directly on the image."""
273
+ if os.environ.get("NO_LLM"):
274
+ time.sleep(2)
275
+ return "This is a simulated response from the Granite Docling model."
276
+
277
+ try:
278
+ image = Image.open(image_path).convert("RGB")
279
+ if apply_padding:
280
+ image = add_random_padding(image)
281
+ messages = [
282
+ {
283
+ "role": "user",
284
+ "content": [
285
+ {"type": "image"},
286
+ {"type": "text", "text": question},
287
+ ],
288
+ }
289
+ ]
290
+ prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
291
+ temperature = 0.0
292
+ inputs = processor(text=prompt, images=[image], return_tensors="pt")
293
+ inputs = {k: v.to(device) for k, v in inputs.items()}
294
+ with torch.no_grad():
295
+ generated_ids = model.generate(
296
+ **inputs,
297
+ max_new_tokens=4096,
298
+ temperature=temperature,
299
+ do_sample=temperature > 0,
300
+ pad_token_id=processor.tokenizer.eos_token_id,
301
+ )
302
+ generated_texts = processor.batch_decode(
303
+ generated_ids[:, inputs["input_ids"].shape[1] :],
304
+ skip_special_tokens=False,
305
+ )[0]
306
+ cleaned_response = clean_model_response(generated_texts)
307
+
308
+ return cleaned_response
309
+
310
+ except Exception as e:
311
+ return f"Error processing image: {e!s}"
312
+
313
+
314
+ _streaming_raw_output = ""
315
+
316
+
317
+ @spaces.GPU()
318
+ def generate_with_model_streaming(question: str, image_path: str, apply_padding: bool = False) -> None:
319
+ """Generate answer using the Granite Docling model with streaming."""
320
+ global _streaming_raw_output
321
+ _streaming_raw_output = ""
322
+
323
+ try:
324
+ image = Image.open(image_path).convert("RGB")
325
+ if apply_padding:
326
+ image = add_random_padding(image)
327
+ messages = [
328
+ {
329
+ "role": "user",
330
+ "content": [
331
+ {"type": "image"},
332
+ {"type": "text", "text": question},
333
+ ],
334
+ }
335
+ ]
336
+
337
+ prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
338
+ temperature = 0.0
339
+
340
+ inputs = processor(text=prompt, images=[image], return_tensors="pt")
341
+ inputs = {k: v.to(device) for k, v in inputs.items()}
342
+
343
+ streamer = TextIteratorStreamer(processor, skip_prompt=True, skip_special_tokens=False)
344
+ generation_args = dict(
345
+ inputs,
346
+ streamer=streamer,
347
+ max_new_tokens=4096,
348
+ temperature=temperature,
349
+ do_sample=temperature > 0,
350
+ pad_token_id=processor.tokenizer.eos_token_id,
351
+ )
352
+
353
+ thread = Thread(target=model.generate, kwargs=generation_args)
354
+ thread.start()
355
+
356
+ yield "..."
357
+ full_output = ""
358
+ escaped_output = ""
359
+
360
+ for new_text in streamer:
361
+ full_output += new_text
362
+ escaped_output += html.escape(new_text)
363
+ yield escaped_output
364
+
365
+ _streaming_raw_output = full_output
366
+
367
+ except Exception as e:
368
+ yield f"Error generating response: {e!s}"
369
+
370
+
371
+ chatbot = gr.Chatbot(
372
+ examples=[{"text": x} for x in sample_data[0]["prompts"]],
373
+ type="messages",
374
+ label=f"Q&A about {sample_data[0]['name']}",
375
+ height=685,
376
+ group_consecutive_messages=True,
377
+ autoscroll=False,
378
+ elem_classes=["chatbot_view"],
379
+ )
380
+
381
+
382
+ css_file_path = Path(Path(__file__).parent / "app.css")
383
+ head_file_path = Path(Path(__file__).parent / "app_head.html")
384
+
385
+ with gr.Blocks(fill_height=True, css_paths=css_file_path, head_paths=head_file_path, theme=theme, title=TITLE) as demo:
386
+ is_in_edit_mode = gr.State(True) # in block to be reactive
387
+ selected_doc = gr.State(0)
388
+ current_question = gr.State("")
389
+ uploaded_image_path = gr.State(None) # Store path to uploaded image
390
+
391
+ gr.Markdown(f"# {TITLE}")
392
+ gr.Markdown(DESCRIPTION)
393
+
394
+ # Create gallery with captions for hover effect
395
+ gallery_with_captions = []
396
+ for sd in sample_data:
397
+ gallery_with_captions.append((sd["preview_image"], sd["name"]))
398
+
399
+ document_gallery = gr.Gallery(
400
+ gallery_with_captions,
401
+ label="Select a document",
402
+ rows=1,
403
+ columns=9,
404
+ height="125px",
405
+ allow_preview=False,
406
+ selected_index=0,
407
+ elem_classes=["preview_im_element"],
408
+ show_label=True,
409
+ )
410
+
411
+ with gr.Row():
412
+ with gr.Column(), gr.Group():
413
+ image_display = gr.Image(
414
+ sample_data[0]["image"],
415
+ label=f"Preview for {sample_data[0]['name']}",
416
+ height=700,
417
+ interactive=False,
418
+ elem_classes=["image_viewer"],
419
+ )
420
+ # Upload button for custom images
421
+ upload_button = gr.UploadButton(
422
+ "📁 Upload Image", file_types=["image"], elem_classes=["upload_button"], scale=1
423
+ )
424
+
425
+ with gr.Column():
426
+ chatbot.render()
427
+ with gr.Row():
428
+ tbb = gr.Textbox(submit_btn=True, show_label=False, placeholder="Type a message...", scale=4)
429
+ fb = gr.Button("Ask new question", visible=False, scale=1)
430
+ fb.click(lambda: [], outputs=[chatbot])
431
+
432
+ def sample_image_selected(d: gr.SelectData) -> tuple:
433
+ """Handle sample image selection."""
434
+ dx = sample_data[d.index]
435
+ return (
436
+ gr.update(examples=[{"text": x} for x in dx["prompts"]], label=f"Q&A about {dx['name']}"),
437
+ gr.update(value=dx["image"], label=f"Preview for {dx['name']}"),
438
+ d.index,
439
+ )
440
+
441
+ document_gallery.select(lambda: [], outputs=[chatbot])
442
+ document_gallery.select(sample_image_selected, inputs=[], outputs=[chatbot, image_display, selected_doc])
443
+
444
+ def update_user_chat_x(x: gr.SelectData) -> list:
445
+ """Update chat with user selection."""
446
+ return [gr.ChatMessage(role="user", content=x.value["text"])]
447
+
448
+ def question_from_selection(x: gr.SelectData) -> str:
449
+ """Extract question text from selection."""
450
+ return x.value["text"]
451
+
452
+ def handle_image_upload(uploaded_file: str | None) -> tuple:
453
+ """Handle uploaded image and update the display."""
454
+ if uploaded_file is None:
455
+ return None, None, None
456
+
457
+ # Update the image display with the uploaded image
458
+ image_update = gr.update(value=uploaded_file, label="Uploaded Image")
459
+
460
+ # Update chatbot to show it's ready for questions about the uploaded image
461
+ chatbot_update = gr.update(
462
+ examples=[{"text": "Convert this page to docling."}], label="Q&A about uploaded image"
463
+ )
464
+
465
+ # Clear the chat history
466
+ chat_update = []
467
+
468
+ return image_update, chatbot_update, chat_update, uploaded_file
469
+
470
+ # Connect upload button to handler
471
+ upload_button.upload(
472
+ handle_image_upload, inputs=[upload_button], outputs=[image_display, chatbot, chatbot, uploaded_image_path]
473
+ )
474
+
475
+ def send_generate(msg: str, cb: list, selected_sample: int, uploaded_img_path: str | None = None) -> None:
476
+ """Generate response using the model."""
477
+ # Use uploaded image if available, otherwise use selected sample
478
+ image_path = uploaded_img_path if uploaded_img_path is not None else sample_data[selected_sample]["image"]
479
+ original_msg = gr.ChatMessage(role="user", content=msg)
480
+ cb.append(original_msg)
481
+
482
+ processing_msg = gr.ChatMessage(
483
+ role="assistant",
484
+ content='<span class="jumping-dots"><span class="dot-1">.</span> <span class="dot-2">.</span> '
485
+ '<span class="dot-3">.</span></span>',
486
+ )
487
+ cb.append(processing_msg)
488
+ yield cb, gr.update()
489
+
490
+ # Apply padding only for sample images, not uploaded images
491
+ apply_padding = False if uploaded_img_path is not None else sample_data[selected_sample].get("pad", False)
492
+
493
+ first_token = True
494
+ try:
495
+ stream_gen = generate_with_model_streaming(msg.strip(), image_path, apply_padding)
496
+
497
+ for partial_answer in stream_gen:
498
+ if first_token:
499
+ cb[-1] = gr.ChatMessage(role="assistant", content=partial_answer)
500
+ first_token = False
501
+ else:
502
+ cb[-1] = gr.ChatMessage(role="assistant", content=partial_answer)
503
+ yield cb, gr.update()
504
+
505
+ except Exception:
506
+ answer = generate_with_model(msg.strip(), image_path, apply_padding)
507
+ cb[-1] = gr.ChatMessage(role="assistant", content=answer)
508
+ yield cb, gr.update()
509
+
510
+ global _streaming_raw_output
511
+ answer = _streaming_raw_output if _streaming_raw_output else partial_answer
512
+
513
+ answer = html.unescape(answer)
514
+ answer = clean_model_response(answer)
515
+ class_loc_pattern = r"<([^>]+)><loc_(\d+)><loc_(\d+)><loc_(\d+)><loc_(\d+)>"
516
+ class_loc_matches = re.findall(class_loc_pattern, answer)
517
+
518
+ loc_only_pattern = r"<loc_(\d+)><loc_(\d+)><loc_(\d+)><loc_(\d+)>"
519
+ loc_only_matches = re.findall(loc_only_pattern, answer)
520
+
521
+ has_doctag = "<doctag>" in answer
522
+ has_loc_tags = class_loc_matches or loc_only_matches
523
+
524
+ xml_tags = ["<doctag>", "<otsl>", "<chart>", "<code>", "<loc_"]
525
+ if any(tag in answer for tag in xml_tags):
526
+ cb[-1] = gr.ChatMessage(role="assistant", content=f"```xml\n{answer}\n```")
527
+ else:
528
+ cb[-1] = gr.ChatMessage(role="assistant", content=answer)
529
+
530
+ if "convert this page to docling" in msg.lower() or ("convert" in msg.lower() and "otsl" in msg.lower()):
531
+ try:
532
+ doctags_doc = DocTagsDocument.from_doctags_and_image_pairs([answer], [Image.open(image_path)])
533
+ doc = DoclingDocument.load_from_doctags(doctags_doc, document_name="Document")
534
+ markdown_output = doc.export_to_markdown()
535
+ response = gr.ChatMessage(
536
+ role="assistant",
537
+ content=f"\nConverted to Markdown using docling.\n\n**MD Output:**\n\n{markdown_output}",
538
+ )
539
+ cb.append(response)
540
+ except Exception as e:
541
+ error_response = gr.ChatMessage(role="assistant", content=f"Error creating markdown output: {e!s}")
542
+ cb.append(error_response)
543
+ elif "convert formula to latex" in msg.lower():
544
+ try:
545
+ doctags_doc = DocTagsDocument.from_doctags_and_image_pairs([answer], [Image.open(image_path)])
546
+ doc = DoclingDocument.load_from_doctags(doctags_doc, document_name="Document")
547
+ markdown_output = doc.export_to_markdown()
548
+ if markdown_output.count("$$") >= 2:
549
+ parts = markdown_output.split("$$", 2)
550
+ formula = parts[1].strip()
551
+ wrapped = f"$$\n\\begin{{aligned}}\n{formula}\n\\end{{aligned}}\n$$"
552
+ markdown_output = parts[0] + wrapped + parts[2]
553
+ md_response = gr.ChatMessage(
554
+ role="assistant",
555
+ content=f"\nConverted to Markdown using docling.\n\n**LaTeX Output:**\n\n{markdown_output}",
556
+ )
557
+ cb.append(md_response)
558
+ except Exception as e:
559
+ error_response = gr.ChatMessage(role="assistant", content=f"Error creating LaTeX output: {e!s}")
560
+ cb.append(error_response)
561
+
562
+ if has_loc_tags:
563
+ try:
564
+ annotated_image = draw_bounding_boxes(image_path, answer, is_doctag_response=has_doctag)
565
+ annotated_array = np.array(annotated_image)
566
+ yield cb, gr.update(value=annotated_array, visible=True)
567
+ except Exception:
568
+ yield cb, gr.update(value=image_path)
569
+ else:
570
+ yield cb, gr.update(value=image_path)
571
+
572
+ chatbot.example_select(lambda: False, outputs=is_in_edit_mode)
573
+ chatbot.example_select(question_from_selection, inputs=[], outputs=[current_question]).then(
574
+ send_generate,
575
+ inputs=[current_question, chatbot, selected_doc, uploaded_image_path],
576
+ outputs=[chatbot, image_display],
577
+ )
578
+
579
+ def textbox_switch(e_mode: bool) -> list:
580
+ """Switch textbox visibility based on edit mode."""
581
+ if not e_mode:
582
+ return [gr.update(visible=False), gr.update(visible=True)]
583
+ else:
584
+ return [gr.update(visible=True), gr.update(visible=False)]
585
+
586
+ tbb.submit(lambda: False, outputs=[is_in_edit_mode])
587
+ fb.click(lambda: True, outputs=[is_in_edit_mode])
588
+ is_in_edit_mode.change(textbox_switch, inputs=[is_in_edit_mode], outputs=[tbb, fb])
589
+
590
+ tbb.submit(lambda x: x, inputs=[tbb], outputs=[current_question]).then(
591
+ send_generate,
592
+ inputs=[current_question, chatbot, selected_doc, uploaded_image_path],
593
+ outputs=[chatbot, image_display],
594
+ )
595
+
596
+ if __name__ == "__main__":
597
+ demo.queue(max_size=20)
598
+ demo.launch()
src/app_head.html ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script
2
+ async
3
+ src="https://www.googletagmanager.com/gtag/js?id=G-C6LFT227RC"
4
+ ></script>
5
+ <script>
6
+ window.dataLayer = window.dataLayer || [];
7
+ function gtag() {
8
+ dataLayer.push(arguments);
9
+ }
10
+ gtag("js", new Date());
11
+ gtag("config", "G-C6LFT227RC");
12
+ </script>
src/themes/carbon.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """IBM Carbon theme for gradio demos.
2
+
3
+ This version builds on top of the Carbon theme to make it more playful with rounded corners, a larger font family to
4
+ enhance readability, and the IBM Cool Gray color palette for better consistency with other IBM Research demos, such as
5
+ Bee.
6
+ """
7
+
8
+ import gradio as gr
9
+ from gradio.themes.utils import sizes
10
+
11
+ theme = gr.themes.Base(
12
+ primary_hue=gr.themes.Color(
13
+ c100="#EDF5FF",
14
+ c200="#D0E2FF",
15
+ c300="#A6C8FF",
16
+ c400="#78A9FF",
17
+ c50="#F9F9FB",
18
+ c500="#4589FF",
19
+ c600="#0F62FE",
20
+ c700="#0043CE",
21
+ c800="#002D9C",
22
+ c900="#001D6C",
23
+ c950="#001141",
24
+ ),
25
+ secondary_hue=gr.themes.Color(
26
+ c100="#EDF5FF",
27
+ c200="#D0E2FF",
28
+ c300="#A6C8FF",
29
+ c400="#78A9FF",
30
+ c50="#F9F9FB",
31
+ c500="#4589FF",
32
+ c600="#0F62FE",
33
+ c700="#0043CE",
34
+ c800="#002D9C",
35
+ c900="#001D6C",
36
+ c950="#001141",
37
+ ),
38
+ neutral_hue=gr.themes.Color(
39
+ c100="#F2F4F8",
40
+ c200="#DDE1E6",
41
+ c300="#C1C7CD",
42
+ c400="#A2A9B0",
43
+ c50="#F9F9FB",
44
+ c500="#878D96",
45
+ c600="#697077",
46
+ c700="#4D5358",
47
+ c800="#393939",
48
+ c900="#21272A",
49
+ c950="#121619",
50
+ ),
51
+ spacing_size=sizes.spacing_md, # change spacing to default size
52
+ radius_size=sizes.radius_md, # change spacing to default size and Keep Radius to make demo feel more playful
53
+ text_size=sizes.text_lg, # change fontsize to default size
54
+ # spacing_size: sizes.Size | str = sizes.spacing_md, #change spacing to default size
55
+ # radius_size: sizes.Size | str = sizes.radius_md, #change spacing to default size and Keep Radius to make
56
+ # demo feel more playful
57
+ # text_size: sizes.Size | str = sizes.text_lg, #change fontsize to default size
58
+ font=["IBM Plex Sans", "ui-sans-serif", "system-ui", "sans-serif"], # update font
59
+ font_mono=["IBM Plex Mono", "ui-monospace", "Consolas", "monospace"], # update font
60
+ ).set(
61
+ # Colors
62
+ background_fill_primary="*neutral_100", # Coolgray10 background
63
+ background_fill_primary_dark="*neutral_950", # Coolgray95 background for dark mode
64
+ slider_color="*primary_600", # Blue60
65
+ slider_color_dark="*primary_500", # Blue50
66
+ # Shadows
67
+ shadow_drop="0 1px 4px 0 rgb(0 0 0 / 0.1)",
68
+ shadow_drop_lg="0 2px 5px 0 rgb(0 0 0 / 0.1)",
69
+ # Block Labels
70
+ block_background_fill="white",
71
+ block_label_background_fill="white", # same color as blockback gound fill
72
+ block_label_radius="*radius_md",
73
+ block_label_text_size="*text_md",
74
+ block_label_text_weight="600",
75
+ block_label_text_color="black",
76
+ block_label_text_color_dark="white",
77
+ block_title_radius="*block_label_radius",
78
+ block_title_background_fill="*block_label_background_fill",
79
+ block_title_text_weight="600",
80
+ block_title_text_color="black",
81
+ block_title_text_color_dark="white",
82
+ block_label_margin="*spacing_md",
83
+ # Inputs
84
+ input_background_fill="white",
85
+ input_background_fill_dark="*block-background-fill",
86
+ input_border_color="*neutral_100",
87
+ input_shadow="*shadow_drop",
88
+ input_shadow_focus="*shadow_drop_lg",
89
+ checkbox_shadow="none",
90
+ # Buttons
91
+ shadow_spread="6px",
92
+ button_primary_shadow="*shadow_drop_lg",
93
+ button_primary_shadow_hover="*shadow_drop_lg",
94
+ button_primary_shadow_active="*shadow_inset",
95
+ button_secondary_shadow="*shadow_drop_lg",
96
+ button_secondary_shadow_hover="*shadow_drop_lg",
97
+ button_secondary_shadow_active="*shadow_inset",
98
+ checkbox_label_shadow="*shadow_drop_lg",
99
+ button_primary_background_fill="*primary_600",
100
+ button_primary_background_fill_hover="*primary_500",
101
+ button_primary_background_fill_hover_dark="*primary_500",
102
+ button_primary_text_color="white",
103
+ button_secondary_background_fill="white",
104
+ button_secondary_background_fill_hover="*neutral_100",
105
+ button_secondary_background_fill_dark="*neutral_800", # Secondary cool gray 80
106
+ button_secondary_background_fill_hover_dark="*primary_500",
107
+ button_secondary_text_color="*neutral_800",
108
+ button_cancel_background_fill="*button_secondary_background_fill",
109
+ button_cancel_background_fill_hover="*button_secondary_background_fill_hover",
110
+ button_cancel_background_fill_hover_dark="*button_secondary_background_fill_hover",
111
+ button_cancel_text_color="*button_secondary_text_color",
112
+ checkbox_label_background_fill_selected="*primary_200",
113
+ checkbox_label_background_fill_selected_dark="*primary_500",
114
+ checkbox_border_width="1px",
115
+ checkbox_border_color="*neutral_200",
116
+ checkbox_background_color_dark="*neutral_700", # Jan 18 test to fix checkbox, radio button background color
117
+ checkbox_background_color_selected="*primary_600",
118
+ checkbox_background_color_selected_dark="*primary_500",
119
+ checkbox_border_color_focus="*primary_600",
120
+ checkbox_border_color_focus_dark="*primary_500",
121
+ checkbox_border_color_selected="*primary_600",
122
+ checkbox_border_color_selected_dark="*primary_500",
123
+ checkbox_label_text_color_selected="black",
124
+ # Borders
125
+ block_border_width="1px", # test example border
126
+ panel_border_width="1px",
127
+ # Chatbubble related colors
128
+ # light
129
+ # color_accent = "*secondary_400",
130
+ border_color_accent_subdued="*color_accent_soft", # chatbubble human border color, use Blue 20 as an accent color
131
+ color_accent_soft="*secondary_200", # chatbubble human color
132
+ # darkmode
133
+ # chatbubble human border color in darkmode, use Blue 20 as an accent color
134
+ border_color_accent_subdued_dark="*secondary_500",
135
+ color_accent_soft_dark="*secondary_500", # chatbubble human color in dark mode
136
+ # Chatbot related font
137
+ chatbot_text_size="*text_md", # make it larger
138
+ # additional dark mode related tweaks:
139
+ # block_background_fill_dark="*neutral_950", # Jan 18 test coolgray95 background for dark mode
140
+ block_label_background_fill_dark="*neutral_800", # same color as blockback gound fill
141
+ block_title_background_fill_dark="*block_label_background_fill",
142
+ # input_background_fill_dark="*neutral_800", #This attribute help match fill color cool gray 80 to match background
143
+ # however cause the problem for the general theme.
144
+ # input_shadow_dark="*shadow_drop", #Test if it could make the border without the color
145
+ # input_border_color_dark="*neutral_200",#add attribute for border Jan 18
146
+ checkbox_border_color_dark="*neutral_600", # Jan 18 test to fix border
147
+ )
src/themes/research_monochrome.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """IBM Research Monochrome theme for gradio demos.
2
+
3
+ This version is a variation of CarbonSoft style, where the primary button is dark gray to create monochrome style. This
4
+ version uses the style from Research demos such as Bee to make it more playful with rounded corners, a larger font
5
+ family to enhance readability, and the IBM Cool Gray color palette for better consistency with other IBM Research demos,
6
+ such as Bee.
7
+ """
8
+
9
+ import gradio as gr
10
+ from gradio.themes.utils import sizes
11
+
12
+ theme = gr.themes.Base(
13
+ primary_hue=gr.themes.Color(
14
+ c100="#EDF5FF",
15
+ c200="#D0E2FF",
16
+ c300="#A6C8FF",
17
+ c400="#78A9FF",
18
+ c50="#F9F9FB",
19
+ c500="#4589FF",
20
+ c600="#0F62FE",
21
+ c700="#0043CE",
22
+ c800="#002D9C",
23
+ c900="#001D6C",
24
+ c950="#001141",
25
+ ),
26
+ secondary_hue=gr.themes.Color(
27
+ c100="#EDF5FF",
28
+ c200="#D0E2FF",
29
+ c300="#A6C8FF",
30
+ c400="#78A9FF",
31
+ c50="#F9F9FB",
32
+ c500="#4589FF",
33
+ c600="#0F62FE",
34
+ c700="#0043CE",
35
+ c800="#002D9C",
36
+ c900="#001D6C",
37
+ c950="#001141",
38
+ ),
39
+ neutral_hue=gr.themes.Color(
40
+ c100="#F2F4F8",
41
+ c200="#DDE1E6",
42
+ c300="#C1C7CD",
43
+ c400="#A2A9B0",
44
+ c50="#F9F9FB",
45
+ c500="#878D96",
46
+ c600="#697077",
47
+ c700="#4D5358",
48
+ c800="#393939",
49
+ c900="#21272A",
50
+ c950="#121619",
51
+ ),
52
+ spacing_size=sizes.spacing_md, # change spacing to default size
53
+ radius_size=sizes.radius_md, # change spacing to default size and Keep Radius to make demo feel more playful
54
+ text_size=sizes.text_md, # change fontsize to default size
55
+ # spacing_size: sizes.Size | str = sizes.spacing_md, #change spacing to default size
56
+ # radius_size: sizes.Size | str = sizes.radius_md, #change spacing to default size and Keep Radius to make
57
+ # demo feel more playful
58
+ # text_size: sizes.Size | str = sizes.text_lg, #change fontsize to default size
59
+ font=gr.themes.GoogleFont("IBM Plex Sans"), # update font
60
+ font_mono=gr.themes.GoogleFont("IBM Plex Mono"), # update font
61
+ ).set(
62
+ # Colors
63
+ background_fill_primary="*neutral_100", # Coolgray10 background
64
+ background_fill_primary_dark="*neutral_950", # Coolgray95 background for dark mode
65
+ # Change blue to black to create monochrome style
66
+ slider_color="*neutral_900",
67
+ slider_color_dark="*primary_500",
68
+ # Shadows
69
+ shadow_drop="0 1px 4px 0 rgb(0 0 0 / 0.1)",
70
+ shadow_drop_lg="0 2px 5px 0 rgb(0 0 0 / 0.1)",
71
+ # Block Labels
72
+ block_background_fill="white",
73
+ block_label_background_fill="white", # same color as blockback gound fill
74
+ block_label_radius="*radius_md",
75
+ block_label_text_size="*text_md",
76
+ block_label_text_weight="600",
77
+ block_label_text_color="black",
78
+ block_label_text_color_dark="white",
79
+ block_title_radius="*block_label_radius",
80
+ block_title_background_fill="*block_label_background_fill",
81
+ block_title_text_weight="400",
82
+ block_title_text_color="black",
83
+ block_title_text_color_dark="white",
84
+ block_label_margin="*spacing_md",
85
+ # Inputs
86
+ input_background_fill="white",
87
+ input_background_fill_dark="*block-background-fill",
88
+ input_border_color="*neutral_100",
89
+ input_shadow="*shadow_drop",
90
+ input_shadow_dark="0 1px 4px #000",
91
+ input_shadow_focus="*shadow_drop_lg",
92
+ checkbox_shadow="none",
93
+ # Buttons
94
+ shadow_spread="6px",
95
+ button_primary_shadow="*shadow_drop_lg",
96
+ button_primary_shadow_hover="*shadow_drop_lg",
97
+ button_primary_shadow_active="*shadow_inset",
98
+ button_secondary_shadow="*shadow_drop_lg",
99
+ button_secondary_shadow_hover="*shadow_drop_lg",
100
+ button_secondary_shadow_active="*shadow_inset",
101
+ checkbox_label_shadow="*shadow_drop_lg",
102
+ # Change blue to black to create monochrome style
103
+ button_primary_background_fill="*neutral_900",
104
+ button_primary_background_fill_dark="*neutral_600",
105
+ button_primary_background_fill_hover="*neutral_700",
106
+ button_primary_background_fill_hover_dark="*primary_500", # hover to be blue
107
+ button_primary_text_color="white",
108
+ button_secondary_background_fill="white",
109
+ button_secondary_background_fill_hover="*neutral_100",
110
+ button_secondary_background_fill_dark="*neutral_800", # Secondary cool gray 80
111
+ button_secondary_background_fill_hover_dark="*primary_500",
112
+ button_secondary_text_color="*neutral_800",
113
+ button_cancel_background_fill="*button_secondary_background_fill",
114
+ button_cancel_background_fill_hover="*button_secondary_background_fill_hover",
115
+ button_cancel_background_fill_hover_dark="*button_secondary_background_fill_hover",
116
+ button_cancel_text_color="*button_secondary_text_color",
117
+ checkbox_label_background_fill_selected="*primary_200",
118
+ checkbox_label_background_fill_selected_dark="*primary_500",
119
+ checkbox_border_width="1px",
120
+ checkbox_border_color="*neutral_200",
121
+ checkbox_background_color_dark="*neutral_700", # Jan 18 test to fix checkbox, radio button background color
122
+ checkbox_background_color_selected="*primary_600",
123
+ checkbox_background_color_selected_dark="*primary_500",
124
+ checkbox_border_color_focus="*primary_600",
125
+ checkbox_border_color_focus_dark="*primary_500",
126
+ checkbox_border_color_selected="*primary_600",
127
+ checkbox_border_color_selected_dark="*primary_500",
128
+ checkbox_label_text_color_selected="black",
129
+ # Borders
130
+ block_border_width="1px", # test example border
131
+ panel_border_width="1px",
132
+ # Chatbubble related colors
133
+ # light
134
+ # color_accent = "*secondary_400",
135
+ border_color_accent_subdued="*color_accent_soft", # chatbubble human border color, use Blue 20 as an accent color
136
+ color_accent_soft="*secondary_200", # chatbubble human color
137
+ # darkmode
138
+ # chatbubble human border color in darkmode, use Blue 20 as an accent color
139
+ border_color_accent_subdued_dark="*secondary_500",
140
+ color_accent_soft_dark="*secondary_500", # chatbubble human color in dark mode
141
+ # Chatbot related font
142
+ chatbot_text_size="*text_md", # make it larger
143
+ # additional dark mode related tweaks:
144
+ # block_background_fill_dark="*neutral_950", # Jan 18 test coolgray95 background for dark mode
145
+ block_label_background_fill_dark="*neutral_800", # same color as blockback gound fill
146
+ block_title_background_fill_dark="*block_label_background_fill",
147
+ # input_background_fill_dark="*neutral_800", #This attribute help match fill color cool gray 80 to match background
148
+ # however cause the problem for the general theme.
149
+ # input_shadow_dark="*shadow_drop", #Test if it could make the border without the color
150
+ # input_border_color_dark="*neutral_200",#add attribute for border Jan 18
151
+ checkbox_border_color_dark="*neutral_600", # Jan 18 test to fix border
152
+ )