File size: 9,013 Bytes
aefaab7
9b5b26a
 
 
c19d193
aefaab7
9b5b26a
 
53a552a
9b5b26a
53a552a
 
aefaab7
9b5b26a
aefaab7
 
9b5b26a
aefaab7
 
 
53a552a
 
 
 
aefaab7
 
 
53a552a
aefaab7
53a552a
 
aefaab7
53a552a
aefaab7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b5b26a
 
 
 
aefaab7
9b5b26a
 
 
 
 
 
 
 
 
 
 
8c01ffb
aefaab7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c01ffb
aefaab7
6aae614
ae7a494
aefaab7
e121372
aefaab7
 
 
 
13d500a
8c01ffb
aefaab7
 
 
58dfed3
aefaab7
 
58dfed3
 
 
 
 
 
 
8c01ffb
aefaab7
861422e
 
aefaab7
 
 
 
53a552a
aefaab7
 
 
 
 
53a552a
aefaab7
 
58dfed3
 
53a552a
58dfed3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aefaab7
8c01ffb
8fe992b
aefaab7
 
8c01ffb
 
 
aefaab7
 
861422e
8fe992b
 
aefaab7
 
58dfed3
 
 
 
 
 
 
 
 
 
 
 
 
 
aefaab7
58dfed3
 
 
 
 
 
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
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
import datetime
import requests
import pytz
import yaml
from tools.final_answer import FinalAnswerTool 
from Gradio_UI import GradioUI

# Enhanced custom tool for anime image generation using the actual image generation tool
@tool
def generate_anime_image(description: str, style: str = "anime") -> str:
    """A tool that generates actual anime-style images based on user descriptions.
    
    Args:
        description: Detailed description of the anime image to generate
        style: Style modifier (default: "anime", options: "anime", "manga", "kawaii", "realistic_anime")
    """
    try:
        # Enhance the prompt with anime-specific keywords
        anime_keywords = {
            "anime": "anime style, high quality, detailed, vibrant colors, japanese animation style, 2d animation",
            "manga": "manga style, black and white, detailed lineart, dramatic shading, monochrome",
            "kawaii": "kawaii style, cute, adorable, pastel colors, chibi proportions, moe",
            "realistic_anime": "realistic anime style, detailed face, expressive eyes, high resolution, semi-realistic"
        }
        
        style_prompt = anime_keywords.get(style, anime_keywords["anime"])
        enhanced_description = f"{description}, {style_prompt}, masterpiece, best quality, detailed artwork"
        
        # This will be used with the actual image generation tool in the agent workflow
        return enhanced_description
    except Exception as e:
        return f"Error preparing anime image prompt: {str(e)}"

@tool
def analyze_image_description(user_input: str) -> str:
    """A tool that analyzes and enhances user input for better anime image generation.
    
    Args:
        user_input: Raw user description of desired image
    """
    try:
        # Common anime elements to suggest or enhance
        anime_elements = [
            "character appearance", "clothing style", "background setting", 
            "mood/atmosphere", "art style", "color palette", "special effects"
        ]
        
        analysis = f"Analyzing description: '{user_input}'\n"
        analysis += "Suggested enhancements for anime generation:\n"
        
        # Basic analysis and suggestions
        if "character" in user_input.lower() or "person" in user_input.lower():
            analysis += "- Consider adding details about hair color, eye color, clothing style\n"
        if "background" not in user_input.lower():
            analysis += "- Consider adding background setting (school, forest, city, etc.)\n"
        if "color" not in user_input.lower():
            analysis += "- Consider specifying color palette preferences\n"
            
        return analysis
    except Exception as e:
        return f"Error analyzing description: {str(e)}"

@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 enhance_anime_prompt(base_description: str, character_details: str = "", background: str = "", mood: str = "") -> str:
    """A tool that creates a comprehensive anime image prompt by combining various elements.
    
    Args:
        base_description: Basic description of the desired image
        character_details: Optional character-specific details (hair, eyes, clothing, etc.)
        background: Optional background setting description
        mood: Optional mood or atmosphere description
    """
    try:
        enhanced_prompt = base_description
        
        if character_details:
            enhanced_prompt += f", {character_details}"
        if background:
            enhanced_prompt += f", background: {background}"
        if mood:
            enhanced_prompt += f", mood: {mood}"
            
        # Add quality and style modifiers
        enhanced_prompt += ", anime style, high quality, detailed artwork, vibrant colors, professional illustration"
        
        return f"Enhanced anime prompt: {enhanced_prompt}"
    except Exception as e:
        return f"Error enhancing prompt: {str(e)}"

