TheAwakenOne commited on
Commit
71f35e4
Β·
verified Β·
1 Parent(s): f726970

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -17
app.py CHANGED
@@ -58,26 +58,67 @@ patch_processor_fast(SiglipProcessor)
58
 
59
  print("🌌 Loading Cosmos-Predict2 model...")
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  # Load the model at startup
62
  model_id = "nvidia/Cosmos-Predict2-2B-Text2Image"
63
 
64
- if COSMOS_PIPELINE_AVAILABLE:
65
- print("πŸ”„ Loading with Cosmos2TextToImagePipeline...")
66
- pipe = Cosmos2TextToImagePipeline.from_pretrained(
67
- model_id,
68
- torch_dtype=torch.bfloat16
69
- )
70
- else:
71
- print("πŸ”„ Loading with DiffusionPipeline (trust_remote_code=True)...")
72
- pipe = DiffusionPipeline.from_pretrained(
73
- model_id,
74
- torch_dtype=torch.bfloat16,
75
- trust_remote_code=True
76
- )
77
-
78
- pipe.to("cuda")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
- print("βœ… Cosmos-Predict2 model loaded successfully!")
 
 
 
 
 
 
 
 
 
 
81
 
82
  # Default negative prompt for better quality
83
  DEFAULT_NEGATIVE_PROMPT = "The video captures a series of frames showing ugly scenes, static with no motion, motion blur, over-saturation, shaky footage, low resolution, grainy texture, pixelated images, poorly lit areas, underexposed and overexposed scenes, poor color balance, washed out colors, choppy sequences, jerky movements, low frame rate, artifacting, color banding, unnatural transitions, outdated special effects, fake elements, unconvincing visuals, poorly edited content, jump cuts, visual noise, and flickering. Overall, the video is of poor quality."
@@ -162,7 +203,7 @@ def create_interface():
162
  with gr.Blocks(title="Cosmos-Predict2 ZeroGPU", theme=gr.themes.Soft()) as interface:
163
  gr.Markdown("""
164
  # 🌌 Cosmos-Predict2 on ZeroGPU
165
- **Powered by Huggingface Spaces β€’ High-resolution generation β€’ Fast inference**
166
 
167
  This Space uses ZeroGPU for efficient GPU allocation. The model is pre-loaded and ready to generate!
168
  """)
 
58
 
59
  print("🌌 Loading Cosmos-Predict2 model...")
60
 
61
+ # Handle authentication for gated model
62
+ try:
63
+ from huggingface_hub import login
64
+ import os
65
+
66
+ # Try to login with token from environment variable
67
+ hf_token = os.getenv("HF_TOKEN")
68
+ if hf_token:
69
+ login(token=hf_token)
70
+ print("βœ… Authenticated with Hugging Face")
71
+ else:
72
+ print("⚠️ No HF_TOKEN found, trying without authentication...")
73
+ except Exception as e:
74
+ print(f"⚠️ Authentication failed: {e}")
75
+
76
  # Load the model at startup
77
  model_id = "nvidia/Cosmos-Predict2-2B-Text2Image"
78
 
79
+ try:
80
+ if COSMOS_PIPELINE_AVAILABLE:
81
+ print("πŸ”„ Loading with Cosmos2TextToImagePipeline...")
82
+ try:
83
+ # Try loading with safety checker first
84
+ pipe = Cosmos2TextToImagePipeline.from_pretrained(
85
+ model_id,
86
+ torch_dtype=torch.bfloat16,
87
+ use_auth_token=True # Use authentication token
88
+ )
89
+ except ImportError as e:
90
+ if "cosmos_guardrail" in str(e):
91
+ print("⚠️ cosmos_guardrail not available, trying without safety checker...")
92
+ # Try loading without safety checker
93
+ pipe = Cosmos2TextToImagePipeline.from_pretrained(
94
+ model_id,
95
+ torch_dtype=torch.bfloat16,
96
+ use_auth_token=True,
97
+ safety_checker=None,
98
+ requires_safety_checker=False
99
+ )
100
+ else:
101
+ raise e
102
+ else:
103
+ print("πŸ”„ Loading with DiffusionPipeline (trust_remote_code=True)...")
104
+ pipe = DiffusionPipeline.from_pretrained(
105
+ model_id,
106
+ torch_dtype=torch.bfloat16,
107
+ trust_remote_code=True,
108
+ use_auth_token=True # Use authentication token
109
+ )
110
 
111
+ pipe.to("cuda")
112
+ print("βœ… Cosmos-Predict2 model loaded successfully!")
113
+
114
+ except Exception as e:
115
+ print(f"❌ Failed to load Cosmos model: {e}")
116
+ print("πŸ”„ This is likely due to the model being gated/restricted or missing dependencies")
117
+ print("πŸ“ Please check the Setup Guide for authentication instructions")
118
+
119
+ # For demo purposes, we could fall back to a different model
120
+ # But for now, let's just exit gracefully
121
+ raise e
122
 
123
  # Default negative prompt for better quality
124
  DEFAULT_NEGATIVE_PROMPT = "The video captures a series of frames showing ugly scenes, static with no motion, motion blur, over-saturation, shaky footage, low resolution, grainy texture, pixelated images, poorly lit areas, underexposed and overexposed scenes, poor color balance, washed out colors, choppy sequences, jerky movements, low frame rate, artifacting, color banding, unnatural transitions, outdated special effects, fake elements, unconvincing visuals, poorly edited content, jump cuts, visual noise, and flickering. Overall, the video is of poor quality."
 
203
  with gr.Blocks(title="Cosmos-Predict2 ZeroGPU", theme=gr.themes.Soft()) as interface:
204
  gr.Markdown("""
205
  # 🌌 Cosmos-Predict2 on ZeroGPU
206
+ **High-resolution generation β€’ Fast inference**
207
 
208
  This Space uses ZeroGPU for efficient GPU allocation. The model is pre-loaded and ready to generate!
209
  """)