Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,161 +1,187 @@
|
|
| 1 |
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
| 2 |
import yaml
|
| 3 |
import logging
|
|
|
|
| 4 |
from tools.final_answer import FinalAnswerTool
|
| 5 |
from Gradio_UI import GradioUI
|
| 6 |
-
import traceback
|
| 7 |
|
| 8 |
-
# Set up
|
| 9 |
-
logging.basicConfig(level=logging.DEBUG
|
| 10 |
logger = logging.getLogger(__name__)
|
| 11 |
|
| 12 |
-
#
|
| 13 |
@tool
|
| 14 |
-
def
|
| 15 |
-
"""Generate an image
|
| 16 |
Args:
|
| 17 |
prompt: A detailed text description of the image to generate
|
| 18 |
"""
|
|
|
|
|
|
|
|
|
|
| 19 |
try:
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
except Exception as e:
|
| 33 |
-
logger.error(f"Image generation failed: {str(e)}")
|
| 34 |
-
logger.error(f"
|
| 35 |
-
return f"Error
|
| 36 |
|
| 37 |
-
# Initialize
|
| 38 |
final_answer = FinalAnswerTool()
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
custom_role_conversions=None,
|
| 48 |
-
)
|
| 49 |
-
logger.info("Primary model initialized successfully")
|
| 50 |
-
return model
|
| 51 |
-
except Exception as e:
|
| 52 |
-
logger.warning(f"Primary model failed: {e}")
|
| 53 |
-
try:
|
| 54 |
-
fallback_model = HfApiModel(
|
| 55 |
-
max_tokens=1024,
|
| 56 |
-
temperature=0.7,
|
| 57 |
-
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
|
| 58 |
-
custom_role_conversions=None,
|
| 59 |
-
)
|
| 60 |
-
logger.info("Fallback model initialized successfully")
|
| 61 |
-
return fallback_model
|
| 62 |
-
except Exception as e2:
|
| 63 |
-
logger.error(f"Fallback model also failed: {e2}")
|
| 64 |
-
raise
|
| 65 |
-
|
| 66 |
-
model = create_model()
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
]
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
# Test the tool
|
| 84 |
-
test_result = tool("test image")
|
| 85 |
-
logger.info(f"Tool test result type: {type(test_result)}")
|
| 86 |
-
|
| 87 |
-
return tool
|
| 88 |
-
|
| 89 |
-
except Exception as e:
|
| 90 |
-
logger.warning(f"Failed to load {description}: {e}")
|
| 91 |
-
continue
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
with open("prompts.yaml", 'r') as stream:
|
| 110 |
-
prompts = yaml.safe_load(stream)
|
| 111 |
-
logger.info("Loaded prompts from prompts.yaml")
|
| 112 |
-
return prompts
|
| 113 |
-
except Exception as e:
|
| 114 |
-
logger.warning(f"Failed to load prompts.yaml: {e}")
|
| 115 |
-
# Fallback prompts optimized for image generation
|
| 116 |
-
return {
|
| 117 |
-
"system": """You are an AI agent specialized in generating images from text descriptions.
|
| 118 |
-
When a user requests an image, use the enhanced_image_generator tool with a detailed, descriptive prompt.
|
| 119 |
-
Always provide clear, vivid descriptions for better image generation results.
|
| 120 |
-
If image generation fails, explain the issue and suggest alternative approaches.""",
|
| 121 |
-
"user": "Generate an image based on this description: {input}"
|
| 122 |
-
}
|
| 123 |
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
-
# Create
|
| 127 |
tools_list = [final_answer]
|
| 128 |
-
if
|
| 129 |
-
tools_list.append(
|
| 130 |
-
logger.info("
|
|
|
|
|
|
|
| 131 |
|
| 132 |
-
# Create agent with comprehensive configuration
|
| 133 |
agent = CodeAgent(
|
| 134 |
model=model,
|
| 135 |
tools=tools_list,
|
| 136 |
-
max_steps=
|
| 137 |
-
verbosity_level=2,
|
| 138 |
grammar=None,
|
| 139 |
planning_interval=None,
|
| 140 |
-
name="
|
| 141 |
-
description="AI agent
|
| 142 |
prompt_templates=prompt_templates
|
| 143 |
)
|
| 144 |
|
| 145 |
-
#
|
| 146 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
try:
|
| 148 |
-
logger.info("
|
| 149 |
-
logger.info(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
-
# Launch
|
|
|
|
| 152 |
GradioUI(agent).launch()
|
| 153 |
|
| 154 |
except Exception as e:
|
| 155 |
-
logger.error(f"
|
| 156 |
-
logger.error(f"
|
| 157 |
-
print(f"\nERROR: {e}")
|
| 158 |
-
print("Please check the logs above for detailed error information.")
|
| 159 |
|
| 160 |
if __name__ == "__main__":
|
| 161 |
-
|
|
|
|
| 1 |
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
| 2 |
import yaml
|
| 3 |
import logging
|
| 4 |
+
import traceback
|
| 5 |
from tools.final_answer import FinalAnswerTool
|
| 6 |
from Gradio_UI import GradioUI
|
|
|
|
| 7 |
|
| 8 |
+
# Set up comprehensive logging
|
| 9 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 10 |
logger = logging.getLogger(__name__)
|
| 11 |
|
| 12 |
+
# Create a diagnostic image generation tool
|
| 13 |
@tool
|
| 14 |
+
def diagnostic_image_generator(prompt: str) -> str:
|
| 15 |
+
"""Generate an image with comprehensive debugging and validation.
|
| 16 |
Args:
|
| 17 |
prompt: A detailed text description of the image to generate
|
| 18 |
"""
|
| 19 |
+
logger.info(f"=== DIAGNOSTIC IMAGE GENERATION START ===")
|
| 20 |
+
logger.info(f"Input prompt: {prompt}")
|
| 21 |
+
|
| 22 |
try:
|
| 23 |
+
# Check if we have a base tool
|
| 24 |
+
if not hasattr(diagnostic_image_generator, '_base_tool'):
|
| 25 |
+
logger.error("No base image generation tool attached")
|
| 26 |
+
return "Error: No image generation tool available"
|
| 27 |
|
| 28 |
+
base_tool = diagnostic_image_generator._base_tool
|
| 29 |
+
logger.info(f"Base tool type: {type(base_tool)}")
|
| 30 |
+
logger.info(f"Base tool: {base_tool}")
|
| 31 |
+
|
| 32 |
+
# Call the base tool
|
| 33 |
+
logger.info("Calling base image generation tool...")
|
| 34 |
+
result = base_tool(prompt)
|
| 35 |
+
|
| 36 |
+
# Analyze the result
|
| 37 |
+
logger.info(f"Raw result type: {type(result)}")
|
| 38 |
+
logger.info(f"Raw result: {result}")
|
| 39 |
+
|
| 40 |
+
# Check if it's an AgentImage
|
| 41 |
+
if hasattr(result, '__class__') and 'AgentImage' in str(type(result)):
|
| 42 |
+
logger.info("Result is an AgentImage")
|
| 43 |
+
logger.info(f"AgentImage attributes: {dir(result)}")
|
| 44 |
|
| 45 |
+
# Try to get image properties
|
| 46 |
+
try:
|
| 47 |
+
if hasattr(result, 'size'):
|
| 48 |
+
logger.info(f"Image size: {result.size}")
|
| 49 |
+
if hasattr(result, 'mode'):
|
| 50 |
+
logger.info(f"Image mode: {result.mode}")
|
| 51 |
+
if hasattr(result, 'width'):
|
| 52 |
+
logger.info(f"Image width: {result.width}")
|
| 53 |
+
if hasattr(result, 'height'):
|
| 54 |
+
logger.info(f"Image height: {result.height}")
|
| 55 |
+
if hasattr(result, 'format'):
|
| 56 |
+
logger.info(f"Image format: {result.format}")
|
| 57 |
+
if hasattr(result, 'show'):
|
| 58 |
+
logger.info("Image has show method")
|
| 59 |
+
if hasattr(result, 'save'):
|
| 60 |
+
logger.info("Image has save method")
|
| 61 |
+
|
| 62 |
+
except Exception as e:
|
| 63 |
+
logger.error(f"Error checking image properties: {e}")
|
| 64 |
+
|
| 65 |
+
# Try to validate the image
|
| 66 |
+
if result and hasattr(result, 'size'):
|
| 67 |
+
width, height = result.size if hasattr(result, 'size') else (0, 0)
|
| 68 |
+
if width > 0 and height > 0:
|
| 69 |
+
logger.info(f"✅ Valid image generated: {width}x{height}")
|
| 70 |
+
return result
|
| 71 |
+
else:
|
| 72 |
+
logger.error(f"❌ Invalid image size: {width}x{height}")
|
| 73 |
+
return "Error: Generated image has invalid size"
|
| 74 |
+
|
| 75 |
+
logger.info(f"=== DIAGNOSTIC IMAGE GENERATION END ===")
|
| 76 |
+
return result
|
| 77 |
+
|
| 78 |
except Exception as e:
|
| 79 |
+
logger.error(f"Image generation failed with exception: {str(e)}")
|
| 80 |
+
logger.error(f"Full traceback: {traceback.format_exc()}")
|
| 81 |
+
return f"Error: {str(e)}"
|
| 82 |
|
| 83 |
+
# Initialize components
|
| 84 |
final_answer = FinalAnswerTool()
|
| 85 |
|
| 86 |
+
# Create model
|
| 87 |
+
model = HfApiModel(
|
| 88 |
+
max_tokens=1024,
|
| 89 |
+
temperature=0.7,
|
| 90 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 91 |
+
custom_role_conversions=None,
|
| 92 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
# Try to load image generation tool with detailed diagnostics
|
| 95 |
+
logger.info("=== LOADING IMAGE GENERATION TOOL ===")
|
| 96 |
+
try:
|
| 97 |
+
# First, let's try the primary tool
|
| 98 |
+
logger.info("Loading agents-course/text-to-image...")
|
| 99 |
+
base_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 100 |
+
logger.info(f"✅ Tool loaded successfully: {type(base_tool)}")
|
|
|
|
| 101 |
|
| 102 |
+
# Test the tool directly
|
| 103 |
+
logger.info("Testing tool directly...")
|
| 104 |
+
test_result = base_tool("a simple red circle")
|
| 105 |
+
logger.info(f"Direct test result type: {type(test_result)}")
|
| 106 |
+
logger.info(f"Direct test result: {test_result}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
+
# Check if test result is valid
|
| 109 |
+
if hasattr(test_result, 'size'):
|
| 110 |
+
logger.info(f"Test image size: {test_result.size}")
|
| 111 |
+
if test_result.size == (0, 0):
|
| 112 |
+
logger.warning("⚠️ Test image has size 0x0 - tool may not be working properly")
|
| 113 |
+
else:
|
| 114 |
+
logger.info("✅ Test image has valid size")
|
| 115 |
+
|
| 116 |
+
# Attach to diagnostic tool
|
| 117 |
+
diagnostic_image_generator._base_tool = base_tool
|
| 118 |
+
image_tool_available = True
|
| 119 |
+
|
| 120 |
+
except Exception as e:
|
| 121 |
+
logger.error(f"❌ Failed to load image generation tool: {e}")
|
| 122 |
+
logger.error(f"Traceback: {traceback.format_exc()}")
|
| 123 |
+
image_tool_available = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
+
# Load prompts
|
| 126 |
+
try:
|
| 127 |
+
with open("prompts.yaml", 'r') as stream:
|
| 128 |
+
prompt_templates = yaml.safe_load(stream)
|
| 129 |
+
except:
|
| 130 |
+
prompt_templates = {
|
| 131 |
+
"system": "You are an AI assistant that can generate images. Use the diagnostic_image_generator tool to create images from text descriptions.",
|
| 132 |
+
"user": "{input}"
|
| 133 |
+
}
|
| 134 |
|
| 135 |
+
# Create agent
|
| 136 |
tools_list = [final_answer]
|
| 137 |
+
if image_tool_available:
|
| 138 |
+
tools_list.append(diagnostic_image_generator)
|
| 139 |
+
logger.info("✅ Diagnostic image generator added to agent")
|
| 140 |
+
else:
|
| 141 |
+
logger.error("❌ No image generation tool available")
|
| 142 |
|
|
|
|
| 143 |
agent = CodeAgent(
|
| 144 |
model=model,
|
| 145 |
tools=tools_list,
|
| 146 |
+
max_steps=3,
|
| 147 |
+
verbosity_level=2,
|
| 148 |
grammar=None,
|
| 149 |
planning_interval=None,
|
| 150 |
+
name="DiagnosticImageAgent",
|
| 151 |
+
description="AI agent with comprehensive image generation diagnostics",
|
| 152 |
prompt_templates=prompt_templates
|
| 153 |
)
|
| 154 |
|
| 155 |
+
# Create a simple test function
|
| 156 |
+
def test_image_generation():
|
| 157 |
+
"""Test image generation directly"""
|
| 158 |
+
logger.info("=== DIRECT IMAGE GENERATION TEST ===")
|
| 159 |
+
if image_tool_available:
|
| 160 |
+
try:
|
| 161 |
+
result = diagnostic_image_generator("a red apple on a white background")
|
| 162 |
+
logger.info(f"Direct test completed. Result: {result}")
|
| 163 |
+
except Exception as e:
|
| 164 |
+
logger.error(f"Direct test failed: {e}")
|
| 165 |
+
else:
|
| 166 |
+
logger.error("Cannot test - no image tool available")
|
| 167 |
+
|
| 168 |
+
# Launch with diagnostics
|
| 169 |
+
def launch_with_diagnostics():
|
| 170 |
try:
|
| 171 |
+
logger.info("=== LAUNCHING DIAGNOSTIC AGENT ===")
|
| 172 |
+
logger.info(f"Tools available: {len(tools_list)}")
|
| 173 |
+
logger.info(f"Image tool available: {image_tool_available}")
|
| 174 |
+
|
| 175 |
+
# Run a quick test
|
| 176 |
+
test_image_generation()
|
| 177 |
|
| 178 |
+
# Launch the UI
|
| 179 |
+
logger.info("Starting Gradio UI...")
|
| 180 |
GradioUI(agent).launch()
|
| 181 |
|
| 182 |
except Exception as e:
|
| 183 |
+
logger.error(f"Launch failed: {e}")
|
| 184 |
+
logger.error(f"Traceback: {traceback.format_exc()}")
|
|
|
|
|
|
|
| 185 |
|
| 186 |
if __name__ == "__main__":
|
| 187 |
+
launch_with_diagnostics()
|