Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,6 +18,73 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
|
|
| 18 |
"""
|
| 19 |
return "What magic will you build ?"
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
"""A tool that fetches the current local time in a specified timezone.
|
|
@@ -55,7 +122,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 55 |
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
|
|
| 18 |
"""
|
| 19 |
return "What magic will you build ?"
|
| 20 |
|
| 21 |
+
@tool
|
| 22 |
+
def simple_object_detection(image_path: str, confidence_threshold: float) -> str:
|
| 23 |
+
"""
|
| 24 |
+
A tool that performs simple object detection on an image using MobileNet SSD with error handling.
|
| 25 |
+
|
| 26 |
+
Args:
|
| 27 |
+
image_path: Path to the input image.
|
| 28 |
+
confidence_threshold: Minimum confidence (e.g., 0.2) to filter weak detections.
|
| 29 |
+
|
| 30 |
+
Returns:
|
| 31 |
+
A string indicating the location of the saved processed image or an error message.
|
| 32 |
+
"""
|
| 33 |
+
try:
|
| 34 |
+
# List of class labels MobileNet SSD was trained on
|
| 35 |
+
classes = ["background", "aeroplane", "bicycle", "bird", "boat",
|
| 36 |
+
"bottle", "bus", "car", "cat", "chair", "cow",
|
| 37 |
+
"diningtable", "dog", "horse", "motorbike", "person",
|
| 38 |
+
"pottedplant", "sheep", "sofa", "train", "tvmonitor"]
|
| 39 |
+
|
| 40 |
+
# Paths to the pre-trained model files (ensure these files are downloaded)
|
| 41 |
+
prototxt_path = "MobileNetSSD_deploy.prototxt.txt"
|
| 42 |
+
model_path = "MobileNetSSD_deploy.caffemodel"
|
| 43 |
+
|
| 44 |
+
# Load the pre-trained model from disk
|
| 45 |
+
net = cv2.dnn.readNetFromCaffe(prototxt_path, model_path)
|
| 46 |
+
|
| 47 |
+
# Load the image and get its dimensions
|
| 48 |
+
image = cv2.imread(image_path)
|
| 49 |
+
if image is None:
|
| 50 |
+
return f"Error: Image at {image_path} could not be loaded."
|
| 51 |
+
(h, w) = image.shape[:2]
|
| 52 |
+
|
| 53 |
+
# Prepare the image as a blob for the network
|
| 54 |
+
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)),
|
| 55 |
+
scalefactor=0.007843, size=(300, 300),
|
| 56 |
+
mean=127.5)
|
| 57 |
+
|
| 58 |
+
# Pass the blob through the network to obtain detections
|
| 59 |
+
net.setInput(blob)
|
| 60 |
+
detections = net.forward()
|
| 61 |
+
|
| 62 |
+
# Loop over the detections and draw bounding boxes for those above the threshold
|
| 63 |
+
for i in range(0, detections.shape[2]):
|
| 64 |
+
confidence = detections[0, 0, i, 2]
|
| 65 |
+
if confidence > confidence_threshold:
|
| 66 |
+
idx = int(detections[0, 0, i, 1])
|
| 67 |
+
# Compute bounding box coordinates
|
| 68 |
+
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
|
| 69 |
+
(startX, startY, endX, endY) = box.astype("int")
|
| 70 |
+
|
| 71 |
+
# Draw the bounding box and label on the image
|
| 72 |
+
label = f"{classes[idx]}: {confidence * 100:.2f}%"
|
| 73 |
+
cv2.rectangle(image, (startX, startY), (endX, endY), (0, 255, 0), 2)
|
| 74 |
+
y = startY - 10 if startY - 10 > 10 else startY + 20
|
| 75 |
+
cv2.putText(image, label, (startX, y),
|
| 76 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
| 77 |
+
|
| 78 |
+
# Save the output image
|
| 79 |
+
output_path = "output.jpg"
|
| 80 |
+
cv2.imwrite(output_path, image)
|
| 81 |
+
|
| 82 |
+
return f"Processed image saved as {output_path}"
|
| 83 |
+
|
| 84 |
+
except Exception as e:
|
| 85 |
+
return f"An error occurred: {str(e)}"
|
| 86 |
+
|
| 87 |
+
|
| 88 |
@tool
|
| 89 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 90 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 122 |
|
| 123 |
agent = CodeAgent(
|
| 124 |
model=model,
|
| 125 |
+
tools=[DuckDuckGoSearchTool(), final_answer], ## add your tools here (don't remove final answer)
|
| 126 |
max_steps=6,
|
| 127 |
verbosity_level=1,
|
| 128 |
grammar=None,
|