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

Delete handler_fallback.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. handler_fallback.py +0 -178
handler_fallback.py DELETED
@@ -1,178 +0,0 @@
1
- from PIL import Image, ImageDraw
2
- import io
3
- import random
4
- import math
5
-
6
- class EndpointHandler:
7
- def __init__(self):
8
- """Initialize the DiffSketcher handler with fallback PIL drawing"""
9
- pass
10
-
11
- def __call__(self, data):
12
- """
13
- Generate a sketch-style image using PIL drawing (fallback method)
14
-
15
- Args:
16
- data (dict): Input data containing:
17
- - inputs (str): Text prompt
18
- - parameters (dict): Generation parameters
19
-
20
- Returns:
21
- PIL.Image.Image: Generated sketch image
22
- """
23
- try:
24
- # Extract inputs
25
- prompt = data.get("inputs", "")
26
- parameters = data.get("parameters", {})
27
-
28
- # Extract parameters
29
- width = parameters.get("width", 224)
30
- height = parameters.get("height", 224)
31
- guidance_scale = parameters.get("guidance_scale", 7.5)
32
- seed = parameters.get("seed", 42)
33
-
34
- # Set random seed for reproducibility
35
- random.seed(seed)
36
-
37
- # Create white background
38
- image = Image.new('RGB', (width, height), 'white')
39
- draw = ImageDraw.Draw(image)
40
-
41
- # Generate sketch based on prompt keywords
42
- self._draw_sketch_from_prompt(draw, prompt, width, height)
43
-
44
- return image
45
-
46
- except Exception as e:
47
- # Return error image
48
- error_image = Image.new('RGB', (224, 224), 'white')
49
- error_draw = ImageDraw.Draw(error_image)
50
- error_draw.text((10, 100), f"Error: {str(e)[:30]}", fill='red')
51
- return error_image
52
-
53
- def _draw_sketch_from_prompt(self, draw, prompt, width, height):
54
- """Draw a simple sketch based on prompt keywords"""
55
- prompt_lower = prompt.lower()
56
-
57
- # Define colors for sketching
58
- colors = ['black', 'gray', 'darkgray']
59
-
60
- if any(word in prompt_lower for word in ['car', 'vehicle', 'automobile']):
61
- self._draw_car(draw, width, height, colors)
62
- elif any(word in prompt_lower for word in ['house', 'building', 'home']):
63
- self._draw_house(draw, width, height, colors)
64
- elif any(word in prompt_lower for word in ['flower', 'plant', 'bloom']):
65
- self._draw_flower(draw, width, height, colors)
66
- elif any(word in prompt_lower for word in ['tree', 'forest']):
67
- self._draw_tree(draw, width, height, colors)
68
- elif any(word in prompt_lower for word in ['mountain', 'landscape']):
69
- self._draw_mountain(draw, width, height, colors)
70
- else:
71
- self._draw_abstract(draw, width, height, colors)
72
-
73
- def _draw_car(self, draw, width, height, colors):
74
- """Draw a simple car sketch"""
75
- cx, cy = width // 2, height // 2
76
-
77
- # Car body
78
- draw.rectangle([cx-60, cy-20, cx+60, cy+20], outline=colors[0], width=2)
79
-
80
- # Car roof
81
- draw.rectangle([cx-40, cy-40, cx+40, cy-20], outline=colors[0], width=2)
82
-
83
- # Wheels
84
- draw.ellipse([cx-50, cy+10, cx-30, cy+30], outline=colors[0], width=2)
85
- draw.ellipse([cx+30, cy+10, cx+50, cy+30], outline=colors[0], width=2)
86
-
87
- # Windows
88
- draw.rectangle([cx-35, cy-35, cx+35, cy-25], outline=colors[1], width=1)
89
-
90
- def _draw_house(self, draw, width, height, colors):
91
- """Draw a simple house sketch"""
92
- cx, cy = width // 2, height // 2
93
-
94
- # House base
95
- draw.rectangle([cx-50, cy-10, cx+50, cy+40], outline=colors[0], width=2)
96
-
97
- # Roof
98
- draw.polygon([cx-60, cy-10, cx, cy-50, cx+60, cy-10], outline=colors[0], width=2)
99
-
100
- # Door
101
- draw.rectangle([cx-15, cy+10, cx+15, cy+40], outline=colors[1], width=2)
102
-
103
- # Windows
104
- draw.rectangle([cx-40, cy-5, cx-25, cy+10], outline=colors[1], width=1)
105
- draw.rectangle([cx+25, cy-5, cx+40, cy+10], outline=colors[1], width=1)
106
-
107
- def _draw_flower(self, draw, width, height, colors):
108
- """Draw a simple flower sketch"""
109
- cx, cy = width // 2, height // 2
110
-
111
- # Stem
112
- draw.line([cx, cy+20, cx, cy+60], fill=colors[0], width=3)
113
-
114
- # Petals
115
- for i in range(6):
116
- angle = i * 60 * math.pi / 180
117
- x = cx + 25 * math.cos(angle)
118
- y = cy + 25 * math.sin(angle)
119
- draw.ellipse([x-8, y-8, x+8, y+8], outline=colors[0], width=2)
120
-
121
- # Center
122
- draw.ellipse([cx-8, cy-8, cx+8, cy+8], fill=colors[1], outline=colors[0], width=2)
123
-
124
- # Leaves
125
- draw.ellipse([cx-10, cy+30, cx+10, cy+50], outline=colors[0], width=2)
126
-
127
- def _draw_tree(self, draw, width, height, colors):
128
- """Draw a simple tree sketch"""
129
- cx, cy = width // 2, height // 2
130
-
131
- # Trunk
132
- draw.rectangle([cx-8, cy+10, cx+8, cy+60], outline=colors[0], width=2)
133
-
134
- # Tree crown (circle)
135
- draw.ellipse([cx-40, cy-40, cx+40, cy+20], outline=colors[0], width=2)
136
-
137
- # Branches
138
- for i in range(5):
139
- angle = (i * 72 - 90) * math.pi / 180
140
- x = cx + 30 * math.cos(angle)
141
- y = cy + 30 * math.sin(angle)
142
- draw.line([cx, cy, x, y], fill=colors[1], width=1)
143
-
144
- def _draw_mountain(self, draw, width, height, colors):
145
- """Draw a simple mountain landscape"""
146
- cx, cy = width // 2, height // 2
147
-
148
- # Mountains
149
- draw.polygon([20, cy+30, 80, cy-40, 140, cy+30], outline=colors[0], width=2)
150
- draw.polygon([100, cy+30, 160, cy-20, 200, cy+30], outline=colors[0], width=2)
151
-
152
- # Ground line
153
- draw.line([0, cy+30, width, cy+30], fill=colors[1], width=1)
154
-
155
- # Sun
156
- draw.ellipse([width-60, 20, width-20, 60], outline=colors[1], width=2)
157
-
158
- def _draw_abstract(self, draw, width, height, colors):
159
- """Draw abstract shapes for unknown prompts"""
160
- cx, cy = width // 2, height // 2
161
-
162
- # Random geometric shapes
163
- for i in range(5):
164
- x = random.randint(20, width-20)
165
- y = random.randint(20, height-20)
166
- size = random.randint(10, 30)
167
-
168
- if i % 3 == 0:
169
- draw.ellipse([x-size, y-size, x+size, y+size], outline=colors[i%len(colors)], width=2)
170
- elif i % 3 == 1:
171
- draw.rectangle([x-size, y-size, x+size, y+size], outline=colors[i%len(colors)], width=2)
172
- else:
173
- points = []
174
- for j in range(3):
175
- px = x + size * math.cos(j * 120 * math.pi / 180)
176
- py = y + size * math.sin(j * 120 * math.pi / 180)
177
- points.extend([px, py])
178
- draw.polygon(points, outline=colors[i%len(colors)], width=2)