Spaces:
Paused
Paused
File size: 3,402 Bytes
e993e6d |
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 |
#!/usr/bin/env python3
"""
Test script to verify view_session functionality
"""
import os
import shutil
from pathlib import Path
from PIL import Image
import io
def create_test_session():
"""Create a test session with sample images for testing view_session"""
print("π§ͺ Creating test session for view_session testing...")
# Change to cache directory
cache_dir = Path(".cache")
if not cache_dir.exists():
print("β .cache directory not found. Run 'python app.py' first.")
return False
os.chdir(cache_dir)
# Create Generated directory if it doesn't exist
generated_dir = Path("Generated")
generated_dir.mkdir(exist_ok=True)
# Create test session directory
test_timestamp = "20250924_090627" # Same as your example
session_dir = generated_dir / f"session_{test_timestamp}"
# Remove existing test session if it exists
if session_dir.exists():
shutil.rmtree(session_dir)
session_dir.mkdir(exist_ok=True)
print(f"β
Created test session directory: {session_dir}")
# Create sample images
test_images = [
("generated_realistic_20250924_090627.jpg", (512, 512, 'red')),
("generated_artistic_20250924_090627.jpg", (512, 512, 'blue')),
("generated_vintage_20250924_090627.jpg", (512, 512, 'green')),
("webcam_input_20250924_090627.jpg", (512, 512, 'yellow'))
]
for filename, (width, height, color) in test_images:
try:
# Create a simple colored image
if color == 'red':
img = Image.new('RGB', (width, height), (255, 100, 100))
elif color == 'blue':
img = Image.new('RGB', (width, height), (100, 100, 255))
elif color == 'green':
img = Image.new('RGB', (width, height), (100, 255, 100))
else: # yellow
img = Image.new('RGB', (width, height), (255, 255, 100))
# Add some text to identify the image
from PIL import ImageDraw, ImageFont
draw = ImageDraw.Draw(img)
try:
# Try to use a better font, fallback to default if not available
font = ImageFont.truetype("/System/Library/Fonts/Arial.ttf", 24)
except:
font = ImageFont.load_default()
style_name = filename.replace('.jpg', '').replace(f'_{test_timestamp}', '').replace('generated_', '').replace('webcam_input', 'Input').title()
draw.text((50, 50), f"{style_name}\nTest Image", fill='white', font=font)
draw.text((50, 400), f"{filename}", fill='white', font=font)
# Save the image
img_path = session_dir / filename
img.save(img_path, 'JPEG', quality=90)
print(f"β
Created test image: {filename}")
except Exception as e:
print(f"β Failed to create {filename}: {e}")
print(f"\nπ― Test session created successfully!")
print(f"π Session directory: {session_dir.absolute()}")
print(f"π Test URL: http://localhost:7860/view_session/{test_timestamp}")
print(f"π Files created: {len(list(session_dir.glob('*.jpg')))} images")
# Go back to main directory
os.chdir("..")
return True
if __name__ == "__main__":
create_test_session() |