ArifKemall's picture
Update app.py
605a5a4 verified
Raw
History Blame Contribute Delete
3.24 kB
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()