Spaces:
Sleeping
Sleeping
anime image generator
Browse files
app.py
CHANGED
|
@@ -1,26 +1,69 @@
|
|
| 1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
-
from tools.final_answer import FinalAnswerTool
|
| 7 |
-
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
-
#
|
| 11 |
@tool
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 24 |
Args:
|
| 25 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 26 |
"""
|
|
@@ -33,37 +76,86 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
|
|
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 41 |
-
|
| 42 |
model = HfApiModel(
|
| 43 |
-
max_tokens=2096,
|
| 44 |
-
temperature=0.
|
| 45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 46 |
-
custom_role_conversions=None,
|
| 47 |
)
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 52 |
-
|
| 53 |
with open("prompts.yaml", 'r') as stream:
|
| 54 |
prompt_templates = yaml.safe_load(stream)
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=
|
| 59 |
-
max_steps=
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
| 62 |
planning_interval=None,
|
| 63 |
-
name=
|
| 64 |
-
description=
|
| 65 |
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
+
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
|
| 9 |
+
# Enhanced custom tool for anime image generation
|
| 10 |
@tool
|
| 11 |
+
def anime_image_generator(description: str, style: str = "anime") -> str:
|
| 12 |
+
"""A tool that generates anime-style images based on user descriptions.
|
| 13 |
+
|
| 14 |
Args:
|
| 15 |
+
description: Detailed description of the anime image to generate
|
| 16 |
+
style: Style modifier (default: "anime", options: "anime", "manga", "kawaii", "realistic_anime")
|
| 17 |
"""
|
| 18 |
+
try:
|
| 19 |
+
# Enhance the prompt with anime-specific keywords
|
| 20 |
+
anime_keywords = {
|
| 21 |
+
"anime": "anime style, high quality, detailed, vibrant colors, japanese animation style",
|
| 22 |
+
"manga": "manga style, black and white, detailed lineart, dramatic shading",
|
| 23 |
+
"kawaii": "kawaii style, cute, adorable, pastel colors, chibi proportions",
|
| 24 |
+
"realistic_anime": "realistic anime style, detailed face, expressive eyes, high resolution"
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
style_prompt = anime_keywords.get(style, anime_keywords["anime"])
|
| 28 |
+
enhanced_description = f"{description}, {style_prompt}, masterpiece, best quality"
|
| 29 |
+
|
| 30 |
+
return f"Generated anime image with description: '{enhanced_description}' in {style} style"
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return f"Error generating anime image: {str(e)}"
|
| 33 |
+
|
| 34 |
+
@tool
|
| 35 |
+
def analyze_image_description(user_input: str) -> str:
|
| 36 |
+
"""A tool that analyzes and enhances user input for better anime image generation.
|
| 37 |
+
|
| 38 |
+
Args:
|
| 39 |
+
user_input: Raw user description of desired image
|
| 40 |
+
"""
|
| 41 |
+
try:
|
| 42 |
+
# Common anime elements to suggest or enhance
|
| 43 |
+
anime_elements = [
|
| 44 |
+
"character appearance", "clothing style", "background setting",
|
| 45 |
+
"mood/atmosphere", "art style", "color palette", "special effects"
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
analysis = f"Analyzing description: '{user_input}'\n"
|
| 49 |
+
analysis += "Suggested enhancements for anime generation:\n"
|
| 50 |
+
|
| 51 |
+
# Basic analysis and suggestions
|
| 52 |
+
if "character" in user_input.lower() or "person" in user_input.lower():
|
| 53 |
+
analysis += "- Consider adding details about hair color, eye color, clothing style\n"
|
| 54 |
+
if "background" not in user_input.lower():
|
| 55 |
+
analysis += "- Consider adding background setting (school, forest, city, etc.)\n"
|
| 56 |
+
if "color" not in user_input.lower():
|
| 57 |
+
analysis += "- Consider specifying color palette preferences\n"
|
| 58 |
+
|
| 59 |
+
return analysis
|
| 60 |
+
except Exception as e:
|
| 61 |
+
return f"Error analyzing description: {str(e)}"
|
| 62 |
|
| 63 |
@tool
|
| 64 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 65 |
"""A tool that fetches the current local time in a specified timezone.
|
| 66 |
+
|
| 67 |
Args:
|
| 68 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 69 |
"""
|
|
|
|
| 76 |
except Exception as e:
|
| 77 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 78 |
|
| 79 |
+
@tool
|
| 80 |
+
def enhance_anime_prompt(base_description: str, character_details: str = "", background: str = "", mood: str = "") -> str:
|
| 81 |
+
"""A tool that creates a comprehensive anime image prompt by combining various elements.
|
| 82 |
+
|
| 83 |
+
Args:
|
| 84 |
+
base_description: Basic description of the desired image
|
| 85 |
+
character_details: Optional character-specific details (hair, eyes, clothing, etc.)
|
| 86 |
+
background: Optional background setting description
|
| 87 |
+
mood: Optional mood or atmosphere description
|
| 88 |
+
"""
|
| 89 |
+
try:
|
| 90 |
+
enhanced_prompt = base_description
|
| 91 |
+
|
| 92 |
+
if character_details:
|
| 93 |
+
enhanced_prompt += f", {character_details}"
|
| 94 |
+
if background:
|
| 95 |
+
enhanced_prompt += f", background: {background}"
|
| 96 |
+
if mood:
|
| 97 |
+
enhanced_prompt += f", mood: {mood}"
|
| 98 |
+
|
| 99 |
+
# Add quality and style modifiers
|
| 100 |
+
enhanced_prompt += ", anime style, high quality, detailed artwork, vibrant colors, professional illustration"
|
| 101 |
+
|
| 102 |
+
return f"Enhanced anime prompt: {enhanced_prompt}"
|
| 103 |
+
except Exception as e:
|
| 104 |
+
return f"Error enhancing prompt: {str(e)}"
|
| 105 |
|
| 106 |
+
# Initialize tools
|
| 107 |
final_answer = FinalAnswerTool()
|
| 108 |
|
| 109 |
+
# Model configuration - using Qwen for better code generation and reasoning
|
|
|
|
|
|
|
| 110 |
model = HfApiModel(
|
| 111 |
+
max_tokens=2096,
|
| 112 |
+
temperature=0.7, # Slightly higher for more creative image descriptions
|
| 113 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 114 |
+
custom_role_conversions=None,
|
| 115 |
)
|
| 116 |
|
| 117 |
+
# Import image generation tool from Hub
|
| 118 |
+
try:
|
| 119 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 120 |
+
print("Successfully loaded image generation tool")
|
| 121 |
+
except Exception as e:
|
| 122 |
+
print(f"Warning: Could not load image generation tool: {e}")
|
| 123 |
+
image_generation_tool = None
|
| 124 |
|
| 125 |
+
# Load prompt templates
|
|
|
|
|
|
|
| 126 |
with open("prompts.yaml", 'r') as stream:
|
| 127 |
prompt_templates = yaml.safe_load(stream)
|
| 128 |
+
|
| 129 |
+
# Create the agent with anime-focused tools
|
| 130 |
+
tools_list = [
|
| 131 |
+
final_answer,
|
| 132 |
+
anime_image_generator,
|
| 133 |
+
analyze_image_description,
|
| 134 |
+
enhance_anime_prompt,
|
| 135 |
+
get_current_time_in_timezone
|
| 136 |
+
]
|
| 137 |
+
|
| 138 |
+
# Add the image generation tool if it loaded successfully
|
| 139 |
+
if image_generation_tool:
|
| 140 |
+
tools_list.append(image_generation_tool)
|
| 141 |
+
|
| 142 |
agent = CodeAgent(
|
| 143 |
model=model,
|
| 144 |
+
tools=tools_list,
|
| 145 |
+
max_steps=8, # Increased steps for more complex image generation workflows
|
| 146 |
verbosity_level=1,
|
| 147 |
grammar=None,
|
| 148 |
planning_interval=None,
|
| 149 |
+
name="AnimeImageGenerator",
|
| 150 |
+
description="An AI agent specialized in generating anime-style images based on user descriptions",
|
| 151 |
prompt_templates=prompt_templates
|
| 152 |
)
|
| 153 |
|
| 154 |
+
# Launch the Gradio interface
|
| 155 |
+
if __name__ == "__main__":
|
| 156 |
+
print("Starting Anime Image Generator...")
|
| 157 |
+
print("Available tools:")
|
| 158 |
+
for tool in tools_list:
|
| 159 |
+
print(f"- {tool.name}")
|
| 160 |
+
|
| 161 |
+
GradioUI(agent).launch()
|