jree423 commited on
Commit
282ccf0
·
verified ·
1 Parent(s): b907da9

Delete handler_minimal.py with huggingface_hub

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