Spaces:
Sleeping
Sleeping
| 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.") | |
| 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)}" | |
| 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)}" | |
| 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() | |