sandeepmajumdar commited on
Commit
5c80069
1 Parent(s): 092a751

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -14
app.py CHANGED
@@ -1,22 +1,39 @@
1
- from transformers import pipeline, Wav2Vec2Processor, AutoModelForCTC
2
  import torch
3
  from torch import autocast
4
  from diffusers import StableDiffusionPipeline
5
- import gradio as gr
6
 
7
  model_id = "CompVis/stable-diffusion-v1-4"
8
- pipe = StableDiffusionPipeline.from_pretrained(model_id, use_auth_token='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', revision="fp16", torch_dtype=torch.float16)
9
- device = 'cuda'
10
- #has_cuda = torch.cuda.is_available()
11
- #device = torch.device('cpu' if not has_cuda else 'cuda')
12
  pipe = pipe.to(device)
13
 
14
- def convert(prompt):
15
- with autocast("cuda"):
16
- image = pipe(prompt)["sample"][0]
17
- return image
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- gr.Interface(convert,
20
- inputs = [gr.inputs.Textbox(label="Enter text")],
21
- outputs = [gr.outputs.Image(label="Generated Image")],
22
- title="Text to Image Generation").launch()
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
  import torch
3
  from torch import autocast
4
  from diffusers import StableDiffusionPipeline
 
5
 
6
  model_id = "CompVis/stable-diffusion-v1-4"
7
+ device = "cuda"
8
+
9
+ pipe = StableDiffusionPipeline.from_pretrained(model_id, use_auth_token='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', torch_dtype=torch.float16, low_cpu_mem_usage=True)
 
10
  pipe = pipe.to(device)
11
 
12
+ def inference(diffusion_prompt):
13
+ samples = 4
14
+ generator = torch.Generator(device=device)
15
+ torch.cuda.empty_cache()
16
+ with autocast("cuda"):
17
+ images_list = pipe(
18
+ [diffusion_prompt] * samples,
19
+ height=256, width=384,
20
+ num_inference_steps=50,
21
+ )
22
+ images = []
23
+ for i, image in enumerate(images_list["sample"]):
24
+ images.append(image)
25
+ return images
26
+
27
 
28
+ with gr.Blocks() as demo:
29
+ gr.Markdown("# Text to Image Generator")
30
+ with gr.Row():
31
+ prompt = gr.Textbox(
32
+ lines=1,
33
+ placeholder="Enter your prompt..",
34
+ interactive=True,
35
+ label="Prompt"
36
+ )
37
+ submit = gr.Button("Run")
38
+ submit.click(fn=inference, inputs=[prompt], outputs=[images])
39
+ demo.launch()