Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,51 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import spaces
|
| 3 |
-
from transformers import
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
@spaces.GPU
|
| 10 |
def expand_prompt(prompt, num_variants=5, max_length=100):
|
| 11 |
"""
|
| 12 |
-
|
| 13 |
-
This function explicitly tokenizes the input with truncation (strategy 'longest_first'),
|
| 14 |
-
moves the input to GPU, generates output using the GPU, and then moves the model back to CPU.
|
| 15 |
"""
|
| 16 |
-
# Move model to GPU
|
| 17 |
-
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
inputs =
|
| 21 |
-
# Move inputs to GPU.
|
| 22 |
-
inputs = {k: v.to("cuda") for k, v in inputs.items()}
|
| 23 |
|
| 24 |
-
# Generate
|
| 25 |
-
outputs =
|
| 26 |
**inputs,
|
| 27 |
max_length=max_length,
|
| 28 |
-
num_return_sequences=num_variants,
|
| 29 |
do_sample=True,
|
| 30 |
-
|
|
|
|
| 31 |
)
|
| 32 |
|
| 33 |
-
# Decode
|
| 34 |
-
|
| 35 |
|
| 36 |
-
# Move model back to CPU
|
| 37 |
-
|
| 38 |
|
| 39 |
-
return "\n\n".join(
|
| 40 |
|
|
|
|
| 41 |
iface = gr.Interface(
|
| 42 |
fn=expand_prompt,
|
| 43 |
inputs=gr.Textbox(lines=2, placeholder="Enter your basic prompt here...", label="Basic Prompt"),
|
| 44 |
outputs=gr.Textbox(lines=10, label="Expanded Prompts"),
|
| 45 |
title="Prompt Expansion Generator",
|
| 46 |
description=(
|
| 47 |
-
"Enter a basic prompt
|
| 48 |
-
"
|
| 49 |
-
"
|
| 50 |
-
"Simply copy the output for use in your downstream image-generation pipeline."
|
| 51 |
)
|
| 52 |
)
|
| 53 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
import torch
|
| 5 |
|
| 6 |
+
# Load the MagicPrompt-Stable-Diffusion model and tokenizer
|
| 7 |
+
model_name = "Gustavosta/MagicPrompt-Stable-Diffusion"
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(model_name).to("cpu")
|
| 10 |
|
| 11 |
+
@spaces.GPU
|
| 12 |
def expand_prompt(prompt, num_variants=5, max_length=100):
|
| 13 |
"""
|
| 14 |
+
Generate expanded prompts using a specialized model fine-tuned for Stable Diffusion.
|
|
|
|
|
|
|
| 15 |
"""
|
| 16 |
+
# Move model to GPU
|
| 17 |
+
model.to("cuda")
|
| 18 |
|
| 19 |
+
# Tokenize input prompt
|
| 20 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Generate multiple prompt variants
|
| 23 |
+
outputs = model.generate(
|
| 24 |
**inputs,
|
| 25 |
max_length=max_length,
|
|
|
|
| 26 |
do_sample=True,
|
| 27 |
+
num_return_sequences=num_variants,
|
| 28 |
+
pad_token_id=tokenizer.eos_token_id
|
| 29 |
)
|
| 30 |
|
| 31 |
+
# Decode generated prompts
|
| 32 |
+
expanded_prompts = [tokenizer.decode(output, skip_special_tokens=True).strip() for output in outputs]
|
| 33 |
|
| 34 |
+
# Move model back to CPU
|
| 35 |
+
model.to("cpu")
|
| 36 |
|
| 37 |
+
return "\n\n".join(expanded_prompts)
|
| 38 |
|
| 39 |
+
# Create a Gradio Interface
|
| 40 |
iface = gr.Interface(
|
| 41 |
fn=expand_prompt,
|
| 42 |
inputs=gr.Textbox(lines=2, placeholder="Enter your basic prompt here...", label="Basic Prompt"),
|
| 43 |
outputs=gr.Textbox(lines=10, label="Expanded Prompts"),
|
| 44 |
title="Prompt Expansion Generator",
|
| 45 |
description=(
|
| 46 |
+
"Enter a basic prompt and receive multiple expanded prompt variants optimized for Stable Diffusion. "
|
| 47 |
+
"This tool uses a specialized model fine-tuned on Stable Diffusion prompts. "
|
| 48 |
+
"Simply copy the output for use with your image-generation pipeline."
|
|
|
|
| 49 |
)
|
| 50 |
)
|
| 51 |
|