from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool import datetime import requests import pytz import yaml import os import numpy as np import cv2 import gradio as gr from tools.final_answer import FinalAnswerTool from PIL import Image from Gradio_UI import GradioUI # OpenCV Haarcascade face model HAAR_CASCADE_PATH = cv2.data.haarcascades + 'haarcascade_frontalface_default.xml' # Supported mime types ALLOWED_MIME_TYPES = ("image/jpeg", "image/png") # Face detection parameters FACE_DETECTION_SCALE_FACTOR = 1.1 FACE_DETECTION_MIN_NEIGHBORS = 5 MIN_FACE_SIZE = (80, 80) # Min Face Size (width, height) @tool def check_passport_photo(image: Image.Image) -> bool: """ Check if the given image is a valid passport photo. Args: image: The image file uploaded through Gradio UI. Returns: bool: True if the image is a valid passport photo, False otherwise. """ if image is None: raise ValueError("No image uploaded.") # Convert PIL image to NumPy array image = np.array(image) # PIL -> NumPy image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # Convert RGB to BGR (OpenCV uses BGR) if image is None: raise ValueError("Invalid or corrupted image file.") # Convert to grayscale gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Load face detector face_detector = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') # Detect faces detected_faces = face_detector.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(80, 80)) return len(detected_faces) == 1 @tool def get_current_time_in_timezone(timezone: str) -> str: """A tool that fetches the current local time in a specified timezone. Args: timezone: A string representing a valid timezone (e.g., 'America/New_York'). """ try: tz = pytz.timezone(timezone) local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S") return f"The current local time in {timezone} is: {local_time}" except Exception as e: return f"Error fetching time for timezone '{timezone}': {str(e)}" final_answer = FinalAnswerTool() model = HfApiModel( max_tokens=2096, temperature=0.5, model_id='Qwen/Qwen2.5-Coder-32B-Instruct', custom_role_conversions=None, ) image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) with open("prompts.yaml", 'r') as stream: prompt_templates = yaml.safe_load(stream) agent = CodeAgent( model=model, tools=[final_answer, check_passport_photo], # check_passport_photo fonksiyonunu ekledik max_steps=6, verbosity_level=1, grammar=None, planning_interval=None, name=None, description=None, prompt_templates=prompt_templates ) def gradio_check_passport_photo(image): return check_passport_photo(image) iface = gr.Interface(fn=gradio_check_passport_photo, inputs="image", outputs="text") iface.launch()