Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from flask import Flask, render_template, request, send_file
|
| 3 |
+
from diffusers import DiffusionPipeline
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
# Modeli yükleyin (modeli sadece bir kez yüklemek için global tanımlıyoruz)
|
| 10 |
+
pipe = DiffusionPipeline.from_pretrained("sd-legacy/stable-diffusion-v1-5")
|
| 11 |
+
|
| 12 |
+
# Ana sayfayı render et
|
| 13 |
+
@app.route('/')
|
| 14 |
+
def index():
|
| 15 |
+
return render_template('index.html') # `templates` dizinine taşıdığınızı varsayıyoruz.
|
| 16 |
+
|
| 17 |
+
# Görsel üretme fonksiyonu
|
| 18 |
+
@app.route('/generate', methods=['POST'])
|
| 19 |
+
def generate_image():
|
| 20 |
+
try:
|
| 21 |
+
# Kullanıcıdan gelen prompt verisini al
|
| 22 |
+
prompt = request.form['prompt']
|
| 23 |
+
|
| 24 |
+
# Modelden görsel üretme
|
| 25 |
+
image = pipe(prompt).images[0]
|
| 26 |
+
|
| 27 |
+
# Görseli bir byte stream'e dönüştür
|
| 28 |
+
img_io = BytesIO()
|
| 29 |
+
image.save(img_io, 'PNG')
|
| 30 |
+
img_io.seek(0)
|
| 31 |
+
|
| 32 |
+
# Görseli kullanıcıya döndür
|
| 33 |
+
return send_file(img_io, mimetype='image/png')
|
| 34 |
+
except Exception as e:
|
| 35 |
+
# Hata durumunda kullanıcıya bir mesaj döndür
|
| 36 |
+
return f"Bir hata oluştu: {str(e)}"
|
| 37 |
+
|
| 38 |
+
if __name__ == '__main__':
|
| 39 |
+
app.run(debug=True)
|