Beeniebeen commited on
Commit
367401f
โ€ข
1 Parent(s): a188063

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -48
app.py CHANGED
@@ -1,60 +1,32 @@
1
  import gradio as gr
2
- import numpy as np
3
- import random
4
- from diffusers import StableDiffusionPipeline
5
  import torch
 
6
  from safetensors.torch import load_file
7
- import os
8
 
9
- # CUDA ์‚ฌ์šฉ ์—ฌ๋ถ€ ํ™•์ธ
10
- device = "cuda" if torch.cuda.is_available() else "cpu"
11
-
12
- # ๋ชจ๋ธ ๋กœ๋“œ ํ•จ์ˆ˜
13
  def load_safetensors_model(model_id, model_file):
14
- try:
15
- # safetensors ํŒŒ์ผ ๋กœ๋“œ
16
- state_dict = load_file(model_file)
17
- # ๋ชจ๋ธ ์ดˆ๊ธฐํ™”
18
- print("Loading model from pretrained...")
19
- model = StableDiffusionPipeline.from_pretrained(model_id)
20
- print("Model loaded from pretrained.")
21
- # ๋ชจ๋ธ์— state_dict ๋กœ๋“œ
22
- model.unet.load_state_dict(state_dict)
23
- model = model.to(device)
24
- print("Model state_dict loaded and moved to device.")
25
- return model
26
- except Exception as e:
27
- print(f"Error loading model: {e}")
28
- return None
29
 
30
- # ๋ชจ๋ธ ID์™€ ํŒŒ์ผ ๊ฒฝ๋กœ ์„ค์ •
31
- model_id = "stabilityai/stable-diffusion-2"
32
- model_file = "./Traditional_Korean_Painting_Model_2.safetensors"
33
 
34
- pipe = load_safetensors_model(model_id, model_file)
 
 
 
 
 
35
 
36
- MAX_SEED = np.iinfo(np.int32).max
37
- MAX_IMAGE_SIZE = 1024
38
-
39
- def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
40
  if not pipe:
41
- return "Model not loaded properly"
42
-
43
- if randomize_seed:
44
- seed = random.randint(0, MAX_SEED)
45
-
46
- generator = torch.Generator().manual_seed(seed)
47
-
48
- image = pipe(
49
- prompt=prompt,
50
- negative_prompt=negative_prompt,
51
- guidance_scale=guidance_scale,
52
- num_inference_steps=num_inference_steps,
53
- width=width,
54
- height=height,
55
- generator=generator
56
- ).images[0]
57
-
58
  return image
59
 
60
  examples = [
 
1
  import gradio as gr
 
 
 
2
  import torch
3
+ from diffusers import StableDiffusionPipeline
4
  from safetensors.torch import load_file
 
5
 
6
+ # safetensors ๋ชจ๋ธ ๋กœ๋“œ ํ•จ์ˆ˜
 
 
 
7
  def load_safetensors_model(model_id, model_file):
8
+ # safetensors ํŒŒ์ผ ๋กœ๋“œ
9
+ state_dict = load_file(model_file)
10
+ # ๋ชจ๋ธ ์ดˆ๊ธฐํ™”
11
+ model = StableDiffusionPipeline.from_pretrained(model_id)
12
+ # ๋ชจ๋ธ์— state_dict ๋กœ๋“œ
13
+ model.model.load_state_dict(state_dict)
14
+ return model
 
 
 
 
 
 
 
 
15
 
16
+ model_id = "gagong/Traditional-Korean-Painting-Model-v2.0"
17
+ model_file = "./Traditional_Korean_Painting_Model_2.safetensors" # ๋ฃจํŠธ ๋””๋ ‰ํ† ๋ฆฌ์— ์žˆ๋Š” ํŒŒ์ผ ๊ฒฝ๋กœ ์„ค์ •
 
18
 
19
+ try:
20
+ pipe = load_safetensors_model(model_id, model_file)
21
+ pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
22
+ except Exception as e:
23
+ print(f"Error loading model: {e}")
24
+ pipe = None
25
 
26
+ def generate_image(prompt):
 
 
 
27
  if not pipe:
28
+ return None
29
+ image = pipe(prompt).images[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  return image
31
 
32
  examples = [