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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -25
app.py CHANGED
@@ -1,7 +1,6 @@
1
  from fastapi import FastAPI
2
  from fastapi.responses import PlainTextResponse, FileResponse
3
- from pydantic import BaseModel
4
- from diffusers import StableDiffusionPipeline
5
  import torch
6
  import uvicorn
7
  import threading
@@ -11,56 +10,62 @@ import os
11
 
12
  app = FastAPI()
13
 
14
- # 🔥 MODEL (small Stable Diffusion)
 
 
 
15
  model_name = "segmind/small-sd"
16
 
17
  pipe = StableDiffusionPipeline.from_pretrained(
18
  model_name,
19
- torch_dtype=torch.float16
20
  )
21
 
22
- device = "cuda" if torch.cuda.is_available() else "cpu"
23
- pipe = pipe.to(device)
 
 
24
 
25
- # 🔥 оптимизации
26
  pipe.enable_attention_slicing()
 
27
 
28
- # если доступно
29
  try:
30
- pipe.enable_xformers_memory_efficient_attention()
31
  except:
32
  pass
33
 
34
-
35
  MAX_HISTORY = 40
36
- NUM_WORKERS = 2 # меньше воркеров для GPU
37
 
38
  db = OrderedDict()
39
  queue = []
40
 
41
- # папка для изображений
42
  IMG_DIR = "images"
43
  os.makedirs(IMG_DIR, exist_ok=True)
44
 
45
 
46
- class Message(BaseModel):
47
- message: str
48
-
49
-
50
- # 🔥 генерация изображения
51
  def generate_ai_stream(message: str):
52
 
53
  try:
 
 
54
  image = pipe(
55
  message,
56
- num_inference_steps=15,
57
- guidance_scale=7.5
 
 
58
  ).images[0]
59
 
60
  filename = f"{IMG_DIR}/img_{int(time.time()*1000)}.png"
61
  image.save(filename)
62
 
63
- result = filename
 
 
64
 
65
  except Exception as e:
66
  result = f"error: {str(e)}"
@@ -86,14 +91,13 @@ def worker():
86
  time.sleep(0.05)
87
 
88
 
89
- # 🔥 запуск воркеров
90
- for _ in range(NUM_WORKERS):
91
- threading.Thread(target=worker, daemon=True).start()
92
 
93
 
94
  @app.get("/")
95
  async def root():
96
- return PlainTextResponse("🎨 Image AI server running (small Stable Diffusion)")
97
 
98
 
99
  @app.get("/ask")
@@ -129,7 +133,6 @@ async def get(message: str):
129
  return PlainTextResponse(data["reply"])
130
 
131
 
132
- # 🔥 отдача изображения
133
  @app.get("/image")
134
  async def get_image(path: str):
135
  if not os.path.exists(path):
 
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
 
10
 
11
  app = FastAPI()
12
 
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 = []
44
 
 
45
  IMG_DIR = "images"
46
  os.makedirs(IMG_DIR, exist_ok=True)
47
 
48
 
49
+ # 🔥 генерация изображения (максимально облегчённая)
 
 
 
 
50
  def generate_ai_stream(message: str):
51
 
52
  try:
53
+ start = time.time()
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]
62
 
63
  filename = f"{IMG_DIR}/img_{int(time.time()*1000)}.png"
64
  image.save(filename)
65
 
66
+ duration = round(time.time() - start, 2)
67
+
68
+ result = f"{filename} | {duration}s"
69
 
70
  except Exception as e:
71
  result = f"error: {str(e)}"
 
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")
 
133
  return PlainTextResponse(data["reply"])
134
 
135
 
 
136
  @app.get("/image")
137
  async def get_image(path: str):
138
  if not os.path.exists(path):