Spaces:
Build error
Build error
Commit Β·
760fccf
1
Parent(s): 8999815
added word
Browse files- app.py +84 -3
- test_api.py +231 -213
app.py
CHANGED
|
@@ -103,6 +103,78 @@ def classify_drawing(image, top_k: int = 5) -> List[Tuple[str, float]]:
|
|
| 103 |
except Exception as e:
|
| 104 |
print(f"Error in classification: {str(e)}")
|
| 105 |
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
def gradio_classify(image):
|
| 108 |
"""Gradio interface function"""
|
|
@@ -219,6 +291,11 @@ def create_interface():
|
|
| 219 |
step=1,
|
| 220 |
label="Top K Predictions"
|
| 221 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
api_btn = gr.Button("π Classify", variant="primary")
|
| 223 |
|
| 224 |
with gr.Column():
|
|
@@ -226,14 +303,14 @@ def create_interface():
|
|
| 226 |
|
| 227 |
api_btn.click(
|
| 228 |
fn=classify_image_api,
|
| 229 |
-
inputs=[api_input, api_top_k],
|
| 230 |
outputs=[api_output]
|
| 231 |
)
|
| 232 |
|
| 233 |
return demo
|
| 234 |
|
| 235 |
# API endpoint function for external use
|
| 236 |
-
def classify_image_api(image_data: str, top_k: int = 5):
|
| 237 |
"""
|
| 238 |
API endpoint for classifying base64 encoded images
|
| 239 |
|
|
@@ -250,7 +327,11 @@ def classify_image_api(image_data: str, top_k: int = 5):
|
|
| 250 |
image = Image.open(io.BytesIO(image_bytes))
|
| 251 |
|
| 252 |
# Classify
|
| 253 |
-
results =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
|
| 255 |
return {
|
| 256 |
"success": True,
|
|
|
|
| 103 |
except Exception as e:
|
| 104 |
print(f"Error in classification: {str(e)}")
|
| 105 |
return []
|
| 106 |
+
|
| 107 |
+
def classify_drawing1(image, top_k: int = 5,word: str = "dog") -> List[Tuple[str, float]]:
|
| 108 |
+
"""
|
| 109 |
+
Classify a quickdraw-style drawing using CLIP zero-shot classification
|
| 110 |
+
|
| 111 |
+
Args:
|
| 112 |
+
image: PIL Image or numpy array of the drawing
|
| 113 |
+
top_k: Number of top predictions to return
|
| 114 |
+
|
| 115 |
+
Returns:
|
| 116 |
+
List of tuples (category, confidence_score)
|
| 117 |
+
"""
|
| 118 |
+
if image is None:
|
| 119 |
+
print("Here")
|
| 120 |
+
return []
|
| 121 |
+
|
| 122 |
+
try:
|
| 123 |
+
# Preprocess the image
|
| 124 |
+
if isinstance(image, dict):
|
| 125 |
+
print("Image is a dictionary")
|
| 126 |
+
# return []
|
| 127 |
+
print(image)
|
| 128 |
+
image_input = preprocess_drawing(image['composite'])
|
| 129 |
+
else:
|
| 130 |
+
image_input = preprocess_drawing(image)
|
| 131 |
+
if image_input is None:
|
| 132 |
+
# print("Here2")
|
| 133 |
+
return []
|
| 134 |
+
if word.lower() not in [cat.lower() for cat in CATEGORIES]:
|
| 135 |
+
CATEGORIES.append(word)
|
| 136 |
+
# print(f"Added new category: {word}")
|
| 137 |
+
|
| 138 |
+
# Create text prompts for all categories
|
| 139 |
+
text_prompts = [f"a drawing of a {category}" for category in CATEGORIES]
|
| 140 |
+
text_inputs = clip.tokenize(text_prompts).to(device)
|
| 141 |
+
|
| 142 |
+
# Get features
|
| 143 |
+
with torch.no_grad():
|
| 144 |
+
image_features = model.encode_image(image_input)
|
| 145 |
+
text_features = model.encode_text(text_inputs)
|
| 146 |
+
|
| 147 |
+
# Normalize features
|
| 148 |
+
image_features = image_features / image_features.norm(dim=-1, keepdim=True)
|
| 149 |
+
text_features = text_features / text_features.norm(dim=-1, keepdim=True)
|
| 150 |
+
|
| 151 |
+
# Calculate similarities
|
| 152 |
+
similarities = (100.0 * image_features @ text_features.T).softmax(dim=-1)
|
| 153 |
+
values, indices = similarities[0].topk(top_k)
|
| 154 |
+
|
| 155 |
+
# Format results
|
| 156 |
+
results = []
|
| 157 |
+
for i in range(top_k):
|
| 158 |
+
category = CATEGORIES[indices[i]]
|
| 159 |
+
confidence = float(values[i])
|
| 160 |
+
results.append((category, confidence))
|
| 161 |
+
|
| 162 |
+
if word.lower() not in [cat.lower() for cat, _ in results]:
|
| 163 |
+
# Find the original confidence score for the word
|
| 164 |
+
word_idx = CATEGORIES.index(word)
|
| 165 |
+
word_confidence = float(similarities[0][word_idx])
|
| 166 |
+
|
| 167 |
+
# Remove the last result if we have top_k results
|
| 168 |
+
if len(results) >= top_k:
|
| 169 |
+
results.pop()
|
| 170 |
+
# Add the word with its original confidence score
|
| 171 |
+
results.append((word, word_confidence))
|
| 172 |
+
|
| 173 |
+
return results
|
| 174 |
+
|
| 175 |
+
except Exception as e:
|
| 176 |
+
print(f"Error in classification: {str(e)}")
|
| 177 |
+
return []
|
| 178 |
|
| 179 |
def gradio_classify(image):
|
| 180 |
"""Gradio interface function"""
|
|
|
|
| 291 |
step=1,
|
| 292 |
label="Top K Predictions"
|
| 293 |
)
|
| 294 |
+
api_word = gr.Textbox(
|
| 295 |
+
label="Word to Add",
|
| 296 |
+
placeholder="Enter a word to add to the categories",
|
| 297 |
+
lines=1
|
| 298 |
+
)
|
| 299 |
api_btn = gr.Button("π Classify", variant="primary")
|
| 300 |
|
| 301 |
with gr.Column():
|
|
|
|
| 303 |
|
| 304 |
api_btn.click(
|
| 305 |
fn=classify_image_api,
|
| 306 |
+
inputs=[api_input, api_top_k, api_word],
|
| 307 |
outputs=[api_output]
|
| 308 |
)
|
| 309 |
|
| 310 |
return demo
|
| 311 |
|
| 312 |
# API endpoint function for external use
|
| 313 |
+
def classify_image_api(image_data: str, top_k: int = 5, word: str = "dog"):
|
| 314 |
"""
|
| 315 |
API endpoint for classifying base64 encoded images
|
| 316 |
|
|
|
|
| 327 |
image = Image.open(io.BytesIO(image_bytes))
|
| 328 |
|
| 329 |
# Classify
|
| 330 |
+
results = classify_drawing1(image, top_k, word)
|
| 331 |
+
# Normalize confidence scores to sum to 100%
|
| 332 |
+
total_conf = sum(conf for _, conf in results)
|
| 333 |
+
if total_conf > 0:
|
| 334 |
+
results = [(cat, (conf/total_conf) * 100) for cat, conf in results]
|
| 335 |
|
| 336 |
return {
|
| 337 |
"success": True,
|
test_api.py
CHANGED
|
@@ -4,251 +4,269 @@ import base64
|
|
| 4 |
import io
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
def test_base64_api(client_url="http://localhost:7860"):
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
|
| 54 |
|
| 55 |
-
def test_main_interface(client_url="http://localhost:7860"):
|
| 56 |
-
|
| 57 |
-
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
|
| 96 |
|
| 97 |
-
def test_huggingface_space():
|
| 98 |
-
|
| 99 |
-
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
def create_test_drawings():
|
| 138 |
-
|
| 139 |
-
|
| 140 |
|
| 141 |
-
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
|
| 164 |
|
| 165 |
-
def test_multiple_drawings():
|
| 166 |
-
|
| 167 |
-
|
| 168 |
|
| 169 |
-
|
| 170 |
|
| 171 |
-
|
| 172 |
-
|
| 173 |
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
|
| 194 |
-
|
| 195 |
-
|
| 196 |
|
| 197 |
|
| 198 |
-
def test_base64_with_file():
|
| 199 |
-
|
| 200 |
-
|
| 201 |
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
|
| 213 |
-
|
| 214 |
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
|
| 227 |
-
|
| 228 |
-
|
| 229 |
|
| 230 |
|
| 231 |
-
if __name__ == "__main__":
|
| 232 |
-
|
| 233 |
-
|
| 234 |
|
| 235 |
-
|
| 236 |
-
|
| 237 |
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
|
| 244 |
-
|
| 245 |
-
|
| 246 |
|
| 247 |
-
|
| 248 |
-
|
| 249 |
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import io
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# def test_base64_api(client_url="http://localhost:7860"):
|
| 8 |
+
# """Test the base64 API endpoint"""
|
| 9 |
+
# print("π§ͺ Testing Base64 API Endpoint...")
|
| 10 |
|
| 11 |
+
# try:
|
| 12 |
+
# # Initialize the client
|
| 13 |
+
# client = Client(client_url)
|
| 14 |
+
|
| 15 |
+
# # Check if temp_drawing.png exists, if not create a simple test image
|
| 16 |
+
# if not os.path.exists("temp_drawing.png"):
|
| 17 |
+
# print("Creating a test image...")
|
| 18 |
+
# # Create a simple test image (white background with black circle)
|
| 19 |
+
# img = Image.new('RGB', (400, 400), 'white')
|
| 20 |
+
# from PIL import ImageDraw
|
| 21 |
+
# draw = ImageDraw.Draw(img)
|
| 22 |
+
# draw.ellipse([150, 150, 250, 250], fill='black')
|
| 23 |
+
# img.save("temp_drawing.png")
|
| 24 |
+
|
| 25 |
+
# # Load the image
|
| 26 |
+
# image = Image.open("temp_drawing.png")
|
| 27 |
+
# print(f"β Loaded image: {image.size}")
|
| 28 |
+
|
| 29 |
+
# # Convert image to base64
|
| 30 |
+
# buffered = io.BytesIO()
|
| 31 |
+
# image.save(buffered, format="PNG")
|
| 32 |
+
# img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 33 |
+
# print(f"β Converted to base64 (length: {len(img_str)})")
|
| 34 |
+
|
| 35 |
+
# # Call the API endpoint with correct parameter names
|
| 36 |
+
# result = client.predict(
|
| 37 |
+
# image_data=img_str,
|
| 38 |
+
# top_k=5,
|
| 39 |
+
# api_name="/classify_image_api"
|
| 40 |
+
# )
|
| 41 |
+
|
| 42 |
+
# print("β API Response:")
|
| 43 |
+
# print(f" Success: {result.get('success', False)}")
|
| 44 |
+
# if result.get('success'):
|
| 45 |
+
# print(" Predictions:")
|
| 46 |
+
# for i, pred in enumerate(result.get('predictions', []), 1):
|
| 47 |
+
# print(f" {i}. {pred['category']} - {pred['confidence']:.3f}")
|
| 48 |
+
# else:
|
| 49 |
+
# print(f" Error: {result.get('error', 'Unknown error')}")
|
| 50 |
|
| 51 |
+
# except Exception as e:
|
| 52 |
+
# print(f"β Base64 API test failed: {str(e)}")
|
| 53 |
|
| 54 |
|
| 55 |
+
# def test_main_interface(client_url="http://localhost:7860"):
|
| 56 |
+
# """Test the main drawing interface"""
|
| 57 |
+
# print("\nπ§ͺ Testing Main Drawing Interface...")
|
| 58 |
|
| 59 |
+
# try:
|
| 60 |
+
# # Initialize the client
|
| 61 |
+
# client = Client(client_url)
|
| 62 |
+
|
| 63 |
+
# # Check if temp_drawing.png exists
|
| 64 |
+
# if not os.path.exists("temp_drawing.png"):
|
| 65 |
+
# print("Creating a test image...")
|
| 66 |
+
# # Create a simple test image (white background with black circle)
|
| 67 |
+
# img = Image.new('RGB', (400, 400), 'white')
|
| 68 |
+
# from PIL import ImageDraw
|
| 69 |
+
# draw = ImageDraw.Draw(img)
|
| 70 |
+
# draw.ellipse([150, 150, 250, 250], fill='black')
|
| 71 |
+
# img.save("temp_drawing.png")
|
| 72 |
+
|
| 73 |
+
# # Load and convert image to the format expected by Sketchpad
|
| 74 |
+
# image = Image.open("temp_drawing.png")
|
| 75 |
+
|
| 76 |
+
# # Create a mock sketchpad data structure
|
| 77 |
+
# sketchpad_data = {
|
| 78 |
+
# "background": image,
|
| 79 |
+
# "layers": [image],
|
| 80 |
+
# "composite": image
|
| 81 |
+
# }
|
| 82 |
+
|
| 83 |
+
# # Test the main gradio_classify function
|
| 84 |
+
# result = client.predict(
|
| 85 |
+
# canvas=sketchpad_data,
|
| 86 |
+
# api_name="/gradio_classify"
|
| 87 |
+
# )
|
| 88 |
+
|
| 89 |
+
# print("β Main Interface Response:")
|
| 90 |
+
# print(result)
|
| 91 |
|
| 92 |
+
# except Exception as e:
|
| 93 |
+
# print(f"β Main interface test failed: {str(e)}")
|
| 94 |
+
# print("π‘ Try using the web interface directly instead")
|
| 95 |
|
| 96 |
|
| 97 |
+
# def test_huggingface_space():
|
| 98 |
+
# """Test the deployed Hugging Face space"""
|
| 99 |
+
# print("\nπ§ͺ Testing Hugging Face Space...")
|
| 100 |
|
| 101 |
+
# try:
|
| 102 |
+
# # Test your deployed space
|
| 103 |
+
# client = Client("souvikg544/quickdraw-classifier")
|
| 104 |
+
|
| 105 |
+
# # Check if temp_drawing.png exists
|
| 106 |
+
# if not os.path.exists("temp_drawing.png"):
|
| 107 |
+
# print("Creating a test image...")
|
| 108 |
+
# img = Image.new('RGB', (400, 400), 'white')
|
| 109 |
+
# from PIL import ImageDraw
|
| 110 |
+
# draw = ImageDraw.Draw(img)
|
| 111 |
+
# draw.ellipse([150, 150, 250, 250], fill='black')
|
| 112 |
+
# img.save("temp_drawing.png")
|
| 113 |
+
|
| 114 |
+
# # Load and convert image to the format expected by Sketchpad
|
| 115 |
+
# image = Image.open("temp_drawing.png")
|
| 116 |
+
|
| 117 |
+
# # Create a mock sketchpad data structure
|
| 118 |
+
# sketchpad_data = {
|
| 119 |
+
# "background": image,
|
| 120 |
+
# "layers": [image],
|
| 121 |
+
# "composite": image
|
| 122 |
+
# }
|
| 123 |
+
|
| 124 |
+
# # Test with sketchpad format
|
| 125 |
+
# result = client.predict(
|
| 126 |
+
# canvas=sketchpad_data,
|
| 127 |
+
# api_name="/gradio_classify"
|
| 128 |
+
# )
|
| 129 |
+
|
| 130 |
+
# print("β Hugging Face Space Response:")
|
| 131 |
+
# print(result)
|
| 132 |
+
|
| 133 |
+
# except Exception as e:
|
| 134 |
+
# print(f"β Hugging Face space test failed: {str(e)}")
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
# def create_test_drawings():
|
| 138 |
+
# """Create various test drawings for testing"""
|
| 139 |
+
# print("\nπ¨ Creating test drawings...")
|
| 140 |
|
| 141 |
+
# from PIL import ImageDraw
|
| 142 |
|
| 143 |
+
# # Test drawing 1: Circle (might be classified as sun, moon, etc.)
|
| 144 |
+
# img1 = Image.new('RGB', (400, 400), 'white')
|
| 145 |
+
# draw1 = ImageDraw.Draw(img1)
|
| 146 |
+
# draw1.ellipse([150, 150, 250, 250], fill='black')
|
| 147 |
+
# img1.save("test_circle.png")
|
| 148 |
+
# print("β Created test_circle.png")
|
| 149 |
|
| 150 |
+
# # Test drawing 2: Rectangle (might be classified as house, car, etc.)
|
| 151 |
+
# img2 = Image.new('RGB', (400, 400), 'white')
|
| 152 |
+
# draw2 = ImageDraw.Draw(img2)
|
| 153 |
+
# draw2.rectangle([100, 150, 300, 250], fill='black')
|
| 154 |
+
# img2.save("test_rectangle.png")
|
| 155 |
+
# print("β Created test_rectangle.png")
|
| 156 |
|
| 157 |
+
# # Test drawing 3: Triangle (might be classified as tree, mountain, etc.)
|
| 158 |
+
# img3 = Image.new('RGB', (400, 400), 'white')
|
| 159 |
+
# draw3 = ImageDraw.Draw(img3)
|
| 160 |
+
# draw3.polygon([(200, 100), (150, 200), (250, 200)], fill='black')
|
| 161 |
+
# img3.save("test_triangle.png")
|
| 162 |
+
# print("β Created test_triangle.png")
|
| 163 |
|
| 164 |
|
| 165 |
+
# def test_multiple_drawings():
|
| 166 |
+
# """Test classification with multiple different drawings"""
|
| 167 |
+
# print("\nπ§ͺ Testing Multiple Drawings...")
|
| 168 |
|
| 169 |
+
# test_files = ["test_circle.png", "test_rectangle.png", "test_triangle.png"]
|
| 170 |
|
| 171 |
+
# try:
|
| 172 |
+
# client = Client("http://localhost:7860")
|
| 173 |
|
| 174 |
+
# for test_file in test_files:
|
| 175 |
+
# if os.path.exists(test_file):
|
| 176 |
+
# print(f"\n--- Testing {test_file} ---")
|
| 177 |
|
| 178 |
+
# # Load image and create sketchpad format
|
| 179 |
+
# image = Image.open(test_file)
|
| 180 |
+
# sketchpad_data = {
|
| 181 |
+
# "background": image,
|
| 182 |
+
# "layers": [image],
|
| 183 |
+
# "composite": image
|
| 184 |
+
# }
|
| 185 |
|
| 186 |
+
# result = client.predict(
|
| 187 |
+
# canvas=sketchpad_data,
|
| 188 |
+
# api_name="/gradio_classify"
|
| 189 |
+
# )
|
| 190 |
+
# print(result)
|
| 191 |
+
# else:
|
| 192 |
+
# print(f"β {test_file} not found")
|
| 193 |
|
| 194 |
+
# except Exception as e:
|
| 195 |
+
# print(f"β Multiple drawings test failed: {str(e)}")
|
| 196 |
|
| 197 |
|
| 198 |
+
# def test_base64_with_file():
|
| 199 |
+
# """Test base64 API with actual file conversion"""
|
| 200 |
+
# print("\nπ§ͺ Testing Base64 API with File Conversion...")
|
| 201 |
|
| 202 |
+
# try:
|
| 203 |
+
# client = Client("http://localhost:7860")
|
| 204 |
+
|
| 205 |
+
# # Use one of our test files
|
| 206 |
+
# test_file = "test_circle.png"
|
| 207 |
+
# if os.path.exists(test_file):
|
| 208 |
+
# # Load and convert to base64
|
| 209 |
+
# with open(test_file, "rb") as f:
|
| 210 |
+
# img_bytes = f.read()
|
| 211 |
+
# img_b64 = base64.b64encode(img_bytes).decode()
|
| 212 |
|
| 213 |
+
# print(f"β Converted {test_file} to base64")
|
| 214 |
|
| 215 |
+
# # Test the base64 API with correct parameter names
|
| 216 |
+
# result = client.predict(
|
| 217 |
+
# image_data=img_b64,
|
| 218 |
+
# top_k=5,
|
| 219 |
+
# api_name="/classify_image_api"
|
| 220 |
+
# )
|
| 221 |
|
| 222 |
+
# print("β Base64 API Response:")
|
| 223 |
+
# print(result)
|
| 224 |
+
# else:
|
| 225 |
+
# print("β No test file found for base64 testing")
|
| 226 |
|
| 227 |
+
# except Exception as e:
|
| 228 |
+
# print(f"β Base64 file test failed: {str(e)}")
|
| 229 |
|
| 230 |
|
| 231 |
+
# if __name__ == "__main__":
|
| 232 |
+
# print("π QuickDraw Classifier API Test Suite")
|
| 233 |
+
# print("=" * 50)
|
| 234 |
|
| 235 |
+
# # Create test drawings first
|
| 236 |
+
# create_test_drawings()
|
| 237 |
|
| 238 |
+
# # Test local server (make sure your app is running on localhost:7860)
|
| 239 |
+
# print("\nπ Testing Local Server...")
|
| 240 |
+
# test_base64_api("http://localhost:7860")
|
| 241 |
+
# test_main_interface("http://localhost:7860")
|
| 242 |
+
# test_multiple_drawings()
|
| 243 |
|
| 244 |
+
# # Test Hugging Face deployment
|
| 245 |
+
# test_huggingface_space()
|
| 246 |
|
| 247 |
+
# # Test base64 API with file conversion
|
| 248 |
+
# test_base64_with_file()
|
| 249 |
|
| 250 |
+
# print("\nβ
Test suite completed!")
|
| 251 |
+
# print("\nπ‘ Tips:")
|
| 252 |
+
# print("- Make sure your local server is running: python app.py")
|
| 253 |
+
# print("- For HF Space testing, ensure your space is public and running")
|
| 254 |
+
# print("- Check the generated test images: test_circle.png, test_rectangle.png, test_triangle.png")
|
| 255 |
+
|
| 256 |
+
|
| 257 |
+
from gradio_client import Client
|
| 258 |
+
|
| 259 |
+
image = Image.open("temp_drawing.png")
|
| 260 |
+
buffered = io.BytesIO()
|
| 261 |
+
image.save(buffered, format="PNG")
|
| 262 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 263 |
+
|
| 264 |
+
# client = Client("souvikg544/quickdraw-classifier")
|
| 265 |
+
client = Client("https://6820c804655322f295.gradio.live/")
|
| 266 |
+
result = client.predict(
|
| 267 |
+
image_data=img_str,
|
| 268 |
+
top_k=5,
|
| 269 |
+
word="broom",
|
| 270 |
+
api_name="/classify_image_api"
|
| 271 |
+
)
|
| 272 |
+
print(result)
|