dedlepexa commited on
Commit
d21beae
·
verified ·
1 Parent(s): ef0fa30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -33
app.py CHANGED
@@ -1,8 +1,7 @@
1
  from fastapi import FastAPI
2
  from fastapi.responses import PlainTextResponse, FileResponse
3
- from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
4
  import torch
5
- import uvicorn
6
  import threading
7
  import time
8
  from collections import OrderedDict
@@ -13,31 +12,28 @@ app = FastAPI()
13
  # 🔥 CPU оптимизация
14
  torch.set_num_threads(2)
15
 
16
- # 🔥 MODEL
17
- model_name = "segmind/small-sd"
18
 
19
  pipe = StableDiffusionPipeline.from_pretrained(
20
  model_name,
21
- torch_dtype=torch.float32
 
22
  )
23
 
24
  pipe = pipe.to("cpu")
25
 
26
- # 🔥 быстрый scheduler
27
- pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
 
 
28
 
29
- # 🔥 CPU ускорения
30
- pipe.enable_attention_slicing()
31
- pipe.enable_sequential_cpu_offload()
32
 
33
- # 🔥 доп. ускорение
34
- try:
35
- pipe.unet.to(memory_format=torch.channels_last)
36
- except:
37
- pass
38
 
39
  MAX_HISTORY = 40
40
- NUM_WORKERS = 1 # важно для CPU
41
 
42
  db = OrderedDict()
43
  queue = []
@@ -46,7 +42,7 @@ IMG_DIR = "images"
46
  os.makedirs(IMG_DIR, exist_ok=True)
47
 
48
 
49
- # 🔥 генерация изображения (максимально облегчённая)
50
  def generate_ai_stream(message: str):
51
 
52
  try:
@@ -54,8 +50,8 @@ def generate_ai_stream(message: str):
54
 
55
  image = pipe(
56
  message,
57
- num_inference_steps=4, # 🔥 минимум
58
- guidance_scale=5.0,
59
  height=256,
60
  width=256
61
  ).images[0]
@@ -77,7 +73,7 @@ def generate_ai_stream(message: str):
77
  return result
78
 
79
 
80
- # 🔥 worker очередь
81
  def worker():
82
  while True:
83
  if queue:
@@ -88,29 +84,22 @@ def worker():
88
 
89
  generate_ai_stream(message)
90
  else:
91
- time.sleep(0.05)
92
 
93
 
94
- # 🔥 запуск воркера
95
  threading.Thread(target=worker, daemon=True).start()
96
 
97
 
98
  @app.get("/")
99
  async def root():
100
- return PlainTextResponse("🎨 SD CPU optimized server running")
101
 
102
 
103
  @app.get("/ask")
104
  async def ask(message: str):
105
 
106
- if message in db and db[message]["status"] == "done":
107
- return PlainTextResponse("cached")
108
-
109
  if message not in db:
110
- db[message] = {
111
- "status": "pending",
112
- "reply": ""
113
- }
114
  queue.append(message)
115
 
116
  if len(db) > MAX_HISTORY:
@@ -135,11 +124,9 @@ async def get(message: str):
135
 
136
  @app.get("/image")
137
  async def get_image(path: str):
138
- if not os.path.exists(path):
139
- return PlainTextResponse("file not found")
140
-
141
  return FileResponse(path)
142
 
143
 
144
  if __name__ == "__main__":
 
145
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
  from fastapi import FastAPI
2
  from fastapi.responses import PlainTextResponse, FileResponse
3
+ from diffusers import StableDiffusionPipeline
4
  import torch
 
5
  import threading
6
  import time
7
  from collections import OrderedDict
 
12
  # 🔥 CPU оптимизация
13
  torch.set_num_threads(2)
14
 
15
+ model_name = "Lykon/dreamshaper-7"
 
16
 
17
  pipe = StableDiffusionPipeline.from_pretrained(
18
  model_name,
19
+ torch_dtype=torch.float32,
20
+ safety_checker=None
21
  )
22
 
23
  pipe = pipe.to("cpu")
24
 
25
+ # 🔥 LCM LOADING (КЛЮЧЕВОЕ)
26
+ pipe.load_lora_weights(
27
+ "latent-consistency/lcm-lora-sdv1-5"
28
+ )
29
 
30
+ pipe.fuse_lora()
 
 
31
 
32
+ # ❌ никаких scheduler замен (важно!)
33
+ pipe.enable_attention_slicing()
 
 
 
34
 
35
  MAX_HISTORY = 40
36
+ NUM_WORKERS = 1
37
 
38
  db = OrderedDict()
39
  queue = []
 
42
  os.makedirs(IMG_DIR, exist_ok=True)
43
 
44
 
45
+ # 🔥 генерация LCM (очень быстрый режим)
46
  def generate_ai_stream(message: str):
47
 
48
  try:
 
50
 
51
  image = pipe(
52
  message,
53
+ num_inference_steps=4, # 🔥 LCM: можно даже 2-4
54
+ guidance_scale=1.5, # 🔥 LCM любит низкий CFG
55
  height=256,
56
  width=256
57
  ).images[0]
 
73
  return result
74
 
75
 
76
+ # 🔥 worker
77
  def worker():
78
  while True:
79
  if queue:
 
84
 
85
  generate_ai_stream(message)
86
  else:
87
+ time.sleep(0.03)
88
 
89
 
 
90
  threading.Thread(target=worker, daemon=True).start()
91
 
92
 
93
  @app.get("/")
94
  async def root():
95
+ return PlainTextResponse("⚡ LCM FAST IMAGE SERVER RUNNING")
96
 
97
 
98
  @app.get("/ask")
99
  async def ask(message: str):
100
 
 
 
 
101
  if message not in db:
102
+ db[message] = {"status": "pending", "reply": ""}
 
 
 
103
  queue.append(message)
104
 
105
  if len(db) > MAX_HISTORY:
 
124
 
125
  @app.get("/image")
126
  async def get_image(path: str):
 
 
 
127
  return FileResponse(path)
128
 
129
 
130
  if __name__ == "__main__":
131
+ import uvicorn
132
  uvicorn.run(app, host="0.0.0.0", port=7860)