chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
# flake8: noqa
|
||||
|
||||
# __import_start__
|
||||
from starlette.requests import Request
|
||||
|
||||
import ray
|
||||
from ray import serve
|
||||
|
||||
# __import_end__
|
||||
|
||||
# __model_start__
|
||||
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
||||
|
||||
|
||||
@serve.deployment(num_replicas=2, ray_actor_options={"num_cpus": 0.2, "num_gpus": 0})
|
||||
class Translator:
|
||||
def __init__(self):
|
||||
# Load model
|
||||
self.tokenizer = AutoTokenizer.from_pretrained("t5-small")
|
||||
self.model = AutoModelForSeq2SeqLM.from_pretrained("t5-small")
|
||||
|
||||
def translate(self, text: str) -> str:
|
||||
# Run inference
|
||||
input_ids = self.tokenizer(
|
||||
f"translate English to French: {text}", return_tensors="pt"
|
||||
).input_ids
|
||||
output_ids = self.model.generate(
|
||||
input_ids, num_beams=4, early_stopping=True, max_length=300
|
||||
)
|
||||
|
||||
# Post-process output to return only the translation text
|
||||
translation = self.tokenizer.decode(
|
||||
output_ids[0], skip_special_tokens=True, clean_up_tokenization_spaces=False
|
||||
)
|
||||
|
||||
return translation
|
||||
|
||||
async def __call__(self, http_request: Request) -> str:
|
||||
english_text: str = await http_request.json()
|
||||
return self.translate(english_text)
|
||||
|
||||
|
||||
# __model_end__
|
||||
|
||||
# __model_deploy_start__
|
||||
translator_app = Translator.bind()
|
||||
# __model_deploy_end__
|
||||
|
||||
translator_app = Translator.options(ray_actor_options={}).bind()
|
||||
serve.run(translator_app)
|
||||
|
||||
# __client_function_start__
|
||||
# File name: model_client.py
|
||||
import requests
|
||||
|
||||
english_text = "Hello world!"
|
||||
|
||||
response = requests.post("http://127.0.0.1:8000/", json=english_text)
|
||||
french_text = response.text
|
||||
|
||||
print(french_text)
|
||||
# __client_function_end__
|
||||
|
||||
assert french_text == "Bonjour monde!"
|
||||
|
||||
serve.shutdown()
|
||||
ray.shutdown()
|
||||
@@ -0,0 +1,53 @@
|
||||
# flake8: noqa
|
||||
|
||||
# __deployment_full_start__
|
||||
# File name: serve_quickstart.py
|
||||
from starlette.requests import Request
|
||||
|
||||
import ray
|
||||
from ray import serve
|
||||
|
||||
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
||||
|
||||
|
||||
@serve.deployment(num_replicas=2, ray_actor_options={"num_cpus": 0.2, "num_gpus": 0})
|
||||
class Translator:
|
||||
def __init__(self):
|
||||
# Load model
|
||||
self.tokenizer = AutoTokenizer.from_pretrained("t5-small")
|
||||
self.model = AutoModelForSeq2SeqLM.from_pretrained("t5-small")
|
||||
|
||||
def translate(self, text: str) -> str:
|
||||
# Run inference
|
||||
input_ids = self.tokenizer(
|
||||
f"translate English to French: {text}", return_tensors="pt"
|
||||
).input_ids
|
||||
output_ids = self.model.generate(
|
||||
input_ids, num_beams=4, early_stopping=True, max_length=300
|
||||
)
|
||||
|
||||
# Post-process output to return only the translation text
|
||||
translation = self.tokenizer.decode(
|
||||
output_ids[0], skip_special_tokens=True, clean_up_tokenization_spaces=False
|
||||
)
|
||||
|
||||
return translation
|
||||
|
||||
async def __call__(self, http_request: Request) -> str:
|
||||
english_text: str = await http_request.json()
|
||||
return self.translate(english_text)
|
||||
|
||||
|
||||
translator_app = Translator.bind()
|
||||
# __deployment_full_end__
|
||||
|
||||
translator_app = Translator.options(ray_actor_options={}).bind()
|
||||
serve.run(translator_app)
|
||||
|
||||
import requests
|
||||
|
||||
response = requests.post("http://127.0.0.1:8000/", json="Hello world!").text
|
||||
assert response == "Bonjour monde!"
|
||||
|
||||
serve.shutdown()
|
||||
ray.shutdown()
|
||||
@@ -0,0 +1,83 @@
|
||||
# flake8: noqa
|
||||
|
||||
# __start_translation_model__
|
||||
# File name: model.py
|
||||
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
||||
|
||||
|
||||
class Translator:
|
||||
def __init__(self):
|
||||
# Load model
|
||||
self.tokenizer = AutoTokenizer.from_pretrained("t5-small")
|
||||
self.model = AutoModelForSeq2SeqLM.from_pretrained("t5-small")
|
||||
|
||||
def translate(self, text: str) -> str:
|
||||
# Run inference
|
||||
input_ids = self.tokenizer(
|
||||
f"translate English to French: {text}", return_tensors="pt"
|
||||
).input_ids
|
||||
output_ids = self.model.generate(
|
||||
input_ids, num_beams=4, early_stopping=True, max_length=300
|
||||
)
|
||||
|
||||
# Post-process output to return only the translation text
|
||||
translation = self.tokenizer.decode(
|
||||
output_ids[0], skip_special_tokens=True, clean_up_tokenization_spaces=False
|
||||
)
|
||||
|
||||
return translation
|
||||
|
||||
|
||||
translator = Translator()
|
||||
|
||||
translation = translator.translate("Hello world!")
|
||||
print(translation)
|
||||
# __end_translation_model__
|
||||
|
||||
# Test model behavior
|
||||
assert translation == "Bonjour monde!"
|
||||
|
||||
|
||||
# __start_summarization_model__
|
||||
# File name: summary_model.py
|
||||
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
||||
|
||||
|
||||
class Summarizer:
|
||||
def __init__(self):
|
||||
# Load model
|
||||
self.tokenizer = AutoTokenizer.from_pretrained("t5-small")
|
||||
self.model = AutoModelForSeq2SeqLM.from_pretrained("t5-small")
|
||||
|
||||
def summarize(self, text: str) -> str:
|
||||
# Run inference
|
||||
input_ids = self.tokenizer(f"summarize: {text}", return_tensors="pt").input_ids
|
||||
output_ids = self.model.generate(
|
||||
input_ids,
|
||||
num_beams=4,
|
||||
early_stopping=True,
|
||||
length_penalty=2.0,
|
||||
no_repeat_ngram_size=3,
|
||||
min_length=5,
|
||||
max_length=15,
|
||||
)
|
||||
|
||||
# Post-process output to return only the summary text
|
||||
summary = self.tokenizer.decode(
|
||||
output_ids[0], skip_special_tokens=True, clean_up_tokenization_spaces=False
|
||||
)
|
||||
|
||||
return summary
|
||||
|
||||
|
||||
summarizer = Summarizer()
|
||||
|
||||
summary = summarizer.summarize(
|
||||
"It was the best of times, it was the worst of times, it was the age "
|
||||
"of wisdom, it was the age of foolishness, it was the epoch of belief"
|
||||
)
|
||||
print(summary)
|
||||
# __end_summarization_model__
|
||||
|
||||
# Test model behavior
|
||||
assert summary == "it was the best of times, it was worst of times ."
|
||||
@@ -0,0 +1,91 @@
|
||||
# flake8: noqa
|
||||
|
||||
# __start_graph__
|
||||
# File name: serve_quickstart_composed.py
|
||||
from starlette.requests import Request
|
||||
|
||||
import ray
|
||||
from ray import serve
|
||||
from ray.serve.handle import DeploymentHandle
|
||||
|
||||
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
||||
|
||||
|
||||
@serve.deployment
|
||||
class Translator:
|
||||
def __init__(self):
|
||||
# Load model
|
||||
self.tokenizer = AutoTokenizer.from_pretrained("t5-small")
|
||||
self.model = AutoModelForSeq2SeqLM.from_pretrained("t5-small")
|
||||
|
||||
def translate(self, text: str) -> str:
|
||||
# Run inference
|
||||
input_ids = self.tokenizer(
|
||||
f"translate English to French: {text}", return_tensors="pt"
|
||||
).input_ids
|
||||
output_ids = self.model.generate(
|
||||
input_ids, num_beams=4, early_stopping=True, max_length=300
|
||||
)
|
||||
|
||||
# Post-process output to return only the translation text
|
||||
translation = self.tokenizer.decode(
|
||||
output_ids[0], skip_special_tokens=True, clean_up_tokenization_spaces=False
|
||||
)
|
||||
|
||||
return translation
|
||||
|
||||
|
||||
@serve.deployment
|
||||
class Summarizer:
|
||||
def __init__(self, translator: DeploymentHandle):
|
||||
self.translator = translator
|
||||
|
||||
# Load model.
|
||||
self.tokenizer = AutoTokenizer.from_pretrained("t5-small")
|
||||
self.model = AutoModelForSeq2SeqLM.from_pretrained("t5-small")
|
||||
|
||||
def summarize(self, text: str) -> str:
|
||||
# Run inference
|
||||
input_ids = self.tokenizer(f"summarize: {text}", return_tensors="pt").input_ids
|
||||
output_ids = self.model.generate(
|
||||
input_ids, num_beams=4, early_stopping=True, max_length=15
|
||||
)
|
||||
|
||||
# Post-process output to return only the summary text
|
||||
summary = self.tokenizer.decode(
|
||||
output_ids[0], skip_special_tokens=True, clean_up_tokenization_spaces=False
|
||||
)
|
||||
|
||||
return summary
|
||||
|
||||
async def __call__(self, http_request: Request) -> str:
|
||||
english_text: str = await http_request.json()
|
||||
summary = self.summarize(english_text)
|
||||
|
||||
translation = await self.translator.translate.remote(summary)
|
||||
return translation
|
||||
|
||||
|
||||
app = Summarizer.bind(Translator.bind())
|
||||
# __end_graph__
|
||||
|
||||
serve.run(app)
|
||||
|
||||
# __start_client__
|
||||
# File name: composed_client.py
|
||||
import requests
|
||||
|
||||
english_text = (
|
||||
"It was the best of times, it was the worst of times, it was the age "
|
||||
"of wisdom, it was the age of foolishness, it was the epoch of belief"
|
||||
)
|
||||
response = requests.post("http://127.0.0.1:8000/", json=english_text)
|
||||
french_text = response.text
|
||||
|
||||
print(french_text)
|
||||
# __end_client__
|
||||
|
||||
assert french_text == "C'était le meilleur des temps, c'était le pire des temps,"
|
||||
|
||||
serve.shutdown()
|
||||
ray.shutdown()
|
||||
Reference in New Issue
Block a user