from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool import datetime import os import requests import pytz import yaml from typing import Dict from tools.final_answer import FinalAnswerTool from Gradio_UI import GradioUI from diffusers import StableDiffusionPipeline # 🚀 Vowel & Consonant Counter Tool @tool def count_vowels_consonants(word: str) -> Dict[str, int]: """Counts the number of vowels and consonants in a given word. Args: word: A string representing the word. Returns: A dictionary with counts of vowels and consonants. """ if not word: return {"error": "Input word cannot be empty."} vowels = "aeiouAEIOU" consonants = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ" vowel_count = sum(1 for letter in word if letter in vowels) consonant_count = sum(1 for letter in word if letter in consonants) return {"vowels": vowel_count, "consonants": consonant_count} # 🌍 Get Time Difference two cities in World. @tool def time_difference(city1: str, city2: str) -> str: """Calculates the time difference between two cities. Args: city1: The first city's timezone (e.g., 'America/New_York'). city2: The second city's timezone (e.g., 'Europe/Istanbul'). Returns: A string indicating the time difference. """ try: tz1 = pytz.timezone(city1) tz2 = pytz.timezone(city2) now = datetime.datetime.utcnow() time1 = now.astimezone(tz1) time2 = now.astimezone(tz2) difference = (time2 - time1).total_seconds() / 3600 return f"{city2} is {abs(difference)} hours {'ahead' if difference > 0 else 'behind'} {city1}" except Exception as e: return f"Error: {str(e)}" # 🌍 Get Current Time in a Given Timezone Tool @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: # Create timezone object tz = pytz.timezone(timezone) # Get current time in that 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 Tool final_answer = FinalAnswerTool() # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder: # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' model = HfApiModel( max_tokens=2096, temperature=0.5, model_id='Qwen/Qwen2.5-VL-72B-Instruct', # it is possible that this model may be overloaded custom_role_conversions=None, ) @tool def generate_image_with_sd(prompt: str) -> str: """Generates an image using Stable Diffusion v1.5 on CPU. Args: prompt (str): A text prompt describing the desired image. Returns: str: File path of the generated image. """ try: # Load the model with CPU settings pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float32) pipe.to("cpu") # Generate the image image = pipe(prompt).images[0] # Save the image output_path = "generated_image.png" image.save(output_path) return f"Image saved at: {output_path}" except Exception as e: return f"Error generating image: {str(e)}" with open("prompts.yaml", 'r') as stream: prompt_templates = yaml.safe_load(stream) agent = CodeAgent( model=model, tools=[final_answer, count_vowels_consonants, time_difference, get_current_time_in_timezone, generate_image_with_sd], # Image generation tool ekleniyor max_steps=6, verbosity_level=1, grammar=None, planning_interval=None, name=None, description=None, prompt_templates=prompt_templates ) GradioUI(agent).launch()