Create server.py
Browse files
server.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
|
3 |
+
import torch
|
4 |
+
|
5 |
+
app = Flask(__name__)
|
6 |
+
|
7 |
+
# ** Hafif SD Turbo Modeli ve LoRA Yükleme **
|
8 |
+
base_model = "stabilityai/sd-turbo" # Hafif model
|
9 |
+
lora_model = "maria26/Floor_Plan_LoRA"
|
10 |
+
|
11 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
12 |
+
base_model, torch_dtype=torch.float16, safety_checker=None
|
13 |
+
)
|
14 |
+
|
15 |
+
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
16 |
+
|
17 |
+
# LoRA'yı yükle
|
18 |
+
pipe.load_lora_weights(lora_model)
|
19 |
+
|
20 |
+
# Eğer GPU yetmezse CPU'ya geçir
|
21 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
22 |
+
pipe.to(device)
|
23 |
+
|
24 |
+
@app.route('/generate', methods=['POST'])
|
25 |
+
def generate():
|
26 |
+
data = request.json
|
27 |
+
prompt = data.get("prompt", "a simple architectural floor plan")
|
28 |
+
|
29 |
+
try:
|
30 |
+
image = pipe(prompt).images[0]
|
31 |
+
image_path = "static/output.png"
|
32 |
+
image.save(image_path)
|
33 |
+
return jsonify({"status": "success", "image_url": image_path})
|
34 |
+
except Exception as e:
|
35 |
+
return jsonify({"status": "error", "message": str(e)})
|
36 |
+
|
37 |
+
if __name__ == '__main__':
|
38 |
+
app.run(host="0.0.0.0", port=5000, debug=True)
|