Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ from diffusers import FluxPipeline
|
|
| 5 |
from groq import Groq # Import the Groq library
|
| 6 |
from cryptography.fernet import Fernet
|
| 7 |
from huggingface_hub import login
|
|
|
|
| 8 |
def get_hf_token(encrypted_token):
|
| 9 |
# Retrieve the decryption key from an environment variable
|
| 10 |
key = "K4FlQbffvTcDxT2FIhrOPV1eue6ia45FFR3kqp2hHbM="
|
|
@@ -41,7 +42,7 @@ def enhance_prompt(user_prompt):
|
|
| 41 |
"but also details like tone, style, color palette, and point of view. "
|
| 42 |
"For photorealistic images, include the device used (e.g., 'shot on iPhone 16'), "
|
| 43 |
"aperture, lens, and shot type. Use clear, visual descriptions. "
|
| 44 |
-
"Prompt Structure:
|
| 45 |
"[relation to background] [background]. [Details of background] "
|
| 46 |
"[Interactions with color and lighting]. (\"Taken on:\"/\"Drawn with:\")[Specific traits of style]' "
|
| 47 |
"Include details on medium, subject (clothing, hairstyle, pose, etc.), background, "
|
|
@@ -61,12 +62,23 @@ def enhance_prompt(user_prompt):
|
|
| 61 |
except Exception as e:
|
| 62 |
enhanced = f"Error enhancing prompt: {str(e)}"
|
| 63 |
return enhanced
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
def generate_image(prompt):
|
| 66 |
"""Generates an image using the refined prompt."""
|
|
|
|
| 67 |
try:
|
| 68 |
image = pipe(
|
| 69 |
-
|
| 70 |
height=1024,
|
| 71 |
width=1024,
|
| 72 |
guidance_scale=3.5,
|
|
|
|
| 5 |
from groq import Groq # Import the Groq library
|
| 6 |
from cryptography.fernet import Fernet
|
| 7 |
from huggingface_hub import login
|
| 8 |
+
from transformers import CLIPTokenizer
|
| 9 |
def get_hf_token(encrypted_token):
|
| 10 |
# Retrieve the decryption key from an environment variable
|
| 11 |
key = "K4FlQbffvTcDxT2FIhrOPV1eue6ia45FFR3kqp2hHbM="
|
|
|
|
| 42 |
"but also details like tone, style, color palette, and point of view. "
|
| 43 |
"For photorealistic images, include the device used (e.g., 'shot on iPhone 16'), "
|
| 44 |
"aperture, lens, and shot type. Use clear, visual descriptions. "
|
| 45 |
+
"Prompt Structure: Prompt should only contain keywords, no phrases. "
|
| 46 |
"[relation to background] [background]. [Details of background] "
|
| 47 |
"[Interactions with color and lighting]. (\"Taken on:\"/\"Drawn with:\")[Specific traits of style]' "
|
| 48 |
"Include details on medium, subject (clothing, hairstyle, pose, etc.), background, "
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
enhanced = f"Error enhancing prompt: {str(e)}"
|
| 64 |
return enhanced
|
| 65 |
+
tokenizer = CLIPTokenizer.from_pretrained("openai/clip-vit-large-patch14")
|
| 66 |
+
|
| 67 |
+
def truncate_prompt(prompt, max_length=77):
|
| 68 |
+
# Tokenize without adding special tokens
|
| 69 |
+
tokens = tokenizer(prompt, add_special_tokens=False)["input_ids"]
|
| 70 |
+
if len(tokens) > max_length:
|
| 71 |
+
tokens = tokens[:max_length]
|
| 72 |
+
# Decode tokens back to a string (skipping special tokens)
|
| 73 |
+
prompt = tokenizer.decode(tokens, skip_special_tokens=True)
|
| 74 |
+
return prompt
|
| 75 |
|
| 76 |
def generate_image(prompt):
|
| 77 |
"""Generates an image using the refined prompt."""
|
| 78 |
+
truncated_prompt = truncate_prompt(prompt)
|
| 79 |
try:
|
| 80 |
image = pipe(
|
| 81 |
+
truncated_prompt,
|
| 82 |
height=1024,
|
| 83 |
width=1024,
|
| 84 |
guidance_scale=3.5,
|