This system provides a unified interface for generating AI images across multiple providers, with automatic fallback and load balancing capabilities. Currently supports:
- Runware - High-quality, fast image generation (paid)
- AI Horde - Free, community-driven image generation
python setup_ai_horde.pyEdit config/providers.json:
{
"providers": {
"runware": {
"api_key": "your_runware_api_key_here"
},
"ai_horde": {
"api_key": "your_ai_horde_api_key_here"
}
}
}export RUNWARE_API_KEY="your_runware_api_key"
export AI_HORDE_API_KEY="your_ai_horde_api_key"python test_multi_provider.pypython nft_collection_generator.py --theme pepe_frog --subject "Pepe the Frog" --count 10- ProviderManager - Orchestrates multiple AI providers
- AIProvider - Abstract base class for all providers
- RunwareProvider - Runware API implementation
- AIHordeProvider - AI Horde API implementation
- GenerationRequest - Standardized request format
- GenerationResult - Standardized result format
{
"primary_provider": "runware",
"fallback_providers": ["ai_horde"],
"providers": {
"runware": {
"enabled": true,
"api_key": "your_key",
"priority": 1,
"max_concurrent": 4,
"models": {
"default": "runware:97@2",
"anime": "civitai:102438@133677"
}
},
"ai_horde": {
"enabled": true,
"api_key": "your_key",
"priority": 2,
"max_concurrent": 2,
"models": {
"default": "stable_diffusion",
"anime": "stable_diffusion_xl"
}
}
}
}from ai_providers import ProviderManager, GenerationRequest
# Initialize with config
config = {
"primary_provider": "ai_horde",
"ai_horde": {"api_key": "your_key"}
}
manager = ProviderManager(config)
# Generate single image
request = GenerationRequest(
prompt="A cute cat wearing a wizard hat",
width=512,
height=512,
steps=20
)
result = await manager.generate_single_image(request)
if result.success:
print(f"Generated: {result.local_path}")from nft_collection_generator import NFTCollectionGenerator
# Initialize with provider config
generator = NFTCollectionGenerator()
# Generate collection
collection_path = await generator.generate_collection(
theme="pepe_frog",
subject="Pepe the Frog",
collection_name="My Pepe Collection",
count=10,
ollama_model="llama3.2:latest"
)- Speed: Sub-second inference times
- Quality: High-quality models (FLUX, Stable Diffusion)
- Cost: Pay-per-use credits
- Models: Custom models via CivitAI
- Features: Upscaling, background removal, ControlNet
- Cost: Free (community-driven)
- Models: 166+ models available
- Speed: Variable (depends on available workers)
- Features: Text-to-image, image-to-image, inpainting
- Community: Volunteer workers
The system automatically handles provider failures:
- Primary Provider: Tries the configured primary provider first
- Fallback Providers: If primary fails, tries fallback providers in order
- Error Handling: Graceful degradation with detailed error reporting
- Retry Logic: Configurable retry attempts per provider
{
"fallback_strategy": {
"enable_fallback": true,
"fallback_on_credits": true,
"fallback_on_error": true,
"fallback_on_timeout": true,
"max_fallback_attempts": 2
}
}The system tracks comprehensive statistics:
# Provider usage statistics
stats = {
"provider_usage": {
"runware": 5,
"ai_horde": 3
},
"generation_times": [2.1, 1.8, 3.2],
"failed_generations": 1,
"total_images_generated": 8
}{
"global_settings": {
"max_retries": 3,
"retry_delay": 5,
"timeout": 300,
"image_format": "webp",
"image_quality": 95,
"save_metadata": true,
"output_directory": "generated_images"
}
}{
"rate_limit": {
"requests_per_minute": 60,
"requests_per_hour": 1000
}
}{
"load_balancing": {
"enable_load_balancing": false,
"strategy": "round_robin",
"weight_runware": 70,
"weight_ai_horde": 30
}
}-
"All providers failed"
- Check API keys are valid
- Verify network connectivity
- Check provider status pages
-
"insufficientCredits" (Runware)
- Add credits to your Runware account
- Switch to AI Horde as primary provider
-
"No workers available" (AI Horde)
- Wait a few minutes and retry
- Check AI Horde status: https://aihorde.net/status
-
Slow generation times
- AI Horde depends on volunteer workers
- Consider using Runware for faster results
Enable detailed logging:
import logging
logging.basicConfig(level=logging.DEBUG)# Check all provider status
credits = await manager.check_all_credits()
models = await manager.get_available_models()runware/
βββ ai_providers.py # Multi-provider system
βββ config/
β βββ providers.json # Provider configuration
βββ nft_collection_generator.py # NFT collection generator
βββ test_multi_provider.py # Test suite
βββ setup_ai_horde.py # AI Horde setup script
βββ generated_images/ # Output directory
βββ 001.webp
βββ 002.webp
βββ ...
To add a new provider:
-
Create a new class inheriting from
AIProvider -
Implement required methods:
connect()disconnect()generate_single_image()generate_batch()get_available_models()check_credits()
-
Add provider configuration to
providers.json -
Update
ProviderManagerto include the new provider
# Generate multiple images efficiently
requests = [
GenerationRequest(prompt="Cat 1", width=512, height=512),
GenerationRequest(prompt="Cat 2", width=512, height=512),
GenerationRequest(prompt="Cat 3", width=512, height=512)
]
results = await manager.generate_batch(requests)# Use specific models per provider
request = GenerationRequest(
prompt="Anime style cat",
model="stable_diffusion_xl", # AI Horde model
width=1024,
height=1024
)- API Key Security: Use environment variables for production
- Rate Limiting: Respect provider rate limits
- Error Handling: Always check
result.success - Resource Management: Disconnect providers when done
- Monitoring: Track usage and costs
- Fallback Strategy: Configure multiple providers for reliability
- β Added AI Horde provider
- β Implemented fallback strategy
- β Added provider management
- β WEBP image optimization
- β Comprehensive error handling
- β Usage statistics tracking