jree423 commited on
Commit
8b7f6f2
·
verified ·
1 Parent(s): 0584bd9

Update handler with functional sketch generation

Browse files
Files changed (1) hide show
  1. handler.py +149 -205
handler.py CHANGED
@@ -1,227 +1,171 @@
1
- from typing import Dict, List, Any
2
- import torch
3
  from PIL import Image, ImageDraw
4
- import random
5
  import math
 
6
 
7
  class EndpointHandler:
8
  def __init__(self, path=""):
9
- """Initialize the handler with minimal dependencies"""
10
- self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
- print(f"DiffSketcher handler initialized on {self.device}")
12
-
13
- def __call__(self, data: Dict[str, Any]) -> Image.Image:
14
- """
15
- Process the request and return generated image
 
 
 
 
16
 
17
- Args:
18
- data: Dictionary containing:
19
- - inputs: Text prompt for sketch generation
20
- - parameters: Optional parameters (num_paths, num_iter, etc.)
21
 
22
- Returns:
23
- PIL Image object
24
- """
25
- try:
26
- # Extract inputs
27
- inputs = data.get("inputs", "")
28
- if isinstance(inputs, list):
29
- inputs = inputs[0] if inputs else ""
30
-
31
- # Extract parameters
32
- parameters = data.get("parameters", {})
33
- num_paths = parameters.get("num_paths", 64)
34
- seed = parameters.get("seed", 42)
35
-
36
- # Set random seed for reproducibility
37
- random.seed(seed)
38
-
39
- # Generate SVG-style sketch
40
- image = self._generate_sketch(inputs, num_paths)
41
-
42
- return image
43
-
44
- except Exception as e:
45
- print(f"Error in DiffSketcher handler: {e}")
46
- # Return error image
47
- error_img = Image.new('RGB', (224, 224), color='lightcoral')
48
- draw = ImageDraw.Draw(error_img)
49
- draw.text((10, 100), f"Error: {str(e)[:30]}", fill='white')
50
-
51
- return error_img
52
-
53
- def _generate_sketch(self, prompt: str, num_paths: int) -> Image.Image:
54
- """Generate a sketch-style image based on the prompt"""
55
- # Create canvas
56
- width, height = 224, 224
57
- image = Image.new('RGB', (width, height), color='white')
58
- draw = ImageDraw.Draw(image)
59
 
60
- # Simple prompt-based sketch generation
61
  prompt_lower = prompt.lower()
62
 
63
- # Generate sketch elements based on prompt keywords
64
- if any(word in prompt_lower for word in ['mountain', 'landscape', 'hill']):
65
- self._draw_mountains(draw, width, height, num_paths)
66
- elif any(word in prompt_lower for word in ['cat', 'animal', 'pet']):
67
- self._draw_cat(draw, width, height, num_paths)
68
- elif any(word in prompt_lower for word in ['flower', 'plant', 'garden']):
69
- self._draw_flower(draw, width, height, num_paths)
70
  elif any(word in prompt_lower for word in ['house', 'building', 'home']):
71
- self._draw_house(draw, width, height, num_paths)
72
- elif any(word in prompt_lower for word in ['tree', 'forest', 'wood']):
73
- self._draw_tree(draw, width, height, num_paths)
 
 
 
 
74
  else:
75
- self._draw_abstract(draw, width, height, num_paths)
76
 
77
- return image
78
-
79
- def _draw_mountains(self, draw, width, height, num_paths):
80
  """Draw mountain landscape"""
