File size: 2,389 Bytes
6b840ff
9b5b26a
 
 
c19d193
6b840ff
 
 
 
 
e3d182f
9b5b26a
6b840ff
 
9b5b26a
c167ea6
3dafad4
 
 
71df676
3dafad4
c167ea6
5bf5eb8
c167ea6
 
 
 
9b5b26a
 
 
3dafad4
 
 
71df676
3dafad4
9b5b26a
 
 
5bf5eb8
9b5b26a
5bf5eb8
8c01ffb
71df676
e121372
6b840ff
 
71df676
6b840ff
13d500a
8c01ffb
9b5b26a
8c01ffb
5bf5eb8
 
9b5b26a
8c01ffb
8fe992b
6b840ff
 
5bf5eb8
6b840ff
 
 
 
 
 
 
861422e
8fe992b
 
5bf5eb8
71df676
5bf5eb8
 
 
 
 
e3d182f
71df676
 
 
e3d182f
5bf5eb8
e3d182f
 
 
71df676
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
from smolagents import CodeAgent, HfApiModel, load_tool, tool
import datetime
import requests
import pytz
import yaml
import os
from dotenv import load_dotenv
from tools.web_search import DuckDuckGoSearchTool
from tools.visit_webpage import VisitWebpageTool
from tools.final_answer import final_answer
import gradio as gr

load_dotenv()

@tool
def get_weather(city: str) -> str:
    """Get current weather conditions for a specified city
    
    Args:
        city (str): The name of the city to check weather for
    """
    try:
        response = requests.get(f"https://wttr.in/{city}?format=%C+%t")
        response.raise_for_status()
        return f"Weather in {city}: {response.text}"
    except Exception as e:
        return f"Weather check failed: {str(e)}"

@tool
def get_current_time_in_timezone(timezone: str) -> str:
    """Get current local time in specified timezone
    
    Args:
        timezone (str): A valid timezone identifier
    """
    try:
        tz = pytz.timezone(timezone)
        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
        return f"Current time in {timezone}: {local_time}"
    except Exception as e:
        return f"Error: {str(e)}"

# Model configuration - minimal and working
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") as f:
    prompt_templates = yaml.safe_load(f)
    
agent = CodeAgent(
    model=model,
    tools=[
        get_weather,
        get_current_time_in_timezone,
        image_generation_tool,
        DuckDuckGoSearchTool(),
        VisitWebpageTool(),
        final_answer
    ],
    max_steps=10,
    verbosity_level=2,
    prompt_templates=prompt_templates
)

def run_agent(query: str) -> str:
    """Wrapper function for Gradio interface"""
    try:
        return str(agent.run(query))
    except Exception as e:
        return f"Error: {str(e)}"

if __name__ == "__main__":
    if not os.getenv("HF_TOKEN"):
        raise ValueError("HF_TOKEN environment variable not set")
    
    gr.Interface(
        fn=run_agent,
        inputs=gr.Textbox(label="Input"),
        outputs=gr.Textbox(label="Output"),
        title="AI Agent"
    ).launch(server_name="0.0.0.0", share=True)