Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,43 +2,120 @@ import os
|
|
| 2 |
import sys
|
| 3 |
import subprocess
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
print("
|
| 10 |
-
|
| 11 |
-
print(f"
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
if os.getcwd() not in sys.path:
|
| 17 |
-
sys.path.insert(0, os.getcwd())
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
try:
|
| 20 |
import gradio as gr
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
import torch
|
|
|
|
| 23 |
from diffusers import CosmosTextToImagePipeline
|
|
|
|
| 24 |
import random
|
| 25 |
-
print("
|
|
|
|
| 26 |
except ImportError as e:
|
| 27 |
-
print(f"Import
|
| 28 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
sys.exit(1)
|
| 30 |
|
| 31 |
# Available checkpoints: nvidia/Cosmos-Predict2-2B-Text2Image, nvidia/Cosmos-Predict2-14B-Text2Image
|
| 32 |
model_id = "diffusers-internal-dev/ct2i-14B"
|
| 33 |
|
| 34 |
# Load the pipeline once to avoid repeated loading
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
pipe.to("cuda")
|
| 38 |
-
print("Pipeline loaded successfully")
|
| 39 |
-
except Exception as e:
|
| 40 |
-
print(f"Error loading pipeline: {e}")
|
| 41 |
-
sys.exit(1)
|
| 42 |
|
| 43 |
@spaces.GPU
|
| 44 |
def generate_image(prompt, negative_prompt, seed, randomize_seed):
|
|
|
|
| 2 |
import sys
|
| 3 |
import subprocess
|
| 4 |
|
| 5 |
+
# Debug: Print environment information
|
| 6 |
+
def print_env_info():
|
| 7 |
+
print("=== ENVIRONMENT DEBUG INFO ===")
|
| 8 |
+
print(f"Python executable: {sys.executable}")
|
| 9 |
+
print(f"Python version: {sys.version}")
|
| 10 |
+
print(f"Current working directory: {os.getcwd()}")
|
| 11 |
+
print(f"Python path: {sys.path[:3]}...") # First 3 entries
|
| 12 |
+
|
| 13 |
+
# Check if we can find pip
|
| 14 |
+
try:
|
| 15 |
+
pip_result = subprocess.run([sys.executable, '-m', 'pip', '--version'],
|
| 16 |
+
capture_output=True, text=True)
|
| 17 |
+
print(f"Pip version: {pip_result.stdout.strip()}")
|
| 18 |
+
except Exception as e:
|
| 19 |
+
print(f"Pip check failed: {e}")
|
| 20 |
+
|
| 21 |
+
# List contents of current directory
|
| 22 |
+
print(f"Directory contents: {os.listdir('.')}")
|
| 23 |
+
if os.path.exists('diffusers_repo'):
|
| 24 |
+
print("β diffusers_repo found")
|
| 25 |
+
else:
|
| 26 |
+
print("β diffusers_repo NOT found")
|
| 27 |
+
print("===============================")
|
| 28 |
|
| 29 |
+
print_env_info()
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# Install diffusers from local repository
|
| 32 |
+
def install_diffusers():
|
| 33 |
+
try:
|
| 34 |
+
# Change to diffusers repo directory
|
| 35 |
+
original_dir = os.getcwd()
|
| 36 |
+
|
| 37 |
+
if not os.path.exists('diffusers_repo'):
|
| 38 |
+
print("ERROR: diffusers_repo directory not found!")
|
| 39 |
+
return False
|
| 40 |
+
|
| 41 |
+
os.chdir('diffusers_repo')
|
| 42 |
+
print(f"Changed to directory: {os.getcwd()}")
|
| 43 |
+
|
| 44 |
+
# Use the exact same Python executable for pip
|
| 45 |
+
python_exe = sys.executable
|
| 46 |
+
print(f"Using Python executable: {python_exe}")
|
| 47 |
+
|
| 48 |
+
# Install in editable mode with verbose output
|
| 49 |
+
cmd = [python_exe, '-m', 'pip', 'install', '-e', '.', '--verbose']
|
| 50 |
+
print(f"Running command: {' '.join(cmd)}")
|
| 51 |
+
|
| 52 |
+
result = subprocess.run(cmd, capture_output=True, text=True)
|
| 53 |
+
print("Installation stdout:", result.stdout[-1000:]) # Last 1000 chars
|
| 54 |
+
if result.stderr:
|
| 55 |
+
print("Installation stderr:", result.stderr[-1000:]) # Last 1000 chars
|
| 56 |
+
|
| 57 |
+
# Change back to original directory
|
| 58 |
+
os.chdir(original_dir)
|
| 59 |
+
|
| 60 |
+
# Add diffusers_repo to Python path
|
| 61 |
+
diffusers_path = os.path.join(original_dir, 'diffusers_repo')
|
| 62 |
+
if diffusers_path not in sys.path:
|
| 63 |
+
sys.path.insert(0, diffusers_path)
|
| 64 |
+
print(f"Added to Python path: {diffusers_path}")
|
| 65 |
+
|
| 66 |
+
# Test if we can import after installation
|
| 67 |
+
try:
|
| 68 |
+
import diffusers
|
| 69 |
+
print(f"β Successfully imported diffusers from: {diffusers.__file__}")
|
| 70 |
+
return True
|
| 71 |
+
except ImportError as e:
|
| 72 |
+
print(f"β Failed to import diffusers after installation: {e}")
|
| 73 |
+
return False
|
| 74 |
+
|
| 75 |
+
except Exception as e:
|
| 76 |
+
print(f"Installation failed with exception: {e}")
|
| 77 |
+
return False
|
| 78 |
+
|
| 79 |
+
# Attempt installation
|
| 80 |
+
print("Starting diffusers installation...")
|
| 81 |
+
if not install_diffusers():
|
| 82 |
+
print("Failed to install local diffusers, exiting...")
|
| 83 |
+
sys.exit(1)
|
| 84 |
+
else:
|
| 85 |
+
print("β Local diffusers installation successful!")
|
| 86 |
+
|
| 87 |
+
# Now import required modules
|
| 88 |
+
print("Importing required modules...")
|
| 89 |
try:
|
| 90 |
import gradio as gr
|
| 91 |
+
print("β Gradio imported")
|
| 92 |
+
import spaces
|
| 93 |
+
print("β Spaces imported")
|
| 94 |
import torch
|
| 95 |
+
print("β Torch imported")
|
| 96 |
from diffusers import CosmosTextToImagePipeline
|
| 97 |
+
print("β CosmosTextToImagePipeline imported")
|
| 98 |
import random
|
| 99 |
+
print("β Random imported")
|
| 100 |
+
print("All imports successful!")
|
| 101 |
except ImportError as e:
|
| 102 |
+
print(f"β Import failed: {e}")
|
| 103 |
+
print("Checking installed packages...")
|
| 104 |
+
try:
|
| 105 |
+
result = subprocess.run([sys.executable, '-m', 'pip', 'list'],
|
| 106 |
+
capture_output=True, text=True)
|
| 107 |
+
print("Installed packages:")
|
| 108 |
+
print(result.stdout)
|
| 109 |
+
except:
|
| 110 |
+
pass
|
| 111 |
sys.exit(1)
|
| 112 |
|
| 113 |
# Available checkpoints: nvidia/Cosmos-Predict2-2B-Text2Image, nvidia/Cosmos-Predict2-14B-Text2Image
|
| 114 |
model_id = "diffusers-internal-dev/ct2i-14B"
|
| 115 |
|
| 116 |
# Load the pipeline once to avoid repeated loading
|
| 117 |
+
pipe = CosmosTextToImagePipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16)
|
| 118 |
+
pipe.to("cuda")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
@spaces.GPU
|
| 121 |
def generate_image(prompt, negative_prompt, seed, randomize_seed):
|