Spaces:
Runtime error
Runtime error
File size: 5,948 Bytes
3310a4f 9b5b26a c19d193 f4809cf 6aae614 9b5b26a 3310a4f 9b5b26a 8c01ffb 363366b f4809cf 363366b f4809cf 363366b f4809cf 363366b f4809cf 363366b 3310a4f 6aae614 3310a4f ae7a494 6b692d0 f4809cf a4710bb f4809cf 0bd130c a4710bb f4809cf a4710bb 6b692d0 f4809cf 6b692d0 0bd130c f4809cf 0bd130c a4710bb 0bd130c a4710bb 0bd130c f4809cf 0bd130c f4809cf 0bd130c f4809cf 363366b e121372 3310a4f 363366b 3310a4f 13d500a 8c01ffb 363366b 861422e 9b5b26a 8c01ffb 8fe992b 363366b f4809cf 0bd130c 363366b 8c01ffb 861422e 8fe992b 363366b | 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 | from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
import datetime
import requests
import pytz
import yaml
import tempfile
import os
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI
@tool
def my_custom_tool(arg1: str, arg2: int) -> str:
"""A tool that does nothing yet
Args:
arg1: the first argument
arg2: the second argument
"""
return "What magic will you build ?"
@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:
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)}"
@tool
def generate_and_save_image(prompt: str, filename: str = "generated_image.png") -> str:
"""Generate an image and save it to file for better display
Args:
prompt: Description of the image to generate
filename: Name of the file to save (optional)
"""
try:
# Verwende das geladene image_generation_tool
image = image_generation_tool(prompt=prompt)
# Speichere in temporärem Verzeichnis für bessere Organisation
temp_dir = tempfile.gettempdir()
full_path = os.path.join(temp_dir, filename)
# Speichere das Bild
image.save(full_path)
# Erstelle auch eine Kopie im aktuellen Verzeichnis für einfachen Zugriff
image.save(filename)
return f"Image generated and saved as {filename} (also available at {full_path}). You can download it from the current directory."
except Exception as e:
return f"Error generating image: {str(e)}"
# Load tools
final_answer = FinalAnswerTool()
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
# Debug: Prüfe den Namen des geladenen Tools
print(f"Loaded image tool name: {image_generation_tool.name}")
# Erstelle einen Enhanced Image Generator mit Download-Funktionalität
@tool
def enhanced_image_generator(prompt: str, filename: str = None) -> str:
"""Enhanced image generator that creates images and provides download information
Args:
prompt: Text description of the image to generate
filename: Optional custom filename (without extension)
"""
try:
image = image_generation_tool(prompt=prompt)
# Generiere Dateinamen falls nicht angegeben
if filename is None:
timestamp = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
filename = f"generated_image_{timestamp}"
# Stelle sicher dass Dateiname .png Extension hat
if not filename.endswith('.png'):
filename += '.png'
# Speichere das Bild für Download
image.save(filename)
# Gib eine klare Download-Anweisung zurück
return f"✅ Image generated successfully!\n📁 File saved as: {filename}\n💡 You can find and download the image file '{filename}' from your current directory.\n🎨 Prompt used: '{prompt}'"
except Exception as e:
return f"❌ Error generating image: {str(e)}"
@tool
def quick_image_generator(prompt: str) -> str:
"""Quick image generator with automatic filename
Args:
prompt: Text description of the image to generate
"""
try:
image = image_generation_tool(prompt=prompt)
# Automatischer Dateiname basierend auf Prompt (erste 3 Wörter)
words = prompt.split()[:3]
safe_name = "_".join(word.lower().replace(",", "").replace(".", "") for word in words)
timestamp = datetime.datetime.now().strftime('%H%M%S')
filename = f"{safe_name}_{timestamp}.png"
# Speichere das Bild
image.save(filename)
return f"🎨 Image created and saved as '{filename}'! You can download it from your file directory. Generated from: '{prompt}'"
except Exception as e:
return f"❌ Error generating image: {str(e)}"
# Test das Tool beim Start (optional - kann entfernt werden)
print("Testing image generation tool...")
try:
test_result = image_generation_tool(prompt="test")
print(f"Image tool loaded successfully: {type(test_result)}")
except Exception as e:
print(f"Warning: Image tool test failed: {e}")
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # it is possible that this model may be overloaded
custom_role_conversions=None,
)
# Load system prompt from prompt.yaml file
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
agent = CodeAgent(
model=model,
tools=[
final_answer,
image_generation_tool, # Das Original-Tool
enhanced_image_generator, # Tool mit besseren Download-Infos
quick_image_generator, # Schnelle Bildgenerierung mit automatischen Namen
get_current_time_in_timezone,
my_custom_tool
],
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name=None,
description=None,
prompt_templates=prompt_templates
)
# Debugging Info
print("Agent initialized with tools:")
for tool in agent.tools:
print(f"- {tool.name if hasattr(tool, 'name') else str(tool)}")
print("\nStarting Gradio UI...")
GradioUI(agent).launch()
# Beispiel für direkten Test (kann auskommentiert werden):
"""
# Direkter Test ohne UI:
if __name__ == "__main__":
print("Testing agent directly...")
response = agent.run("Generiere ein Bild eines Fisches")
print(f"Agent response: {response}")
""" |