chandrakalagowda commited on
Commit
196732e
1 Parent(s): 70ad05b

Upload 4 files

Browse files
Files changed (4) hide show
  1. README.md +6 -5
  2. app.py +65 -0
  3. model.py +67 -0
  4. requirements.txt +9 -0
README.md CHANGED
@@ -1,12 +1,13 @@
1
  ---
2
- title: Testimg
3
- emoji:
4
- colorFrom: pink
5
- colorTo: pink
6
  sdk: gradio
7
- sdk_version: 3.35.2
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: SD Txt2img
3
+ emoji: 😻
4
+ colorFrom: purple
5
+ colorTo: gray
6
  sdk: gradio
7
+ sdk_version: 3.29.0
8
  app_file: app.py
9
  pinned: false
10
+ license: creativeml-openrail-m
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ def generateImage(prompt, n_prompt, modelName):
4
+
5
+ return models[modelName].process(prompt, n_prompt)
6
+
7
+
8
+
9
+
10
+ def create_demo():
11
+
12
+ with gr.Blocks() as demo:
13
+ with gr.Column():
14
+ prompt = gr.Textbox(label='Prompt')
15
+ n_prompt = gr.Textbox(
16
+ label='Negative Prompt',
17
+ value=
18
+ 'ugly, disfigured, deformed'
19
+ )
20
+
21
+ modelName = gr.Dropdown(choices = list(models.keys()),
22
+ label = "Model",
23
+ value=list(models.keys())[0])
24
+
25
+ run_button = gr.Button('Run')
26
+ gr.Markdown("### [Stable Diffusion Art](https://stable-diffusion-art.com/) -- tutorials and resources. Read [Model license](https://huggingface.co/spaces/CompVis/stable-diffusion-license).")
27
+ result = gr.Gallery(label='Output',
28
+ show_label=False,
29
+ elem_id='gallery').style(columns=1, rows=1, preview=True)
30
+
31
+
32
+ inputs = [
33
+ prompt,
34
+ n_prompt,
35
+ modelName,
36
+ ]
37
+
38
+ prompt.submit(
39
+ fn=generateImage,
40
+ inputs=inputs,
41
+ outputs=result
42
+ )
43
+ n_prompt.submit(
44
+ fn=generateImage,
45
+ inputs=inputs,
46
+ outputs=result
47
+ )
48
+
49
+ run_button.click(
50
+ fn=generateImage,
51
+ inputs=inputs,
52
+ outputs=result
53
+ )
54
+ return demo
55
+
56
+
57
+ if __name__ == '__main__':
58
+ from model import Model
59
+ models = {
60
+ "Stable Diffusion v1.5": Model("runwayml/stable-diffusion-v1-5"),
61
+ "Realistic Vision v2.0": Model("SG161222/Realistic_Vision_V2.0"),
62
+ "Anything v3.0": Model("Linaqruf/anything-v3.0")
63
+ }
64
+ demo = create_demo()
65
+ demo.queue().launch()
model.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+
4
+ from diffusers import StableDiffusionPipeline, EulerDiscreteScheduler
5
+ from diffusers import DPMSolverMultistepScheduler
6
+ import torch
7
+ import PIL.Image
8
+ import numpy as np
9
+ import datetime
10
+
11
+ # # Check environment
12
+ # print(f"Is CUDA available: {torch.cuda.is_available()}")
13
+ # # True
14
+ # print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
15
+ # # Tesla T4
16
+
17
+
18
+ device = "cpu"
19
+
20
+
21
+
22
+ class Model:
23
+ def __init__(self, modelID):
24
+ #modelID = "runwayml/stable-diffusion-v1-5"
25
+
26
+ self.modelID = modelID
27
+ self.pipe = StableDiffusionPipeline.from_pretrained(modelID, torch_dtype=torch.float16)
28
+ self.pipe = self.pipe.to(device)
29
+ self.pipe.scheduler = DPMSolverMultistepScheduler.from_config(self.pipe.scheduler.config)
30
+ self.pipe.enable_xformers_memory_efficient_attention()
31
+
32
+ #self.pipe = StableDiffusionPipeline.from_pretrained(modelID)
33
+ #prompt = "a photo of an astronaut riding a horse on mars"
34
+ #n_prompt = "deformed, disfigured"
35
+
36
+ def process(self,
37
+ prompt: str,
38
+ negative_prompt: str,
39
+ guidance_scale:int = 7,
40
+ num_images:int = 1,
41
+ num_steps:int = 20,
42
+ ):
43
+ seed = np.random.randint(0, np.iinfo(np.int32).max)
44
+ generator = torch.Generator(device).manual_seed(seed)
45
+ now = datetime.datetime.now()
46
+ print(now)
47
+ print(self.modelID)
48
+ print(prompt)
49
+ print(negative_prompt)
50
+ with torch.inference_mode():
51
+ images = self.pipe(prompt=prompt,
52
+ negative_prompt=negative_prompt,
53
+ guidance_scale=guidance_scale,
54
+ num_images_per_prompt=num_images,
55
+ num_inference_steps=num_steps,
56
+ generator=generator).images
57
+
58
+ return images
59
+
60
+
61
+
62
+ # image = pipeline(prompt=prompt,
63
+ # negative_prompt = n_prompt,
64
+ # num_inference_steps = 2,
65
+ # guidance_scale = 7).images
66
+
67
+
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ --extra-index-url https://download.pytorch.org/whl/cu113
2
+ torch
3
+ accelerate==0.18.0
4
+ diffusers==0.16.0
5
+ gradio==3.30.0
6
+ safetensors==0.3.0
7
+ torchvision==0.15.1
8
+ transformers==4.28.1
9
+ xformers==0.0.18