File size: 7,359 Bytes
4bf5aea
 
 
9b5b26a
4bf5aea
9b5b26a
4bf5aea
 
 
 
 
 
0c3a95b
c2fb256
a33f19e
4bf5aea
9b5b26a
 
4bf5aea
 
9dee4c2
4bf5aea
 
 
9dee4c2
4bf5aea
 
 
 
 
 
 
 
 
 
cd969b0
cd7fdc0
9b5b26a
 
 
 
 
 
 
 
 
 
 
 
 
 
8c01ffb
 
4bf5aea
 
 
 
 
 
 
 
 
 
 
 
a33f19e
 
4bf5aea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c01ffb
9d1b2b6
4bf5aea
 
 
 
 
 
 
 
 
 
fca9c3d
4bf5aea
 
fca9c3d
 
4bf5aea
fca9c3d
 
 
4bf5aea
 
 
 
 
 
 
 
a33f19e
 
 
 
 
 
 
 
 
 
 
4bf5aea
 
 
 
 
c2fb256
4bf5aea
0c3a95b
4bf5aea
 
8fe992b
4bf5aea
 
fca9c3d
a33f19e
4bf5aea
fca9c3d
4bf5aea
c2fb256
4bf5aea
0c3a95b
4bf5aea
 
 
 
 
 
 
 
 
 
8fe992b
 
a33f19e
4bf5aea
a33f19e
 
 
 
4bf5aea
a33f19e
 
 
 
 
4bf5aea
 
a33f19e
 
 
 
9dee4c2
9b5b26a
4bf5aea
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import os
from dotenv import load_dotenv
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
import datetime
import requests
import pytz
import yaml
from tools.final_answer import FinalAnswerTool
from tools.wordpress_post import WordPressPostTool
from tools.blog_generator import BlogGeneratorTool
from tools.visit_webpage import VisitWebpageTool
from tools.web_search import DuckDuckGoSearchTool
from tools.html_to_wp_blocks import HTMLToWPBlocksTool
from tools.wordpress_media import WordPressMediaTool
from tools.image_handler import ImageHandlerTool

from Gradio_UI import GradioUI

# Load environment variables
load_dotenv()

# Verify required environment variables
if not os.getenv("OPENWEATHER_API_KEY"):
    print("Warning: OPENWEATHER_API_KEY not set. Weather functionality will be limited.")

# Load WordPress credentials from environment variables
wordpress_credentials = {
    'site_url': os.getenv('WORDPRESS_SITE_URL'),
    'username': os.getenv('WORDPRESS_USERNAME'),
    'app_password': os.getenv('WORDPRESS_APP_PASSWORD')
}

# Verify WordPress credentials
if not all(wordpress_credentials.values()):
    print("Warning: WordPress credentials not fully set. Blog functionality will be limited.")


@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)}"


@tool
def quick_research(query: str, max_results: int = 3) -> str:
    """Searches the web for travel-related information and provides a concise summary.
    Args:
        query: The search query to look up (typically a destination or attraction)
        max_results: Maximum number of search results to consider (default: 3)
    """
    search_tool = DuckDuckGoSearchTool()

    try:
        # Add travel-specific terms to the query
        travel_query = f"{query} travel guide tourist attractions things to do"
        search_results = search_tool.forward(
            query=travel_query, max_results=max_results)

        if not search_results:
            return "No travel information found for the given destination."

        # Format the results
        formatted_results = []
        for i, result in enumerate(search_results, 1):
            title = result.get('title', 'No title')
            snippet = result.get('snippet', 'No description available')
            url = result.get('link', 'No link available')

            formatted_results.append(
                f"{i}. {title}\n"
                f"Travel Info: {snippet}\n"
                f"Source: {url}\n"
            )

        return "Travel Research Results:\n\n" + "\n".join(formatted_results)

    except Exception as e:
        return f"Error performing travel research: {str(e)}"


@tool
def get_weather(city: str) -> str:
    """Fetches current weather information for a given city.
    Args:
        city: Name of the city to get weather for
    """
    try:
        # Using OpenWeatherMap API (you'll need to add your API key)
        # Get API key from environment variable
        API_KEY = os.getenv("OPENWEATHER_API_KEY")
        base_url = "http://api.openweathermap.org/data/2.5/weather"

        params = {
            'q': city,
            'appid': API_KEY,
            'units': 'metric'  # For Celsius
        }

        response = requests.get(base_url, params=params)
        data = response.json()

        if response.status_code == 200:
            temp = data['main']['temp']
            weather_desc = data['weather'][0]['description']
            humidity = data['main']['humidity']

            return (f"Weather in {city}:\n"
                    f"Temperature: {temp}°C\n"
                    f"Conditions: {weather_desc}\n"
                    f"Humidity: {humidity}%")
        else:
            return f"Error: Could not fetch weather for {city}"

    except Exception as e:
        return f"Error fetching weather data: {str(e)}"


final_answer = FinalAnswerTool()
visit_webpage = VisitWebpageTool()
web_search = DuckDuckGoSearchTool()

# 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'

# it is possible that this model may be overloaded
# model_id='Qwen/Qwen2.5-Coder-32B-Instruct',

model = HfApiModel(
    max_tokens=2096,
    temperature=0.5,
    # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
    model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
    # token=os.getenv("HUGGINGFACEHUB_API_TOKEN"),  # Ensure this is set
    custom_role_conversions=None,
    # top_p=0.9,
    # frequency_penalty=0.0,
    # presence_penalty=0.0,
)

# Add error handling wrapper for the model

# Import tool from Hub
image_generation_tool = load_tool(
    "agents-course/text-to-image", trust_remote_code=True)

# Create temp directory for images
image_temp_dir = os.path.join(os.getcwd(), 'temp_images')
os.makedirs(image_temp_dir, exist_ok=True)

# Initialize tools
image_handler = ImageHandlerTool(
    web_search_tool=web_search,
    image_gen_tool=image_generation_tool,
    temp_dir=image_temp_dir
)

with open("prompts.yaml", 'r') as stream:
    prompt_templates = yaml.safe_load(stream)

# Initialize WordPress tools
wordpress_post = WordPressPostTool(wordpress_credentials)
wordpress_media = WordPressMediaTool(wordpress_credentials)
blog_generator = BlogGeneratorTool(model=model)
html_to_blocks = HTMLToWPBlocksTool()

agent = CodeAgent(
    model=model,
    tools=[
        final_answer,
        # get_current_time_in_timezone,
        image_handler,
        quick_research,
        # get_weather,
        wordpress_post,
        wordpress_media,
        blog_generator,
        html_to_blocks,
        visit_webpage,
        web_search
    ],
    max_steps=6,
    verbosity_level=1,
    grammar=None,
    planning_interval=None,
    name=None,
    description=None,
    prompt_templates=prompt_templates
)

# Update system prompt to include image handling capabilities
prompt_templates["system_prompt"] += """
You can now handle images in blog posts more effectively:
- image_handler: Gets images from web search or generates them if needed
- wordpress_media: Uploads media files to WordPress
- html_to_blocks: Converts HTML content to WordPress blocks, including image blocks

When creating posts with images:
1. Use image_handler to get images (it will try web search first, then generation)
2. Upload obtained images using wordpress_media
3. Include the WordPress media URLs in your HTML content
4. Convert to blocks and publish

Remember to:
- Handle image search/generation errors gracefully
- Provide appropriate alt text and titles for images
- Include image attribution if using web images
- Generate images as a fallback when web search fails
"""

GradioUI(agent).launch()