Upload handler.py with huggingface_hub
Browse files- handler.py +12 -10
handler.py
CHANGED
|
@@ -1,22 +1,19 @@
|
|
| 1 |
|
| 2 |
-
from typing import Dict, List, Any
|
| 3 |
import base64
|
| 4 |
import io
|
| 5 |
from PIL import Image, ImageDraw
|
| 6 |
-
import numpy as np
|
| 7 |
-
import torch
|
| 8 |
import json
|
| 9 |
|
| 10 |
class EndpointHandler:
|
| 11 |
def __init__(self, path=""):
|
| 12 |
-
self.initialized = False
|
| 13 |
self.path = path
|
|
|
|
| 14 |
|
| 15 |
def __call__(self, data):
|
| 16 |
"""Handle a request to the model."""
|
| 17 |
if not self.initialized:
|
| 18 |
self.initialize()
|
| 19 |
-
|
| 20 |
if data is None:
|
| 21 |
return None
|
| 22 |
|
|
@@ -30,14 +27,19 @@ class EndpointHandler:
|
|
| 30 |
|
| 31 |
def preprocess(self, request):
|
| 32 |
"""Process the input request."""
|
| 33 |
-
|
| 34 |
-
if isinstance(inputs, str):
|
| 35 |
# Single prompt
|
| 36 |
-
prompt =
|
| 37 |
payload = {"prompt": prompt}
|
| 38 |
-
|
| 39 |
# Full payload
|
| 40 |
-
payload =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
return payload
|
| 43 |
|
|
|
|
| 1 |
|
|
|
|
| 2 |
import base64
|
| 3 |
import io
|
| 4 |
from PIL import Image, ImageDraw
|
|
|
|
|
|
|
| 5 |
import json
|
| 6 |
|
| 7 |
class EndpointHandler:
|
| 8 |
def __init__(self, path=""):
|
|
|
|
| 9 |
self.path = path
|
| 10 |
+
self.initialized = False
|
| 11 |
|
| 12 |
def __call__(self, data):
|
| 13 |
"""Handle a request to the model."""
|
| 14 |
if not self.initialized:
|
| 15 |
self.initialize()
|
| 16 |
+
|
| 17 |
if data is None:
|
| 18 |
return None
|
| 19 |
|
|
|
|
| 27 |
|
| 28 |
def preprocess(self, request):
|
| 29 |
"""Process the input request."""
|
| 30 |
+
if isinstance(request, str):
|
|
|
|
| 31 |
# Single prompt
|
| 32 |
+
prompt = request
|
| 33 |
payload = {"prompt": prompt}
|
| 34 |
+
elif isinstance(request, dict):
|
| 35 |
# Full payload
|
| 36 |
+
payload = request
|
| 37 |
+
else:
|
| 38 |
+
# Try to parse as JSON
|
| 39 |
+
try:
|
| 40 |
+
payload = json.loads(request)
|
| 41 |
+
except:
|
| 42 |
+
payload = {"prompt": str(request)}
|
| 43 |
|
| 44 |
return payload
|
| 45 |
|