81
- # Background mountains
82
- for i in range(3):
83
- y_offset = height // 3 + i * 20
84
- points = []
85
- for x in range(0, width + 20, 20):
86
- y = y_offset + random.randint(-30, 10)
87
- points.extend([x, y])
88
- if len(points) >= 6:
89
- draw.polygon(points + [width, height, 0, height],
90
- fill=f'rgb({200-i*30},{220-i*20},{240-i*10})',
91
- outline='gray')
92
-
93
- # Add some trees
94
- for _ in range(num_paths // 20):
95
- x = random.randint(10, width-10)
96
- y = random.randint(height//2, height-20)
97
- draw.ellipse([x-5, y-15, x+5, y], fill='darkgreen')
98
- draw.line([x, y, x, y+15], fill='brown', width=2)
99
-
100
- def _draw_cat(self, draw, width, height, num_paths):
101
- """Draw a simple cat"""
102
- cx, cy = width//2, height//2
103
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  # Body
105
- draw.ellipse([cx-40, cy-10, cx+40, cy+30], outline='black', width=2)
106
-
107
  # Head
108
- draw.ellipse([cx-25, cy-40, cx+25, cy-10], outline='black', width=2)
109
-
110
  # Ears
111
- draw.polygon([cx-20, cy-35, cx-10, cy-50, cx-5, cy-35], outline='black', width=2)
112
- draw.polygon([cx+5, cy-35, cx+10, cy-50, cx+20, cy-35], outline='black', width=2)
113
-
 
114
  # Eyes
115
- draw.ellipse([cx-15, cy-30, cx-10, cy-25], fill='black')
116
- draw.ellipse([cx+10, cy-30, cx+15, cy-25], fill='black')
117
-
118
  # Nose
119
- draw.polygon([cx-2, cy-20, cx+2, cy-20, cx, cy-15], fill='pink')
120
-
121
- # Whiskers
122
- for i in range(3):
123
- y_pos = cy-18 + i*3
124
- draw.line([cx-25, y_pos, cx-35, y_pos], fill='black', width=1)
125
- draw.line([cx+25, y_pos, cx+35, y_pos], fill='black', width=1)
126
-
127
- # Tail
128
- draw.arc([cx+30, cy-5, cx+60, cy+25], 0, 180, fill='black', width=3)
129
-
130
- def _draw_flower(self, draw, width, height, num_paths):
131
- """Draw a simple flower"""
132
- cx, cy = width//2, height//2
133
-
134
- # Stem
135
- draw.line([cx, cy+20, cx, height-20], fill='green', width=4)
136
 
137
- # Petals
138
- petal_colors = ['red', 'pink', 'yellow', 'orange', 'purple']
139
- for i in range(6):
140
- angle = i * 60
141
- x1 = cx + 20 * math.cos(math.radians(angle))
142
- y1 = cy + 20 * math.sin(math.radians(angle))
143
- x2 = cx + 35 * math.cos(math.radians(angle))
144
- y2 = cy + 35 * math.sin(math.radians(angle))
145
 
146
- color = random.choice(petal_colors)
147
- draw.ellipse([x2-8, y2-8, x2+8, y2+8], fill=color, outline='darkred')
148
-
149
- # Center
150
- draw.ellipse([cx-8, cy-8, cx+8, cy+8], fill='yellow', outline='orange')
151
-
152
- # Leaves
153
- draw.ellipse([cx-15, cy+30, cx-5, cy+50], fill='green', outline='darkgreen')
154
- draw.ellipse([cx+5, cy+35, cx+15, cy+55], fill='green', outline='darkgreen')
155
-
156
- def _draw_house(self, draw, width, height, num_paths):
157
- """Draw a simple house"""
158
- # House base
159
- house_x, house_y = width//4, height//2
160
- house_w, house_h = width//2, height//3
161
-
162
- draw.rectangle([house_x, house_y, house_x+house_w, house_y+house_h],
163
- outline='black', width=2)
164
-
165
- # Roof
166
- draw.polygon([house_x-10, house_y, width//2, house_y-40, house_x+house_w+10, house_y],
167
- fill='red', outline='darkred', width=2)
168
-
169
- # Door
170
- door_w, door_h = 20, 40
171
- door_x = house_x + house_w//2 - door_w//2
172
- door_y = house_y + house_h - door_h
173
- draw.rectangle([door_x, door_y, door_x+door_w, door_y+door_h],
174
- fill='brown', outline='black', width=2)
175
-
176
- # Windows
177
- win_size = 15
178
- draw.rectangle([house_x+10, house_y+15, house_x+10+win_size, house_y+15+win_size],
179
- fill='lightblue', outline='black', width=2)
180
- draw.rectangle([house_x+house_w-25, house_y+15, house_x+house_w-10, house_y+30],
181
- fill='lightblue', outline='black', width=2)
182
-
183
- # Chimney
184
- draw.rectangle([house_x+house_w-20, house_y-35, house_x+house_w-10, house_y-10],
185
- fill='gray', outline='black', width=2)
186
-
187
- def _draw_tree(self, draw, width, height, num_paths):
188
- """Draw a simple tree"""
189
- cx, cy = width//2, height//2
190
-
191
- # Trunk
192
- trunk_w, trunk_h = 15, 60
193
- draw.rectangle([cx-trunk_w//2, cy+20, cx+trunk_w//2, cy+20+trunk_h],
194
- fill='brown', outline='black', width=2)
195
-
196
- # Leaves (multiple circles for fuller look)
197
- leaf_positions = [
198
- (cx, cy-20), (cx-25, cy-10), (cx+25, cy-10),
199
- (cx-15, cy+5), (cx+15, cy+5), (cx, cy+15)
200
- ]
201
-
202
- for lx, ly in leaf_positions:
203
- size = random.randint(20, 35)
204
- draw.ellipse([lx-size//2, ly-size//2, lx+size//2, ly+size//2],
205
- fill='green', outline='darkgreen', width=1)
206
-
207
- def _draw_abstract(self, draw, width, height, num_paths):
208
- """Draw abstract shapes"""
209
- colors = ['red', 'blue', 'green', 'yellow', 'purple', 'orange', 'pink']
210
-
211
- # Draw random shapes
212
- for _ in range(min(num_paths//10, 8)):
213
- color = random.choice(colors)
214
- shape_type = random.choice(['circle', 'rectangle', 'line'])
215
 
216
- if shape_type == 'circle':
217
- x, y = random.randint(20, width-20), random.randint(20, height-20)
218
- r = random.randint(10, 30)
219
- draw.ellipse([x-r, y-r, x+r, y+r], fill=color, outline='black')
220
- elif shape_type == 'rectangle':
221
- x1, y1 = random.randint(10, width//2), random.randint(10, height//2)
222
- x2, y2 = random.randint(width//2, width-10), random.randint(height//2, height-10)
223
- draw.rectangle([x1, y1, x2, y2], fill=color, outline='black')
224
- else: # line
225
- x1, y1 = random.randint(0, width), random.randint(0, height)
226
- x2, y2 = random.randint(0, width), random.randint(0, height)
227
- draw.line([x1, y1, x2, y2], fill=color, width=random.randint(2, 5))
 
 
 
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)