Rúben Almeida commited on
Commit
d75b820
·
1 Parent(s): be48c0d

First Commit with /health route and Dockerfile

Browse files
Files changed (8) hide show
  1. .gitignore +210 -0
  2. Dockerfile +24 -0
  3. README.md +93 -11
  4. __init__.py +0 -0
  5. load_gguf.py +21 -0
  6. main.py +25 -0
  7. requirements.txt +12 -0
  8. thrash.py +37 -0
.gitignore ADDED
@@ -0,0 +1,210 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Created by https://www.toptal.com/developers/gitignore/api/python,visualstudiocode,git
2
+ # Edit at https://www.toptal.com/developers/gitignore?templates=python,visualstudiocode,git
3
+
4
+ ### Git ###
5
+ # Created by git for backups. To disable backups in Git:
6
+ # $ git config --global mergetool.keepBackup false
7
+ *.orig
8
+
9
+ # Created by git when using merge tools for conflicts
10
+ *.BACKUP.*
11
+ *.BASE.*
12
+ *.LOCAL.*
13
+ *.REMOTE.*
14
+ *_BACKUP_*.txt
15
+ *_BASE_*.txt
16
+ *_LOCAL_*.txt
17
+ *_REMOTE_*.txt
18
+
19
+ ### Python ###
20
+ # Byte-compiled / optimized / DLL files
21
+ __pycache__/
22
+ *.py[cod]
23
+ *$py.class
24
+
25
+ # C extensions
26
+ *.so
27
+
28
+ # Distribution / packaging
29
+ .Python
30
+ build/
31
+ develop-eggs/
32
+ dist/
33
+ downloads/
34
+ eggs/
35
+ .eggs/
36
+ lib/
37
+ lib64/
38
+ parts/
39
+ sdist/
40
+ var/
41
+ wheels/
42
+ share/python-wheels/
43
+ *.egg-info/
44
+ .installed.cfg
45
+ *.egg
46
+ MANIFEST
47
+
48
+ # PyInstaller
49
+ # Usually these files are written by a python script from a template
50
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
51
+ *.manifest
52
+ *.spec
53
+
54
+ # Installer logs
55
+ pip-log.txt
56
+ pip-delete-this-directory.txt
57
+
58
+ # Unit test / coverage reports
59
+ htmlcov/
60
+ .tox/
61
+ .nox/
62
+ .coverage
63
+ .coverage.*
64
+ .cache
65
+ nosetests.xml
66
+ coverage.xml
67
+ *.cover
68
+ *.py,cover
69
+ .hypothesis/
70
+ .pytest_cache/
71
+ cover/
72
+
73
+ # Translations
74
+ *.mo
75
+ *.pot
76
+
77
+ # Django stuff:
78
+ *.log
79
+ local_settings.py
80
+ db.sqlite3
81
+ db.sqlite3-journal
82
+
83
+ # Flask stuff:
84
+ instance/
85
+ .webassets-cache
86
+
87
+ # Scrapy stuff:
88
+ .scrapy
89
+
90
+ # Sphinx documentation
91
+ docs/_build/
92
+
93
+ # PyBuilder
94
+ .pybuilder/
95
+ target/
96
+
97
+ # Jupyter Notebook
98
+ .ipynb_checkpoints
99
+
100
+ # IPython
101
+ profile_default/
102
+ ipython_config.py
103
+
104
+ # pyenv
105
+ # For a library or package, you might want to ignore these files since the code is
106
+ # intended to run in multiple environments; otherwise, check them in:
107
+ # .python-version
108
+
109
+ # pipenv
110
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
111
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
112
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
113
+ # install all needed dependencies.
114
+ #Pipfile.lock
115
+
116
+ # poetry
117
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
118
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
119
+ # commonly ignored for libraries.
120
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
121
+ #poetry.lock
122
+
123
+ # pdm
124
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
125
+ #pdm.lock
126
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
127
+ # in version control.
128
+ # https://pdm.fming.dev/#use-with-ide
129
+ .pdm.toml
130
+
131
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
132
+ __pypackages__/
133
+
134
+ # Celery stuff
135
+ celerybeat-schedule
136
+ celerybeat.pid
137
+
138
+ # SageMath parsed files
139
+ *.sage.py
140
+
141
+ # Environments
142
+ .env
143
+ .venv
144
+ env/
145
+ venv/
146
+ ENV/
147
+ env.bak/
148
+ venv.bak/
149
+
150
+ # Spyder project settings
151
+ .spyderproject
152
+ .spyproject
153
+
154
+ # Rope project settings
155
+ .ropeproject
156
+
157
+ # mkdocs documentation
158
+ /site
159
+
160
+ # mypy
161
+ .mypy_cache/
162
+ .dmypy.json
163
+ dmypy.json
164
+
165
+ # Pyre type checker
166
+ .pyre/
167
+
168
+ # pytype static type analyzer
169
+ .pytype/
170
+
171
+ # Cython debug symbols
172
+ cython_debug/
173
+
174
+ # PyCharm
175
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
176
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
177
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
178
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
179
+ #.idea/
180
+
181
+ ### Python Patch ###
182
+ # Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
183
+ poetry.toml
184
+
185
+ # ruff
186
+ .ruff_cache/
187
+
188
+ # LSP config files
189
+ pyrightconfig.json
190
+
191
+ ### VisualStudioCode ###
192
+ .vscode/*
193
+ !.vscode/settings.json
194
+ !.vscode/tasks.json
195
+ !.vscode/launch.json
196
+ !.vscode/extensions.json
197
+ !.vscode/*.code-snippets
198
+
199
+ # Local History for Visual Studio Code
200
+ .history/
201
+
202
+ # Built Visual Studio Code Extensions
203
+ *.vsix
204
+
205
+ ### VisualStudioCode Patch ###
206
+ # Ignore all local history of files
207
+ .history
208
+ .ionide
209
+
210
+ # End of https://www.toptal.com/developers/gitignore/api/python,visualstudiocode,git
Dockerfile ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.13-slim
2
+
3
+ # Set the working directory
4
+ WORKDIR /app
5
+
6
+ RUN apt-get update && apt-get install -y build-essential
7
+
8
+ # Update pip to the latest version
9
+ RUN pip install --upgrade pip
10
+
11
+ RUN pip install -U setuptools wheel
12
+
13
+ # Install the dependencies
14
+ RUN pip install --no-cache-dir -r requirements.txt
15
+
16
+ # Copy the requirements file into the container
17
+ COPY requirements.txt .
18
+
19
+ # Copy the rest of the application code into the container
20
+ COPY . .
21
+
22
+ EXPOSE 7860
23
+
24
+ ENTRYPOINT [ "fastapi", "run", "main.py", "--host=0.0.0.0", "--port=7860" ]
README.md CHANGED
@@ -1,11 +1,93 @@
1
- ---
2
- title: AWQ Converter
3
- emoji: 🦀
4
- colorFrom: green
5
- colorTo: gray
6
- sdk: docker
7
- pinned: false
8
- license: mit
9
- ---
10
-
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # GGUF-2-GPTQ Converter
2
+
3
+
4
+
5
+ ## Getting started
6
+
7
+ To make it easy for you to get started with GitLab, here's a list of recommended next steps.
8
+
9
+ Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
10
+
11
+ ## Add your files
12
+
13
+ - [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
14
+ - [ ] [Add files using the command line](https://docs.gitlab.com/topics/git/add_files/#add-files-to-a-git-repository) or push an existing Git repository with the following command:
15
+
16
+ ```
17
+ cd existing_repo
18
+ git remote add origin https://gitlab.com/i9p/ml/template/gguf-2-gptq-converter.git
19
+ git branch -M main
20
+ git push -uf origin main
21
+ ```
22
+
23
+ ## Integrate with your tools
24
+
25
+ - [ ] [Set up project integrations](https://gitlab.com/i9p/ml/template/gguf-2-gptq-converter/-/settings/integrations)
26
+
27
+ ## Collaborate with your team
28
+
29
+ - [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
30
+ - [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
31
+ - [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
32
+ - [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
33
+ - [ ] [Set auto-merge](https://docs.gitlab.com/user/project/merge_requests/auto_merge/)
34
+
35
+ ## Test and Deploy
36
+
37
+ Use the built-in continuous integration in GitLab.
38
+
39
+ - [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/)
40
+ - [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
41
+ - [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
42
+ - [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
43
+ - [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
44
+
45
+ ***
46
+
47
+ # Editing this README
48
+
49
+ When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template.
50
+
51
+ ## Suggestions for a good README
52
+
53
+ Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
54
+
55
+ ## Name
56
+ Choose a self-explaining name for your project.
57
+
58
+ ## Description
59
+ Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
60
+
61
+ ## Badges
62
+ On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
63
+
64
+ ## Visuals
65
+ Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
66
+
67
+ ## Installation
68
+ Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
69
+
70
+ ## Usage
71
+ Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
72
+
73
+ ## Support
74
+ Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
75
+
76
+ ## Roadmap
77
+ If you have ideas for releases in the future, it is a good idea to list them in the README.
78
+
79
+ ## Contributing
80
+ State if you are open to contributions and what your requirements are for accepting them.
81
+
82
+ For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
83
+
84
+ You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
85
+
86
+ ## Authors and acknowledgment
87
+ Show your appreciation to those who have contributed to the project.
88
+
89
+ ## License
90
+ For open source projects, say how it is licensed.
91
+
92
+ ## Project status
93
+ If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
__init__.py ADDED
File without changes
load_gguf.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModel
2
+
3
+
4
+ base_model = "ibm-research/granite-3.2-8b-instruct-GGUF"
5
+ GGUF_MODEL = "granite-3.2-8b-instruct-Q4_K_M.gguf"
6
+ #model = AutoModel.from_pretrained("ibm-research/granite-3.2-8b-instruct", device_map="auto")
7
+
8
+ model = AutoModel.from_pretrained(base_model, device_map="auto", torch_dtype="auto", quantization_config=None, gguf_file=GGUF_MODEL)
9
+ model.config
10
+ """
11
+ # pip install gguf
12
+ from transformers import AutoTokenizer, AutoModelForCausalLM
13
+
14
+ model_id = "TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF"
15
+ filename = "tinyllama-1.1b-chat-v1.0.Q6_K.gguf"
16
+
17
+ torch_dtype = torch.float32 # could be torch.float16 or torch.bfloat16 too
18
+ tokenizer = AutoTokenizer.from_pretrained(model_id, gguf_file=filename)
19
+ model = AutoModelForCausalLM.from_pretrained(model_id, gguf_file=filename, torch_dtype=torch_dtype)
20
+
21
+ """
main.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Optional
2
+ from pydantic import BaseModel, Field
3
+ from fastapi import FastAPI, HTTPException
4
+ from contextlib import asynccontextmanager
5
+ from starlette.responses import FileResponse
6
+
7
+ class ConvertRequest(BaseModel):
8
+ hf_model_name: str
9
+ hf_token: Optional[str] = Field(None, description="Hugging Face token for private models")
10
+
11
+ @asynccontextmanager
12
+ async def lifespan(app:FastAPI):
13
+ yield
14
+
15
+ app = FastAPI(title="Huggingface Safetensor Model Converter to AWQ", version="0.1.0", lifespan=lifespan)
16
+
17
+ @app.get("/health")
18
+ def read_root():
19
+ return {"status": "ok"}
20
+
21
+ @app.post("/convert")
22
+ def convert(request: ConvertRequest)->FileResponse:
23
+ raise HTTPException(status_code=501, detail="Not Implemented yet")
24
+
25
+ #return FileResponse(file_location, media_type='application/octet-stream',filename=file_name)
requirements.txt ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ pip
2
+ torch
3
+ torchvision
4
+ torchaudio
5
+ setuptools
6
+ wheel
7
+ pydantic
8
+ fastapi[standard]
9
+ transformers
10
+ huggingface_hub
11
+ autoawq[kernels]
12
+ starlette>=0.46.2
thrash.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer, GPTQConfig
2
+
3
+ #model_name = "ibm-granite/granite-3.2-8b-instruct"
4
+ #
5
+ #for bits in [4, 8]:
6
+ # tokenizer = AutoTokenizer.from_pretrained("ibm-granite/granite-3.2-8b-instruct")
7
+ #
8
+ #
9
+ # gptq_config = GPTQConfig(bits=bits, tokenizer=tokenizer)
10
+ #
11
+ # quantized_model = AutoModelForCausalLM.from_pretrained("ibm-granite/granite-3.2-8b-instruct", device_map="auto", quantization_config=gptq_config)
12
+ #
13
+ # quantized_model.save_pretrained(f"ai-i9p/{model_name.split('/')[-1]}-GPTQ-Int{bits}")
14
+ # tokenizer.save_pretrained(f"ai-i9p/{model_name.split('/')[-1]}-GPTQ-Int{bits}")
15
+ #
16
+ # quantized_model.to("cpu")
17
+ # quantized_model.save_pretrained(f"ai-i9p/{model_name.split('/')[-1]}-GPTQ-Int{bits}")
18
+
19
+ from awq import AutoAWQForCausalLM
20
+ from transformers import AutoTokenizer
21
+
22
+ model_path = 'Qwen/Qwen2.5-14B-Instruct'
23
+ quant_path = 'Qwen2.5-14B-Instruct-awq'
24
+ quant_config = { "zero_point": True, "q_group_size": 128, "w_bit": 4, "version": "GEMM" }
25
+
26
+ # Load model
27
+ model = AutoAWQForCausalLM.from_pretrained(model_path)
28
+ tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
29
+
30
+ # Quantize
31
+ model.quantize(tokenizer, quant_config=quant_config)
32
+
33
+ # Save quantized model
34
+ model.save_quantized(quant_path)
35
+ tokenizer.save_pretrained(quant_path)
36
+
37
+ print(f'Model is quantized and saved at "{quant_path}"')