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from typing import Dict
|
3 |
+
from typing import List
|
4 |
+
|
5 |
+
|
6 |
+
def report_gpu_usage() -> str:
|
7 |
+
import os
|
8 |
+
return os.popen("nvidia-smi").read()
|
9 |
+
|
10 |
+
|
11 |
+
class EndpointHandler:
|
12 |
+
def __init__(self, path=""):
|
13 |
+
import torch
|
14 |
+
from transformers import AutoModelForSeq2SeqLM
|
15 |
+
from transformers import AutoTokenizer
|
16 |
+
import os
|
17 |
+
|
18 |
+
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
19 |
+
model_kwargs: dict[str, any] = dict()
|
20 |
+
if torch.cuda.is_available():
|
21 |
+
model_kwargs["load_in_8bit"] = True
|
22 |
+
model_kwargs["torch_dtype"] = torch.bfloat16
|
23 |
+
model_kwargs["device_map"] = "auto"
|
24 |
+
model_kwargs["low_cpu_mem_usage"] = True
|
25 |
+
self.model = AutoModelForSeq2SeqLM.from_pretrained(path, **model_kwargs)
|
26 |
+
self.tokenizer = AutoTokenizer.from_pretrained(path)
|
27 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
28 |
+
print(f"Loaded model {path} to {self.device}")
|
29 |
+
|
30 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, any]]:
|
31 |
+
import os
|
32 |
+
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
33 |
+
|
34 |
+
inputs = data.pop("inputs", data)
|
35 |
+
input_ids = self.tokenizer(inputs, return_tensors="pt").input_ids.to(self.device)
|
36 |
+
|
37 |
+
parameters = data.pop("parameters", None)
|
38 |
+
if parameters is None:
|
39 |
+
parameters = dict()
|
40 |
+
|
41 |
+
with BlockTimer() as timer:
|
42 |
+
outputs = self.model.generate(input_ids, **parameters)
|
43 |
+
print(f"Inference elapsed: {round(timer.duration, 2)}")
|
44 |
+
return_value: List[Dict[str, any]] = list()
|
45 |
+
# postprocess the prediction
|
46 |
+
gpu_info = report_gpu_usage()
|
47 |
+
for output in outputs:
|
48 |
+
prediction = self.tokenizer.decode(output, skip_special_tokens=True)
|
49 |
+
entry = {"generated_text": prediction, "elapsed": timer.duration}
|
50 |
+
if gpu_info:
|
51 |
+
entry["gpu_info"] = gpu_info
|
52 |
+
gpu_info = None
|
53 |
+
return_value.append(entry)
|
54 |
+
return return_value
|
55 |
+
|
56 |
+
|
57 |
+
class BlockTimer(object):
|
58 |
+
def __enter__(self):
|
59 |
+
import time
|
60 |
+
self.start = time.perf_counter()
|
61 |
+
return self
|
62 |
+
|
63 |
+
def __exit__(self, typ, value, traceback):
|
64 |
+
import time
|
65 |
+
self.duration = time.perf_counter() - self.start
|
66 |
+
|
67 |
+
|
68 |
+
def _force_not_available() -> bool:
|
69 |
+
return False
|
70 |
+
|
71 |
+
|
72 |
+
def test() -> None:
|
73 |
+
import textwrap
|
74 |
+
# torch.cuda.is_available = _force_not_available
|
75 |
+
handler = EndpointHandler(path="bigscience/mt0-xl")
|
76 |
+
parameters: dict[str, any] = {"max_length": 256, "min_length": 1, #
|
77 |
+
"no_repeat_ngram_size": 3, #
|
78 |
+
# "encoder_no_repeat_ngram_size": 7, #
|
79 |
+
"repetition_penalty": 3.5, #
|
80 |
+
# "num_beams": 1, #
|
81 |
+
# "top_p": 0.7, # 0.3, 0.7
|
82 |
+
"do_sample": True,
|
83 |
+
"temperature": 0.1,
|
84 |
+
"early_stopping": True, } # parameters for text generation
|
85 |
+
payload = {"inputs": f"{wall_of_text()}", "parameters": parameters}
|
86 |
+
results = handler.__call__(payload)
|
87 |
+
for entry in results[0].items():
|
88 |
+
print()
|
89 |
+
print(f"=== {entry[0]}")
|
90 |
+
if entry[0] == "gpu_info":
|
91 |
+
gpu_info_lines = entry[1].split("\n")
|
92 |
+
for line in gpu_info_lines:
|
93 |
+
if "Default |" in line:
|
94 |
+
print(line)
|
95 |
+
else:
|
96 |
+
print(textwrap.fill(str(
|
97 |
+
entry[1]), 140, drop_whitespace=False, replace_whitespace=False))
|
98 |
+
|
99 |
+
|
100 |
+
def wall_of_text() -> str:
|
101 |
+
return """
|
102 |
+
Write a journal article headline for the following.
|
103 |
+
|
104 |
+
The present invention relates to compositions and methods for the treatment of the
|
105 |
+
Charcot-Marie-Tooth disease and related disorders. Charcot-Marie-Tooth disease (“CMT
|
106 |
+
Mining
|
107 |
+
of publicly available data, describing molecular mechanisms and pathological
|
108 |
+
manifestations
|
109 |
+
of the CMT1A disease, allowed us to prioritize a few functional cellular
|
110 |
+
modules-transcriptional regulation of PMP22 gene, PMP22 protein folding/degradation,
|
111 |
+
Schwann cell proliferation and apoptosis, death of neurons, extra-cellular matrix
|
112 |
+
deposition
|
113 |
+
and remodelling, immune response-as potential legitimate targets for CMT-relevant
|
114 |
+
therapeutic interventions. The combined impact of these deregulated functional modules on
|
115 |
+
onset and progression of pathological manifestations of Charcot-Marie-Tooth justifies a
|
116 |
+
potential efficacy of combinatorial CMT treatment. International patent application No.
|
117 |
+
PCT/EP2008/066457 describes a method of identifying drug candidates for the treatment of
|
118 |
+
the
|
119 |
+
Charcot-Marie-Tooth disease by building a dynamic model of the pathology and targeting
|
120 |
+
functional cellular pathways which are relevant in the regulation of CMT disease.
|
121 |
+
International patent application No. PCT/EP2008/066468 describes compositions for the
|
122 |
+
treatment of the Charcot-Marie-Tooth disease which comprise at least two compounds
|
123 |
+
selected
|
124 |
+
from the group of multiple drug candidates. The purpose of the present invention is to
|
125 |
+
provide new therapeutic combinations for treating CMT and related disorders. The invention
|
126 |
+
thus relates to compositions and methods for treating CMT and related disorders,
|
127 |
+
in particular toxic or traumatic neuropathy and amyotrophic lateral sclerosis,
|
128 |
+
using particular drug combinations. An object of this invention more specifically
|
129 |
+
relates to
|
130 |
+
a composition comprising baclofen, sorbitol and a compound selected from pilocarpine,
|
131 |
+
methimazole, mifepristone, naltrexone, rapamycin, flurbiprofen and ketoprofen, salts or
|
132 |
+
prodrugs thereof, for simultaneous, separate or sequential administration to a mammalian
|
133 |
+
subject. A particular object of the present invention relates to a composition comprising
|
134 |
+
baclofen, sorbitol and naltrexone, for simultaneous, separate or sequential administration
|
135 |
+
to a mammalian subject. Another object of the invention relates to a composition
|
136 |
+
comprising
|
137 |
+
(a) rapamycin, (b) mifepristone or naltrexone, and © a PMP22 modulator, for simultaneous,
|
138 |
+
separate or sequential administration to a mammalian subject. In a particular embodiment,
|
139 |
+
the PMP22 modulator is selected from acetazolamide, albuterol, amiloride,
|
140 |
+
aminoglutethimide,
|
141 |
+
amiodarone, aztreonam, baclofen, balsalazide, betaine, bethanechol, bicalutamide,
|
142 |
+
bromocriptine, bumetanide, buspirone, carbachol, carbamazepine, carbimazole, cevimeline,
|
143 |
+
ciprofloxacin, clonidine, curcumin, cyclosporine A, diazepam, diclofenac, dinoprostone,
|
144 |
+
disulfiram, D-sorbitol, dutasteride, estradiol, exemestane, felbamate, fenofibrate,
|
145 |
+
finasteride, flumazenil, flunitrazepam, flurbiprofen, furosemide, gabapentingabapentin,
|
146 |
+
galantamine, haloperidol, ibuprofen, isoproterenol, ketoconazole, ketoprofen, L-carnitine,
|
147 |
+
liothyronine (T3), lithium, losartan, loxapine, meloxicam, metaproterenol, metaraminol,
|
148 |
+
metformin, methacholine, methimazole, methylergonovine, metoprolol, metyrapone,
|
149 |
+
miconazole,
|
150 |
+
mifepristone, nadolol, naloxone, naltrexone; norfloxacin, pentazocine, phenoxybenzamine,
|
151 |
+
phenylbutyrate, pilocarpine, pioglitazone, prazosin, propylthiouracil, raloxifene,
|
152 |
+
rapamycin, rifampin, simvastatin, spironolactone, tacrolimus, tamoxifen, trehalose,
|
153 |
+
trilostane, valproic acid, salts or prodrugs thereof. 1. A method of improving nerve
|
154 |
+
regeneration in a human subject suffering from amyotrophic lateral sclerosis,
|
155 |
+
or a neuropathy selected from an idiopathic neuropathy, diabetic neuropathy,
|
156 |
+
a toxic neuropathy, a neuropathy induced by a drug treatment, a neuropathy provoked by
|
157 |
+
HIV,
|
158 |
+
a neuropathy provoked by radiation, a neuropathy provoked by heavy metals, a neuropathy
|
159 |
+
provoked by vitamin deficiency states, or a traumatic neuropathy, comprising administering
|
160 |
+
to the human subject an amount of a composition effective to improve nerve regeneration;
|
161 |
+
and
|
162 |
+
wherein the composition comprises baclofen or a pharmaceutically acceptable salt thereof
|
163 |
+
in
|
164 |
+
an amount from 1 to 300 mg/kg of the human subject per day; D-sorbitol or a
|
165 |
+
pharmaceutically
|
166 |
+
acceptable salt thereof; and naltrexone or a pharmaceutically acceptable salt thereof in
|
167 |
+
an
|
168 |
+
amount from 1 to 100 mg/kg of the human subject per day. 2. The method of claim 1,
|
169 |
+
wherein the composition further comprises a pharmaceutically suitable excipient or
|
170 |
+
carrier.
|
171 |
+
3. The method of claim 2, wherein the composition is formulated with a drug eluting
|
172 |
+
polymer,
|
173 |
+
a biomolecule, a micelle or liposome-forming lipids or oil in water emulsions,
|
174 |
+
or pegylated
|
175 |
+
or solid nanoparticles or microparticles for oral or parenteral or intrathecal
|
176 |
+
administration. 4. The method of claim 1, wherein the subject suffers from a traumatic
|
177 |
+
neuropathy arising from brain injury, spinal cord injury, or an injury to peripheral
|
178 |
+
nerves.
|
179 |
+
5. The method of claim 1, wherein the D-sorbitol or a pharmaceutically acceptable salt
|
180 |
+
thereof is D-sorbitol. 6. The method of claim 1, wherein the composition is formulated for
|
181 |
+
oral administration. 7. The method of claim 6, wherein the composition is a liquid
|
182 |
+
formulation. 8. The method of claim 1, wherein baclofen or a pharmaceutically acceptable
|
183 |
+
salt thereof, D-sorbitol or a pharmaceutically acceptable salt thereof, and naltrexone
|
184 |
+
or a
|
185 |
+
pharmaceutically acceptable salt thereof are the sole active ingredients. 9. The method of
|
186 |
+
claim 1, comprising administering to the human subject baclofen or a pharmaceutically
|
187 |
+
acceptable salt thereof in an amount from 10 to 200 mg/kg of the human subject per day and
|
188 |
+
naltrexone or a pharmaceutically acceptable salt thereof in an amount from 1 to 50 mg/kg
|
189 |
+
of
|
190 |
+
the human subject per day. 10. The method of claim 1, comprising administering to the
|
191 |
+
human
|
192 |
+
subject baclofen or a pharmaceutically acceptable salt thereof in an amount from 10 to 200
|
193 |
+
mg/kg of the human subject per day and naltrexone or a pharmaceutically acceptable salt
|
194 |
+
thereof in an amount from 1 to 50 mg/kg of the human subject per day. 11. The method of
|
195 |
+
claim 1, comprising administering to the human subject baclofen or a pharmaceutically
|
196 |
+
acceptable salt thereof in an amount from 60 mg to 18 mg per day and naltrexone or a
|
197 |
+
pharmaceutically acceptable salt thereof in an amount from 60 mg to 6 mg per day. 12. The
|
198 |
+
method of claim 1, comprising administering to the human subject baclofen or a
|
199 |
+
pharmaceutically acceptable salt thereof in an amount from 60 mg to 12 mg per day and
|
200 |
+
naltrexone or a pharmaceutically acceptable salt thereof in an amount from 60 mg to 3 mg
|
201 |
+
per
|
202 |
+
day. 13. The method of claim 10, wherein baclofen or a pharmaceutically acceptable salt
|
203 |
+
thereof, D-sorbitol or a pharmaceutically acceptable salt thereof, and naltrexone or a
|
204 |
+
pharmaceutically acceptable salt thereof are administered orally to the human subject. 14.
|
205 |
+
The method of claim 10, wherein baclofen or a pharmaceutically acceptable salt thereof,
|
206 |
+
D-sorbitol or a pharmaceutically acceptable salt thereof, and naltrexone or a
|
207 |
+
pharmaceutically acceptable salt thereof are administered separately to the human subject.
|
208 |
+
15. The method of claim 13, wherein baclofen or a pharmaceutically acceptable salt
|
209 |
+
thereof,
|
210 |
+
D-sorbitol or a pharmaceutically acceptable salt thereof, and naltrexone or a
|
211 |
+
pharmaceutically acceptable salt thereof are formulated in a liquid formulation. 16. The
|
212 |
+
method of claim 15, wherein baclofen or a pharmaceutically acceptable salt thereof,
|
213 |
+
D-sorbitol or a pharmaceutically acceptable salt thereof, and naltrexone or a
|
214 |
+
pharmaceutically acceptable salt thereof are administered to the human subject in divided
|
215 |
+
doses. 17. The method of claim 15, wherein baclofen or a pharmaceutically acceptable salt
|
216 |
+
thereof, D-sorbitol or a pharmaceutically acceptable salt thereof, and naltrexone or a
|
217 |
+
pharmaceutically acceptable salt thereof are administered to the human subject in divided
|
218 |
+
doses two times daily.
|
219 |
+
""".replace("\n", " ")
|
220 |
+
|
221 |
+
|
222 |
+
if __name__ == '__main__':
|
223 |
+
test()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
accelerate~=0.18
|
2 |
+
bitsandbytes~=0.37
|
3 |
+
transformers~=4.27
|
4 |
+
ctranslate2~=3.10
|