Update app.py
Browse files
app.py
CHANGED
|
@@ -1,149 +1,63 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
from pathlib import Path
|
| 6 |
-
from flask import Flask, request, send_file, jsonify, send_from_directory
|
| 7 |
-
from PIL import Image
|
| 8 |
-
from database import init_db, close_db, get_db, get_generate_list, add_to_generate_list, get_db_path
|
| 9 |
|
| 10 |
-
# Proje yolu ayarı
|
| 11 |
-
sys.path.insert(0, str(Path(__file__).parent))
|
| 12 |
-
|
| 13 |
-
# Modül importları
|
| 14 |
-
from demo_generate.halftone_method import apply_halftone
|
| 15 |
-
from demo_generate.animegan_method import generate_anime
|
| 16 |
-
from demo_generate.rembg_method import remove_background
|
| 17 |
-
|
| 18 |
-
# Ortam ayarları
|
| 19 |
-
os.environ['NUMBA_DISABLE_JIT'] = '1'
|
| 20 |
-
os.environ['TORCH_HOME'] = '/tmp/torch_cache'
|
| 21 |
-
os.environ['U2NET_HOME'] = '/tmp/.u2net'
|
| 22 |
-
|
| 23 |
-
# Önbellek klasörleri
|
| 24 |
-
os.makedirs('/tmp/torch_cache', exist_ok=True)
|
| 25 |
-
os.makedirs('/tmp/.u2net', exist_ok=True)
|
| 26 |
-
|
| 27 |
-
# Log ayarı
|
| 28 |
-
logging.basicConfig(level=logging.INFO)
|
| 29 |
-
logger = logging.getLogger(__name__)
|
| 30 |
-
|
| 31 |
-
# Flask uygulaması
|
| 32 |
app = Flask(__name__)
|
| 33 |
-
init_db(app)
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
# Ana sayfa
|
| 37 |
@app.route('/')
|
| 38 |
def home():
|
| 39 |
-
return "
|
| 40 |
-
|
| 41 |
-
# Generate List Endpoint'leri
|
| 42 |
-
@app.route('/generate_list', methods=['GET'])
|
| 43 |
-
def handle_get_generate_list():
|
| 44 |
-
try:
|
| 45 |
-
db = get_db()
|
| 46 |
-
items = get_generate_list(db)
|
| 47 |
-
return jsonify({
|
| 48 |
-
"data": {
|
| 49 |
-
"generate_list": [dict(item) for item in items]
|
| 50 |
-
},
|
| 51 |
-
"success": True
|
| 52 |
-
})
|
| 53 |
-
except Exception as e:
|
| 54 |
-
logger.error(f"Hata: {str(e)}")
|
| 55 |
-
return jsonify({
|
| 56 |
-
"data": None,
|
| 57 |
-
"success": False,
|
| 58 |
-
"error": str(e)
|
| 59 |
-
}), 500
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
try:
|
| 66 |
-
if 'image' not in request.files:
|
| 67 |
-
return jsonify({'error': 'Resim yüklenmedi'}), 400
|
| 68 |
-
|
| 69 |
-
file = request.files['image']
|
| 70 |
-
if not file.filename:
|
| 71 |
-
return jsonify({'error': 'Geçersiz dosya'}), 400
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
logger.error(f"Hata: {str(e)}")
|
| 78 |
-
return jsonify({'error': 'İşlem sırasında hata oluştu'}), 500
|
| 79 |
|
| 80 |
-
#
|
| 81 |
-
@app.route('/
|
| 82 |
-
def
|
| 83 |
try:
|
| 84 |
-
if
|
| 85 |
-
return jsonify({
|
| 86 |
-
|
| 87 |
-
file = request.files['image']
|
| 88 |
-
if file.filename == '':
|
| 89 |
-
return jsonify({'error': 'Boş dosya'}), 400
|
| 90 |
-
|
| 91 |
-
output = remove_background(file.read())
|
| 92 |
-
img_io = io.BytesIO()
|
| 93 |
-
output.save(img_io, 'PNG')
|
| 94 |
-
img_io.seek(0)
|
| 95 |
-
return send_file(img_io, mimetype='image/png')
|
| 96 |
-
|
| 97 |
except Exception as e:
|
| 98 |
-
|
| 99 |
-
return jsonify({'error': str(e)}), 500
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
def halftone_route():
|
| 104 |
try:
|
| 105 |
-
if
|
| 106 |
-
return jsonify({
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
output = apply_halftone(file.read(), dot_size)
|
| 116 |
-
|
| 117 |
-
img_io = io.BytesIO()
|
| 118 |
-
output.save(img_io, 'PNG')
|
| 119 |
-
img_io.seek(0)
|
| 120 |
-
return send_file(img_io, mimetype='image/png')
|
| 121 |
-
|
| 122 |
except Exception as e:
|
| 123 |
-
|
| 124 |
-
return jsonify({'error': str(e)}), 500
|
| 125 |
-
|
| 126 |
-
# Veritabanı işlemleri
|
| 127 |
-
@app.route('/users', methods=['GET'])
|
| 128 |
-
def get_users_route():
|
| 129 |
-
from get_methods import get_users
|
| 130 |
-
return get_users()
|
| 131 |
-
|
| 132 |
-
@app.route('/user/<string:platform>/<string:udid>', methods=['GET'])
|
| 133 |
-
def get_user_route(udid, platform):
|
| 134 |
-
from get_methods import get_user
|
| 135 |
-
return get_user(udid, platform)
|
| 136 |
-
|
| 137 |
-
@app.route('/add-user', methods=['POST'])
|
| 138 |
-
def add_user_route():
|
| 139 |
-
from post_methods import add_user
|
| 140 |
-
return add_user()
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
# Kapatma
|
| 144 |
-
@app.teardown_appcontext
|
| 145 |
-
def shutdown_session(exception=None):
|
| 146 |
-
close_db()
|
| 147 |
|
| 148 |
-
|
| 149 |
-
app.run(host='0.0.0.0', port=7860
|
|
|
|
| 1 |
+
app.py
|
| 2 |
+
from flask import Flask, request, jsonify
|
| 3 |
+
import numpy as np
|
| 4 |
+
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
app = Flask(__name__)
|
|
|
|
| 7 |
|
| 8 |
+
# Paylaşılan veri
|
| 9 |
+
shared_data = {
|
| 10 |
+
"users": [
|
| 11 |
+
{"id": 1, "name": "Ahmet", "score": 85},
|
| 12 |
+
{"id": 2, "name": "Mehmet", "score": 92}
|
| 13 |
+
],
|
| 14 |
+
"models": ["model1", "model2", "model3"]
|
| 15 |
+
}
|
| 16 |
|
| 17 |
# Ana sayfa
|
| 18 |
@app.route('/')
|
| 19 |
def home():
|
| 20 |
+
return "stablediffusionapi test"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# GET Endpoint'leri
|
| 23 |
+
@app.route('/users', methods=['GET'])
|
| 24 |
+
def get_users():
|
| 25 |
+
return jsonify({"users": shared_data["users"]})
|
| 26 |
|
| 27 |
+
@app.route('/random-model', methods=['GET'])
|
| 28 |
+
def random_model():
|
| 29 |
+
return jsonify({"selected_model": random.choice(shared_data["models"])})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
@app.route('/user/<int:user_id>', methods=['GET'])
|
| 32 |
+
def get_user(user_id):
|
| 33 |
+
user = next((u for u in shared_data["users"] if u["id"] == user_id), None)
|
| 34 |
+
return jsonify(user) if user else (jsonify({"error": "User not found"}), 404)
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# POST Endpoint'leri
|
| 37 |
+
@app.route('/predict', methods=['POST'])
|
| 38 |
+
def predict():
|
| 39 |
try:
|
| 40 |
+
if not request.json:
|
| 41 |
+
return jsonify({"error": "No data received"}), 400
|
| 42 |
+
return jsonify({"probability_up": np.random.rand()})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
except Exception as e:
|
| 44 |
+
return jsonify({"error": str(e)}), 500
|
|
|
|
| 45 |
|
| 46 |
+
@app.route('/add-user', methods=['POST'])
|
| 47 |
+
def add_user():
|
|
|
|
| 48 |
try:
|
| 49 |
+
if not request.json or "name" not in request.json:
|
| 50 |
+
return jsonify({"error": "Name is required"}), 400
|
| 51 |
|
| 52 |
+
new_user = {
|
| 53 |
+
"id": len(shared_data["users"]) + 1,
|
| 54 |
+
"name": request.json["name"],
|
| 55 |
+
"score": request.json.get("score", 0)
|
| 56 |
+
}
|
| 57 |
+
shared_data["users"].append(new_user)
|
| 58 |
+
return jsonify({"message": "User added", "user": new_user}), 201
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
+
return jsonify({"error": str(e)}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
if __name__ == '__main__':
|
| 63 |
+
app.run(host='0.0.0.0', port=7860)
|