File size: 3,033 Bytes
80e0251
9b5b26a
 
 
c19d193
80e0251
8c47dfc
80e0251
 
6aae614
97e56e7
9b5b26a
 
80e0251
 
 
 
 
 
 
 
 
 
 
9b5b26a
97e56e7
80e0251
 
 
9b5b26a
80e0251
 
 
 
9b5b26a
80e0251
 
fc44ca4
 
 
 
80e0251
 
 
 
fc44ca4
80e0251
fc44ca4
 
 
80e0251
fc44ca4
 
80e0251
 
9b5b26a
 
 
 
 
 
 
 
 
 
 
 
 
8c01ffb
6aae614
ae7a494
e121372
80e0251
 
 
 
13d500a
8c01ffb
9b5b26a
8c01ffb
861422e
 
80e0251
8c01ffb
8fe992b
80e0251
8c01ffb
 
 
 
 
 
861422e
8fe992b
 
80e0251
 
9b5b26a
80e0251
 
1
2
3
4
5
6
7
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
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
98
99
100
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()