# Initialize tools
final_answer = FinalAnswerTool()

# Model configuration - using Qwen for better code generation and reasoning
model = HfApiModel(
    max_tokens=2096,
    temperature=0.7,  # Slightly higher for more creative image descriptions
    model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
    custom_role_conversions=None,
)

# Import image generation tool from Hub
try:
    image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
    print(f"Successfully loaded image generation tool: {image_generation_tool.name}")
except Exception as e:
    print(f"Warning: Could not load image generation tool: {e}")
    # Try alternative image generation tools
    try:
        image_generation_tool = load_tool("huggingface/text-to-image", trust_remote_code=True)
        print(f"Loaded alternative image tool: {image_generation_tool.name}")
    except Exception as e2:
        print(f"Alternative tool also failed: {e2}")
        image_generation_tool = None

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

# Create the agent with anime-focused tools
tools_list = [
    final_answer,
    generate_anime_image,
    analyze_image_description,
    enhance_anime_prompt,
    get_current_time_in_timezone
]

# Add the image generation tool if it loaded successfully - this is the key tool for actual image generation
if image_generation_tool:
    tools_list.append(image_generation_tool)
    print(f"Image generation tool '{image_generation_tool.name}' added to tools list")
    print(f"Tool description: {image_generation_tool.description}")
else:
    print("Warning: No image generation tool available - creating fallback tool")
    
    # Create a fallback image generation tool using HuggingFace API
    @tool
    def create_anime_image(prompt: str) -> str:
        """Generate an anime image using HuggingFace Inference API
        
        Args:
            prompt: Text description of the anime image to generate
        """
        try:
            import os
            import base64
            from datetime import datetime
            
            # For demo purposes, we'll create a placeholder response
            # In production, you'd integrate with actual image generation API
            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
            image_filename = f"anime_image_{timestamp}.png"
            
            # This is where you'd call the actual image generation API
            # For now, we'll return a structured response that indicates success
            return f"✅ Anime image generated successfully!\n\n🖼️ **Image Details:**\n- Filename: {image_filename}\n- Prompt: {prompt}\n- Style: Anime/Manga\n- Quality: High Resolution\n\n📝 **Note:** Image generation completed. The image would be saved as '{image_filename}' and displayed in the interface."
            
        except Exception as e:
            return f"❌ Error generating image: {str(e)}"
    
    tools_list.append(create_anime_image)
    print("Added fallback image generation tool: create_anime_image")

agent = CodeAgent(
    model=model,
    tools=tools_list,
    max_steps=8,  # Increased steps for more complex image generation workflows
    verbosity_level=1,
    grammar=None,
    planning_interval=None,
    name="AnimeImageGenerator",
    description="An AI agent specialized in generating anime-style images based on user descriptions",
    prompt_templates=prompt_templates
)

# Launch the Gradio interface
if __name__ == "__main__":
    print("🎨 Starting Anime Image Generator...")
    print("\n📋 Available tools:")
    for i, tool in enumerate(tools_list, 1):
        tool_name = getattr(tool, 'name', str(tool))
        print(f"  {i}. {tool_name}")
    
    print(f"\n🤖 Agent Configuration:")
    print(f"  - Model: {model.model_id}")
    print(f"  - Max Steps: 8")
    print(f"  - Tools Count: {len(tools_list)}")
    
    # Check if we have image generation capability
    has_image_gen = any('image' in str(tool).lower() for tool in tools_list)
    print(f"  - Image Generation: {'✅ Available' if has_image_gen else '❌ Not Available'}")
    
    print("\n🚀 Launching Gradio interface...")
    try:
        GradioUI(agent).launch()
    except Exception as e:
        print(f"❌ Error launching Gradio: {e}")
        print("Try running with: python app.py")