from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool import datetime import requests import pytz import yaml import re from tools.final_answer import FinalAnswerTool from Gradio_UI import GradioUI # Below is an example of a tool that does nothing. Amaze us with your creativity! @tool def create_new_vday_card_tool(theme: str) -> str: # it's import to specify the return type # Keep this format for the description / args / args description but feel free to modify the tool """A tool that generates a custom Valentine's Day card based on a given theme. Args: theme: A string representing the theme for the Valentine's Day card. """ try: # Import tool from Hub ''' image_generation_tool = Tool.from_space( space_id="black-forest-labs/FLUX.1-schnell", name="generate-image", description="Generate image from prompt" ) ''' image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) image = image_generation_tool(theme) return final_answer(image) except Exception as e: return f"Error creating card for theme '{theme}': {str(e)}" # image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) final_answer = FinalAnswerTool() # Using Mistral-7B-Instruct-v0.1 instead of Qwen2.5-Coder-32B-Instruct model = HfApiModel( max_tokens=2096, temperature=0.7, # Slightly higher temperature for Mistral as it's smaller model_id='mistralai/Mistral-7B-Instruct-v0.1', # Changed to Mistral model # custom_role_conversions can be adjusted for Mistral custom_role_conversions={ "system": "[INST] <>\n{message}\n<>\n\n", "human": "{message} [/INST]", "assistant": "{message} [INST] " }, ) # Let's add a fix for the Gradio issue by patching the launch method def modified_launch(): try: # Original code with open("prompts.yaml", 'r') as stream: prompt_templates = yaml.safe_load(stream) agent = CodeAgent( model=model, tools=[final_answer, create_new_vday_card_tool], # add your tools here (don't remove final answer) max_steps=6, verbosity_level=1, grammar=None, planning_interval=None, name=None, description=None, prompt_templates=prompt_templates ) # Get the Gradio UI instance but don't launch yet ui = GradioUI(agent) # Patch the launch method to avoid API issues original_launch = ui.launch def patched_launch(*args, **kwargs): # Try to patch the problematic API processing in routes.py try: import types from gradio import routes # Create a dummy method that returns an empty dict to avoid the error def dummy_api_info(self): return {"endpoints": {}, "components": [], "dependencies": []} # Try to patch FastAPI.api_info try: if hasattr(routes, 'FastAPI'): routes.FastAPI.api_info = types.MethodType(dummy_api_info, routes.FastAPI) except: pass except: pass # Call the original launch method return original_launch(*args, **kwargs) # Replace the launch method with our patched version ui.launch = patched_launch # Now launch the UI with our patched method return ui.launch() except Exception as e: # If there's an error with Gradio, fall back to a Flask interface print(f"Error with Gradio: {str(e)}") print("Falling back to Flask interface...") # Import Flask and create a simple interface from flask import Flask, request, render_template_string, jsonify import base64 from PIL import Image from io import BytesIO app = Flask(__name__) # Simple HTML template html_template = """ Valentine's Day Card Generator

Valentine's Day Card Generator

Enter a theme to generate a custom Valentine's Day card.

{% if error %}

{{ error }}

{% endif %} {% if image_data %}

Your Valentine's Day Card

Theme: {{ theme }}

Valentine's Day Card
{% endif %} """ @app.route('/', methods=['GET', 'POST']) def home(): if request.method == 'POST': theme = request.form.get('theme', '') if theme: try: # Add Valentine's context to the theme valentine_theme = f"Valentine's Day card with theme: {theme}, romantic, love, hearts" # Use the agent to generate the card image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) image = image_generation_tool(valentine_theme) # Convert PIL Image to base64 string buffered = BytesIO() image.save(buffered, format="PNG") img_str = base64.b64encode(buffered.getvalue()).decode() return render_template_string(html_template, image_data=img_str, theme=theme) except Exception as e: return render_template_string(html_template, error=f"Error: {str(e)}") return render_template_string(html_template) # Run the Flask app return app.run(debug=True, host='0.0.0.0', port=8080) # Run the modified launch function if __name__ == "__main__": modified_launch()