kusumakar commited on
Commit
393a5f3
1 Parent(s): 1446e80

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -53
app.py CHANGED
@@ -1,55 +1,3 @@
1
  import gradio as gr
2
- from PIL import Image
3
- import torch
4
- from torch import autocast
5
- from diffusers import StableDiffusionImg2ImgPipeline
6
- from torch import autocast
7
- from tqdm.auto import tqdm
8
- import requests
9
- from io import BytesIO
10
- from PIL import Image
11
- from typing import List, Optional, Union
12
- import inspect
13
- import warnings
14
- import sentence_transformers
15
 
16
- # Load the Stable Diffusion model
17
- modelid = "CompVis/stable-diffusion-v1-4"
18
- device = "cuda" if torch.cuda.is_available() else "cpu"
19
- pipe = StableDiffusionImg2ImgPipeline.from_pretrained(modelid, revision="fp16", torch_dtype=torch.float16)
20
- pipe.to(device)
21
-
22
- url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
23
- # Load the Sentence-BERT model for text embeddings
24
- text_embedding_model = sentence_transformers.SentenceTransformer("paraphrase-MiniLM-L6-v2")
25
-
26
-
27
- def generate_image(prompt):
28
- response = requests.get(url)
29
- init_img = Image.open(BytesIO(response.content)).convert("RGB")
30
- init_img = init_img.resize((768, 512))
31
-
32
- generator = torch.Generator(device=device).manual_seed(1024)
33
- with autocast("cuda"):
34
- prompt_embedding = text_embedding_model.encode([str(prompt)])[0]
35
- prompt_tensor = torch.tensor(prompt_embedding, device=device).half()
36
- image = pipe(prompt=prompt_tensor, init_image=init_img, strength=0.75, guidance_scale=7.5, generator=generator).images[0]
37
-
38
- return image
39
-
40
- # Define the input and output components
41
- input_text = gr.inputs.Textbox(lines=10, label="Enter a prompt")
42
- output_image = gr.outputs.Image(type="pil", label="Generated Image")
43
-
44
- # Create the Gradio interface
45
- iface = gr.Interface(
46
- fn=generate_image,
47
- inputs=input_text,
48
- outputs=output_image,
49
- title="Stable Bud",
50
- description="Generate images using Stable Diffusion",
51
- layout="vertical",
52
- )
53
-
54
- if __name__ == "__main__":
55
- iface.launch()
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
+ gr.Interface.load("models/CompVis/stable-diffusion-v1-4").launch()