File size: 3,242 Bytes
d3ee373
9b5b26a
 
 
c19d193
a770b06
605a5a4
 
 
9b5b26a
605a5a4
9b5b26a
605a5a4
 
9b5b26a
605a5a4
 
 
9b5b26a
605a5a4
 
 
 
 
 
9b5b26a
605a5a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
567bec5
605a5a4
 
d51c39d
605a5a4
a770b06
605a5a4
 
 
6191cf2
c683b17
605a5a4
6191cf2
605a5a4
 
 
6191cf2
 
605a5a4
6191cf2
605a5a4
6191cf2
605a5a4
9b5b26a
 
605a5a4
9b5b26a
605a5a4
 
 
9b5b26a
 
 
 
605a5a4
9b5b26a
605a5a4
ae7a494
605a5a4
e121372
d3ee373
 
605a5a4
 
13d500a
8c01ffb
605a5a4
9b5b26a
8c01ffb
605a5a4
861422e
 
605a5a4
 
8c01ffb
8fe992b
605a5a4
8c01ffb
 
 
 
 
 
861422e
8fe992b
 
605a5a4
d3ee373
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
101
102
103
104
105
106
107
108
109
110
111
112
from smolagents import CodeAgent, HfApiModel, load_tool, tool
import datetime
import requests
import pytz
import yaml
from mimesis import Address
from gradio_ui import GradioUI
from duckduckgo_search import DDGS
import wikipedia

# Web'de arama yapma aracı
@tool
def web_search(query: str) -> str:
    """DuckDuckGo kullanarak web'de arama yapar.
    Args:
        query (str): Arama terimi.
    Returns:
        str: İlk sonuç metni veya hata mesajı.
    """
    try:
        with DDGS() as ddgs:
            results = list(ddgs.text(query, max_results=1))
            return results[0]['body'] if results else "No results found."
    except Exception as e:
        return f"Error: {str(e)}"

# Wikipedia'dan bilgi alma aracı
@tool
def get_wikipedia_summary(topic: str) -> str:
    """Wikipedia'dan özet bilgi çeker.
    Args:
        topic (str): Konu başlığı.
    Returns:
        str: Wikipedia özeti veya hata mesajı.
    """
    try:
        return wikipedia.summary(topic, sentences=2)
    except wikipedia.exceptions.DisambiguationError as e:
        return f"Disambiguation error: {str(e)}"
    except wikipedia.exceptions.PageError:
        return "No Wikipedia page found."
    except Exception as e:
        return f"Error: {str(e)}"

# Rastgele şehir adı seçen fonksiyon
@tool
def get_random_city() -> str:
    """Rastgele bir şehir adı döndürür."""
    address = Address()
    return address.city()

# Şehir hakkında bilgi getiren araç
@tool
def get_city_fact(city: str) -> str:
    """Şehir hakkında bilgi getirir.
    Args:
        city (str): Şehir adı.
    Returns:
        str: Şehir hakkında bilgiler.
    """
    try:
        return web_search(f"{city} hakkında bilgiler")
    except Exception as e:
        return f"Error: {str(e)}"

# Saat dilimine göre güncel saati getiren araç
@tool
def get_current_time_in_timezone(timezone: str) -> str:
    """Belirtilen saat dilimine göre şu anki saati döndürür.
    Args:
        timezone (str): Saat dilimi (örn. 'Europe/Istanbul').
    Returns:
        str: Saat bilgisi veya hata mesajı.
    """
    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)}"

# Hugging Face modelini tanımla
model = HfApiModel(
    max_tokens=2096,
    temperature=0.5,
    model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
    custom_role_conversions=None,
)

# Görüntü oluşturma aracını yükle
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)

# Prompt dosyasını yükle
with open("prompts.yaml", 'r') as stream:
    prompt_templates = yaml.safe_load(stream)

# Ajanı tanımla
agent = CodeAgent(
    model=model,
    tools=[get_city_fact, get_current_time_in_timezone, get_wikipedia_summary, web_search, image_generation_tool],
    max_steps=6,
    verbosity_level=1,
    grammar=None,
    planning_interval=None,
    name=None,
    description=None,
    prompt_templates=prompt_templates
)

# Gradio UI başlat
GradioUI(agent).launch()