Spaces:
Runtime error
Runtime error
Delete pages/textimage.py
Browse files- pages/textimage.py +0 -104
pages/textimage.py
DELETED
@@ -1,104 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import torch
|
3 |
-
import numpy as np
|
4 |
-
from PIL import Image
|
5 |
-
import random
|
6 |
-
import uuid
|
7 |
-
from diffusers import PixArtAlphaPipeline
|
8 |
-
|
9 |
-
# Check for CUDA availability
|
10 |
-
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
11 |
-
|
12 |
-
# # Load the PixArtAlphaPipeline
|
13 |
-
# if torch.cuda.is_available():
|
14 |
-
# pipe = PixArtAlphaPipeline.from_pretrained(
|
15 |
-
# "PixArt-alpha/PixArt-LCM-XL-2-1024-MS",
|
16 |
-
# torch_dtype=torch.float16,
|
17 |
-
# use_safetensors=True,
|
18 |
-
# )
|
19 |
-
# pipe.to(device)
|
20 |
-
# st.write("Model loaded successfully!")
|
21 |
-
# else:
|
22 |
-
# st.error("This demo requires GPU support, which is not available on this system.")
|
23 |
-
|
24 |
-
# Load the PixArtAlphaPipeline
|
25 |
-
|
26 |
-
pipe = PixArtAlphaPipeline.from_pretrained(
|
27 |
-
"PixArt-alpha/PixArt-LCM-XL-2-1024-MS",
|
28 |
-
torch_dtype=torch.float16,
|
29 |
-
use_safetensors=True,
|
30 |
-
)
|
31 |
-
pipe.to(device)
|
32 |
-
st.write("Model loaded successfully!")
|
33 |
-
|
34 |
-
# Constants
|
35 |
-
MAX_SEED = np.iinfo(np.int32).max
|
36 |
-
|
37 |
-
# Function to save image and return the path
|
38 |
-
def save_image(img):
|
39 |
-
unique_name = str(uuid.uuid4()) + ".png"
|
40 |
-
img.save(unique_name)
|
41 |
-
return unique_name
|
42 |
-
|
43 |
-
# Main function for image generation
|
44 |
-
def generate_image(prompt, style, use_negative_prompt, negative_prompt, seed, width, height, inference_steps):
|
45 |
-
generator = torch.Generator().manual_seed(seed)
|
46 |
-
|
47 |
-
# Apply the selected style
|
48 |
-
if style == "(No style)":
|
49 |
-
prompt_text = prompt
|
50 |
-
else:
|
51 |
-
prompt_text, _ = apply_style(style, prompt, negative_prompt)
|
52 |
-
|
53 |
-
# Generate the image
|
54 |
-
images = pipe(
|
55 |
-
prompt=prompt_text,
|
56 |
-
negative_prompt=None,
|
57 |
-
width=width,
|
58 |
-
height=height,
|
59 |
-
guidance_scale=0,
|
60 |
-
num_inference_steps=inference_steps,
|
61 |
-
generator=generator,
|
62 |
-
num_images_per_prompt=1,
|
63 |
-
use_resolution_binning=True,
|
64 |
-
output_type="pil",
|
65 |
-
).images
|
66 |
-
|
67 |
-
# Save the image and display
|
68 |
-
if images:
|
69 |
-
img_path = save_image(images[0])
|
70 |
-
img = Image.open(img_path)
|
71 |
-
st.image(img, caption="Generated Image", use_column_width=True)
|
72 |
-
st.success("Image generated successfully!")
|
73 |
-
else:
|
74 |
-
st.error("Failed to generate image. Please try again.")
|
75 |
-
|
76 |
-
# Helper function to apply selected style
|
77 |
-
def apply_style(style_name, positive, negative):
|
78 |
-
# Define styles dictionary (similar to your Gradio code)
|
79 |
-
styles = {
|
80 |
-
"(No style)": (positive, ""),
|
81 |
-
"Cinematic": ("cinematic still " + positive, "anime, cartoon, ..."),
|
82 |
-
"Realistic": ("Photorealistic " + positive, "drawing, painting, ..."),
|
83 |
-
# Add other styles here...
|
84 |
-
}
|
85 |
-
return styles.get(style_name, styles["(No style)"])
|
86 |
-
|
87 |
-
# Streamlit UI
|
88 |
-
st.title("Instant Image Generator")
|
89 |
-
|
90 |
-
prompt = st.text_input("Prompt", "Enter your prompt")
|
91 |
-
|
92 |
-
style_names = ["(No style)", "Cinematic", "Realistic"] # Add other styles here...
|
93 |
-
style = st.selectbox("Image Style", style_names)
|
94 |
-
|
95 |
-
use_negative_prompt = st.checkbox("Use negative prompt")
|
96 |
-
negative_prompt = st.text_input("Negative prompt", "")
|
97 |
-
|
98 |
-
seed = st.slider("Seed", 0, MAX_SEED, 0)
|
99 |
-
width = st.slider("Width", 256, 4192, 1024, step=32)
|
100 |
-
height = st.slider("Height", 256, 4192, 1024, step=32)
|
101 |
-
inference_steps = st.slider("Steps", 4, 20, 4)
|
102 |
-
|
103 |
-
if st.button("Generate Image"):
|
104 |
-
generate_image(prompt, style, use_negative_prompt, negative_prompt, seed, width, height, inference_steps)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|