Aklavya commited on
Commit
fb2a5f2
·
verified ·
1 Parent(s): 6e64d2f

adding history

Browse files
Files changed (1) hide show
  1. app.py +54 -24
app.py CHANGED
@@ -9,11 +9,12 @@ import torch
9
  from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
10
  from typing import Tuple
11
 
 
12
  def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
13
  styles = {
14
  "3840 x 2160": (
15
- "hyper-realistic image of {prompt}. lifelike, authentic, natural colors, true-to-life details, realistic lighting, immersive, highly detailed",
16
- "unrealistic, low resolution, artificial, over-saturated, distorted, fake",
17
  ),
18
  "Style Zero": ("{prompt}", ""),
19
  }
@@ -22,6 +23,7 @@ def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str
22
  p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
23
  return p.replace("{prompt}", positive), n + negative
24
 
 
25
  def load_and_prepare_model():
26
  model_id = "SG161222/RealVisXL_V5.0_Lightning"
27
  pipe = StableDiffusionXLPipeline.from_pretrained(
@@ -35,16 +37,30 @@ def load_and_prepare_model():
35
 
36
  model = load_and_prepare_model()
37
 
 
38
  def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
39
  if randomize_seed:
40
  seed = random.randint(0, np.iinfo(np.int32).max)
41
  return seed
42
 
 
43
  def save_image(img):
44
  unique_name = str(uuid.uuid4()) + ".png"
45
  img.save(unique_name)
46
  return unique_name
47
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  @spaces.GPU(duration=60, enable_queue=True)
49
  def generate(
50
  prompt: str,
@@ -77,6 +93,7 @@ def generate(
77
  image_path = save_image(images[0]) # Saving the first generated image
78
  return image_path
79
 
 
80
  with gr.Blocks(theme="soft") as demo:
81
  # Block for "SNAPSCRIBE" centered at the top
82
  with gr.Row():
@@ -84,6 +101,7 @@ with gr.Blocks(theme="soft") as demo:
84
  gr.Markdown("<h1 style='text-align:center; color:white; font-weight:bold; text-decoration:underline;'>SNAPSCRIBE</h1>")
85
  gr.Markdown("<h2 style='text-align:center; color:white; font-weight:bold; text-decoration:underline;'>Developed using RealVisXL_V5.0_Lightning model with ❤ by Aklavya</h2>")
86
 
 
87
  with gr.Row():
88
  with gr.Column(scale=3):
89
  prompt = gr.Textbox(
@@ -93,36 +111,48 @@ with gr.Blocks(theme="soft") as demo:
93
  )
94
  run_button = gr.Button("Generate Image")
95
 
96
- # Example prompts box
97
- example_prompts_text = (
98
- "Dew-covered spider web in morning sunlight, with blurred greenery\n"
99
- "--------------------------------------------\n"
100
- "Glass of cold water with ice cubes and condensation on a wooden table\n"
101
- "--------------------------------------------\n"
102
- "Coffee cup with latte art, steam rising, and morning sunlight\n"
103
- "--------------------------------------------\n"
104
- "Autumn forest with golden leaves, sunlight through trees, and a breeze"
105
- )
106
-
107
- example_prompts = gr.Textbox(
108
- value=example_prompts_text,
109
- lines=5,
110
- label="Sample Inputs",
111
- interactive=False,
112
- )
113
-
114
  with gr.Column(scale=7):
115
  result_image = gr.Image(
116
  label="Generated Image",
117
  type="filepath",
118
  elem_id="output_image",
119
- height=650 # Increased the height by 100%
120
  )
121
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  run_button.click(
123
- fn=generate,
124
  inputs=[prompt],
125
- outputs=[result_image],
126
  )
127
 
128
- demo.launch()
 
9
  from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
10
  from typing import Tuple
11
 
12
+ # Function to apply the style
13
  def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
14
  styles = {
15
  "3840 x 2160": (
16
+ "hyper-realistic 8K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
17
+ "",
18
  ),
19
  "Style Zero": ("{prompt}", ""),
20
  }
 
23
  p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
24
  return p.replace("{prompt}", positive), n + negative
25
 
26
+ # Load and prepare the model
27
  def load_and_prepare_model():
28
  model_id = "SG161222/RealVisXL_V5.0_Lightning"
29
  pipe = StableDiffusionXLPipeline.from_pretrained(
 
37
 
38
  model = load_and_prepare_model()
39
 
40
+ # Helper function for randomizing seed
41
  def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
42
  if randomize_seed:
43
  seed = random.randint(0, np.iinfo(np.int32).max)
44
  return seed
45
 
46
+ # Helper function to save the image
47
  def save_image(img):
48
  unique_name = str(uuid.uuid4()) + ".png"
49
  img.save(unique_name)
50
  return unique_name
51
 
52
+ # Function to save the thumbnail
53
+ def save_thumbnail(img):
54
+ # Ensure the thumbnails directory exists
55
+ os.makedirs('/tmp/thumbnails', exist_ok=True)
56
+ thumbnail_path = "/tmp/thumbnails/" + str(uuid.uuid4()) + "_thumb.png"
57
+
58
+ # Resize the image to a smaller size (150x150 px)
59
+ img.thumbnail((100, 100)) # Resize the thumbnail
60
+ img.save(thumbnail_path)
61
+ return thumbnail_path
62
+
63
+ # Generate the image
64
  @spaces.GPU(duration=60, enable_queue=True)
65
  def generate(
66
  prompt: str,
 
93
  image_path = save_image(images[0]) # Saving the first generated image
94
  return image_path
95
 
96
+ # Gradio app with layout and History section
97
  with gr.Blocks(theme="soft") as demo:
98
  # Block for "SNAPSCRIBE" centered at the top
99
  with gr.Row():
 
101
  gr.Markdown("<h1 style='text-align:center; color:white; font-weight:bold; text-decoration:underline;'>SNAPSCRIBE</h1>")
102
  gr.Markdown("<h2 style='text-align:center; color:white; font-weight:bold; text-decoration:underline;'>Developed using RealVisXL_V5.0_Lightning model with ❤ by Aklavya</h2>")
103
 
104
+ # Input prompt and generate button
105
  with gr.Row():
106
  with gr.Column(scale=3):
107
  prompt = gr.Textbox(
 
111
  )
112
  run_button = gr.Button("Generate Image")
113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  with gr.Column(scale=7):
115
  result_image = gr.Image(
116
  label="Generated Image",
117
  type="filepath",
118
  elem_id="output_image",
119
+ height=650 # Height for the generated image
120
  )
121
 
122
+ # Create a section for history below the output image section
123
+ with gr.Row():
124
+ with gr.Column(scale=7):
125
+ gr.Markdown("<h3 style='text-align:center; color:white; font-weight:bold;'>History</h3>")
126
+ history_images_output = gr.Gallery(
127
+ label="History",
128
+ elem_id="history_images",
129
+ columns=5, # Number of columns for thumbnails
130
+ show_label=False, # Hide the label
131
+ height=100 # Set the height for smaller thumbnails
132
+ )
133
+
134
+ # Function to handle image generation and saving history
135
+ history_images = [] # List to store the paths of generated images
136
+
137
+ def handle_generate_and_history(prompt):
138
+ # Generate image
139
+ image_path = generate(prompt)
140
+
141
+ # Load the image and create a mini thumbnail
142
+ img = Image.open(image_path)
143
+ thumbnail_path = save_thumbnail(img)
144
+
145
+ # Add the new thumbnail path at the beginning of the history list
146
+ history_images.insert(0, thumbnail_path) # Prepend the new thumbnail
147
+
148
+ # Return the generated image and updated history
149
+ return image_path, history_images
150
+
151
+ # Run the generate button click action
152
  run_button.click(
153
+ fn=handle_generate_and_history,
154
  inputs=[prompt],
155
+ outputs=[result_image, history_images_output],
156
  )
157
 
158
+ demo.launch()