Spaces:
Running
Running
initial commit
Browse files- .gitignore +7 -0
- Dockerfile +17 -0
- README.md +18 -5
- docker-compose.yaml +15 -0
- requirements.txt +8 -0
- src/main.py +303 -0
.gitignore
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# local config
|
2 |
+
docker-compose.override.yaml
|
3 |
+
|
4 |
+
# PhpStorm / IDEA
|
5 |
+
.idea
|
6 |
+
# NetBeans
|
7 |
+
nbproject
|
Dockerfile
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
RUN useradd -m -u 1000 user
|
4 |
+
USER user
|
5 |
+
|
6 |
+
ENV HOME=/home/user \
|
7 |
+
PATH=/home/user/.local/bin:$PATH
|
8 |
+
|
9 |
+
WORKDIR $HOME/app
|
10 |
+
|
11 |
+
COPY --chown=user requirements.txt requirements.txt
|
12 |
+
|
13 |
+
RUN pip install --upgrade -r requirements.txt
|
14 |
+
|
15 |
+
COPY --chown=user . .
|
16 |
+
|
17 |
+
CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
@@ -1,12 +1,25 @@
|
|
1 |
---
|
2 |
title: Local Inference Service
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: docker
|
7 |
pinned: false
|
8 |
license: other
|
9 |
-
short_description: This services allows HF inference provider compatible infere
|
10 |
---
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
title: Local Inference Service
|
3 |
+
emoji: 🦀
|
4 |
+
colorFrom: green
|
5 |
+
colorTo: yellow
|
6 |
sdk: docker
|
7 |
pinned: false
|
8 |
license: other
|
|
|
9 |
---
|
10 |
|
11 |
+
# Pimcore Local Inference Service
|
12 |
+
|
13 |
+
This services allows HF inference provider compatible inference to models which are not available at HF inference providers.
|
14 |
+
|
15 |
+
## Supported Tasks / Models
|
16 |
+
|
17 |
+
- Zero-Shot Image Classification
|
18 |
+
- Translation
|
19 |
+
- Image To Text
|
20 |
+
|
21 |
+
|
22 |
+
|
23 |
+
|
24 |
+
|
25 |
+
|
docker-compose.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
services:
|
2 |
+
server:
|
3 |
+
build:
|
4 |
+
context: .
|
5 |
+
ports:
|
6 |
+
- 7860:7860
|
7 |
+
develop:
|
8 |
+
watch:
|
9 |
+
- action: rebuild
|
10 |
+
path: .
|
11 |
+
volumes:
|
12 |
+
- python-cache:/home/user/.cache
|
13 |
+
|
14 |
+
volumes:
|
15 |
+
python-cache:
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi==0.111.*
|
2 |
+
requests==2.*
|
3 |
+
uvicorn[standard]==0.30.*
|
4 |
+
transformers
|
5 |
+
sentencepiece
|
6 |
+
sacremoses
|
7 |
+
torch
|
8 |
+
# Optional dependencies for specific features
|
src/main.py
ADDED
@@ -0,0 +1,303 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -------------------------------------------------------------------
|
2 |
+
# This source file is available under the terms of the
|
3 |
+
# Pimcore Open Core License (POCL)
|
4 |
+
# Full copyright and license information is available in
|
5 |
+
# LICENSE.md which is distributed with this source code.
|
6 |
+
#
|
7 |
+
# @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com)
|
8 |
+
# @license Pimcore Open Core License (POCL)
|
9 |
+
# -------------------------------------------------------------------
|
10 |
+
|
11 |
+
import os
|
12 |
+
import torch
|
13 |
+
|
14 |
+
#from .training_status import Status
|
15 |
+
#from .environment_variable_checker import EnvironmentVariableChecker
|
16 |
+
|
17 |
+
#from .training_manager import TrainingManager
|
18 |
+
#from .image_classification.image_classification_trainer import ImageClassificationTrainer
|
19 |
+
#from .image_classification.image_classification_parameters import ImageClassificationParameters, map_image_classification_training_parameters, ImageClassificationTrainingParameters
|
20 |
+
#from .text_classification.text_classification_trainer import TextClassificationTrainer
|
21 |
+
#from .text_classification.text_classification_parameters import TextClassificationParameters, map_text_classification_training_parameters, TextClassificationTrainingParameters
|
22 |
+
|
23 |
+
|
24 |
+
from fastapi import FastAPI, Depends, HTTPException, UploadFile, Form, File, status
|
25 |
+
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
26 |
+
from pydantic import BaseModel
|
27 |
+
from typing import Annotated
|
28 |
+
|
29 |
+
|
30 |
+
import logging
|
31 |
+
from pathlib import Path
|
32 |
+
import tempfile
|
33 |
+
import sys
|
34 |
+
|
35 |
+
|
36 |
+
from transformers import pipeline
|
37 |
+
|
38 |
+
app = FastAPI(
|
39 |
+
title="Pimcore Local Inference Service",
|
40 |
+
description="This services allows HF inference provider compatible inference to models which are not available at HF inference providers.",
|
41 |
+
version="1.0.0"
|
42 |
+
)
|
43 |
+
|
44 |
+
#environmentVariableChecker = EnvironmentVariableChecker()
|
45 |
+
#environmentVariableChecker.validate_environment_variables()
|
46 |
+
|
47 |
+
logging.basicConfig(format='%(asctime)s %(levelname)-8s %(message)s')
|
48 |
+
logger = logging.getLogger(__name__)
|
49 |
+
logger.setLevel(logging.DEBUG)
|
50 |
+
|
51 |
+
|
52 |
+
class StreamToLogger(object):
|
53 |
+
def __init__(self, logger, log_level):
|
54 |
+
self.logger = logger
|
55 |
+
self.log_level = log_level
|
56 |
+
self.linebuf = ''
|
57 |
+
|
58 |
+
def write(self, buf):
|
59 |
+
for line in buf.rstrip().splitlines():
|
60 |
+
self.logger.log(self.log_level, line.rstrip())
|
61 |
+
|
62 |
+
def flush(self):
|
63 |
+
pass
|
64 |
+
|
65 |
+
sys.stdout = StreamToLogger(logger, logging.INFO)
|
66 |
+
sys.stderr = StreamToLogger(logger, logging.ERROR)
|
67 |
+
|
68 |
+
#classification_trainer: TrainingManager = TrainingManager()
|
69 |
+
|
70 |
+
|
71 |
+
class ResponseModel(BaseModel):
|
72 |
+
""" Default response model for endpoints. """
|
73 |
+
message: str
|
74 |
+
success: bool = True
|
75 |
+
|
76 |
+
|
77 |
+
# ===========================================
|
78 |
+
# Security Check
|
79 |
+
# ===========================================
|
80 |
+
|
81 |
+
# security = HTTPBearer()
|
82 |
+
# def verify_token(credentials: HTTPAuthorizationCredentials = Depends(security)):
|
83 |
+
# """Verify the token provided by the user."""
|
84 |
+
|
85 |
+
# token = environmentVariableChecker.get_authentication_token()
|
86 |
+
|
87 |
+
# if credentials.credentials != token:
|
88 |
+
# raise HTTPException(
|
89 |
+
# status_code=status.HTTP_401_UNAUTHORIZED,
|
90 |
+
# detail="Invalid token",
|
91 |
+
# headers={"WWW-Authenticate": "Bearer"},
|
92 |
+
# )
|
93 |
+
# return {"token": credentials.credentials}
|
94 |
+
|
95 |
+
|
96 |
+
# ===========================================
|
97 |
+
# Training Status Endpoints
|
98 |
+
# ===========================================
|
99 |
+
|
100 |
+
# @app.get("/get_training_status")
|
101 |
+
# async def get_task_status(token_data: dict = Depends(verify_token)):
|
102 |
+
# """ Get the status of the currently running training (if any). """
|
103 |
+
# status = classification_trainer.get_task_status()
|
104 |
+
# return {
|
105 |
+
# "project": status.get_project_name(),
|
106 |
+
# "progress": status.get_progress(),
|
107 |
+
# "task": status.get_task(),
|
108 |
+
# "status": status.get_status().value
|
109 |
+
# }
|
110 |
+
|
111 |
+
# @app.put("/stop_training")
|
112 |
+
# async def stop_task(token_data: dict = Depends(verify_token)):
|
113 |
+
# """ Stop the currently running training (if any). """
|
114 |
+
# try:
|
115 |
+
# status = classification_trainer.get_task_status()
|
116 |
+
# classification_trainer.stop_task()
|
117 |
+
# return ResponseModel(message=f"Training stopped for `{ status.get_project_name() }`")
|
118 |
+
# except Exception as e:
|
119 |
+
# raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
120 |
+
|
121 |
+
|
122 |
+
@app.get("/gpu_check")
|
123 |
+
async def gpu_check():
|
124 |
+
""" Check if a GPU is available """
|
125 |
+
|
126 |
+
gpu = 'GPU not available'
|
127 |
+
if torch.cuda.is_available():
|
128 |
+
gpu = 'GPU is available'
|
129 |
+
print("GPU is available")
|
130 |
+
else:
|
131 |
+
print("GPU is not available")
|
132 |
+
|
133 |
+
return {'success': True, 'gpu': gpu}
|
134 |
+
|
135 |
+
|
136 |
+
from fastapi import Body
|
137 |
+
from typing import Optional
|
138 |
+
|
139 |
+
class TranslationRequest(BaseModel):
|
140 |
+
inputs: str
|
141 |
+
parameters: Optional[dict] = None
|
142 |
+
|
143 |
+
@app.post(
|
144 |
+
"/translation/{model_name:path}/",
|
145 |
+
)
|
146 |
+
async def translation(
|
147 |
+
model_name: str,
|
148 |
+
body: TranslationRequest = Body(
|
149 |
+
...,
|
150 |
+
example={
|
151 |
+
"inputs": "I am a car",
|
152 |
+
"parameters": {
|
153 |
+
"repetition_penalty": 1.6,
|
154 |
+
}
|
155 |
+
}
|
156 |
+
)
|
157 |
+
):
|
158 |
+
"""
|
159 |
+
Execute translation tasks.
|
160 |
+
|
161 |
+
Args:
|
162 |
+
model_name (str): The HuggingFace model name to use for translation.
|
163 |
+
body (TranslationRequest): The request payload containing translation parameters.
|
164 |
+
|
165 |
+
Returns:
|
166 |
+
list: The translation result(s) as returned by the pipeline.
|
167 |
+
"""
|
168 |
+
|
169 |
+
try:
|
170 |
+
pipe = pipeline("translation", model=model_name)
|
171 |
+
except Exception as e:
|
172 |
+
logger.error(f"Failed to load model '{model_name}': {str(e)}")
|
173 |
+
raise HTTPException(
|
174 |
+
status_code=404,
|
175 |
+
detail=f"Model '{model_name}' could not be loaded: {str(e)}"
|
176 |
+
)
|
177 |
+
|
178 |
+
try:
|
179 |
+
result = pipe(body.inputs, **(body.parameters or {}))
|
180 |
+
except Exception as e:
|
181 |
+
logger.error(f"Inference failed for model '{model_name}': {str(e)}")
|
182 |
+
raise HTTPException(
|
183 |
+
status_code=500,
|
184 |
+
detail=f"Inference failed: {str(e)}"
|
185 |
+
)
|
186 |
+
|
187 |
+
return result
|
188 |
+
|
189 |
+
|
190 |
+
# ===========================================
|
191 |
+
# Fine-Tuning Image Classification
|
192 |
+
# ===========================================
|
193 |
+
|
194 |
+
# @app.post(
|
195 |
+
# "/training/image_classification",
|
196 |
+
# response_model=ResponseModel
|
197 |
+
# )
|
198 |
+
# async def image_classification(
|
199 |
+
# training_params: Annotated[ImageClassificationTrainingParameters, Depends(map_image_classification_training_parameters)],
|
200 |
+
# training_data_zip: Annotated[UploadFile, File(description="The ZIP file containing the training data, with a folder per class which contains images belonging to that class.")],
|
201 |
+
# token_data: dict = Depends(verify_token),
|
202 |
+
# project_name: str = Form(description="The name of the project. Will also be used as name of resulting model that will be created after fine tuning and as the name of the repository at huggingface."),
|
203 |
+
# source_model_name: str = Form('google/vit-base-patch16-224-in21k', description="The source model to be used as basis for fine tuning."),
|
204 |
+
# ):
|
205 |
+
# """
|
206 |
+
# Start fine tuning an image classification model with the provided data.
|
207 |
+
# """
|
208 |
+
|
209 |
+
# # check if training is running, if so then exit
|
210 |
+
# status = classification_trainer.get_task_status()
|
211 |
+
# if status.get_status() == Status.IN_PROGRESS or status.get_status() == Status.CANCELLING:
|
212 |
+
# raise HTTPException(status_code=405, detail="Training is already in progress.")
|
213 |
+
|
214 |
+
# # Ensure the uploaded file is a ZIP file
|
215 |
+
# if not training_data_zip.filename.endswith(".zip"):
|
216 |
+
# raise HTTPException(status_code=422, detail="Uploaded file is not a zip file.")
|
217 |
+
|
218 |
+
# try:
|
219 |
+
# # Create a temporary directory to extract the contents
|
220 |
+
# tmp_path = os.path.join(tempfile.gettempdir(), 'training_data')
|
221 |
+
# path = Path(tmp_path)
|
222 |
+
# path.mkdir(parents=True, exist_ok=True)
|
223 |
+
|
224 |
+
# contents = await training_data_zip.read()
|
225 |
+
# zip_path = os.path.join(tmp_path, 'image_classification_data.zip')
|
226 |
+
# with open(zip_path, 'wb') as temp_file:
|
227 |
+
# temp_file.write(contents)
|
228 |
+
|
229 |
+
# # prepare parameters
|
230 |
+
# parameters = ImageClassificationParameters(
|
231 |
+
# training_files_path=tmp_path,
|
232 |
+
# training_zip_file_path=zip_path,
|
233 |
+
# project_name=project_name,
|
234 |
+
# source_model_name=source_model_name,
|
235 |
+
# training_parameters=training_params
|
236 |
+
# )
|
237 |
+
|
238 |
+
# # start training
|
239 |
+
# await classification_trainer.start_training(ImageClassificationTrainer(), parameters)
|
240 |
+
|
241 |
+
# return ResponseModel(message="Training started.")
|
242 |
+
|
243 |
+
# except Exception as e:
|
244 |
+
# raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
245 |
+
|
246 |
+
|
247 |
+
|
248 |
+
|
249 |
+
# ===========================================
|
250 |
+
# Fine-Tuning Text Classification
|
251 |
+
# ===========================================
|
252 |
+
|
253 |
+
# @app.post(
|
254 |
+
# "/training/text_classification",
|
255 |
+
# response_model=ResponseModel
|
256 |
+
# )
|
257 |
+
# async def text_classificaiton(
|
258 |
+
# training_params: Annotated[TextClassificationTrainingParameters, Depends(map_text_classification_training_parameters)],
|
259 |
+
# training_data_csv: Annotated[UploadFile, File(description="The CSV file containing the training data, necessary columns `value` (text data) and `target` (classification).")],
|
260 |
+
# token_data: dict = Depends(verify_token),
|
261 |
+
# project_name: str = Form(description="The name of the project. Will also be used as name of resulting model that will be created after fine tuning and as the name of the repository at huggingface."),
|
262 |
+
# training_csv_limiter: str = Form(';', description="The delimiter used in the CSV file."),
|
263 |
+
# source_model_name: str = Form('distilbert/distilbert-base-uncased'),
|
264 |
+
# ):
|
265 |
+
# """Start fine tuning an text classification model with the provided data."""
|
266 |
+
|
267 |
+
# # check if training is running, if so then exit
|
268 |
+
# status = classification_trainer.get_task_status()
|
269 |
+
# if status.get_status() == Status.IN_PROGRESS or status.get_status() == Status.CANCELLING:
|
270 |
+
# raise HTTPException(status_code=405, detail="Training is already in progress")
|
271 |
+
|
272 |
+
# # Ensure the uploaded file is a CSV file
|
273 |
+
# if not training_data_csv.filename.endswith(".csv"):
|
274 |
+
# raise HTTPException(status_code=422, detail="Uploaded file is not a csv file.")
|
275 |
+
|
276 |
+
# try:
|
277 |
+
# # Create a temporary directory to extract the contents
|
278 |
+
# tmp_path = os.path.join(tempfile.gettempdir(), 'training_data')
|
279 |
+
# path = Path(tmp_path)
|
280 |
+
# path.mkdir(parents=True, exist_ok=True)
|
281 |
+
|
282 |
+
# contents = await training_data_csv.read()
|
283 |
+
# csv_path = os.path.join(tmp_path, 'data.csv')
|
284 |
+
# with open(csv_path, 'wb') as temp_file:
|
285 |
+
# temp_file.write(contents)
|
286 |
+
|
287 |
+
# # prepare parameters
|
288 |
+
# parameters = TextClassificationParameters(
|
289 |
+
# training_csv_file_path=csv_path,
|
290 |
+
# training_csv_limiter=training_csv_limiter,
|
291 |
+
# project_name=project_name,
|
292 |
+
# source_model_name=source_model_name,
|
293 |
+
# training_parameters=training_params
|
294 |
+
# )
|
295 |
+
|
296 |
+
# # start training
|
297 |
+
# await classification_trainer.start_training(TextClassificationTrainer(), parameters)
|
298 |
+
|
299 |
+
# return ResponseModel(message="Training started.")
|
300 |
+
|
301 |
+
# except Exception as e:
|
302 |
+
# raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
303 |
+
|