初始化项目,由ModelHub XC社区提供模型
Model: prithivMLmods/Qwen2-VL-OCR-2B-Instruct Source: Original Platform
This commit is contained in:
37
.gitattributes
vendored
Normal file
37
.gitattributes
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
*.7z filter=lfs diff=lfs merge=lfs -text
|
||||
*.arrow filter=lfs diff=lfs merge=lfs -text
|
||||
*.bin filter=lfs diff=lfs merge=lfs -text
|
||||
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
||||
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
||||
*.ftz filter=lfs diff=lfs merge=lfs -text
|
||||
*.gz filter=lfs diff=lfs merge=lfs -text
|
||||
*.h5 filter=lfs diff=lfs merge=lfs -text
|
||||
*.joblib filter=lfs diff=lfs merge=lfs -text
|
||||
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
||||
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
||||
*.model filter=lfs diff=lfs merge=lfs -text
|
||||
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
||||
*.npy filter=lfs diff=lfs merge=lfs -text
|
||||
*.npz filter=lfs diff=lfs merge=lfs -text
|
||||
*.onnx filter=lfs diff=lfs merge=lfs -text
|
||||
*.ot filter=lfs diff=lfs merge=lfs -text
|
||||
*.parquet filter=lfs diff=lfs merge=lfs -text
|
||||
*.pb filter=lfs diff=lfs merge=lfs -text
|
||||
*.pickle filter=lfs diff=lfs merge=lfs -text
|
||||
*.pkl filter=lfs diff=lfs merge=lfs -text
|
||||
*.pt filter=lfs diff=lfs merge=lfs -text
|
||||
*.pth filter=lfs diff=lfs merge=lfs -text
|
||||
*.rar filter=lfs diff=lfs merge=lfs -text
|
||||
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
||||
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
||||
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
||||
*.tar filter=lfs diff=lfs merge=lfs -text
|
||||
*.tflite filter=lfs diff=lfs merge=lfs -text
|
||||
*.tgz filter=lfs diff=lfs merge=lfs -text
|
||||
*.wasm filter=lfs diff=lfs merge=lfs -text
|
||||
*.xz filter=lfs diff=lfs merge=lfs -text
|
||||
*.zip filter=lfs diff=lfs merge=lfs -text
|
||||
*.zst filter=lfs diff=lfs merge=lfs -text
|
||||
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
||||
Qwen2vl[[:space:]]With[[:space:]]ReportLab[[:space:]]Documentation/font/calibri.ttf filter=lfs diff=lfs merge=lfs -text
|
||||
Qwen2vl[[:space:]]With[[:space:]]ReportLab[[:space:]]Documentation/font/youyuan.TTF filter=lfs diff=lfs merge=lfs -text
|
||||
556
Demo/ocrtest_qwen.ipynb
Normal file
556
Demo/ocrtest_qwen.ipynb
Normal file
@@ -0,0 +1,556 @@
|
||||
{
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"provenance": [],
|
||||
"gpuType": "T4"
|
||||
},
|
||||
"kernelspec": {
|
||||
"name": "python3",
|
||||
"display_name": "Python 3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python"
|
||||
},
|
||||
"accelerator": "GPU"
|
||||
},
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"# **FT; Key Information Extraction**\n",
|
||||
"Qwen2VLForConditionalGeneration"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "-b4-SW1aGOcF"
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"!pip install gradio spaces transformers accelerate numpy requests torch torchvision qwen-vl-utils av ipython reportlab fpdf python-docx pillow huggingface_hub"
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "oDmd1ZObGSel",
|
||||
"outputId": "5b01f267-d5af-4409-cf67-6c318388d584"
|
||||
},
|
||||
"execution_count": 1,
|
||||
"outputs": [
|
||||
{
|
||||
"output_type": "stream",
|
||||
"name": "stdout",
|
||||
"text": [
|
||||
"Collecting gradio\n",
|
||||
" Downloading gradio-5.9.1-py3-none-any.whl.metadata (16 kB)\n",
|
||||
"Collecting spaces\n",
|
||||
" Downloading spaces-0.31.1-py3-none-any.whl.metadata (1.0 kB)\n",
|
||||
"Requirement already satisfied: transformers in /usr/local/lib/python3.10/dist-packages (4.47.1)\n",
|
||||
"Requirement already satisfied: accelerate in /usr/local/lib/python3.10/dist-packages (1.2.1)\n",
|
||||
"Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (1.26.4)\n",
|
||||
"Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (2.32.3)\n",
|
||||
"Requirement already satisfied: torch in /usr/local/lib/python3.10/dist-packages (2.5.1+cu121)\n",
|
||||
"Requirement already satisfied: torchvision in /usr/local/lib/python3.10/dist-packages (0.20.1+cu121)\n",
|
||||
"Collecting qwen-vl-utils\n",
|
||||
" Downloading qwen_vl_utils-0.0.8-py3-none-any.whl.metadata (3.6 kB)\n",
|
||||
"Collecting av\n",
|
||||
" Downloading av-14.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (4.5 kB)\n",
|
||||
"Requirement already satisfied: ipython in /usr/local/lib/python3.10/dist-packages (7.34.0)\n",
|
||||
"Collecting reportlab\n",
|
||||
" Downloading reportlab-4.2.5-py3-none-any.whl.metadata (1.5 kB)\n",
|
||||
"Collecting fpdf\n",
|
||||
" Downloading fpdf-1.7.2.tar.gz (39 kB)\n",
|
||||
" Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
|
||||
"Collecting python-docx\n",
|
||||
" Downloading python_docx-1.1.2-py3-none-any.whl.metadata (2.0 kB)\n",
|
||||
"Requirement already satisfied: pillow in /usr/local/lib/python3.10/dist-packages (11.0.0)\n",
|
||||
"Requirement already satisfied: huggingface_hub in /usr/local/lib/python3.10/dist-packages (0.27.0)\n",
|
||||
"Collecting aiofiles<24.0,>=22.0 (from gradio)\n",
|
||||
" Downloading aiofiles-23.2.1-py3-none-any.whl.metadata (9.7 kB)\n",
|
||||
"Requirement already satisfied: anyio<5.0,>=3.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.7.1)\n",
|
||||
"Collecting fastapi<1.0,>=0.115.2 (from gradio)\n",
|
||||
" Downloading fastapi-0.115.6-py3-none-any.whl.metadata (27 kB)\n",
|
||||
"Collecting ffmpy (from gradio)\n",
|
||||
" Downloading ffmpy-0.5.0-py3-none-any.whl.metadata (3.0 kB)\n",
|
||||
"Collecting gradio-client==1.5.2 (from gradio)\n",
|
||||
" Downloading gradio_client-1.5.2-py3-none-any.whl.metadata (7.1 kB)\n",
|
||||
"Requirement already satisfied: httpx>=0.24.1 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.28.1)\n",
|
||||
"Requirement already satisfied: jinja2<4.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.1.4)\n",
|
||||
"Collecting markupsafe~=2.0 (from gradio)\n",
|
||||
" Downloading MarkupSafe-2.1.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (3.0 kB)\n",
|
||||
"Requirement already satisfied: orjson~=3.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.10.12)\n",
|
||||
"Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from gradio) (24.2)\n",
|
||||
"Requirement already satisfied: pandas<3.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.2.2)\n",
|
||||
"Requirement already satisfied: pydantic>=2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.10.3)\n",
|
||||
"Collecting pydub (from gradio)\n",
|
||||
" Downloading pydub-0.25.1-py2.py3-none-any.whl.metadata (1.4 kB)\n",
|
||||
"Collecting python-multipart>=0.0.18 (from gradio)\n",
|
||||
" Downloading python_multipart-0.0.20-py3-none-any.whl.metadata (1.8 kB)\n",
|
||||
"Requirement already satisfied: pyyaml<7.0,>=5.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (6.0.2)\n",
|
||||
"Collecting ruff>=0.2.2 (from gradio)\n",
|
||||
" Downloading ruff-0.8.5-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (25 kB)\n",
|
||||
"Collecting safehttpx<0.2.0,>=0.1.6 (from gradio)\n",
|
||||
" Downloading safehttpx-0.1.6-py3-none-any.whl.metadata (4.2 kB)\n",
|
||||
"Collecting semantic-version~=2.0 (from gradio)\n",
|
||||
" Downloading semantic_version-2.10.0-py2.py3-none-any.whl.metadata (9.7 kB)\n",
|
||||
"Collecting starlette<1.0,>=0.40.0 (from gradio)\n",
|
||||
" Downloading starlette-0.45.1-py3-none-any.whl.metadata (6.3 kB)\n",
|
||||
"Collecting tomlkit<0.14.0,>=0.12.0 (from gradio)\n",
|
||||
" Downloading tomlkit-0.13.2-py3-none-any.whl.metadata (2.7 kB)\n",
|
||||
"Requirement already satisfied: typer<1.0,>=0.12 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.15.1)\n",
|
||||
"Requirement already satisfied: typing-extensions~=4.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (4.12.2)\n",
|
||||
"Collecting uvicorn>=0.14.0 (from gradio)\n",
|
||||
" Downloading uvicorn-0.34.0-py3-none-any.whl.metadata (6.5 kB)\n",
|
||||
"Requirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-packages (from gradio-client==1.5.2->gradio) (2024.10.0)\n",
|
||||
"Requirement already satisfied: websockets<15.0,>=10.0 in /usr/local/lib/python3.10/dist-packages (from gradio-client==1.5.2->gradio) (14.1)\n",
|
||||
"Requirement already satisfied: psutil<6,>=2 in /usr/local/lib/python3.10/dist-packages (from spaces) (5.9.5)\n",
|
||||
"Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from transformers) (3.16.1)\n",
|
||||
"Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.10/dist-packages (from transformers) (2024.11.6)\n",
|
||||
"Requirement already satisfied: tokenizers<0.22,>=0.21 in /usr/local/lib/python3.10/dist-packages (from transformers) (0.21.0)\n",
|
||||
"Requirement already satisfied: safetensors>=0.4.1 in /usr/local/lib/python3.10/dist-packages (from transformers) (0.4.5)\n",
|
||||
"Requirement already satisfied: tqdm>=4.27 in /usr/local/lib/python3.10/dist-packages (from transformers) (4.67.1)\n",
|
||||
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests) (3.4.0)\n",
|
||||
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests) (3.10)\n",
|
||||
"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests) (2.2.3)\n",
|
||||
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests) (2024.12.14)\n",
|
||||
"Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch) (3.4.2)\n",
|
||||
"Requirement already satisfied: sympy==1.13.1 in /usr/local/lib/python3.10/dist-packages (from torch) (1.13.1)\n",
|
||||
"Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from sympy==1.13.1->torch) (1.3.0)\n",
|
||||
"Requirement already satisfied: setuptools>=18.5 in /usr/local/lib/python3.10/dist-packages (from ipython) (75.1.0)\n",
|
||||
"Collecting jedi>=0.16 (from ipython)\n",
|
||||
" Downloading jedi-0.19.2-py2.py3-none-any.whl.metadata (22 kB)\n",
|
||||
"Requirement already satisfied: decorator in /usr/local/lib/python3.10/dist-packages (from ipython) (4.4.2)\n",
|
||||
"Requirement already satisfied: pickleshare in /usr/local/lib/python3.10/dist-packages (from ipython) (0.7.5)\n",
|
||||
"Requirement already satisfied: traitlets>=4.2 in /usr/local/lib/python3.10/dist-packages (from ipython) (5.7.1)\n",
|
||||
"Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from ipython) (3.0.48)\n",
|
||||
"Requirement already satisfied: pygments in /usr/local/lib/python3.10/dist-packages (from ipython) (2.18.0)\n",
|
||||
"Requirement already satisfied: backcall in /usr/local/lib/python3.10/dist-packages (from ipython) (0.2.0)\n",
|
||||
"Requirement already satisfied: matplotlib-inline in /usr/local/lib/python3.10/dist-packages (from ipython) (0.1.7)\n",
|
||||
"Requirement already satisfied: pexpect>4.3 in /usr/local/lib/python3.10/dist-packages (from ipython) (4.9.0)\n",
|
||||
"Requirement already satisfied: chardet in /usr/local/lib/python3.10/dist-packages (from reportlab) (5.2.0)\n",
|
||||
"Requirement already satisfied: lxml>=3.1.0 in /usr/local/lib/python3.10/dist-packages (from python-docx) (5.3.0)\n",
|
||||
"Requirement already satisfied: sniffio>=1.1 in /usr/local/lib/python3.10/dist-packages (from anyio<5.0,>=3.0->gradio) (1.3.1)\n",
|
||||
"Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5.0,>=3.0->gradio) (1.2.2)\n",
|
||||
"Collecting starlette<1.0,>=0.40.0 (from gradio)\n",
|
||||
" Downloading starlette-0.41.3-py3-none-any.whl.metadata (6.0 kB)\n",
|
||||
"Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.10/dist-packages (from httpx>=0.24.1->gradio) (1.0.7)\n",
|
||||
"Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.10/dist-packages (from httpcore==1.*->httpx>=0.24.1->gradio) (0.14.0)\n",
|
||||
"Requirement already satisfied: parso<0.9.0,>=0.8.4 in /usr/local/lib/python3.10/dist-packages (from jedi>=0.16->ipython) (0.8.4)\n",
|
||||
"Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.10/dist-packages (from pandas<3.0,>=1.0->gradio) (2.8.2)\n",
|
||||
"Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas<3.0,>=1.0->gradio) (2024.2)\n",
|
||||
"Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.10/dist-packages (from pandas<3.0,>=1.0->gradio) (2024.2)\n",
|
||||
"Requirement already satisfied: ptyprocess>=0.5 in /usr/local/lib/python3.10/dist-packages (from pexpect>4.3->ipython) (0.7.0)\n",
|
||||
"Requirement already satisfied: wcwidth in /usr/local/lib/python3.10/dist-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->ipython) (0.2.13)\n",
|
||||
"Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.10/dist-packages (from pydantic>=2.0->gradio) (0.7.0)\n",
|
||||
"Requirement already satisfied: pydantic-core==2.27.1 in /usr/local/lib/python3.10/dist-packages (from pydantic>=2.0->gradio) (2.27.1)\n",
|
||||
"Requirement already satisfied: click>=8.0.0 in /usr/local/lib/python3.10/dist-packages (from typer<1.0,>=0.12->gradio) (8.1.7)\n",
|
||||
"Requirement already satisfied: shellingham>=1.3.0 in /usr/local/lib/python3.10/dist-packages (from typer<1.0,>=0.12->gradio) (1.5.4)\n",
|
||||
"Requirement already satisfied: rich>=10.11.0 in /usr/local/lib/python3.10/dist-packages (from typer<1.0,>=0.12->gradio) (13.9.4)\n",
|
||||
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.8.2->pandas<3.0,>=1.0->gradio) (1.17.0)\n",
|
||||
"Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.10/dist-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (3.0.0)\n",
|
||||
"Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.10/dist-packages (from markdown-it-py>=2.2.0->rich>=10.11.0->typer<1.0,>=0.12->gradio) (0.1.2)\n",
|
||||
"Downloading gradio-5.9.1-py3-none-any.whl (57.2 MB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m57.2/57.2 MB\u001b[0m \u001b[31m14.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hDownloading gradio_client-1.5.2-py3-none-any.whl (320 kB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m320.4/320.4 kB\u001b[0m \u001b[31m29.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hDownloading spaces-0.31.1-py3-none-any.whl (28 kB)\n",
|
||||
"Downloading qwen_vl_utils-0.0.8-py3-none-any.whl (5.9 kB)\n",
|
||||
"Downloading av-14.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (33.0 MB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m33.0/33.0 MB\u001b[0m \u001b[31m62.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hDownloading reportlab-4.2.5-py3-none-any.whl (1.9 MB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.9/1.9 MB\u001b[0m \u001b[31m84.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hDownloading python_docx-1.1.2-py3-none-any.whl (244 kB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m244.3/244.3 kB\u001b[0m \u001b[31m22.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hDownloading aiofiles-23.2.1-py3-none-any.whl (15 kB)\n",
|
||||
"Downloading fastapi-0.115.6-py3-none-any.whl (94 kB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m94.8/94.8 kB\u001b[0m \u001b[31m9.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hDownloading jedi-0.19.2-py2.py3-none-any.whl (1.6 MB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.6/1.6 MB\u001b[0m \u001b[31m75.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hDownloading MarkupSafe-2.1.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25 kB)\n",
|
||||
"Downloading python_multipart-0.0.20-py3-none-any.whl (24 kB)\n",
|
||||
"Downloading ruff-0.8.5-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.3 MB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m11.3/11.3 MB\u001b[0m \u001b[31m114.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hDownloading safehttpx-0.1.6-py3-none-any.whl (8.7 kB)\n",
|
||||
"Downloading semantic_version-2.10.0-py2.py3-none-any.whl (15 kB)\n",
|
||||
"Downloading starlette-0.41.3-py3-none-any.whl (73 kB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m73.2/73.2 kB\u001b[0m \u001b[31m6.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hDownloading tomlkit-0.13.2-py3-none-any.whl (37 kB)\n",
|
||||
"Downloading uvicorn-0.34.0-py3-none-any.whl (62 kB)\n",
|
||||
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m62.3/62.3 kB\u001b[0m \u001b[31m6.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
||||
"\u001b[?25hDownloading ffmpy-0.5.0-py3-none-any.whl (6.0 kB)\n",
|
||||
"Downloading pydub-0.25.1-py2.py3-none-any.whl (32 kB)\n",
|
||||
"Building wheels for collected packages: fpdf\n",
|
||||
" Building wheel for fpdf (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
|
||||
" Created wheel for fpdf: filename=fpdf-1.7.2-py2.py3-none-any.whl size=40704 sha256=442a41ba3b572ac9bae1220ac5f13b34f02252630b892bab6e55af0e878115c0\n",
|
||||
" Stored in directory: /root/.cache/pip/wheels/f9/95/ba/f418094659025eb9611f17cbcaf2334236bf39a0c3453ea455\n",
|
||||
"Successfully built fpdf\n",
|
||||
"Installing collected packages: pydub, fpdf, uvicorn, tomlkit, semantic-version, ruff, reportlab, python-multipart, python-docx, markupsafe, jedi, ffmpy, av, aiofiles, starlette, qwen-vl-utils, safehttpx, gradio-client, fastapi, gradio, spaces\n",
|
||||
" Attempting uninstall: markupsafe\n",
|
||||
" Found existing installation: MarkupSafe 3.0.2\n",
|
||||
" Uninstalling MarkupSafe-3.0.2:\n",
|
||||
" Successfully uninstalled MarkupSafe-3.0.2\n",
|
||||
"Successfully installed aiofiles-23.2.1 av-14.0.1 fastapi-0.115.6 ffmpy-0.5.0 fpdf-1.7.2 gradio-5.9.1 gradio-client-1.5.2 jedi-0.19.2 markupsafe-2.1.5 pydub-0.25.1 python-docx-1.1.2 python-multipart-0.0.20 qwen-vl-utils-0.0.8 reportlab-4.2.5 ruff-0.8.5 safehttpx-0.1.6 semantic-version-2.10.0 spaces-0.31.1 starlette-0.41.3 tomlkit-0.13.2 uvicorn-0.34.0\n"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# Authenticate with Hugging Face\n",
|
||||
"from huggingface_hub import login\n",
|
||||
"\n",
|
||||
"# Log in to Hugging Face using the provided token\n",
|
||||
"hf_token = '----xxx----'\n",
|
||||
"login(hf_token)\n",
|
||||
"\n",
|
||||
"#Demo\n",
|
||||
"import gradio as gr\n",
|
||||
"import spaces\n",
|
||||
"from transformers import Qwen2VLForConditionalGeneration, AutoProcessor, TextIteratorStreamer\n",
|
||||
"from qwen_vl_utils import process_vision_info\n",
|
||||
"import torch\n",
|
||||
"from PIL import Image\n",
|
||||
"import os\n",
|
||||
"import uuid\n",
|
||||
"import io\n",
|
||||
"from threading import Thread\n",
|
||||
"from reportlab.lib.pagesizes import A4\n",
|
||||
"from reportlab.lib.styles import getSampleStyleSheet\n",
|
||||
"from reportlab.lib import colors\n",
|
||||
"from reportlab.platypus import SimpleDocTemplate, Image as RLImage, Paragraph, Spacer\n",
|
||||
"from reportlab.lib.units import inch\n",
|
||||
"from reportlab.pdfbase import pdfmetrics\n",
|
||||
"from reportlab.pdfbase.ttfonts import TTFont\n",
|
||||
"import docx\n",
|
||||
"from docx.enum.text import WD_ALIGN_PARAGRAPH\n",
|
||||
"\n",
|
||||
"# Define model options\n",
|
||||
"MODEL_OPTIONS = {\n",
|
||||
" \"OCR-KIE\": \"prithivMLmods/Qwen2-VL-OCR-2B-Instruct\",\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"# Preload models and processors into CUDA\n",
|
||||
"models = {}\n",
|
||||
"processors = {}\n",
|
||||
"for name, model_id in MODEL_OPTIONS.items():\n",
|
||||
" print(f\"Loading {name}...\")\n",
|
||||
" models[name] = Qwen2VLForConditionalGeneration.from_pretrained(\n",
|
||||
" model_id,\n",
|
||||
" trust_remote_code=True,\n",
|
||||
" torch_dtype=torch.float16\n",
|
||||
" ).to(\"cuda\").eval()\n",
|
||||
" processors[name] = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)\n",
|
||||
"\n",
|
||||
"image_extensions = Image.registered_extensions()\n",
|
||||
"\n",
|
||||
"def identify_and_save_blob(blob_path):\n",
|
||||
" \"\"\"Identifies if the blob is an image and saves it.\"\"\"\n",
|
||||
" try:\n",
|
||||
" with open(blob_path, 'rb') as file:\n",
|
||||
" blob_content = file.read()\n",
|
||||
" try:\n",
|
||||
" Image.open(io.BytesIO(blob_content)).verify() # Check if it's a valid image\n",
|
||||
" extension = \".png\" # Default to PNG for saving\n",
|
||||
" media_type = \"image\"\n",
|
||||
" except (IOError, SyntaxError):\n",
|
||||
" raise ValueError(\"Unsupported media type. Please upload a valid image.\")\n",
|
||||
"\n",
|
||||
" filename = f\"temp_{uuid.uuid4()}_media{extension}\"\n",
|
||||
" with open(filename, \"wb\") as f:\n",
|
||||
" f.write(blob_content)\n",
|
||||
"\n",
|
||||
" return filename, media_type\n",
|
||||
"\n",
|
||||
" except FileNotFoundError:\n",
|
||||
" raise ValueError(f\"The file {blob_path} was not found.\")\n",
|
||||
" except Exception as e:\n",
|
||||
" raise ValueError(f\"An error occurred while processing the file: {e}\")\n",
|
||||
"\n",
|
||||
"@spaces.GPU\n",
|
||||
"def qwen_inference(model_name, media_input, text_input=None):\n",
|
||||
" \"\"\"Handles inference for the selected model.\"\"\"\n",
|
||||
" model = models[model_name]\n",
|
||||
" processor = processors[model_name]\n",
|
||||
"\n",
|
||||
" if isinstance(media_input, str):\n",
|
||||
" media_path = media_input\n",
|
||||
" if media_path.endswith(tuple([i for i in image_extensions.keys()])):\n",
|
||||
" media_type = \"image\"\n",
|
||||
" else:\n",
|
||||
" try:\n",
|
||||
" media_path, media_type = identify_and_save_blob(media_input)\n",
|
||||
" except Exception as e:\n",
|
||||
" raise ValueError(\"Unsupported media type. Please upload a valid image.\")\n",
|
||||
"\n",
|
||||
" messages = [\n",
|
||||
" {\n",
|
||||
" \"role\": \"user\",\n",
|
||||
" \"content\": [\n",
|
||||
" {\n",
|
||||
" \"type\": media_type,\n",
|
||||
" media_type: media_path\n",
|
||||
" },\n",
|
||||
" {\"type\": \"text\", \"text\": text_input},\n",
|
||||
" ],\n",
|
||||
" }\n",
|
||||
" ]\n",
|
||||
"\n",
|
||||
" text = processor.apply_chat_template(\n",
|
||||
" messages, tokenize=False, add_generation_prompt=True\n",
|
||||
" )\n",
|
||||
" image_inputs, _ = process_vision_info(messages)\n",
|
||||
" inputs = processor(\n",
|
||||
" text=[text],\n",
|
||||
" images=image_inputs,\n",
|
||||
" padding=True,\n",
|
||||
" return_tensors=\"pt\",\n",
|
||||
" ).to(\"cuda\")\n",
|
||||
"\n",
|
||||
" streamer = TextIteratorStreamer(\n",
|
||||
" processor.tokenizer, skip_prompt=True, skip_special_tokens=True\n",
|
||||
" )\n",
|
||||
" generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=1024)\n",
|
||||
"\n",
|
||||
" thread = Thread(target=model.generate, kwargs=generation_kwargs)\n",
|
||||
" thread.start()\n",
|
||||
"\n",
|
||||
" buffer = \"\"\n",
|
||||
" for new_text in streamer:\n",
|
||||
" buffer += new_text\n",
|
||||
" # Remove <|im_end|> or similar tokens from the output\n",
|
||||
" buffer = buffer.replace(\"<|im_end|>\", \"\")\n",
|
||||
" yield buffer\n",
|
||||
"\n",
|
||||
"def format_plain_text(output_text):\n",
|
||||
" \"\"\"Formats the output text as plain text without LaTeX delimiters.\"\"\"\n",
|
||||
" # Remove LaTeX delimiters and convert to plain text\n",
|
||||
" plain_text = output_text.replace(\"\\\\(\", \"\").replace(\"\\\\)\", \"\").replace(\"\\\\[\", \"\").replace(\"\\\\]\", \"\")\n",
|
||||
" return plain_text\n",
|
||||
"\n",
|
||||
"def generate_document(media_path, output_text, file_format, font_size, line_spacing, alignment, image_size):\n",
|
||||
" \"\"\"Generates a document with the input image and plain text output.\"\"\"\n",
|
||||
" plain_text = format_plain_text(output_text)\n",
|
||||
" if file_format == \"pdf\":\n",
|
||||
" return generate_pdf(media_path, plain_text, font_size, line_spacing, alignment, image_size)\n",
|
||||
" elif file_format == \"docx\":\n",
|
||||
" return generate_docx(media_path, plain_text, font_size, line_spacing, alignment, image_size)\n",
|
||||
"\n",
|
||||
"def generate_pdf(media_path, plain_text, font_size, line_spacing, alignment, image_size):\n",
|
||||
" \"\"\"Generates a PDF document.\"\"\"\n",
|
||||
" filename = f\"output_{uuid.uuid4()}.pdf\"\n",
|
||||
" doc = SimpleDocTemplate(\n",
|
||||
" filename,\n",
|
||||
" pagesize=A4,\n",
|
||||
" rightMargin=inch,\n",
|
||||
" leftMargin=inch,\n",
|
||||
" topMargin=inch,\n",
|
||||
" bottomMargin=inch\n",
|
||||
" )\n",
|
||||
" styles = getSampleStyleSheet()\n",
|
||||
" styles[\"Normal\"].fontSize = int(font_size)\n",
|
||||
" styles[\"Normal\"].leading = int(font_size) * line_spacing\n",
|
||||
" styles[\"Normal\"].alignment = {\n",
|
||||
" \"Left\": 0,\n",
|
||||
" \"Center\": 1,\n",
|
||||
" \"Right\": 2,\n",
|
||||
" \"Justified\": 4\n",
|
||||
" }[alignment]\n",
|
||||
"\n",
|
||||
" story = []\n",
|
||||
"\n",
|
||||
" # Add image with size adjustment\n",
|
||||
" image_sizes = {\n",
|
||||
" \"Small\": (200, 200),\n",
|
||||
" \"Medium\": (400, 400),\n",
|
||||
" \"Large\": (600, 600)\n",
|
||||
" }\n",
|
||||
" img = RLImage(media_path, width=image_sizes[image_size][0], height=image_sizes[image_size][1])\n",
|
||||
" story.append(img)\n",
|
||||
" story.append(Spacer(1, 12))\n",
|
||||
"\n",
|
||||
" # Add plain text output\n",
|
||||
" text = Paragraph(plain_text, styles[\"Normal\"])\n",
|
||||
" story.append(text)\n",
|
||||
"\n",
|
||||
" doc.build(story)\n",
|
||||
" return filename\n",
|
||||
"\n",
|
||||
"def generate_docx(media_path, plain_text, font_size, line_spacing, alignment, image_size):\n",
|
||||
" \"\"\"Generates a DOCX document.\"\"\"\n",
|
||||
" filename = f\"output_{uuid.uuid4()}.docx\"\n",
|
||||
" doc = docx.Document()\n",
|
||||
"\n",
|
||||
" # Add image with size adjustment\n",
|
||||
" image_sizes = {\n",
|
||||
" \"Small\": docx.shared.Inches(2),\n",
|
||||
" \"Medium\": docx.shared.Inches(4),\n",
|
||||
" \"Large\": docx.shared.Inches(6)\n",
|
||||
" }\n",
|
||||
" doc.add_picture(media_path, width=image_sizes[image_size])\n",
|
||||
" doc.add_paragraph()\n",
|
||||
"\n",
|
||||
" # Add plain text output\n",
|
||||
" paragraph = doc.add_paragraph()\n",
|
||||
" paragraph.paragraph_format.line_spacing = line_spacing\n",
|
||||
" paragraph.paragraph_format.alignment = {\n",
|
||||
" \"Left\": WD_ALIGN_PARAGRAPH.LEFT,\n",
|
||||
" \"Center\": WD_ALIGN_PARAGRAPH.CENTER,\n",
|
||||
" \"Right\": WD_ALIGN_PARAGRAPH.RIGHT,\n",
|
||||
" \"Justified\": WD_ALIGN_PARAGRAPH.JUSTIFY\n",
|
||||
" }[alignment]\n",
|
||||
" run = paragraph.add_run(plain_text)\n",
|
||||
" run.font.size = docx.shared.Pt(int(font_size))\n",
|
||||
"\n",
|
||||
" doc.save(filename)\n",
|
||||
" return filename\n",
|
||||
"\n",
|
||||
"# CSS for output styling\n",
|
||||
"css = \"\"\"\n",
|
||||
" #output {\n",
|
||||
" height: 500px;\n",
|
||||
" overflow: auto;\n",
|
||||
" border: 1px solid #ccc;\n",
|
||||
" }\n",
|
||||
".submit-btn {\n",
|
||||
" background-color: #cf3434 !important;\n",
|
||||
" color: white !important;\n",
|
||||
"}\n",
|
||||
".submit-btn:hover {\n",
|
||||
" background-color: #ff2323 !important;\n",
|
||||
"}\n",
|
||||
".download-btn {\n",
|
||||
" background-color: #35a6d6 !important;\n",
|
||||
" color: white !important;\n",
|
||||
"}\n",
|
||||
".download-btn:hover {\n",
|
||||
" background-color: #22bcff !important;\n",
|
||||
"}\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"# Gradio app setup\n",
|
||||
"with gr.Blocks(css=css) as demo:\n",
|
||||
" gr.Markdown(\"# Qwen2VL Models: Vision and Language Processing\")\n",
|
||||
"\n",
|
||||
" with gr.Tab(label=\"Image Input\"):\n",
|
||||
"\n",
|
||||
" with gr.Row():\n",
|
||||
" with gr.Column():\n",
|
||||
" model_choice = gr.Dropdown(\n",
|
||||
" label=\"Model Selection\",\n",
|
||||
" choices=list(MODEL_OPTIONS.keys()),\n",
|
||||
" value=\"OCR-KIE\"\n",
|
||||
" )\n",
|
||||
" input_media = gr.File(\n",
|
||||
" label=\"Upload Image\", type=\"filepath\"\n",
|
||||
" )\n",
|
||||
" text_input = gr.Textbox(label=\"Question\", placeholder=\"Ask a question about the image...\")\n",
|
||||
" submit_btn = gr.Button(value=\"Submit\", elem_classes=\"submit-btn\")\n",
|
||||
"\n",
|
||||
" with gr.Column():\n",
|
||||
" output_text = gr.Textbox(label=\"Output Text\", lines=10)\n",
|
||||
" plain_text_output = gr.Textbox(label=\"Standardized Plain Text\", lines=10)\n",
|
||||
"\n",
|
||||
" submit_btn.click(\n",
|
||||
" qwen_inference, [model_choice, input_media, text_input], [output_text]\n",
|
||||
" ).then(\n",
|
||||
" lambda output_text: format_plain_text(output_text), [output_text], [plain_text_output]\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" # Add examples directly usable by clicking\n",
|
||||
" with gr.Row():\n",
|
||||
" with gr.Column():\n",
|
||||
" line_spacing = gr.Dropdown(\n",
|
||||
" choices=[0.5, 1.0, 1.15, 1.5, 2.0, 2.5, 3.0],\n",
|
||||
" value=1.5,\n",
|
||||
" label=\"Line Spacing\"\n",
|
||||
" )\n",
|
||||
" font_size = gr.Dropdown(\n",
|
||||
" choices=[\"8\", \"10\", \"12\", \"14\", \"16\", \"18\", \"20\", \"22\", \"24\"],\n",
|
||||
" value=\"18\",\n",
|
||||
" label=\"Font Size\"\n",
|
||||
" )\n",
|
||||
" alignment = gr.Dropdown(\n",
|
||||
" choices=[\"Left\", \"Center\", \"Right\", \"Justified\"],\n",
|
||||
" value=\"Justified\",\n",
|
||||
" label=\"Text Alignment\"\n",
|
||||
" )\n",
|
||||
" image_size = gr.Dropdown(\n",
|
||||
" choices=[\"Small\", \"Medium\", \"Large\"],\n",
|
||||
" value=\"Small\",\n",
|
||||
" label=\"Image Size\"\n",
|
||||
" )\n",
|
||||
" file_format = gr.Radio([\"pdf\", \"docx\"], label=\"File Format\", value=\"pdf\")\n",
|
||||
" get_document_btn = gr.Button(value=\"Get Document\", elem_classes=\"download-btn\")\n",
|
||||
"\n",
|
||||
" get_document_btn.click(\n",
|
||||
" generate_document, [input_media, output_text, file_format, font_size, line_spacing, alignment, image_size], gr.File(label=\"Download Document\")\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
"demo.launch(debug=True)"
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/",
|
||||
"height": 715
|
||||
},
|
||||
"id": "ovBSsRFhGbs2",
|
||||
"outputId": "2a5bc724-4eab-4167-9c52-b378ce3799e3"
|
||||
},
|
||||
"execution_count": 4,
|
||||
"outputs": [
|
||||
{
|
||||
"output_type": "stream",
|
||||
"name": "stderr",
|
||||
"text": [
|
||||
"The argument `trust_remote_code` is to be used with Auto classes. It has no effect here and is ignored.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"output_type": "stream",
|
||||
"name": "stdout",
|
||||
"text": [
|
||||
"Loading OCR-KIE...\n",
|
||||
"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",
|
||||
"\n",
|
||||
"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",
|
||||
"* Running on public URL: https://49b22c4dd53a6ec06e.gradio.live\n",
|
||||
"\n",
|
||||
"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"
|
||||
]
|
||||
},
|
||||
{
|
||||
"output_type": "display_data",
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"<IPython.core.display.HTML object>"
|
||||
],
|
||||
"text/html": [
|
||||
"<div><iframe src=\"https://49b22c4dd53a6ec06e.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
||||
]
|
||||
},
|
||||
"metadata": {}
|
||||
},
|
||||
{
|
||||
"output_type": "stream",
|
||||
"name": "stdout",
|
||||
"text": [
|
||||
"Keyboard interruption in main thread... closing server.\n",
|
||||
"Killing tunnel 127.0.0.1:7860 <> https://49b22c4dd53a6ec06e.gradio.live\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"output_type": "execute_result",
|
||||
"data": {
|
||||
"text/plain": []
|
||||
},
|
||||
"metadata": {},
|
||||
"execution_count": 4
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
14
Qwen2vl With ReportLab Documentation/README.md
Normal file
14
Qwen2vl With ReportLab Documentation/README.md
Normal file
@@ -0,0 +1,14 @@
|
||||
---
|
||||
title: QWEN2 VL
|
||||
emoji: 🍍
|
||||
colorFrom: blue
|
||||
colorTo: yellow
|
||||
sdk: gradio
|
||||
sdk_version: 5.11.0
|
||||
app_file: app.py
|
||||
pinned: true
|
||||
license: creativeml-openrail-m
|
||||
short_description: Qwen VL 2B
|
||||
---
|
||||
|
||||
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
||||
343
Qwen2vl With ReportLab Documentation/app.py
Normal file
343
Qwen2vl With ReportLab Documentation/app.py
Normal file
@@ -0,0 +1,343 @@
|
||||
import gradio as gr
|
||||
import spaces
|
||||
from transformers import Qwen2VLForConditionalGeneration, AutoProcessor, TextIteratorStreamer
|
||||
from qwen_vl_utils import process_vision_info
|
||||
import torch
|
||||
from PIL import Image
|
||||
import os
|
||||
import uuid
|
||||
import io
|
||||
from threading import Thread
|
||||
from reportlab.lib.pagesizes import A4
|
||||
from reportlab.lib.styles import getSampleStyleSheet
|
||||
from reportlab.lib import colors
|
||||
from reportlab.platypus import SimpleDocTemplate, Image as RLImage, Paragraph, Spacer
|
||||
from reportlab.lib.units import inch
|
||||
from reportlab.pdfbase import pdfmetrics
|
||||
from reportlab.pdfbase.ttfonts import TTFont
|
||||
import docx
|
||||
from docx.enum.text import WD_ALIGN_PARAGRAPH
|
||||
|
||||
# Define model options
|
||||
MODEL_OPTIONS = {
|
||||
"Qwen2VL Base": "Qwen/Qwen2-VL-2B-Instruct",
|
||||
"Latex OCR": "prithivMLmods/Qwen2-VL-OCR-2B-Instruct",
|
||||
"Math Prase": "prithivMLmods/Qwen2-VL-Math-Prase-2B-Instruct",
|
||||
"Text Analogy Ocrtest": "prithivMLmods/Qwen2-VL-Ocrtest-2B-Instruct"
|
||||
}
|
||||
|
||||
# Preload models and processors into CUDA
|
||||
models = {}
|
||||
processors = {}
|
||||
for name, model_id in MODEL_OPTIONS.items():
|
||||
print(f"Loading {name}...")
|
||||
models[name] = Qwen2VLForConditionalGeneration.from_pretrained(
|
||||
model_id,
|
||||
trust_remote_code=True,
|
||||
torch_dtype=torch.float16
|
||||
).to("cuda").eval()
|
||||
processors[name] = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
||||
|
||||
image_extensions = Image.registered_extensions()
|
||||
|
||||
def identify_and_save_blob(blob_path):
|
||||
"""Identifies if the blob is an image and saves it."""
|
||||
try:
|
||||
with open(blob_path, 'rb') as file:
|
||||
blob_content = file.read()
|
||||
try:
|
||||
Image.open(io.BytesIO(blob_content)).verify() # Check if it's a valid image
|
||||
extension = ".png" # Default to PNG for saving
|
||||
media_type = "image"
|
||||
except (IOError, SyntaxError):
|
||||
raise ValueError("Unsupported media type. Please upload a valid image.")
|
||||
|
||||
filename = f"temp_{uuid.uuid4()}_media{extension}"
|
||||
with open(filename, "wb") as f:
|
||||
f.write(blob_content)
|
||||
|
||||
return filename, media_type
|
||||
|
||||
except FileNotFoundError:
|
||||
raise ValueError(f"The file {blob_path} was not found.")
|
||||
except Exception as e:
|
||||
raise ValueError(f"An error occurred while processing the file: {e}")
|
||||
|
||||
@spaces.GPU
|
||||
def qwen_inference(model_name, media_input, text_input=None):
|
||||
"""Handles inference for the selected model."""
|
||||
model = models[model_name]
|
||||
processor = processors[model_name]
|
||||
|
||||
if isinstance(media_input, str):
|
||||
media_path = media_input
|
||||
if media_path.endswith(tuple([i for i in image_extensions.keys()])):
|
||||
media_type = "image"
|
||||
else:
|
||||
try:
|
||||
media_path, media_type = identify_and_save_blob(media_input)
|
||||
except Exception as e:
|
||||
raise ValueError("Unsupported media type. Please upload a valid image.")
|
||||
|
||||
messages = [
|
||||
{
|
||||
"role": "user",
|
||||
"content": [
|
||||
{
|
||||
"type": media_type,
|
||||
media_type: media_path
|
||||
},
|
||||
{"type": "text", "text": text_input},
|
||||
],
|
||||
}
|
||||
]
|
||||
|
||||
text = processor.apply_chat_template(
|
||||
messages, tokenize=False, add_generation_prompt=True
|
||||
)
|
||||
image_inputs, _ = process_vision_info(messages)
|
||||
inputs = processor(
|
||||
text=[text],
|
||||
images=image_inputs,
|
||||
padding=True,
|
||||
return_tensors="pt",
|
||||
).to("cuda")
|
||||
|
||||
streamer = TextIteratorStreamer(
|
||||
processor.tokenizer, skip_prompt=True, skip_special_tokens=True
|
||||
)
|
||||
generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=1024)
|
||||
|
||||
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
||||
thread.start()
|
||||
|
||||
buffer = ""
|
||||
for new_text in streamer:
|
||||
buffer += new_text
|
||||
# Remove <|im_end|> or similar tokens from the output
|
||||
buffer = buffer.replace("<|im_end|>", "")
|
||||
yield buffer
|
||||
|
||||
def format_plain_text(output_text):
|
||||
"""Formats the output text as plain text without LaTeX delimiters."""
|
||||
# Remove LaTeX delimiters and convert to plain text
|
||||
plain_text = output_text.replace("\\(", "").replace("\\)", "").replace("\\[", "").replace("\\]", "")
|
||||
return plain_text
|
||||
|
||||
def generate_document(media_path, output_text, file_format, font_choice, font_size, line_spacing, alignment, image_size):
|
||||
"""Generates a document with the input image and plain text output."""
|
||||
plain_text = format_plain_text(output_text)
|
||||
if file_format == "pdf":
|
||||
return generate_pdf(media_path, plain_text, font_choice, font_size, line_spacing, alignment, image_size)
|
||||
elif file_format == "docx":
|
||||
return generate_docx(media_path, plain_text, font_choice, font_size, line_spacing, alignment, image_size)
|
||||
|
||||
def generate_pdf(media_path, plain_text, font_choice, font_size, line_spacing, alignment, image_size):
|
||||
"""Generates a PDF document."""
|
||||
filename = f"output_{uuid.uuid4()}.pdf"
|
||||
doc = SimpleDocTemplate(
|
||||
filename,
|
||||
pagesize=A4,
|
||||
rightMargin=inch,
|
||||
leftMargin=inch,
|
||||
topMargin=inch,
|
||||
bottomMargin=inch
|
||||
)
|
||||
styles = getSampleStyleSheet()
|
||||
styles["Normal"].fontName = font_choice
|
||||
styles["Normal"].fontSize = int(font_size)
|
||||
styles["Normal"].leading = int(font_size) * line_spacing
|
||||
styles["Normal"].alignment = {
|
||||
"Left": 0,
|
||||
"Center": 1,
|
||||
"Right": 2,
|
||||
"Justified": 4
|
||||
}[alignment]
|
||||
|
||||
# Register font
|
||||
font_path = f"font/{font_choice}"
|
||||
pdfmetrics.registerFont(TTFont(font_choice, font_path))
|
||||
|
||||
story = []
|
||||
|
||||
# Add image with size adjustment
|
||||
image_sizes = {
|
||||
"Small": (200, 200),
|
||||
"Medium": (400, 400),
|
||||
"Large": (600, 600)
|
||||
}
|
||||
img = RLImage(media_path, width=image_sizes[image_size][0], height=image_sizes[image_size][1])
|
||||
story.append(img)
|
||||
story.append(Spacer(1, 12))
|
||||
|
||||
# Add plain text output
|
||||
text = Paragraph(plain_text, styles["Normal"])
|
||||
story.append(text)
|
||||
|
||||
doc.build(story)
|
||||
return filename
|
||||
|
||||
def generate_docx(media_path, plain_text, font_choice, font_size, line_spacing, alignment, image_size):
|
||||
"""Generates a DOCX document."""
|
||||
filename = f"output_{uuid.uuid4()}.docx"
|
||||
doc = docx.Document()
|
||||
|
||||
# Add image with size adjustment
|
||||
image_sizes = {
|
||||
"Small": docx.shared.Inches(2),
|
||||
"Medium": docx.shared.Inches(4),
|
||||
"Large": docx.shared.Inches(6)
|
||||
}
|
||||
doc.add_picture(media_path, width=image_sizes[image_size])
|
||||
doc.add_paragraph()
|
||||
|
||||
# Add plain text output
|
||||
paragraph = doc.add_paragraph()
|
||||
paragraph.paragraph_format.line_spacing = line_spacing
|
||||
paragraph.paragraph_format.alignment = {
|
||||
"Left": WD_ALIGN_PARAGRAPH.LEFT,
|
||||
"Center": WD_ALIGN_PARAGRAPH.CENTER,
|
||||
"Right": WD_ALIGN_PARAGRAPH.RIGHT,
|
||||
"Justified": WD_ALIGN_PARAGRAPH.JUSTIFY
|
||||
}[alignment]
|
||||
run = paragraph.add_run(plain_text)
|
||||
run.font.name = font_choice
|
||||
run.font.size = docx.shared.Pt(int(font_size))
|
||||
|
||||
doc.save(filename)
|
||||
return filename
|
||||
|
||||
# CSS for output styling
|
||||
css = """
|
||||
#output {
|
||||
height: 500px;
|
||||
overflow: auto;
|
||||
border: 1px solid #ccc;
|
||||
}
|
||||
.submit-btn {
|
||||
background-color: #cf3434 !important;
|
||||
color: white !important;
|
||||
}
|
||||
.submit-btn:hover {
|
||||
background-color: #ff2323 !important;
|
||||
}
|
||||
.download-btn {
|
||||
background-color: #35a6d6 !important;
|
||||
color: white !important;
|
||||
}
|
||||
.download-btn:hover {
|
||||
background-color: #22bcff !important;
|
||||
}
|
||||
"""
|
||||
|
||||
# Gradio app setup
|
||||
with gr.Blocks(css=css) as demo:
|
||||
gr.Markdown("# Qwen2VL Models: Vision and Language Processing")
|
||||
|
||||
with gr.Tab(label="Image Input"):
|
||||
|
||||
with gr.Row():
|
||||
with gr.Column():
|
||||
model_choice = gr.Dropdown(
|
||||
label="Model Selection",
|
||||
choices=list(MODEL_OPTIONS.keys()),
|
||||
value="Latex OCR"
|
||||
)
|
||||
input_media = gr.File(
|
||||
label="Upload Image", type="filepath"
|
||||
)
|
||||
text_input = gr.Textbox(label="Question", placeholder="Ask a question about the image...")
|
||||
submit_btn = gr.Button(value="Submit", elem_classes="submit-btn")
|
||||
|
||||
with gr.Column():
|
||||
output_text = gr.Textbox(label="Output Text", lines=10)
|
||||
plain_text_output = gr.Textbox(label="Standardized Plain Text", lines=10)
|
||||
|
||||
submit_btn.click(
|
||||
qwen_inference, [model_choice, input_media, text_input], [output_text]
|
||||
).then(
|
||||
lambda output_text: format_plain_text(output_text), [output_text], [plain_text_output]
|
||||
)
|
||||
|
||||
# Add examples directly usable by clicking
|
||||
with gr.Row():
|
||||
gr.Examples(
|
||||
examples=[
|
||||
["examples/1.png", "summarize the letter", "Text Analogy Ocrtest"],
|
||||
["examples/2.jpg", "Summarize the full image in detail", "Latex OCR"],
|
||||
["examples/3.png", "Describe the photo", "Qwen2VL Base"],
|
||||
["examples/4.png", "summarize and solve the problem", "Math Prase"],
|
||||
],
|
||||
inputs=[input_media, text_input, model_choice],
|
||||
outputs=[output_text, plain_text_output],
|
||||
fn=lambda img, question, model: qwen_inference(model, img, question),
|
||||
cache_examples=False,
|
||||
)
|
||||
|
||||
with gr.Row():
|
||||
with gr.Column():
|
||||
line_spacing = gr.Dropdown(
|
||||
choices=[0.5, 1.0, 1.15, 1.5, 2.0, 2.5, 3.0],
|
||||
value=1.5,
|
||||
label="Line Spacing"
|
||||
)
|
||||
font_size = gr.Dropdown(
|
||||
choices=["8", "10", "12", "14", "16", "18", "20", "22", "24"],
|
||||
value="18",
|
||||
label="Font Size"
|
||||
)
|
||||
font_choice = gr.Dropdown(
|
||||
choices=[
|
||||
"DejaVuMathTeXGyre.ttf",
|
||||
"FiraCode-Medium.ttf",
|
||||
"InputMono-Light.ttf",
|
||||
"JetBrainsMono-Thin.ttf",
|
||||
"ProggyCrossed Regular Mac.ttf",
|
||||
"SourceCodePro-Black.ttf",
|
||||
"arial.ttf",
|
||||
"calibri.ttf",
|
||||
"mukta-malar-extralight.ttf",
|
||||
"noto-sans-arabic-medium.ttf",
|
||||
"times new roman.ttf",
|
||||
"ANGSA.ttf",
|
||||
"Book-Antiqua.ttf",
|
||||
"CONSOLA.TTF",
|
||||
"COOPBL.TTF",
|
||||
"Rockwell-Bold.ttf",
|
||||
"Candara Light.TTF",
|
||||
"Carlito-Regular.ttf Carlito-Regular.ttf",
|
||||
"Castellar.ttf",
|
||||
"Courier New.ttf",
|
||||
"LSANS.TTF",
|
||||
"Lucida Bright Regular.ttf",
|
||||
"TRTempusSansITC.ttf",
|
||||
"Verdana.ttf",
|
||||
"bell-mt.ttf",
|
||||
"eras-itc-light.ttf",
|
||||
"fonnts.com-aptos-light.ttf",
|
||||
"georgia.ttf",
|
||||
"segoeuithis.ttf",
|
||||
"youyuan.TTF",
|
||||
"TfPonetoneExpanded-7BJZA.ttf",
|
||||
],
|
||||
value="youyuan.TTF",
|
||||
label="Font Choice"
|
||||
)
|
||||
alignment = gr.Dropdown(
|
||||
choices=["Left", "Center", "Right", "Justified"],
|
||||
value="Justified",
|
||||
label="Text Alignment"
|
||||
)
|
||||
image_size = gr.Dropdown(
|
||||
choices=["Small", "Medium", "Large"],
|
||||
value="Small",
|
||||
label="Image Size"
|
||||
)
|
||||
file_format = gr.Radio(["pdf", "docx"], label="File Format", value="pdf")
|
||||
get_document_btn = gr.Button(value="Get Document", elem_classes="download-btn")
|
||||
|
||||
get_document_btn.click(
|
||||
generate_document, [input_media, output_text, file_format, font_choice, font_size, line_spacing, alignment, image_size], gr.File(label="Download Document")
|
||||
)
|
||||
|
||||
demo.launch(debug=True)
|
||||
BIN
Qwen2vl With ReportLab Documentation/examples/1.png
Normal file
BIN
Qwen2vl With ReportLab Documentation/examples/1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 167 KiB |
BIN
Qwen2vl With ReportLab Documentation/examples/2.jpg
Normal file
BIN
Qwen2vl With ReportLab Documentation/examples/2.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 326 KiB |
BIN
Qwen2vl With ReportLab Documentation/examples/3.png
Normal file
BIN
Qwen2vl With ReportLab Documentation/examples/3.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 254 KiB |
BIN
Qwen2vl With ReportLab Documentation/examples/4.png
Normal file
BIN
Qwen2vl With ReportLab Documentation/examples/4.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 50 KiB |
BIN
Qwen2vl With ReportLab Documentation/font/ANGSA.ttf
Normal file
BIN
Qwen2vl With ReportLab Documentation/font/ANGSA.ttf
Normal file
Binary file not shown.
BIN
Qwen2vl With ReportLab Documentation/font/Book-Antiqua.ttf
Normal file
BIN
Qwen2vl With ReportLab Documentation/font/Book-Antiqua.ttf
Normal file
Binary file not shown.
BIN
Qwen2vl With ReportLab Documentation/font/CONSOLA.TTF
Normal file
BIN
Qwen2vl With ReportLab Documentation/font/CONSOLA.TTF
Normal file
Binary file not shown.
BIN
Qwen2vl With ReportLab Documentation/font/COOPBL.TTF
Normal file
BIN
Qwen2vl With ReportLab Documentation/font/COOPBL.TTF
Normal file
Binary file not shown.
BIN
Qwen2vl With ReportLab Documentation/font/Candara Light.TTF
Normal file
BIN
Qwen2vl With ReportLab Documentation/font/Candara Light.TTF
Normal file
Binary file not shown.
BIN
Qwen2vl With ReportLab Documentation/font/Carlito-Regular.ttf
Normal file
BIN
Qwen2vl With ReportLab Documentation/font/Carlito-Regular.ttf
Normal file
Binary file not shown.
BIN
Qwen2vl With ReportLab Documentation/font/Castellar.ttf
Normal file
BIN
Qwen2vl With ReportLab Documentation/font/Castellar.ttf
Normal file
Binary file not shown.
BIN
Qwen2vl With ReportLab Documentation/font/Courier New.ttf
Normal file
BIN
Qwen2vl With ReportLab Documentation/font/Courier New.ttf
Normal file
Binary file not shown.
BIN
Qwen2vl With ReportLab Documentation/font/DejaVuMathTeXGyre.ttf
Normal file
BIN
Qwen2vl With ReportLab Documentation/font/DejaVuMathTeXGyre.ttf
Normal file
Binary file not shown.
BIN
Qwen2vl With ReportLab Documentation/font/FiraCode-Medium.ttf
Normal file
BIN
Qwen2vl With ReportLab Documentation/font/FiraCode-Medium.ttf
Normal file
Binary file not shown.
BIN
Qwen2vl With ReportLab Documentation/font/InputMono-Light.ttf
Normal file
BIN
Qwen2vl With ReportLab Documentation/font/InputMono-Light.ttf
Normal file
Binary file not shown.
BIN
Qwen2vl With ReportLab Documentation/font/JetBrainsMono-Thin.ttf
Normal file
BIN
Qwen2vl With ReportLab Documentation/font/JetBrainsMono-Thin.ttf
Normal file
Binary file not shown.
BIN
Qwen2vl With ReportLab Documentation/font/LSANS.TTF
Normal file
BIN
Qwen2vl With ReportLab Documentation/font/LSANS.TTF
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Qwen2vl With ReportLab Documentation/font/Rockwell-Bold.ttf
Normal file
BIN
Qwen2vl With ReportLab Documentation/font/Rockwell-Bold.ttf
Normal file
Binary file not shown.
Binary file not shown.
BIN
Qwen2vl With ReportLab Documentation/font/TRTempusSansITC.ttf
Normal file
BIN
Qwen2vl With ReportLab Documentation/font/TRTempusSansITC.ttf
Normal file
Binary file not shown.
Binary file not shown.
BIN
Qwen2vl With ReportLab Documentation/font/Verdana.ttf
Normal file
BIN
Qwen2vl With ReportLab Documentation/font/Verdana.ttf
Normal file
Binary file not shown.
BIN
Qwen2vl With ReportLab Documentation/font/arial.ttf
Normal file
BIN
Qwen2vl With ReportLab Documentation/font/arial.ttf
Normal file
Binary file not shown.
BIN
Qwen2vl With ReportLab Documentation/font/bell-mt.ttf
Normal file
BIN
Qwen2vl With ReportLab Documentation/font/bell-mt.ttf
Normal file
Binary file not shown.
3
Qwen2vl With ReportLab Documentation/font/calibri.ttf
Normal file
3
Qwen2vl With ReportLab Documentation/font/calibri.ttf
Normal file
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:d7114f0913256fa13f757eb2db8669c5f6dfd2fe2afa4e161e15d9e3574e6dc1
|
||||
size 1329860
|
||||
0
Qwen2vl With ReportLab Documentation/font/demo.txt
Normal file
0
Qwen2vl With ReportLab Documentation/font/demo.txt
Normal file
BIN
Qwen2vl With ReportLab Documentation/font/eras-itc-light.ttf
Normal file
BIN
Qwen2vl With ReportLab Documentation/font/eras-itc-light.ttf
Normal file
Binary file not shown.
Binary file not shown.
BIN
Qwen2vl With ReportLab Documentation/font/georgia.ttf
Normal file
BIN
Qwen2vl With ReportLab Documentation/font/georgia.ttf
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Qwen2vl With ReportLab Documentation/font/segoeuithis.ttf
Normal file
BIN
Qwen2vl With ReportLab Documentation/font/segoeuithis.ttf
Normal file
Binary file not shown.
BIN
Qwen2vl With ReportLab Documentation/font/times new roman.ttf
Normal file
BIN
Qwen2vl With ReportLab Documentation/font/times new roman.ttf
Normal file
Binary file not shown.
3
Qwen2vl With ReportLab Documentation/font/youyuan.TTF
Normal file
3
Qwen2vl With ReportLab Documentation/font/youyuan.TTF
Normal file
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:16747b04038770d7ceeade94e943d7d79247cc1c95b550403dd0a0286d46c6c4
|
||||
size 6794984
|
||||
14
Qwen2vl With ReportLab Documentation/requirements.txt
Normal file
14
Qwen2vl With ReportLab Documentation/requirements.txt
Normal file
@@ -0,0 +1,14 @@
|
||||
transformers
|
||||
accelerate
|
||||
numpy
|
||||
Requests
|
||||
torch
|
||||
torchvision
|
||||
qwen-vl-utils
|
||||
av
|
||||
ipython
|
||||
reportlab
|
||||
fpdf
|
||||
python-docx
|
||||
pillow
|
||||
huggingface_hub
|
||||
166
README.md
Normal file
166
README.md
Normal file
@@ -0,0 +1,166 @@
|
||||
---
|
||||
license: apache-2.0
|
||||
datasets:
|
||||
- unsloth/LaTeX_OCR
|
||||
- linxy/LaTeX_OCR
|
||||
language:
|
||||
- en
|
||||
base_model:
|
||||
- Qwen/Qwen2-VL-2B-Instruct
|
||||
pipeline_tag: image-text-to-text
|
||||
library_name: transformers
|
||||
tags:
|
||||
- Math
|
||||
- OCR
|
||||
- Latex
|
||||
- VLM
|
||||
- Plain_Text
|
||||
- KIE
|
||||
- Equations
|
||||
- VQA
|
||||
---
|
||||
# **Qwen2-VL-OCR-2B-Instruct [ VL / OCR ]**
|
||||
|
||||

|
||||
|
||||
> The **Qwen2-VL-OCR-2B-Instruct** model is a fine-tuned version of **Qwen/Qwen2-VL-2B-Instruct**, tailored for tasks that involve **Optical Character Recognition (OCR)**, **image-to-text conversion**, and **math problem solving with LaTeX formatting**. This model integrates a conversational approach with visual and textual understanding to handle multi-modal tasks effectively.
|
||||
|
||||
[](https://huggingface.co/prithivMLmods/Qwen2-VL-OCR-2B-Instruct/blob/main/Demo/ocrtest_qwen.ipynb)
|
||||
|
||||
#### Key Enhancements:
|
||||
|
||||
* **SoTA understanding of images of various resolution & ratio**: Qwen2-VL achieves state-of-the-art performance on visual understanding benchmarks, including MathVista, DocVQA, RealWorldQA, MTVQA, etc.
|
||||
|
||||
* **Understanding videos of 20min+**: Qwen2-VL can understand videos over 20 minutes for high-quality video-based question answering, dialog, content creation, etc.
|
||||
|
||||
* **Agent that can operate your mobiles, robots, etc.**: with the abilities of complex reasoning and decision making, Qwen2-VL can be integrated with devices like mobile phones, robots, etc., for automatic operation based on visual environment and text instructions.
|
||||
|
||||
* **Multilingual Support**: to serve global users, besides English and Chinese, Qwen2-VL now supports the understanding of texts in different languages inside images, including most European languages, Japanese, Korean, Arabic, Vietnamese, etc.
|
||||
|
||||
|
||||
### Sample Inference
|
||||
|
||||

|
||||
|
||||
| **File Name** | **Size** | **Description** | **Upload Status** |
|
||||
|---------------------------|------------|------------------------------------------------|-------------------|
|
||||
| `.gitattributes` | 1.52 kB | Configures LFS tracking for specific model files. | Initial commit |
|
||||
| `README.md` | 203 Bytes | Minimal details about the uploaded model. | Updated |
|
||||
| `added_tokens.json` | 408 Bytes | Additional tokens used by the model tokenizer. | Uploaded |
|
||||
| `chat_template.json` | 1.05 kB | Template for chat-based model input/output. | Uploaded |
|
||||
| `config.json` | 1.24 kB | Model configuration metadata. | Uploaded |
|
||||
| `generation_config.json` | 252 Bytes | Configuration for text generation settings. | Uploaded |
|
||||
| `merges.txt` | 1.82 MB | BPE merge rules for tokenization. | Uploaded |
|
||||
| `model.safetensors` | 4.42 GB | Serialized model weights in a secure format. | Uploaded (LFS) |
|
||||
| `preprocessor_config.json`| 596 Bytes | Preprocessing configuration for input data. | Uploaded |
|
||||
| `vocab.json` | 2.78 MB | Vocabulary file for tokenization. | Uploaded |
|
||||
|
||||
---
|
||||
|
||||
### How to Use
|
||||
|
||||
```python
|
||||
from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
|
||||
from qwen_vl_utils import process_vision_info
|
||||
|
||||
# default: Load the model on the available device(s)
|
||||
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
||||
"prithivMLmods/Qwen2-VL-OCR-2B-Instruct", torch_dtype="auto", device_map="auto"
|
||||
)
|
||||
|
||||
# We recommend enabling flash_attention_2 for better acceleration and memory saving, especially in multi-image and video scenarios.
|
||||
# model = Qwen2VLForConditionalGeneration.from_pretrained(
|
||||
# "prithivMLmods/Qwen2-VL-OCR-2B-Instruct",
|
||||
# torch_dtype=torch.bfloat16,
|
||||
# attn_implementation="flash_attention_2",
|
||||
# device_map="auto",
|
||||
# )
|
||||
|
||||
# default processer
|
||||
processor = AutoProcessor.from_pretrained("prithivMLmods/Qwen2-VL-OCR-2B-Instruct")
|
||||
|
||||
# The default range for the number of visual tokens per image in the model is 4-16384. You can set min_pixels and max_pixels according to your needs, such as a token count range of 256-1280, to balance speed and memory usage.
|
||||
# min_pixels = 256*28*28
|
||||
# max_pixels = 1280*28*28
|
||||
# processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels)
|
||||
|
||||
messages = [
|
||||
{
|
||||
"role": "user",
|
||||
"content": [
|
||||
{
|
||||
"type": "image",
|
||||
"image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",
|
||||
},
|
||||
{"type": "text", "text": "Describe this image."},
|
||||
],
|
||||
}
|
||||
]
|
||||
|
||||
# Preparation for inference
|
||||
text = processor.apply_chat_template(
|
||||
messages, tokenize=False, add_generation_prompt=True
|
||||
)
|
||||
image_inputs, video_inputs = process_vision_info(messages)
|
||||
inputs = processor(
|
||||
text=[text],
|
||||
images=image_inputs,
|
||||
videos=video_inputs,
|
||||
padding=True,
|
||||
return_tensors="pt",
|
||||
)
|
||||
inputs = inputs.to("cuda")
|
||||
|
||||
# Inference: Generation of the output
|
||||
generated_ids = model.generate(**inputs, max_new_tokens=128)
|
||||
generated_ids_trimmed = [
|
||||
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
||||
]
|
||||
output_text = processor.batch_decode(
|
||||
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
||||
)
|
||||
print(output_text)
|
||||
```
|
||||
### Buf
|
||||
```python
|
||||
buffer = ""
|
||||
for new_text in streamer:
|
||||
buffer += new_text
|
||||
# Remove <|im_end|> or similar tokens from the output
|
||||
buffer = buffer.replace("<|im_end|>", "")
|
||||
yield buffer
|
||||
```
|
||||
### **Key Features**
|
||||
|
||||
1. **Vision-Language Integration:**
|
||||
- Combines **image understanding** with **natural language processing** to convert images into text.
|
||||
|
||||
2. **Optical Character Recognition (OCR):**
|
||||
- Extracts and processes textual information from images with high accuracy.
|
||||
|
||||
3. **Math and LaTeX Support:**
|
||||
- Solves math problems and outputs equations in **LaTeX format**.
|
||||
|
||||
4. **Conversational Capabilities:**
|
||||
- Designed to handle **multi-turn interactions**, providing context-aware responses.
|
||||
|
||||
5. **Image-Text-to-Text Generation:**
|
||||
- Inputs can include **images, text, or a combination**, and the model generates descriptive or problem-solving text.
|
||||
|
||||
6. **Secure Weight Format:**
|
||||
- Uses **Safetensors** for faster and more secure model weight loading.
|
||||
|
||||
---
|
||||
|
||||
### **Training Details**
|
||||
|
||||
- **Base Model:** [Qwen/Qwen2-VL-2B-Instruct](#)
|
||||
- **Model Size:**
|
||||
- 2.21 Billion parameters
|
||||
- Optimized for **BF16** tensor type, enabling efficient inference.
|
||||
|
||||
- **Specializations:**
|
||||
- OCR tasks in images containing text.
|
||||
- Mathematical reasoning and LaTeX output for equations.
|
||||
|
||||
---
|
||||
BIN
Sample_Inference/123.png
Normal file
BIN
Sample_Inference/123.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 390 KiB |
16
added_tokens.json
Normal file
16
added_tokens.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"<|box_end|>": 151649,
|
||||
"<|box_start|>": 151648,
|
||||
"<|endoftext|>": 151643,
|
||||
"<|im_end|>": 151645,
|
||||
"<|im_start|>": 151644,
|
||||
"<|image_pad|>": 151655,
|
||||
"<|object_ref_end|>": 151647,
|
||||
"<|object_ref_start|>": 151646,
|
||||
"<|quad_end|>": 151651,
|
||||
"<|quad_start|>": 151650,
|
||||
"<|video_pad|>": 151656,
|
||||
"<|vision_end|>": 151653,
|
||||
"<|vision_pad|>": 151654,
|
||||
"<|vision_start|>": 151652
|
||||
}
|
||||
3
chat_template.json
Normal file
3
chat_template.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"chat_template": "{% set image_count = namespace(value=0) %}{% set video_count = namespace(value=0) %}{% for message in messages %}{% if loop.first and message['role'] != 'system' %}<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n{% endif %}<|im_start|>{{ message['role'] }}\n{% if message['content'] is string %}{{ message['content'] }}<|im_end|>\n{% else %}{% for content in message['content'] %}{% if content['type'] == 'image' or 'image' in content or 'image_url' in content %}{% set image_count.value = image_count.value + 1 %}{% if add_vision_id %}Picture {{ image_count.value }}: {% endif %}<|vision_start|><|image_pad|><|vision_end|>{% elif content['type'] == 'video' or 'video' in content %}{% set video_count.value = video_count.value + 1 %}{% if add_vision_id %}Video {{ video_count.value }}: {% endif %}<|vision_start|><|video_pad|><|vision_end|>{% elif 'text' in content %}{{ content['text'] }}{% endif %}{% endfor %}<|im_end|>\n{% endif %}{% endfor %}{% if add_generation_prompt %}<|im_start|>assistant\n{% endif %}"
|
||||
}
|
||||
49
config.json
Normal file
49
config.json
Normal file
@@ -0,0 +1,49 @@
|
||||
{
|
||||
"_name_or_path": "Qwen/Qwen2-VL-2B-Instruct",
|
||||
"architectures": [
|
||||
"Qwen2VLForConditionalGeneration"
|
||||
],
|
||||
"attention_dropout": 0.0,
|
||||
"bos_token_id": 151643,
|
||||
"eos_token_id": 151645,
|
||||
"hidden_act": "silu",
|
||||
"hidden_size": 1536,
|
||||
"image_token_id": 151655,
|
||||
"initializer_range": 0.02,
|
||||
"intermediate_size": 8960,
|
||||
"max_position_embeddings": 32768,
|
||||
"max_window_layers": 28,
|
||||
"model_type": "qwen2_vl",
|
||||
"num_attention_heads": 12,
|
||||
"num_hidden_layers": 28,
|
||||
"num_key_value_heads": 2,
|
||||
"pad_token_id": 151654,
|
||||
"rms_norm_eps": 1e-06,
|
||||
"rope_scaling": {
|
||||
"mrope_section": [
|
||||
16,
|
||||
24,
|
||||
24
|
||||
],
|
||||
"rope_type": "default",
|
||||
"type": "default"
|
||||
},
|
||||
"rope_theta": 1000000.0,
|
||||
"sliding_window": 32768,
|
||||
"tie_word_embeddings": true,
|
||||
"torch_dtype": "bfloat16",
|
||||
"transformers_version": "4.46.3",
|
||||
"use_cache": true,
|
||||
"use_sliding_window": false,
|
||||
"video_token_id": 151656,
|
||||
"vision_config": {
|
||||
"hidden_size": 1536,
|
||||
"in_chans": 3,
|
||||
"model_type": "qwen2_vl",
|
||||
"spatial_patch_size": 14
|
||||
},
|
||||
"vision_end_token_id": 151653,
|
||||
"vision_start_token_id": 151652,
|
||||
"vision_token_id": 151654,
|
||||
"vocab_size": 151936
|
||||
}
|
||||
1
configuration.json
Normal file
1
configuration.json
Normal file
@@ -0,0 +1 @@
|
||||
{"framework": "pytorch", "task": "image-text-to-text", "allow_remote": true}
|
||||
14
generation_config.json
Normal file
14
generation_config.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"bos_token_id": 151643,
|
||||
"do_sample": true,
|
||||
"eos_token_id": [
|
||||
151645,
|
||||
151643
|
||||
],
|
||||
"max_length": 32768,
|
||||
"pad_token_id": 151654,
|
||||
"temperature": 0.01,
|
||||
"top_k": 1,
|
||||
"top_p": 0.001,
|
||||
"transformers_version": "4.46.3"
|
||||
}
|
||||
151388
merges.txt
Normal file
151388
merges.txt
Normal file
File diff suppressed because it is too large
Load Diff
3
model.safetensors
Normal file
3
model.safetensors
Normal file
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:8d07b671e4c086bc6c05ab6230026550a49d39b8d1dadaa14b2732223ca63a11
|
||||
size 4418050848
|
||||
29
preprocessor_config.json
Normal file
29
preprocessor_config.json
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"do_convert_rgb": true,
|
||||
"do_normalize": true,
|
||||
"do_rescale": true,
|
||||
"do_resize": true,
|
||||
"image_mean": [
|
||||
0.48145466,
|
||||
0.4578275,
|
||||
0.40821073
|
||||
],
|
||||
"image_processor_type": "Qwen2VLImageProcessor",
|
||||
"image_std": [
|
||||
0.26862954,
|
||||
0.26130258,
|
||||
0.27577711
|
||||
],
|
||||
"max_pixels": 12845056,
|
||||
"merge_size": 2,
|
||||
"min_pixels": 3136,
|
||||
"patch_size": 14,
|
||||
"processor_class": "Qwen2VLProcessor",
|
||||
"resample": 3,
|
||||
"rescale_factor": 0.00392156862745098,
|
||||
"size": {
|
||||
"longest_edge": 12845056,
|
||||
"shortest_edge": 3136
|
||||
},
|
||||
"temporal_patch_size": 2
|
||||
}
|
||||
1
vocab.json
Normal file
1
vocab.json
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user