panelforge commited on
Commit
38d2462
1 Parent(s): dedbf1f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -20
app.py CHANGED
@@ -5,7 +5,6 @@ import random
5
  import spaces #[uncomment to use ZeroGPU]
6
  from diffusers import DiffusionPipeline
7
  import torch
8
- from io import BytesIO
9
 
10
  token = os.getenv('HUGGINGFACE_TOKEN')
11
 
@@ -41,13 +40,7 @@ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance
41
  generator = generator
42
  ).images[0]
43
 
44
- # Convert image to base64
45
- buffered = BytesIO()
46
- image.save(buffered, format="WEBP") # Save image to buffer in WebP format
47
- image_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8") # Convert buffer to base64 string
48
-
49
- # Return the base64 image string and seed
50
- return f"data:image/webp;base64,{image_base64}", seed
51
 
52
  examples = [
53
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
@@ -63,12 +56,14 @@ css="""
63
  """
64
 
65
  with gr.Blocks(css=css) as demo:
 
66
  with gr.Column(elem_id="col-container"):
67
  gr.Markdown(f"""
68
  # Text-to-Image Gradio Template
69
  """)
70
 
71
  with gr.Row():
 
72
  prompt = gr.Text(
73
  label="Prompt",
74
  show_label=False,
@@ -76,18 +71,20 @@ with gr.Blocks(css=css) as demo:
76
  placeholder="Enter your prompt",
77
  container=False,
78
  )
 
79
  run_button = gr.Button("Run", scale=0)
80
 
81
- # Use gr.Textbox for base64-encoded image output
82
- result = gr.Textbox(label="Result (Base64 Image)", show_label=False, interactive=False)
83
 
84
  with gr.Accordion("Advanced Settings", open=False):
 
85
  negative_prompt = gr.Text(
86
  label="Negative prompt",
87
  max_lines=1,
88
  placeholder="Enter a negative prompt",
89
  visible=False,
90
  )
 
91
  seed = gr.Slider(
92
  label="Seed",
93
  minimum=0,
@@ -95,48 +92,54 @@ with gr.Blocks(css=css) as demo:
95
  step=1,
96
  value=0,
97
  )
 
98
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
 
99
  with gr.Row():
 
100
  width = gr.Slider(
101
  label="Width",
102
  minimum=256,
103
  maximum=MAX_IMAGE_SIZE,
104
  step=32,
105
- value=1024, # Default values
106
  )
 
107
  height = gr.Slider(
108
  label="Height",
109
  minimum=256,
110
  maximum=MAX_IMAGE_SIZE,
111
  step=32,
112
- value=1024, # Default values
113
  )
 
114
  with gr.Row():
 
115
  guidance_scale = gr.Slider(
116
  label="Guidance scale",
117
  minimum=0.0,
118
  maximum=10.0,
119
  step=0.1,
120
- value=7.5, # Default value
121
  )
 
122
  num_inference_steps = gr.Slider(
123
  label="Number of inference steps",
124
  minimum=1,
125
  maximum=50,
126
  step=1,
127
- value=35, # Default value
128
  )
129
 
130
  gr.Examples(
131
- examples=examples,
132
- inputs=[prompt]
133
  )
134
-
135
  gr.on(
136
  triggers=[run_button.click, prompt.submit],
137
- fn=infer,
138
- inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
139
- outputs=[result, seed] # Seed is optional, result now holds base64 image string
140
  )
141
 
142
  demo.queue().launch()
 
5
  import spaces #[uncomment to use ZeroGPU]
6
  from diffusers import DiffusionPipeline
7
  import torch
 
8
 
9
  token = os.getenv('HUGGINGFACE_TOKEN')
10
 
 
40
  generator = generator
41
  ).images[0]
42
 
43
+ return image, seed
 
 
 
 
 
 
44
 
45
  examples = [
46
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
 
56
  """
57
 
58
  with gr.Blocks(css=css) as demo:
59
+
60
  with gr.Column(elem_id="col-container"):
61
  gr.Markdown(f"""
62
  # Text-to-Image Gradio Template
63
  """)
64
 
65
  with gr.Row():
66
+
67
  prompt = gr.Text(
68
  label="Prompt",
69
  show_label=False,
 
71
  placeholder="Enter your prompt",
72
  container=False,
73
  )
74
+
75
  run_button = gr.Button("Run", scale=0)
76
 
77
+ result = gr.Image(label="Result", show_label=False)
 
78
 
79
  with gr.Accordion("Advanced Settings", open=False):
80
+
81
  negative_prompt = gr.Text(
82
  label="Negative prompt",
83
  max_lines=1,
84
  placeholder="Enter a negative prompt",
85
  visible=False,
86
  )
87
+
88
  seed = gr.Slider(
89
  label="Seed",
90
  minimum=0,
 
92
  step=1,
93
  value=0,
94
  )
95
+
96
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
97
+
98
  with gr.Row():
99
+
100
  width = gr.Slider(
101
  label="Width",
102
  minimum=256,
103
  maximum=MAX_IMAGE_SIZE,
104
  step=32,
105
+ value=1024, #Replace with defaults that work for your model
106
  )
107
+
108
  height = gr.Slider(
109
  label="Height",
110
  minimum=256,
111
  maximum=MAX_IMAGE_SIZE,
112
  step=32,
113
+ value=1024, #Replace with defaults that work for your model
114
  )
115
+
116
  with gr.Row():
117
+
118
  guidance_scale = gr.Slider(
119
  label="Guidance scale",
120
  minimum=0.0,
121
  maximum=10.0,
122
  step=0.1,
123
+ value=0.0, #Replace with defaults that work for your model
124
  )
125
+
126
  num_inference_steps = gr.Slider(
127
  label="Number of inference steps",
128
  minimum=1,
129
  maximum=50,
130
  step=1,
131
+ value=2, #Replace with defaults that work for your model
132
  )
133
 
134
  gr.Examples(
135
+ examples = examples,
136
+ inputs = [prompt]
137
  )
 
138
  gr.on(
139
  triggers=[run_button.click, prompt.submit],
140
+ fn = infer,
141
+ inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
142
+ outputs = [result, seed]
143
  )
144
 
145
  demo.queue().launch()