Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -34,34 +34,25 @@ from gtts import gTTS
|
|
| 34 |
from pydub import AudioSegment
|
| 35 |
import textwrap
|
| 36 |
|
| 37 |
-
|
| 38 |
# Initialize FLUX pipeline only if CUDA is available
|
| 39 |
dtype = torch.bfloat16
|
| 40 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
return None
|
| 50 |
-
|
| 51 |
-
flux_pipe = None # Do not load at startup
|
| 52 |
|
| 53 |
MAX_SEED = np.iinfo(np.int32).max
|
| 54 |
MAX_IMAGE_SIZE = 2048
|
| 55 |
|
| 56 |
nltk.download('punkt')
|
| 57 |
|
| 58 |
-
|
| 59 |
# Ensure proper multiprocessing start method
|
| 60 |
-
|
| 61 |
-
multiprocessing.set_start_method("spawn", force=True)
|
| 62 |
-
except RuntimeError:
|
| 63 |
-
pass # Ignore errors if the start method is already set
|
| 64 |
-
|
| 65 |
|
| 66 |
# Download necessary NLTK data
|
| 67 |
def setup_nltk():
|
|
@@ -79,7 +70,7 @@ DESCRIPTION = (
|
|
| 79 |
TITLE = "Video Story Generator with Audio by using FLUX, distilbart, and GTTS."
|
| 80 |
|
| 81 |
# Load Tokenizer and Model for Text Summarization
|
| 82 |
-
def
|
| 83 |
"""Load the tokenizer and model for text summarization."""
|
| 84 |
print("Loading text summarization model...")
|
| 85 |
tokenizer = AutoTokenizer.from_pretrained("sshleifer/distilbart-cnn-12-6")
|
|
@@ -89,25 +80,6 @@ def load_text_summarization_model_v1():
|
|
| 89 |
model.to(device)
|
| 90 |
return tokenizer, model, device
|
| 91 |
|
| 92 |
-
def load_text_summarization_model():
|
| 93 |
-
"""Load the tokenizer and model for text summarization without triggering CUDA init."""
|
| 94 |
-
print("Loading text summarization model...")
|
| 95 |
-
|
| 96 |
-
if "SPACE_ID" in os.environ: # Detect if running in Hugging Face Spaces
|
| 97 |
-
os.environ["CUDA_VISIBLE_DEVICES"] = "" # Prevent CUDA initialization
|
| 98 |
-
|
| 99 |
-
tokenizer = AutoTokenizer.from_pretrained("sshleifer/distilbart-cnn-12-6")
|
| 100 |
-
model = AutoModelForSeq2SeqLM.from_pretrained("sshleifer/distilbart-cnn-12-6")
|
| 101 |
-
|
| 102 |
-
if torch.cuda.is_available() and "SPACE_ID" not in os.environ:
|
| 103 |
-
device = torch.device("cuda:0")
|
| 104 |
-
else:
|
| 105 |
-
device = torch.device("cpu")
|
| 106 |
-
|
| 107 |
-
print(f"Using device: {device}")
|
| 108 |
-
model.to(device)
|
| 109 |
-
return tokenizer, model, device
|
| 110 |
-
|
| 111 |
tokenizer, model, device = load_text_summarization_model()
|
| 112 |
|
| 113 |
# Log GPU Memory (optional, for debugging)
|
|
@@ -130,8 +102,8 @@ def check_gpu_availability():
|
|
| 130 |
|
| 131 |
check_gpu_availability()
|
| 132 |
|
| 133 |
-
|
| 134 |
-
def
|
| 135 |
text: str,
|
| 136 |
seed: int = 42,
|
| 137 |
width: int = 1024,
|
|
@@ -169,45 +141,6 @@ def generate_image_with_flux_old(
|
|
| 169 |
print("DEBUG: Image generated successfully.")
|
| 170 |
return image
|
| 171 |
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
@spaces.GPU()
|
| 175 |
-
def generate_image_with_flux(
|
| 176 |
-
text: str,
|
| 177 |
-
seed: int = 42,
|
| 178 |
-
width: int = 1024,
|
| 179 |
-
height: int = 1024,
|
| 180 |
-
num_inference_steps: int = 4,
|
| 181 |
-
randomize_seed: bool = True
|
| 182 |
-
):
|
| 183 |
-
print(f"DEBUG: Generating image with FLUX for text: '{text}'")
|
| 184 |
-
|
| 185 |
-
if randomize_seed:
|
| 186 |
-
seed = random.randint(0, MAX_SEED)
|
| 187 |
-
|
| 188 |
-
generator = torch.Generator().manual_seed(seed)
|
| 189 |
-
|
| 190 |
-
# Load FLUX pipeline only when needed
|
| 191 |
-
global flux_pipe
|
| 192 |
-
if flux_pipe is None:
|
| 193 |
-
flux_pipe = get_flux_pipeline() # Delayed initialization
|
| 194 |
-
|
| 195 |
-
if flux_pipe is None:
|
| 196 |
-
raise RuntimeError("FLUX pipeline is not available. Check CUDA or environment settings.")
|
| 197 |
-
|
| 198 |
-
image = flux_pipe(
|
| 199 |
-
prompt=text,
|
| 200 |
-
width=width,
|
| 201 |
-
height=height,
|
| 202 |
-
num_inference_steps=num_inference_steps,
|
| 203 |
-
generator=generator,
|
| 204 |
-
guidance_scale=0.0
|
| 205 |
-
).images[0]
|
| 206 |
-
|
| 207 |
-
print("DEBUG: Image generated successfully.")
|
| 208 |
-
return image
|
| 209 |
-
|
| 210 |
-
|
| 211 |
# --------- End of MinDalle Functions ---------
|
| 212 |
# Merge audio files
|
| 213 |
|
|
@@ -451,6 +384,4 @@ with demo:
|
|
| 451 |
)
|
| 452 |
|
| 453 |
# Launch the Gradio app
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
demo.launch(debug=True, share="SPACE_ID" in os.environ)
|
|
|
|
| 34 |
from pydub import AudioSegment
|
| 35 |
import textwrap
|
| 36 |
|
|
|
|
| 37 |
# Initialize FLUX pipeline only if CUDA is available
|
| 38 |
dtype = torch.bfloat16
|
| 39 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 40 |
|
| 41 |
+
if device == "cuda":
|
| 42 |
+
flux_pipe = DiffusionPipeline.from_pretrained(
|
| 43 |
+
"black-forest-labs/FLUX.1-schnell",
|
| 44 |
+
torch_dtype=dtype
|
| 45 |
+
).to(device)
|
| 46 |
+
else:
|
| 47 |
+
flux_pipe = None # Avoid initializing the model when CUDA is unavailable
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
MAX_SEED = np.iinfo(np.int32).max
|
| 50 |
MAX_IMAGE_SIZE = 2048
|
| 51 |
|
| 52 |
nltk.download('punkt')
|
| 53 |
|
|
|
|
| 54 |
# Ensure proper multiprocessing start method
|
| 55 |
+
multiprocessing.set_start_method("spawn", force=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# Download necessary NLTK data
|
| 58 |
def setup_nltk():
|
|
|
|
| 70 |
TITLE = "Video Story Generator with Audio by using FLUX, distilbart, and GTTS."
|
| 71 |
|
| 72 |
# Load Tokenizer and Model for Text Summarization
|
| 73 |
+
def load_text_summarization_model():
|
| 74 |
"""Load the tokenizer and model for text summarization."""
|
| 75 |
print("Loading text summarization model...")
|
| 76 |
tokenizer = AutoTokenizer.from_pretrained("sshleifer/distilbart-cnn-12-6")
|
|
|
|
| 80 |
model.to(device)
|
| 81 |
return tokenizer, model, device
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
tokenizer, model, device = load_text_summarization_model()
|
| 84 |
|
| 85 |
# Log GPU Memory (optional, for debugging)
|
|
|
|
| 102 |
|
| 103 |
check_gpu_availability()
|
| 104 |
|
| 105 |
+
@spaces.GPU()
|
| 106 |
+
def generate_image_with_flux(
|
| 107 |
text: str,
|
| 108 |
seed: int = 42,
|
| 109 |
width: int = 1024,
|
|
|
|
| 141 |
print("DEBUG: Image generated successfully.")
|
| 142 |
return image
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
# --------- End of MinDalle Functions ---------
|
| 145 |
# Merge audio files
|
| 146 |
|
|
|
|
| 384 |
)
|
| 385 |
|
| 386 |
# Launch the Gradio app
|
| 387 |
+
demo.launch(debug=True, share=False)
|
|
|
|
|
|