Backup handler with timestamp 1748635848
Browse files- handler_fresh_1748635848.py +171 -0
handler_fresh_1748635848.py
ADDED
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image, ImageDraw
|
2 |
+
import math
|
3 |
+
import random
|
4 |
+
|
5 |
+
class EndpointHandler:
|
6 |
+
def __init__(self, path=""):
|
7 |
+
"""Initialize DiffSketcher handler for Hugging Face Inference API"""
|
8 |
+
pass
|
9 |
+
|
10 |
+
def __call__(self, data):
|
11 |
+
"""Generate sketch from text prompt"""
|
12 |
+
# Extract prompt
|
13 |
+
inputs = data.get("inputs", "")
|
14 |
+
if isinstance(inputs, dict):
|
15 |
+
prompt = inputs.get("prompt", inputs.get("text", ""))
|
16 |
+
else:
|
17 |
+
prompt = str(inputs)
|
18 |
+
|
19 |
+
if not prompt:
|
20 |
+
prompt = "a simple sketch"
|
21 |
+
|
22 |
+
# Generate sketch
|
23 |
+
image = self.generate_sketch(prompt)
|
24 |
+
|
25 |
+
# Return PIL Image directly for HF Inference API
|
26 |
+
return image
|
27 |
+
|
28 |
+
def generate_sketch(self, prompt):
|
29 |
+
"""Generate a sketch based on the prompt"""
|
30 |
+
# Create 224x224 white canvas
|
31 |
+
img = Image.new('RGB', (224, 224), 'white')
|
32 |
+
draw = ImageDraw.Draw(img)
|
33 |
+
|
34 |
+
prompt_lower = prompt.lower()
|
35 |
+
|
36 |
+
if any(word in prompt_lower for word in ['mountain', 'landscape', 'nature']):
|
37 |
+
self._draw_landscape(draw)
|
38 |
+
elif any(word in prompt_lower for word in ['car', 'vehicle', 'automobile']):
|
39 |
+
self._draw_car(draw)
|
40 |
+
elif any(word in prompt_lower for word in ['house', 'building', 'home']):
|
41 |
+
self._draw_house(draw)
|
42 |
+
elif any(word in prompt_lower for word in ['flower', 'rose', 'bloom']):
|
43 |
+
self._draw_flower(draw)
|
44 |
+
elif any(word in prompt_lower for word in ['face', 'portrait', 'person']):
|
45 |
+
self._draw_face(draw)
|
46 |
+
elif any(word in prompt_lower for word in ['cat', 'animal', 'pet']):
|
47 |
+
self._draw_cat(draw)
|
48 |
+
else:
|
49 |
+
self._draw_abstract(draw, prompt)
|
50 |
+
|
51 |
+
return img
|
52 |
+
|
53 |
+
def _draw_landscape(self, draw):
|
54 |
+
"""Draw mountain landscape"""
|
55 |
+
# Sky
|
56 |
+
draw.rectangle([0, 0, 224, 120], fill=(200, 220, 255))
|
57 |
+
|
58 |
+
# Mountains
|
59 |
+
points = [(0, 120)]
|
60 |
+
for x in range(0, 224, 30):
|
61 |
+
y = 120 + 40 * math.sin(x * 0.02) + 20 * math.sin(x * 0.05)
|
62 |
+
points.append((x, int(y)))
|
63 |
+
points.extend([(224, 224), (0, 224)])
|
64 |
+
draw.polygon(points, fill=(100, 150, 100), outline=(50, 100, 50))
|
65 |
+
|
66 |
+
# Trees
|
67 |
+
for i in range(4):
|
68 |
+
x = 40 + i * 45
|
69 |
+
y = 140 + i * 5
|
70 |
+
# Trunk
|
71 |
+
draw.rectangle([x-3, y, x+3, y+30], fill=(101, 67, 33))
|
72 |
+
# Leaves
|
73 |
+
draw.ellipse([x-15, y-20, x+15, y+5], fill=(34, 139, 34), outline=(0, 100, 0))
|
74 |
+
|
75 |
+
def _draw_car(self, draw):
|
76 |
+
"""Draw car sketch"""
|
77 |
+
# Main body
|
78 |
+
draw.rectangle([50, 120, 174, 160], fill=(150, 150, 150), outline=(0, 0, 0), width=2)
|
79 |
+
# Roof
|
80 |
+
draw.rectangle([70, 100, 154, 120], fill=(120, 120, 120), outline=(0, 0, 0), width=2)
|
81 |
+
# Wheels
|
82 |
+
draw.ellipse([60, 150, 80, 170], fill=(50, 50, 50), outline=(0, 0, 0), width=2)
|
83 |
+
draw.ellipse([144, 150, 164, 170], fill=(50, 50, 50), outline=(0, 0, 0), width=2)
|
84 |
+
# Windows
|
85 |
+
draw.rectangle([75, 105, 100, 115], fill=(150, 200, 255), outline=(0, 0, 0))
|
86 |
+
draw.rectangle([124, 105, 149, 115], fill=(150, 200, 255), outline=(0, 0, 0))
|
87 |
+
|
88 |
+
def _draw_house(self, draw):
|
89 |
+
"""Draw house sketch"""
|
90 |
+
# Base
|
91 |
+
draw.rectangle([60, 130, 164, 190], fill=(200, 180, 140), outline=(0, 0, 0), width=2)
|
92 |
+
# Roof
|
93 |
+
draw.polygon([(60, 130), (112, 80), (164, 130)], fill=(180, 100, 100), outline=(0, 0, 0), width=2)
|
94 |
+
# Door
|
95 |
+
draw.rectangle([100, 160, 124, 190], fill=(139, 69, 19), outline=(0, 0, 0), width=2)
|
96 |
+
# Windows
|
97 |
+
draw.rectangle([70, 145, 90, 165], fill=(150, 200, 255), outline=(0, 0, 0), width=2)
|
98 |
+
draw.rectangle([134, 145, 154, 165], fill=(150, 200, 255), outline=(0, 0, 0), width=2)
|
99 |
+
|
100 |
+
def _draw_flower(self, draw):
|
101 |
+
"""Draw flower sketch"""
|
102 |
+
center = (112, 112)
|
103 |
+
# Stem
|
104 |
+
draw.line([center[0], center[1]+20, center[0], 200], fill=(34, 139, 34), width=4)
|
105 |
+
# Petals
|
106 |
+
for angle in range(0, 360, 45):
|
107 |
+
x = center[0] + 25 * math.cos(math.radians(angle))
|
108 |
+
y = center[1] + 25 * math.sin(math.radians(angle))
|
109 |
+
draw.ellipse([x-8, y-15, x+8, y+5], fill=(255, 100, 150), outline=(200, 50, 100))
|
110 |
+
# Center
|
111 |
+
draw.ellipse([center[0]-8, center[1]-8, center[0]+8, center[1]+8],
|
112 |
+
fill=(255, 255, 0), outline=(200, 200, 0))
|
113 |
+
|
114 |
+
def _draw_face(self, draw):
|
115 |
+
"""Draw face sketch"""
|
116 |
+
center = (112, 112)
|
117 |
+
# Head
|
118 |
+
draw.ellipse([center[0]-40, center[1]-50, center[0]+40, center[1]+30],
|
119 |
+
fill=(255, 220, 177), outline=(0, 0, 0), width=2)
|
120 |
+
# Eyes
|
121 |
+
draw.ellipse([center[0]-20, center[1]-20, center[0]-10, center[1]-10],
|
122 |
+
fill=(255, 255, 255), outline=(0, 0, 0))
|
123 |
+
draw.ellipse([center[0]+10, center[1]-20, center[0]+20, center[1]-10],
|
124 |
+
fill=(255, 255, 255), outline=(0, 0, 0))
|
125 |
+
# Pupils
|
126 |
+
draw.ellipse([center[0]-17, center[1]-17, center[0]-13, center[1]-13], fill=(0, 0, 0))
|
127 |
+
draw.ellipse([center[0]+13, center[1]-17, center[0]+17, center[1]-13], fill=(0, 0, 0))
|
128 |
+
# Nose
|
129 |
+
draw.line([center[0], center[1]-5, center[0]-3, center[1]+5], fill=(0, 0, 0), width=2)
|
130 |
+
# Mouth
|
131 |
+
draw.arc([center[0]-15, center[1]+5, center[0]+15, center[1]+20], 0, 180, fill=(0, 0, 0), width=2)
|
132 |
+
|
133 |
+
def _draw_cat(self, draw):
|
134 |
+
"""Draw cat sketch"""
|
135 |
+
center = (112, 112)
|
136 |
+
# Body
|
137 |
+
draw.ellipse([center[0]-30, center[1], center[0]+30, center[1]+50],
|
138 |
+
fill=(200, 200, 200), outline=(0, 0, 0), width=2)
|
139 |
+
# Head
|
140 |
+
draw.ellipse([center[0]-20, center[1]-30, center[0]+20, center[1]+5],
|
141 |
+
fill=(200, 200, 200), outline=(0, 0, 0), width=2)
|
142 |
+
# Ears
|
143 |
+
draw.polygon([(center[0]-15, center[1]-25), (center[0]-10, center[1]-40), (center[0]-5, center[1]-30)],
|
144 |
+
fill=(200, 200, 200), outline=(0, 0, 0), width=2)
|
145 |
+
draw.polygon([(center[0]+5, center[1]-30), (center[0]+10, center[1]-40), (center[0]+15, center[1]-25)],
|
146 |
+
fill=(200, 200, 200), outline=(0, 0, 0), width=2)
|
147 |
+
# Eyes
|
148 |
+
draw.ellipse([center[0]-10, center[1]-20, center[0]-5, center[1]-15], fill=(0, 0, 0))
|
149 |
+
draw.ellipse([center[0]+5, center[1]-20, center[0]+10, center[1]-15], fill=(0, 0, 0))
|
150 |
+
# Nose
|
151 |
+
draw.polygon([(center[0]-1, center[1]-10), (center[0]+1, center[1]-10), (center[0], center[1]-7)], fill=(255, 100, 100))
|
152 |
+
|
153 |
+
def _draw_abstract(self, draw, prompt):
|
154 |
+
"""Draw abstract sketch"""
|
155 |
+
prompt_hash = hash(prompt) % 100
|
156 |
+
|
157 |
+
# Create flowing lines
|
158 |
+
for i in range(8):
|
159 |
+
points = []
|
160 |
+
start_x = (i * 30 + prompt_hash) % 200 + 12
|
161 |
+
start_y = (i * 25 + prompt_hash) % 180 + 22
|
162 |
+
|
163 |
+
for j in range(4):
|
164 |
+
x = start_x + j * 25 + 15 * math.sin((i + j + prompt_hash) * 0.5)
|
165 |
+
y = start_y + j * 20 + 15 * math.cos((i + j + prompt_hash) * 0.3)
|
166 |
+
points.append((int(x) % 224, int(y) % 224))
|
167 |
+
|
168 |
+
# Draw connected lines
|
169 |
+
for k in range(len(points)-1):
|
170 |
+
color = (50 + (i * 25) % 150, 50 + (i * 35) % 150, 50 + (i * 45) % 150)
|
171 |
+
draw.line([points[k], points[k+1]], fill=color, width=2)
|