prithivMLmods commited on
Commit
7dd2f28
·
verified ·
1 Parent(s): 9ad693e

Upload latexmind-codec.ipynb

Browse files
Files changed (1) hide show
  1. latexmind/latexmind-codec.ipynb +622 -0
latexmind/latexmind-codec.ipynb ADDED
@@ -0,0 +1,622 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {
6
+ "id": "-b4-SW1aGOcF"
7
+ },
8
+ "source": [
9
+ "# `Latex Mind 2B Codec`\n",
10
+ "\n",
11
+ "Qwen2VLForConditionalGeneration"
12
+ ]
13
+ },
14
+ {
15
+ "cell_type": "code",
16
+ "execution_count": 1,
17
+ "metadata": {
18
+ "colab": {
19
+ "base_uri": "https://localhost:8080/"
20
+ },
21
+ "id": "oDmd1ZObGSel",
22
+ "outputId": "d672ebea-c7ec-4d77-e634-484078e9ef39"
23
+ },
24
+ "outputs": [
25
+ {
26
+ "name": "stdout",
27
+ "output_type": "stream",
28
+ "text": [
29
+ "Collecting gradio\n",
30
+ " Downloading gradio-5.14.0-py3-none-any.whl.metadata (16 kB)\n",
31
+ "Collecting spaces\n",
32
+ " Downloading spaces-0.32.0-py3-none-any.whl.metadata (1.0 kB)\n",
33
+ "Requirement already satisfied: transformers in /usr/local/lib/python3.11/dist-packages (4.47.1)\n",
34
+ "Requirement already satisfied: accelerate in /usr/local/lib/python3.11/dist-packages (1.2.1)\n",
35
+ "Requirement already satisfied: numpy in /usr/local/lib/python3.11/dist-packages (1.26.4)\n",
36
+ "Requirement already satisfied: requests in /usr/local/lib/python3.11/dist-packages (2.32.3)\n",
37
+ "Requirement already satisfied: torch in /usr/local/lib/python3.11/dist-packages (2.5.1+cu124)\n",
38
+ "Requirement already satisfied: torchvision in /usr/local/lib/python3.11/dist-packages (0.20.1+cu124)\n",
39
+ "Collecting qwen-vl-utils\n",
40
+ " Downloading qwen_vl_utils-0.0.10-py3-none-any.whl.metadata (6.3 kB)\n",
41
+ "Collecting av\n",
42
+ " Downloading av-14.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (4.7 kB)\n",
43
+ "Requirement already satisfied: ipython in /usr/local/lib/python3.11/dist-packages (7.34.0)\n",
44
+ "Collecting reportlab\n",
45
+ " Downloading reportlab-4.2.5-py3-none-any.whl.metadata (1.5 kB)\n",
46
+ "Collecting fpdf\n",
47
+ " Downloading fpdf-1.7.2.tar.gz (39 kB)\n",
48
+ " Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
49
+ "Collecting python-docx\n",
50
+ " Downloading python_docx-1.1.2-py3-none-any.whl.metadata (2.0 kB)\n",
51
+ "Requirement already satisfied: pillow in /usr/local/lib/python3.11/dist-packages (11.1.0)\n",
52
+ "Requirement already satisfied: huggingface_hub in /usr/local/lib/python3.11/dist-packages (0.27.1)\n",
53
+ "Collecting aiofiles<24.0,>=22.0 (from gradio)\n",
54
+ " Downloading aiofiles-23.2.1-py3-none-any.whl.metadata (9.7 kB)\n",
55
+ "Requirement already satisfied: anyio<5.0,>=3.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (3.7.1)\n",
56
+ "Collecting fastapi<1.0,>=0.115.2 (from gradio)\n",
57
+ " Downloading fastapi-0.115.8-py3-none-any.whl.metadata (27 kB)\n",
58
+ "Collecting ffmpy (from gradio)\n",
59
+ " Downloading ffmpy-0.5.0-py3-none-any.whl.metadata (3.0 kB)\n",
60
+ "Collecting gradio-client==1.7.0 (from gradio)\n",
61
+ " Downloading gradio_client-1.7.0-py3-none-any.whl.metadata (7.1 kB)\n",
62
+ "Requirement already satisfied: httpx>=0.24.1 in /usr/local/lib/python3.11/dist-packages (from gradio) (0.28.1)\n",
63
+ "Requirement already satisfied: jinja2<4.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (3.1.5)\n",
64
+ "Collecting markupsafe~=2.0 (from gradio)\n",
65
+ " Downloading MarkupSafe-2.1.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (3.0 kB)\n",
66
+ "Requirement already satisfied: orjson~=3.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (3.10.15)\n",
67
+ "Requirement already satisfied: packaging in /usr/local/lib/python3.11/dist-packages (from gradio) (24.2)\n",
68
+ "Requirement already satisfied: pandas<3.0,>=1.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (2.2.2)\n",
69
+ "Requirement already satisfied: pydantic>=2.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (2.10.6)\n",
70
+ "Collecting pydub (from gradio)\n",
71
+ " Downloading pydub-0.25.1-py2.py3-none-any.whl.metadata (1.4 kB)\n",
72
+ "Collecting python-multipart>=0.0.18 (from gradio)\n",
73
+ " Downloading python_multipart-0.0.20-py3-none-any.whl.metadata (1.8 kB)\n",
74
+ "Requirement already satisfied: pyyaml<7.0,>=5.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (6.0.2)\n",
75
+ "Collecting ruff>=0.9.3 (from gradio)\n",
76
+ " Downloading ruff-0.9.4-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (25 kB)\n",
77
+ "Collecting safehttpx<0.2.0,>=0.1.6 (from gradio)\n",
78
+ " Downloading safehttpx-0.1.6-py3-none-any.whl.metadata (4.2 kB)\n",
79
+ "Collecting semantic-version~=2.0 (from gradio)\n",
80
+ " Downloading semantic_version-2.10.0-py2.py3-none-any.whl.metadata (9.7 kB)\n",
81
+ "Collecting starlette<1.0,>=0.40.0 (from gradio)\n",
82
+ " Downloading starlette-0.45.3-py3-none-any.whl.metadata (6.3 kB)\n",
83
+ "Collecting tomlkit<0.14.0,>=0.12.0 (from gradio)\n",
84
+ " Downloading tomlkit-0.13.2-py3-none-any.whl.metadata (2.7 kB)\n",
85
+ "Requirement already satisfied: typer<1.0,>=0.12 in /usr/local/lib/python3.11/dist-packages (from gradio) (0.15.1)\n",
86
+ "Requirement already satisfied: typing-extensions~=4.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (4.12.2)\n",
87
+ "Collecting uvicorn>=0.14.0 (from gradio)\n",
88
+ " Downloading uvicorn-0.34.0-py3-none-any.whl.metadata (6.5 kB)\n",
89
+ "Requirement already satisfied: fsspec in /usr/local/lib/python3.11/dist-packages (from gradio-client==1.7.0->gradio) (2024.10.0)\n",
90
+ "Requirement already satisfied: websockets<15.0,>=10.0 in /usr/local/lib/python3.11/dist-packages (from gradio-client==1.7.0->gradio) (14.2)\n",
91
+ "Requirement already satisfied: psutil<6,>=2 in /usr/local/lib/python3.11/dist-packages (from spaces) (5.9.5)\n",
92
+ "Requirement already satisfied: filelock in /usr/local/lib/python3.11/dist-packages (from transformers) (3.17.0)\n",
93
+ "Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.11/dist-packages (from transformers) (2024.11.6)\n",
94
+ "Requirement already satisfied: tokenizers<0.22,>=0.21 in /usr/local/lib/python3.11/dist-packages (from transformers) (0.21.0)\n",
95
+ "Requirement already satisfied: safetensors>=0.4.1 in /usr/local/lib/python3.11/dist-packages (from transformers) (0.5.2)\n",
96
+ "Requirement already satisfied: tqdm>=4.27 in /usr/local/lib/python3.11/dist-packages (from transformers) (4.67.1)\n",
97
+ "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/dist-packages (from requests) (3.4.1)\n",
98
+ "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.11/dist-packages (from requests) (3.10)\n",
99
+ "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.11/dist-packages (from requests) (2.3.0)\n",
100
+ "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.11/dist-packages (from requests) (2024.12.14)\n",
101
+ "Requirement already satisfied: networkx in /usr/local/lib/python3.11/dist-packages (from torch) (3.4.2)\n",
102
+ "Collecting nvidia-cuda-nvrtc-cu12==12.4.127 (from torch)\n",
103
+ " Downloading nvidia_cuda_nvrtc_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl.metadata (1.5 kB)\n",
104
+ "Collecting nvidia-cuda-runtime-cu12==12.4.127 (from torch)\n",
105
+ " Downloading nvidia_cuda_runtime_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl.metadata (1.5 kB)\n",
106
+ "Collecting nvidia-cuda-cupti-cu12==12.4.127 (from torch)\n",
107
+ " Downloading nvidia_cuda_cupti_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl.metadata (1.6 kB)\n",
108
+ "Collecting nvidia-cudnn-cu12==9.1.0.70 (from torch)\n",
109
+ " Downloading nvidia_cudnn_cu12-9.1.0.70-py3-none-manylinux2014_x86_64.whl.metadata (1.6 kB)\n",
110
+ "Collecting nvidia-cublas-cu12==12.4.5.8 (from torch)\n",
111
+ " Downloading nvidia_cublas_cu12-12.4.5.8-py3-none-manylinux2014_x86_64.whl.metadata (1.5 kB)\n",
112
+ "Collecting nvidia-cufft-cu12==11.2.1.3 (from torch)\n",
113
+ " Downloading nvidia_cufft_cu12-11.2.1.3-py3-none-manylinux2014_x86_64.whl.metadata (1.5 kB)\n",
114
+ "Collecting nvidia-curand-cu12==10.3.5.147 (from torch)\n",
115
+ " Downloading nvidia_curand_cu12-10.3.5.147-py3-none-manylinux2014_x86_64.whl.metadata (1.5 kB)\n",
116
+ "Collecting nvidia-cusolver-cu12==11.6.1.9 (from torch)\n",
117
+ " Downloading nvidia_cusolver_cu12-11.6.1.9-py3-none-manylinux2014_x86_64.whl.metadata (1.6 kB)\n",
118
+ "Collecting nvidia-cusparse-cu12==12.3.1.170 (from torch)\n",
119
+ " Downloading nvidia_cusparse_cu12-12.3.1.170-py3-none-manylinux2014_x86_64.whl.metadata (1.6 kB)\n",
120
+ "Requirement already satisfied: nvidia-nccl-cu12==2.21.5 in /usr/local/lib/python3.11/dist-packages (from torch) (2.21.5)\n",
121
+ "Requirement already satisfied: nvidia-nvtx-cu12==12.4.127 in /usr/local/lib/python3.11/dist-packages (from torch) (12.4.127)\n",
122
+ "Collecting nvidia-nvjitlink-cu12==12.4.127 (from torch)\n",
123
+ " Downloading nvidia_nvjitlink_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl.metadata (1.5 kB)\n",
124
+ "Requirement already satisfied: triton==3.1.0 in /usr/local/lib/python3.11/dist-packages (from torch) (3.1.0)\n",
125
+ "Requirement already satisfied: sympy==1.13.1 in /usr/local/lib/python3.11/dist-packages (from torch) (1.13.1)\n",
126
+ "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.11/dist-packages (from sympy==1.13.1->torch) (1.3.0)\n",
127
+ "Requirement already satisfied: setuptools>=18.5 in /usr/local/lib/python3.11/dist-packages (from ipython) (75.1.0)\n",
128
+ "Collecting jedi>=0.16 (from ipython)\n",
129
+ " Downloading jedi-0.19.2-py2.py3-none-any.whl.metadata (22 kB)\n",
130
+ "Requirement already satisfied: decorator in /usr/local/lib/python3.11/dist-packages (from ipython) (4.4.2)\n",
131
+ "Requirement already satisfied: pickleshare in /usr/local/lib/python3.11/dist-packages (from ipython) (0.7.5)\n",
132
+ "Requirement already satisfied: traitlets>=4.2 in /usr/local/lib/python3.11/dist-packages (from ipython) (5.7.1)\n",
133
+ "Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in /usr/local/lib/python3.11/dist-packages (from ipython) (3.0.50)\n",
134
+ "Requirement already satisfied: pygments in /usr/local/lib/python3.11/dist-packages (from ipython) (2.18.0)\n",
135
+ "Requirement already satisfied: backcall in /usr/local/lib/python3.11/dist-packages (from ipython) (0.2.0)\n",
136
+ "Requirement already satisfied: matplotlib-inline in /usr/local/lib/python3.11/dist-packages (from ipython) (0.1.7)\n",
137
+ "Requirement already satisfied: pexpect>4.3 in /usr/local/lib/python3.11/dist-packages (from ipython) (4.9.0)\n",
138
+ "Requirement already satisfied: chardet in /usr/local/lib/python3.11/dist-packages (from reportlab) (5.2.0)\n",
139
+ "Requirement already satisfied: lxml>=3.1.0 in /usr/local/lib/python3.11/dist-packages (from python-docx) (5.3.0)\n",
140
+ "Requirement already satisfied: sniffio>=1.1 in /usr/local/lib/python3.11/dist-packages (from anyio<5.0,>=3.0->gradio) (1.3.1)\n",
141
+ "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.11/dist-packages (from httpx>=0.24.1->gradio) (1.0.7)\n",
142
+ "Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.11/dist-packages (from httpcore==1.*->httpx>=0.24.1->gradio) (0.14.0)\n",
143
+ "Requirement already satisfied: parso<0.9.0,>=0.8.4 in /usr/local/lib/python3.11/dist-packages (from jedi>=0.16->ipython) (0.8.4)\n",
144
+ "Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.11/dist-packages (from pandas<3.0,>=1.0->gradio) (2.8.2)\n",
145
+ "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.11/dist-packages (from pandas<3.0,>=1.0->gradio) (2024.2)\n",
146
+ "Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.11/dist-packages (from pandas<3.0,>=1.0->gradio) (2025.1)\n",
147
+ "Requirement already satisfied: ptyprocess>=0.5 in /usr/local/lib/python3.11/dist-packages (from pexpect>4.3->ipython) (0.7.0)\n",
148
+ "Requirement already satisfied: wcwidth in /usr/local/lib/python3.11/dist-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->ipython) (0.2.13)\n",
149
+ "Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.11/dist-packages (from pydantic>=2.0->gradio) (0.7.0)\n",
150
+ "Requirement already satisfied: pydantic-core==2.27.2 in /usr/local/lib/python3.11/dist-packages (from pydantic>=2.0->gradio) (2.27.2)\n",
151
+ "Requirement already satisfied: click>=8.0.0 in /usr/local/lib/python3.11/dist-packages (from typer<1.0,>=0.12->gradio) (8.1.8)\n",
152
+ "Requirement already satisfied: shellingham>=1.3.0 in /usr/local/lib/python3.11/dist-packages (from typer<1.0,>=0.12->gradio) (1.5.4)\n",
153
+ "Requirement already satisfied: rich>=10.11.0 in /usr/local/lib/python3.11/dist-packages (from typer<1.0,>=0.12->gradio) (13.9.4)\n",
154
+ "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.11/dist-packages (from python-dateutil>=2.8.2->pandas<3.0,>=1.0->gradio) (1.17.0)\n",
155
+ "Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.11/dist-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (3.0.0)\n",
156
+ "Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.11/dist-packages (from markdown-it-py>=2.2.0->rich>=10.11.0->typer<1.0,>=0.12->gradio) (0.1.2)\n",
157
+ "Downloading gradio-5.14.0-py3-none-any.whl (57.7 MB)\n",
158
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m57.7/57.7 MB\u001b[0m \u001b[31m38.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
159
+ "\u001b[?25hDownloading gradio_client-1.7.0-py3-none-any.whl (321 kB)\n",
160
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m321.9/321.9 kB\u001b[0m \u001b[31m31.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
161
+ "\u001b[?25hDownloading spaces-0.32.0-py3-none-any.whl (29 kB)\n",
162
+ "Downloading nvidia_cublas_cu12-12.4.5.8-py3-none-manylinux2014_x86_64.whl (363.4 MB)\n",
163
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━���━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m363.4/363.4 MB\u001b[0m \u001b[31m2.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
164
+ "\u001b[?25hDownloading nvidia_cuda_cupti_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl (13.8 MB)\n",
165
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m13.8/13.8 MB\u001b[0m \u001b[31m111.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
166
+ "\u001b[?25hDownloading nvidia_cuda_nvrtc_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl (24.6 MB)\n",
167
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m24.6/24.6 MB\u001b[0m \u001b[31m86.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
168
+ "\u001b[?25hDownloading nvidia_cuda_runtime_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl (883 kB)\n",
169
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m883.7/883.7 kB\u001b[0m \u001b[31m58.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
170
+ "\u001b[?25hDownloading nvidia_cudnn_cu12-9.1.0.70-py3-none-manylinux2014_x86_64.whl (664.8 MB)\n",
171
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m664.8/664.8 MB\u001b[0m \u001b[31m2.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
172
+ "\u001b[?25hDownloading nvidia_cufft_cu12-11.2.1.3-py3-none-manylinux2014_x86_64.whl (211.5 MB)\n",
173
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m211.5/211.5 MB\u001b[0m \u001b[31m4.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
174
+ "\u001b[?25hDownloading nvidia_curand_cu12-10.3.5.147-py3-none-manylinux2014_x86_64.whl (56.3 MB)\n",
175
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m56.3/56.3 MB\u001b[0m \u001b[31m39.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
176
+ "\u001b[?25hDownloading nvidia_cusolver_cu12-11.6.1.9-py3-none-manylinux2014_x86_64.whl (127.9 MB)\n",
177
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m127.9/127.9 MB\u001b[0m \u001b[31m18.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
178
+ "\u001b[?25hDownloading nvidia_cusparse_cu12-12.3.1.170-py3-none-manylinux2014_x86_64.whl (207.5 MB)\n",
179
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m207.5/207.5 MB\u001b[0m \u001b[31m4.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
180
+ "\u001b[?25hDownloading nvidia_nvjitlink_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl (21.1 MB)\n",
181
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m21.1/21.1 MB\u001b[0m \u001b[31m92.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
182
+ "\u001b[?25hDownloading qwen_vl_utils-0.0.10-py3-none-any.whl (6.7 kB)\n",
183
+ "Downloading av-14.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (39.5 MB)\n",
184
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m39.5/39.5 MB\u001b[0m \u001b[31m57.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
185
+ "\u001b[?25hDownloading reportlab-4.2.5-py3-none-any.whl (1.9 MB)\n",
186
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.9/1.9 MB\u001b[0m \u001b[31m86.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
187
+ "\u001b[?25hDownloading python_docx-1.1.2-py3-none-any.whl (244 kB)\n",
188
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m244.3/244.3 kB\u001b[0m \u001b[31m24.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
189
+ "\u001b[?25hDownloading aiofiles-23.2.1-py3-none-any.whl (15 kB)\n",
190
+ "Downloading fastapi-0.115.8-py3-none-any.whl (94 kB)\n",
191
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m94.8/94.8 kB\u001b[0m \u001b[31m10.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
192
+ "\u001b[?25hDownloading jedi-0.19.2-py2.py3-none-any.whl (1.6 MB)\n",
193
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.6/1.6 MB\u001b[0m \u001b[31m74.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
194
+ "\u001b[?25hDownloading MarkupSafe-2.1.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (28 kB)\n",
195
+ "Downloading python_multipart-0.0.20-py3-none-any.whl (24 kB)\n",
196
+ "Downloading ruff-0.9.4-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.4 MB)\n",
197
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m12.4/12.4 MB\u001b[0m \u001b[31m105.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
198
+ "\u001b[?25hDownloading safehttpx-0.1.6-py3-none-any.whl (8.7 kB)\n",
199
+ "Downloading semantic_version-2.10.0-py2.py3-none-any.whl (15 kB)\n",
200
+ "Downloading starlette-0.45.3-py3-none-any.whl (71 kB)\n",
201
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m71.5/71.5 kB\u001b[0m \u001b[31m7.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
202
+ "\u001b[?25hDownloading tomlkit-0.13.2-py3-none-any.whl (37 kB)\n",
203
+ "Downloading uvicorn-0.34.0-py3-none-any.whl (62 kB)\n",
204
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m62.3/62.3 kB\u001b[0m \u001b[31m6.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
205
+ "\u001b[?25hDownloading ffmpy-0.5.0-py3-none-any.whl (6.0 kB)\n",
206
+ "Downloading pydub-0.25.1-py2.py3-none-any.whl (32 kB)\n",
207
+ "Building wheels for collected packages: fpdf\n",
208
+ " Building wheel for fpdf (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
209
+ " Created wheel for fpdf: filename=fpdf-1.7.2-py2.py3-none-any.whl size=40704 sha256=ca4d2c3df640c199d761f48bf1315c641e2cddc8aad3891cb9088fc05ab88413\n",
210
+ " Stored in directory: /root/.cache/pip/wheels/65/4f/66/bbda9866da446a72e206d6484cd97381cbc7859a7068541c36\n",
211
+ "Successfully built fpdf\n",
212
+ "Installing collected packages: pydub, fpdf, uvicorn, tomlkit, semantic-version, ruff, reportlab, python-multipart, python-docx, nvidia-nvjitlink-cu12, nvidia-curand-cu12, nvidia-cufft-cu12, nvidia-cuda-runtime-cu12, nvidia-cuda-nvrtc-cu12, nvidia-cuda-cupti-cu12, nvidia-cublas-cu12, markupsafe, jedi, ffmpy, av, aiofiles, starlette, qwen-vl-utils, nvidia-cusparse-cu12, nvidia-cudnn-cu12, safehttpx, nvidia-cusolver-cu12, gradio-client, fastapi, gradio, spaces\n",
213
+ " Attempting uninstall: nvidia-nvjitlink-cu12\n",
214
+ " Found existing installation: nvidia-nvjitlink-cu12 12.5.82\n",
215
+ " Uninstalling nvidia-nvjitlink-cu12-12.5.82:\n",
216
+ " Successfully uninstalled nvidia-nvjitlink-cu12-12.5.82\n",
217
+ " Attempting uninstall: nvidia-curand-cu12\n",
218
+ " Found existing installation: nvidia-curand-cu12 10.3.6.82\n",
219
+ " Uninstalling nvidia-curand-cu12-10.3.6.82:\n",
220
+ " Successfully uninstalled nvidia-curand-cu12-10.3.6.82\n",
221
+ " Attempting uninstall: nvidia-cufft-cu12\n",
222
+ " Found existing installation: nvidia-cufft-cu12 11.2.3.61\n",
223
+ " Uninstalling nvidia-cufft-cu12-11.2.3.61:\n",
224
+ " Successfully uninstalled nvidia-cufft-cu12-11.2.3.61\n",
225
+ " Attempting uninstall: nvidia-cuda-runtime-cu12\n",
226
+ " Found existing installation: nvidia-cuda-runtime-cu12 12.5.82\n",
227
+ " Uninstalling nvidia-cuda-runtime-cu12-12.5.82:\n",
228
+ " Successfully uninstalled nvidia-cuda-runtime-cu12-12.5.82\n",
229
+ " Attempting uninstall: nvidia-cuda-nvrtc-cu12\n",
230
+ " Found existing installation: nvidia-cuda-nvrtc-cu12 12.5.82\n",
231
+ " Uninstalling nvidia-cuda-nvrtc-cu12-12.5.82:\n",
232
+ " Successfully uninstalled nvidia-cuda-nvrtc-cu12-12.5.82\n",
233
+ " Attempting uninstall: nvidia-cuda-cupti-cu12\n",
234
+ " Found existing installation: nvidia-cuda-cupti-cu12 12.5.82\n",
235
+ " Uninstalling nvidia-cuda-cupti-cu12-12.5.82:\n",
236
+ " Successfully uninstalled nvidia-cuda-cupti-cu12-12.5.82\n",
237
+ " Attempting uninstall: nvidia-cublas-cu12\n",
238
+ " Found existing installation: nvidia-cublas-cu12 12.5.3.2\n",
239
+ " Uninstalling nvidia-cublas-cu12-12.5.3.2:\n",
240
+ " Successfully uninstalled nvidia-cublas-cu12-12.5.3.2\n",
241
+ " Attempting uninstall: markupsafe\n",
242
+ " Found existing installation: MarkupSafe 3.0.2\n",
243
+ " Uninstalling MarkupSafe-3.0.2:\n",
244
+ " Successfully uninstalled MarkupSafe-3.0.2\n",
245
+ " Attempting uninstall: nvidia-cusparse-cu12\n",
246
+ " Found existing installation: nvidia-cusparse-cu12 12.5.1.3\n",
247
+ " Uninstalling nvidia-cusparse-cu12-12.5.1.3:\n",
248
+ " Successfully uninstalled nvidia-cusparse-cu12-12.5.1.3\n",
249
+ " Attempting uninstall: nvidia-cudnn-cu12\n",
250
+ " Found existing installation: nvidia-cudnn-cu12 9.3.0.75\n",
251
+ " Uninstalling nvidia-cudnn-cu12-9.3.0.75:\n",
252
+ " Successfully uninstalled nvidia-cudnn-cu12-9.3.0.75\n",
253
+ " Attempting uninstall: nvidia-cusolver-cu12\n",
254
+ " Found existing installation: nvidia-cusolver-cu12 11.6.3.83\n",
255
+ " Uninstalling nvidia-cusolver-cu12-11.6.3.83:\n",
256
+ " Successfully uninstalled nvidia-cusolver-cu12-11.6.3.83\n",
257
+ "Successfully installed aiofiles-23.2.1 av-14.1.0 fastapi-0.115.8 ffmpy-0.5.0 fpdf-1.7.2 gradio-5.14.0 gradio-client-1.7.0 jedi-0.19.2 markupsafe-2.1.5 nvidia-cublas-cu12-12.4.5.8 nvidia-cuda-cupti-cu12-12.4.127 nvidia-cuda-nvrtc-cu12-12.4.127 nvidia-cuda-runtime-cu12-12.4.127 nvidia-cudnn-cu12-9.1.0.70 nvidia-cufft-cu12-11.2.1.3 nvidia-curand-cu12-10.3.5.147 nvidia-cusolver-cu12-11.6.1.9 nvidia-cusparse-cu12-12.3.1.170 nvidia-nvjitlink-cu12-12.4.127 pydub-0.25.1 python-docx-1.1.2 python-multipart-0.0.20 qwen-vl-utils-0.0.10 reportlab-4.2.5 ruff-0.9.4 safehttpx-0.1.6 semantic-version-2.10.0 spaces-0.32.0 starlette-0.45.3 tomlkit-0.13.2 uvicorn-0.34.0\n"
258
+ ]
259
+ }
260
+ ],
261
+ "source": [
262
+ "!pip install gradio spaces transformers accelerate numpy requests torch torchvision qwen-vl-utils av ipython reportlab fpdf python-docx pillow huggingface_hub"
263
+ ]
264
+ },
265
+ {
266
+ "cell_type": "code",
267
+ "execution_count": null,
268
+ "metadata": {
269
+ "colab": {
270
+ "base_uri": "https://localhost:8080/",
271
+ "height": 682
272
+ },
273
+ "id": "ovBSsRFhGbs2",
274
+ "outputId": "3a438213-78a7-4378-d07c-c7bc832e3773"
275
+ },
276
+ "outputs": [
277
+ {
278
+ "name": "stderr",
279
+ "output_type": "stream",
280
+ "text": [
281
+ "The argument `trust_remote_code` is to be used with Auto classes. It has no effect here and is ignored.\n"
282
+ ]
283
+ },
284
+ {
285
+ "name": "stdout",
286
+ "output_type": "stream",
287
+ "text": [
288
+ "Loading latexmind...\n",
289
+ "Running Gradio in a Colab notebook requires sharing enabled. Automatically setting `share=True` (you can turn this off by setting `share=False` in `launch()` explicitly).\n",
290
+ "\n",
291
+ "Colab notebook detected. This cell will run indefinitely so that you can see errors and logs. To turn off, set debug=False in launch().\n",
292
+ "* Running on public URL: https://22ef056d58ffefce09.gradio.live\n",
293
+ "\n",
294
+ "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from the terminal in the working directory to deploy to Hugging Face Spaces (https://huggingface.co/spaces)\n"
295
+ ]
296
+ },
297
+ {
298
+ "data": {
299
+ "text/html": [
300
+ "<div><iframe src=\"https://22ef056d58ffefce09.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
301
+ ],
302
+ "text/plain": [
303
+ "<IPython.core.display.HTML object>"
304
+ ]
305
+ },
306
+ "metadata": {},
307
+ "output_type": "display_data"
308
+ }
309
+ ],
310
+ "source": [
311
+ "# Authenticate with Hugging Face\n",
312
+ "from huggingface_hub import login\n",
313
+ "\n",
314
+ "# Log in to Hugging Face using the provided token\n",
315
+ "hf_token = '--------xxx---------'\n",
316
+ "login(hf_token)\n",
317
+ "\n",
318
+ "#Demo\n",
319
+ "import gradio as gr\n",
320
+ "import spaces\n",
321
+ "from transformers import Qwen2VLForConditionalGeneration, AutoProcessor, TextIteratorStreamer\n",
322
+ "from qwen_vl_utils import process_vision_info\n",
323
+ "import torch\n",
324
+ "from PIL import Image\n",
325
+ "import os\n",
326
+ "import uuid\n",
327
+ "import io\n",
328
+ "from threading import Thread\n",
329
+ "from reportlab.lib.pagesizes import A4\n",
330
+ "from reportlab.lib.styles import getSampleStyleSheet\n",
331
+ "from reportlab.lib import colors\n",
332
+ "from reportlab.platypus import SimpleDocTemplate, Image as RLImage, Paragraph, Spacer\n",
333
+ "from reportlab.lib.units import inch\n",
334
+ "from reportlab.pdfbase import pdfmetrics\n",
335
+ "from reportlab.pdfbase.ttfonts import TTFont\n",
336
+ "import docx\n",
337
+ "from docx.enum.text import WD_ALIGN_PARAGRAPH\n",
338
+ "\n",
339
+ "# Define model options\n",
340
+ "MODEL_OPTIONS = {\n",
341
+ " \"latexmind\": \"prithivMLmods/LatexMind-2B-Codec\",\n",
342
+ "}\n",
343
+ "\n",
344
+ "# Preload models and processors into CUDA\n",
345
+ "models = {}\n",
346
+ "processors = {}\n",
347
+ "for name, model_id in MODEL_OPTIONS.items():\n",
348
+ " print(f\"Loading {name}...\")\n",
349
+ " models[name] = Qwen2VLForConditionalGeneration.from_pretrained(\n",
350
+ " model_id,\n",
351
+ " trust_remote_code=True,\n",
352
+ " torch_dtype=torch.float16\n",
353
+ " ).to(\"cuda\").eval()\n",
354
+ " processors[name] = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)\n",
355
+ "\n",
356
+ "image_extensions = Image.registered_extensions()\n",
357
+ "\n",
358
+ "def identify_and_save_blob(blob_path):\n",
359
+ " \"\"\"Identifies if the blob is an image and saves it.\"\"\"\n",
360
+ " try:\n",
361
+ " with open(blob_path, 'rb') as file:\n",
362
+ " blob_content = file.read()\n",
363
+ " try:\n",
364
+ " Image.open(io.BytesIO(blob_content)).verify() # Check if it's a valid image\n",
365
+ " extension = \".png\" # Default to PNG for saving\n",
366
+ " media_type = \"image\"\n",
367
+ " except (IOError, SyntaxError):\n",
368
+ " raise ValueError(\"Unsupported media type. Please upload a valid image.\")\n",
369
+ "\n",
370
+ " filename = f\"temp_{uuid.uuid4()}_media{extension}\"\n",
371
+ " with open(filename, \"wb\") as f:\n",
372
+ " f.write(blob_content)\n",
373
+ "\n",
374
+ " return filename, media_type\n",
375
+ "\n",
376
+ " except FileNotFoundError:\n",
377
+ " raise ValueError(f\"The file {blob_path} was not found.\")\n",
378
+ " except Exception as e:\n",
379
+ " raise ValueError(f\"An error occurred while processing the file: {e}\")\n",
380
+ "\n",
381
+ "@spaces.GPU\n",
382
+ "def qwen_inference(model_name, media_input, text_input=None):\n",
383
+ " \"\"\"Handles inference for the selected model.\"\"\"\n",
384
+ " model = models[model_name]\n",
385
+ " processor = processors[model_name]\n",
386
+ "\n",
387
+ " if isinstance(media_input, str):\n",
388
+ " media_path = media_input\n",
389
+ " if media_path.endswith(tuple([i for i in image_extensions.keys()])):\n",
390
+ " media_type = \"image\"\n",
391
+ " else:\n",
392
+ " try:\n",
393
+ " media_path, media_type = identify_and_save_blob(media_input)\n",
394
+ " except Exception as e:\n",
395
+ " raise ValueError(\"Unsupported media type. Please upload a valid image.\")\n",
396
+ "\n",
397
+ " messages = [\n",
398
+ " {\n",
399
+ " \"role\": \"user\",\n",
400
+ " \"content\": [\n",
401
+ " {\n",
402
+ " \"type\": media_type,\n",
403
+ " media_type: media_path\n",
404
+ " },\n",
405
+ " {\"type\": \"text\", \"text\": text_input},\n",
406
+ " ],\n",
407
+ " }\n",
408
+ " ]\n",
409
+ "\n",
410
+ " text = processor.apply_chat_template(\n",
411
+ " messages, tokenize=False, add_generation_prompt=True\n",
412
+ " )\n",
413
+ " image_inputs, _ = process_vision_info(messages)\n",
414
+ " inputs = processor(\n",
415
+ " text=[text],\n",
416
+ " images=image_inputs,\n",
417
+ " padding=True,\n",
418
+ " return_tensors=\"pt\",\n",
419
+ " ).to(\"cuda\")\n",
420
+ "\n",
421
+ " streamer = TextIteratorStreamer(\n",
422
+ " processor.tokenizer, skip_prompt=True, skip_special_tokens=True\n",
423
+ " )\n",
424
+ " generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=1024)\n",
425
+ "\n",
426
+ " thread = Thread(target=model.generate, kwargs=generation_kwargs)\n",
427
+ " thread.start()\n",
428
+ "\n",
429
+ " buffer = \"\"\n",
430
+ " for new_text in streamer:\n",
431
+ " buffer += new_text\n",
432
+ " # Remove <|im_end|> or similar tokens from the output\n",
433
+ " buffer = buffer.replace(\"<|im_end|>\", \"\")\n",
434
+ " yield buffer\n",
435
+ "\n",
436
+ "def format_plain_text(output_text):\n",
437
+ " \"\"\"Formats the output text as plain text without LaTeX delimiters.\"\"\"\n",
438
+ " # Remove LaTeX delimiters and convert to plain text\n",
439
+ " plain_text = output_text.replace(\"\\\\(\", \"\").replace(\"\\\\)\", \"\").replace(\"\\\\[\", \"\").replace(\"\\\\]\", \"\")\n",
440
+ " return plain_text\n",
441
+ "\n",
442
+ "def generate_document(media_path, output_text, file_format, font_size, line_spacing, alignment, image_size):\n",
443
+ " \"\"\"Generates a document with the input image and plain text output.\"\"\"\n",
444
+ " plain_text = format_plain_text(output_text)\n",
445
+ " if file_format == \"pdf\":\n",
446
+ " return generate_pdf(media_path, plain_text, font_size, line_spacing, alignment, image_size)\n",
447
+ " elif file_format == \"docx\":\n",
448
+ " return generate_docx(media_path, plain_text, font_size, line_spacing, alignment, image_size)\n",
449
+ "\n",
450
+ "def generate_pdf(media_path, plain_text, font_size, line_spacing, alignment, image_size):\n",
451
+ " \"\"\"Generates a PDF document.\"\"\"\n",
452
+ " filename = f\"output_{uuid.uuid4()}.pdf\"\n",
453
+ " doc = SimpleDocTemplate(\n",
454
+ " filename,\n",
455
+ " pagesize=A4,\n",
456
+ " rightMargin=inch,\n",
457
+ " leftMargin=inch,\n",
458
+ " topMargin=inch,\n",
459
+ " bottomMargin=inch\n",
460
+ " )\n",
461
+ " styles = getSampleStyleSheet()\n",
462
+ " styles[\"Normal\"].fontSize = int(font_size)\n",
463
+ " styles[\"Normal\"].leading = int(font_size) * line_spacing\n",
464
+ " styles[\"Normal\"].alignment = {\n",
465
+ " \"Left\": 0,\n",
466
+ " \"Center\": 1,\n",
467
+ " \"Right\": 2,\n",
468
+ " \"Justified\": 4\n",
469
+ " }[alignment]\n",
470
+ "\n",
471
+ " story = []\n",
472
+ "\n",
473
+ " # Add image with size adjustment\n",
474
+ " image_sizes = {\n",
475
+ " \"Small\": (200, 200),\n",
476
+ " \"Medium\": (400, 400),\n",
477
+ " \"Large\": (600, 600)\n",
478
+ " }\n",
479
+ " img = RLImage(media_path, width=image_sizes[image_size][0], height=image_sizes[image_size][1])\n",
480
+ " story.append(img)\n",
481
+ " story.append(Spacer(1, 12))\n",
482
+ "\n",
483
+ " # Add plain text output\n",
484
+ " text = Paragraph(plain_text, styles[\"Normal\"])\n",
485
+ " story.append(text)\n",
486
+ "\n",
487
+ " doc.build(story)\n",
488
+ " return filename\n",
489
+ "\n",
490
+ "def generate_docx(media_path, plain_text, font_size, line_spacing, alignment, image_size):\n",
491
+ " \"\"\"Generates a DOCX document.\"\"\"\n",
492
+ " filename = f\"output_{uuid.uuid4()}.docx\"\n",
493
+ " doc = docx.Document()\n",
494
+ "\n",
495
+ " # Add image with size adjustment\n",
496
+ " image_sizes = {\n",
497
+ " \"Small\": docx.shared.Inches(2),\n",
498
+ " \"Medium\": docx.shared.Inches(4),\n",
499
+ " \"Large\": docx.shared.Inches(6)\n",
500
+ " }\n",
501
+ " doc.add_picture(media_path, width=image_sizes[image_size])\n",
502
+ " doc.add_paragraph()\n",
503
+ "\n",
504
+ " # Add plain text output\n",
505
+ " paragraph = doc.add_paragraph()\n",
506
+ " paragraph.paragraph_format.line_spacing = line_spacing\n",
507
+ " paragraph.paragraph_format.alignment = {\n",
508
+ " \"Left\": WD_ALIGN_PARAGRAPH.LEFT,\n",
509
+ " \"Center\": WD_ALIGN_PARAGRAPH.CENTER,\n",
510
+ " \"Right\": WD_ALIGN_PARAGRAPH.RIGHT,\n",
511
+ " \"Justified\": WD_ALIGN_PARAGRAPH.JUSTIFY\n",
512
+ " }[alignment]\n",
513
+ " run = paragraph.add_run(plain_text)\n",
514
+ " run.font.size = docx.shared.Pt(int(font_size))\n",
515
+ "\n",
516
+ " doc.save(filename)\n",
517
+ " return filename\n",
518
+ "\n",
519
+ "# CSS for output styling\n",
520
+ "css = \"\"\"\n",
521
+ " #output {\n",
522
+ " height: 500px;\n",
523
+ " overflow: auto;\n",
524
+ " border: 1px solid #ccc;\n",
525
+ " }\n",
526
+ ".submit-btn {\n",
527
+ " background-color: #cf3434 !important;\n",
528
+ " color: white !important;\n",
529
+ "}\n",
530
+ ".submit-btn:hover {\n",
531
+ " background-color: #ff2323 !important;\n",
532
+ "}\n",
533
+ ".download-btn {\n",
534
+ " background-color: #35a6d6 !important;\n",
535
+ " color: white !important;\n",
536
+ "}\n",
537
+ ".download-btn:hover {\n",
538
+ " background-color: #22bcff !important;\n",
539
+ "}\n",
540
+ "\"\"\"\n",
541
+ "\n",
542
+ "# Gradio app setup\n",
543
+ "with gr.Blocks(css=css) as demo:\n",
544
+ " gr.Markdown(\"# Latexmind\")\n",
545
+ "\n",
546
+ " with gr.Tab(label=\"Image Input\"):\n",
547
+ "\n",
548
+ " with gr.Row():\n",
549
+ " with gr.Column():\n",
550
+ " model_choice = gr.Dropdown(\n",
551
+ " label=\"Model Selection\",\n",
552
+ " choices=list(MODEL_OPTIONS.keys()),\n",
553
+ " value=\"latexmind\"\n",
554
+ " )\n",
555
+ " input_media = gr.File(\n",
556
+ " label=\"Upload Image\", type=\"filepath\"\n",
557
+ " )\n",
558
+ " text_input = gr.Textbox(label=\"Question\", placeholder=\"Ask a question about the image...\")\n",
559
+ " submit_btn = gr.Button(value=\"Submit\", elem_classes=\"submit-btn\")\n",
560
+ "\n",
561
+ " with gr.Column():\n",
562
+ " output_text = gr.Textbox(label=\"Output Text\", lines=10)\n",
563
+ " plain_text_output = gr.Textbox(label=\"Standardized Plain Text\", lines=10)\n",
564
+ "\n",
565
+ " submit_btn.click(\n",
566
+ " qwen_inference, [model_choice, input_media, text_input], [output_text]\n",
567
+ " ).then(\n",
568
+ " lambda output_text: format_plain_text(output_text), [output_text], [plain_text_output]\n",
569
+ " )\n",
570
+ "\n",
571
+ " # Add examples directly usable by clicking\n",
572
+ " with gr.Row():\n",
573
+ " with gr.Column():\n",
574
+ " line_spacing = gr.Dropdown(\n",
575
+ " choices=[0.5, 1.0, 1.15, 1.5, 2.0, 2.5, 3.0],\n",
576
+ " value=1.5,\n",
577
+ " label=\"Line Spacing\"\n",
578
+ " )\n",
579
+ " font_size = gr.Dropdown(\n",
580
+ " choices=[\"8\", \"10\", \"12\", \"14\", \"16\", \"18\", \"20\", \"22\", \"24\"],\n",
581
+ " value=\"18\",\n",
582
+ " label=\"Font Size\"\n",
583
+ " )\n",
584
+ " alignment = gr.Dropdown(\n",
585
+ " choices=[\"Left\", \"Center\", \"Right\", \"Justified\"],\n",
586
+ " value=\"Justified\",\n",
587
+ " label=\"Text Alignment\"\n",
588
+ " )\n",
589
+ " image_size = gr.Dropdown(\n",
590
+ " choices=[\"Small\", \"Medium\", \"Large\"],\n",
591
+ " value=\"Small\",\n",
592
+ " label=\"Image Size\"\n",
593
+ " )\n",
594
+ " file_format = gr.Radio([\"pdf\", \"docx\"], label=\"File Format\", value=\"pdf\")\n",
595
+ " get_document_btn = gr.Button(value=\"Get Document\", elem_classes=\"download-btn\")\n",
596
+ "\n",
597
+ " get_document_btn.click(\n",
598
+ " generate_document, [input_media, output_text, file_format, font_size, line_spacing, alignment, image_size], gr.File(label=\"Download Document\")\n",
599
+ " )\n",
600
+ "\n",
601
+ "demo.launch(debug=True)"
602
+ ]
603
+ }
604
+ ],
605
+ "metadata": {
606
+ "accelerator": "GPU",
607
+ "colab": {
608
+ "gpuType": "L4",
609
+ "machine_shape": "hm",
610
+ "provenance": []
611
+ },
612
+ "kernelspec": {
613
+ "display_name": "Python 3",
614
+ "name": "python3"
615
+ },
616
+ "language_info": {
617
+ "name": "python"
618
+ }
619
+ },
620
+ "nbformat": 4,
621
+ "nbformat_minor": 0
622
+ }