Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sagemaker
|
2 |
+
import boto3
|
3 |
+
from sagemaker.huggingface import HuggingFaceModel
|
4 |
+
|
5 |
+
try:
|
6 |
+
role = sagemaker.get_execution_role()
|
7 |
+
except ValueError:
|
8 |
+
iam = boto3.client('iam')
|
9 |
+
role = iam.get_role(RoleName='sagemaker_execution_role')['Role']['Arn']
|
10 |
+
|
11 |
+
# Hub Model configuration. https://huggingface.co/models
|
12 |
+
hub = {
|
13 |
+
'HF_MODEL_ID':'cloudqi/cqi_text_to_image_pt_v0',
|
14 |
+
'HF_TASK':'text-to-image'
|
15 |
+
}
|
16 |
+
|
17 |
+
# create Hugging Face Model Class
|
18 |
+
huggingface_model = HuggingFaceModel(
|
19 |
+
transformers_version='4.37.0',
|
20 |
+
pytorch_version='2.1.0',
|
21 |
+
py_version='py310',
|
22 |
+
env=hub,
|
23 |
+
role=role,
|
24 |
+
)
|
25 |
+
|
26 |
+
# deploy model to SageMaker Inference
|
27 |
+
predictor = huggingface_model.deploy(
|
28 |
+
initial_instance_count=1, # number of instances
|
29 |
+
instance_type='ml.m5.xlarge' # ec2 instance type
|
30 |
+
)
|
31 |
+
|
32 |
+
image_bytes = predictor.predict({
|
33 |
+
"inputs": "Astronaut riding a horse",
|
34 |
+
})
|
35 |
+
# You can access the image with PIL.Image for example
|
36 |
+
import io
|
37 |
+
from PIL import Image
|
38 |
+
image = Image.open(io.BytesIO(image_bytes))
|