sam2ai commited on
Commit
20a070f
·
1 Parent(s): 0785644

Synced repo using 'sync_with_huggingface' Github Action

Browse files
Files changed (5) hide show
  1. Dockerfile +67 -0
  2. app.py +171 -0
  3. requirements.txt +16 -0
  4. utils/__init__.py +0 -0
  5. utils/utils.py +0 -0
Dockerfile ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM nvidia/cuda:11.7.1-cudnn8-runtime-ubuntu20.04
2
+
3
+ # Use Python 3.11 for better Python perf
4
+ # Update the package lists and install necessary dependencies
5
+ RUN apt-get update && apt-get install -y \
6
+ software-properties-common \
7
+ && add-apt-repository -y ppa:deadsnakes/ppa \
8
+ && apt-get update \
9
+ && apt-get install -y python3.11 python3.11-dev
10
+
11
+ # Set Python 3.11 as the default version (for python3)
12
+ RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1
13
+
14
+ # Download get-pip.py script
15
+ RUN apt install curl -y
16
+ RUN curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
17
+
18
+ # Install pip for Python 3.11
19
+ RUN python3 get-pip.py
20
+
21
+ # Verify Python and pip versions
22
+ RUN python3 --version && pip3.11 --version
23
+
24
+ # Set pip3.11 as the default pip command
25
+ RUN update-alternatives --install /usr/bin/pip3 pip3 /usr/local/lib/python3.11/dist-packages/pip 1
26
+
27
+ ENV PYTHONUNBUFFERED=1
28
+
29
+ # Install necessary dependencies
30
+ # RUN apt-get update && \
31
+ # apt-get install -y python3-pip
32
+
33
+ # Set the working directory. /app is mounted to the container with -v,
34
+ # but we want to have the right cwd for uvicorn command below
35
+ RUN mkdir /app
36
+ # WORKDIR /app
37
+
38
+ # # Copy the app code and requirements filed
39
+ # COPY . /app
40
+ # COPY requirements.txt .
41
+ # WORKDIR $PYSETUP_PATH
42
+ COPY ./requirements.txt /app
43
+
44
+
45
+ COPY ./utils /app/utils
46
+ # COPY ./static /app/static
47
+ # COPY ./templates /app/templates
48
+ COPY ./app.py /app/app.py
49
+ # COPY ./download.py /app/download.py
50
+
51
+ WORKDIR /app
52
+
53
+
54
+ # Install the app dependencies
55
+ # RUN pip3 install -r requirements.txt
56
+
57
+ RUN --mount=type=cache,target=/root/.cache/pip \
58
+ pip3 install -r requirements.txt
59
+
60
+ # Expose the FastAPI port
61
+ EXPOSE 7860
62
+
63
+ # Start the FastAPI app using Uvicorn web server
64
+ # CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "14000", "--limit-concurrency", "1000"]
65
+ # RUN python3 download.py
66
+
67
+ CMD ["python3", "app.py", "--host=0.0.0.0", "--port=7860", "--model_path=BAAI/bge-small-en-v1.5", "--num_workers=2"]
app.py ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import asyncio
3
+ import functools
4
+ import json
5
+ import os
6
+ from io import BytesIO
7
+
8
+ import uvicorn
9
+ from fastapi import FastAPI, BackgroundTasks, File, Body, UploadFile, Request
10
+ from fastapi.responses import StreamingResponse
11
+ # from faster_whisper import WhisperModel
12
+ from starlette.staticfiles import StaticFiles
13
+ from starlette.templating import Jinja2Templates
14
+ from sentence_transformers import SentenceTransformer
15
+ # from zhconv import convert
16
+
17
+ # from utils.data_utils import remove_punctuation
18
+ # from utils.utils import add_arguments, print_arguments
19
+
20
+
21
+ import hashlib
22
+ import os
23
+ import tarfile
24
+ import urllib.request
25
+
26
+ # from tqdm import tqdm
27
+
28
+
29
+ def print_arguments(args):
30
+ print("----------- Configuration Arguments -----------")
31
+ for arg, value in vars(args).items():
32
+ print("%s: %s" % (arg, value))
33
+ print("------------------------------------------------")
34
+
35
+
36
+ def strtobool(val):
37
+ val = val.lower()
38
+ if val in ('y', 'yes', 't', 'true', 'on', '1'):
39
+ return True
40
+ elif val in ('n', 'no', 'f', 'false', 'off', '0'):
41
+ return False
42
+ else:
43
+ raise ValueError("invalid truth value %r" % (val,))
44
+
45
+
46
+ def str_none(val):
47
+ if val == 'None':
48
+ return None
49
+ else:
50
+ return val
51
+
52
+
53
+ def add_arguments(argname, type, default, help, argparser, **kwargs):
54
+ type = strtobool if type == bool else type
55
+ type = str_none if type == str else type
56
+ argparser.add_argument("--" + argname,
57
+ default=default,
58
+ type=type,
59
+ help=help + ' Default: %(default)s.',
60
+ **kwargs)
61
+
62
+ os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
63
+
64
+ parser = argparse.ArgumentParser(description=__doc__)
65
+ add_arg = functools.partial(add_arguments, argparser=parser)
66
+
67
+ add_arg("host", type=str, default="0.0.0.0", help="")
68
+ add_arg("port", type=int, default=5000, help="")
69
+ add_arg("model_path", type=str, default="BAAI/bge-small-en-v1.5", help="")
70
+ add_arg("use_gpu", type=bool, default=False, help="")
71
+ # add_arg("use_int8", type=bool, default=True, help="")
72
+ add_arg("beam_size", type=int, default=10, help="")
73
+ add_arg("num_workers", type=int, default=2, help="")
74
+ add_arg("vad_filter", type=bool, default=True, help="")
75
+ add_arg("local_files_only", type=bool, default=True, help="")
76
+ args = parser.parse_args()
77
+ print_arguments(args)
78
+
79
+ #
80
+ # assert os.path.exists(args.model_path), f"{args.model_path}"
81
+ #
82
+ if args.use_gpu:
83
+ model = SentenceTransformer(args.model_path, device="cuda", compute_type="float16")
84
+ else:
85
+ model = SentenceTransformer(args.model_path, device='cpu')
86
+
87
+
88
+ #
89
+ # _, _ = model.transcribe("dataset/test.wav", beam_size=5)
90
+
91
+ app = FastAPI(title="embedding Inference")
92
+ # app.mount('/static', StaticFiles(directory='static'), name='static')
93
+ # templates = Jinja2Templates(directory="templates")
94
+ # model_semaphore = None
95
+
96
+
97
+ # def release_model_semaphore():
98
+ # model_semaphore.release()
99
+
100
+
101
+ # def recognition(file: File, to_simple: int,
102
+ # remove_pun: int, language: str = "bn",
103
+ # task: str = "transcribe"
104
+ # ):
105
+
106
+ # segments, info = model.transcribe(file, beam_size=10, task=task, language=language, vad_filter=args.vad_filter)
107
+ # for segment in segments:
108
+ # text = segment.text
109
+ # if to_simple == 1:
110
+ # # text = convert(text, '')
111
+ # pass
112
+ # if remove_pun == 1:
113
+ # # text = remove_punctuation(text)
114
+ # pass
115
+ # ret = {"result": text, "start": round(segment.start, 2), "end": round(segment.end, 2)}
116
+ # #
117
+ # yield json.dumps(ret).encode() + b"\0"
118
+
119
+
120
+ # @app.post("/recognition_stream")
121
+ # async def api_recognition_stream(
122
+ # to_simple: int = Body(1, description="", embed=True),
123
+ # remove_pun: int = Body(0, description="", embed=True),
124
+ # language: str = Body("bn", description="", embed=True),
125
+ # task: str = Body("transcribe", description="", embed=True),
126
+ # audio: UploadFile = File(..., description="")
127
+ # ):
128
+
129
+ # global model_semaphore
130
+ # if language == "None": language = None
131
+ # if model_semaphore is None:
132
+ # model_semaphore = asyncio.Semaphore(5)
133
+ # await model_semaphore.acquire()
134
+ # contents = await audio.read()
135
+ # data = BytesIO(contents)
136
+ # generator = recognition(
137
+ # file=data, to_simple=to_simple,
138
+ # remove_pun=remove_pun, language=language,
139
+ # task=task
140
+ # )
141
+ # background_tasks = BackgroundTasks()
142
+ # background_tasks.add_task(release_model_semaphore)
143
+ # return StreamingResponse(generator, background=background_tasks)
144
+
145
+
146
+ @app.post("/embed")
147
+ async def api_embed(
148
+ textA: str = Body("text1", description="", embed=True),
149
+ textB: str = Body("text2", description="", embed=True),
150
+ ):
151
+
152
+ q_embeddings = model.encode(textA, normalize_embeddings=True)
153
+ p_embeddings = model.encode(textB, normalize_embeddings=True)
154
+
155
+ scores = q_embeddings @ p_embeddings.T
156
+ print(scores)
157
+ scores = scores.tolist()
158
+
159
+ ret = {"similarity score": scores, "status_code": 200}
160
+ return ret
161
+
162
+
163
+ # @app.get("/")
164
+ # async def index(request: Request):
165
+ # return templates.TemplateResponse(
166
+ # "index.html", {"request": request, "id": id}
167
+ # )
168
+
169
+
170
+ if __name__ == '__main__':
171
+ uvicorn.run(app, host=args.host, port=args.port)
requirements.txt ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ numpy>=1.23.1
2
+ dataclasses>=0.6
3
+ transformers>=4.31.0
4
+ bitsandbytes>=0.41.0
5
+ datasets>=2.11.0
6
+ evaluate>=0.4.0
7
+ peft>=0.4.0
8
+ accelerate>=0.21.0
9
+ tqdm>=4.62.1
10
+ uvicorn>=0.21.1
11
+ fastapi>=0.95.1
12
+ starlette>=0.26.1
13
+ python-multipart
14
+ sentence_transformers
15
+ FlagEmbedding
16
+ gensim
utils/__init__.py ADDED
File without changes
utils/utils.py ADDED
File without changes