|
|
|
from flask import Flask, request, jsonify, send_from_directory, url_for, render_template, send_file |
|
from flask_cors import CORS |
|
import os |
|
import ctypes |
|
import store_helper as sh |
|
import block_builder |
|
import sd_generator as sd |
|
|
|
|
|
libc = ctypes.cdll.LoadLibrary("libc.so.6") |
|
M_MMAP_THRESHOLD = -3 |
|
|
|
|
|
libc.mallopt(M_MMAP_THRESHOLD, 2**20) |
|
|
|
|
|
app = Flask(__name__) |
|
os.makedirs('static/images', exist_ok=True) |
|
|
|
|
|
@app.route('/static/<path:filename>') |
|
def custom_static(filename): |
|
return send_from_directory('static', filename) |
|
|
|
|
|
@app.route('/<path:filename>') |
|
def serve_html(filename): |
|
return send_from_directory('.', filename) |
|
|
|
@app.after_request |
|
def apply_headers(response): |
|
response.headers["Cross-Origin-Opener-Policy"] = "same-origin-allow-popups" |
|
return response |
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/') |
|
def index(): |
|
css_files = { |
|
'all_css': url_for('static', filename='all.css'), |
|
'font_css': url_for('static', filename='css.css?family=Open+Sans:400,300,600,700'), |
|
'bundle_css': url_for('static', filename='bundle.css'), |
|
'style_css': url_for('static', filename='style.css'), |
|
'phb_style_css': url_for('static', filename='5ePHBstyle.css'), |
|
'store_ui_css': url_for('static', filename='storeUI.css') |
|
} |
|
return render_template('storeUI.html', css_files=css_files) |
|
|
|
|
|
CORS(app) |
|
|
|
@app.route('/process-description', methods=['POST']) |
|
def process_description(): |
|
data = request.json |
|
user_input = data.get('user_input', '') |
|
|
|
print(f"Received user input: {user_input}") |
|
|
|
llm_output = sh.call_llm_and_cleanup(user_input) |
|
processed_blocks = block_builder.build_blocks(llm_output, block_builder.block_id) |
|
|
|
|
|
return jsonify({'html_blocks': processed_blocks}) |
|
|
|
@app.route('/generate-image', methods=['POST']) |
|
def generate_image(): |
|
data = request.get_json() |
|
image_subject = data.get('store_name') |
|
sd_prompt = data.get('sd_prompt') |
|
image_subject_name = data.get('store_front_sd_prompt') |
|
|
|
if not sd_prompt: |
|
return jsonify({'error': 'Missing sd_prompt'}), 400 |
|
image_subject = data.get('image_subject') |
|
if not sd_prompt: |
|
return jsonify({'error': 'Missing sd_prompt'}), 400 |
|
|
|
try: |
|
image_url = sd.preview_and_generate_image(image_subject,image_subject_name, sd_prompt) |
|
return jsonify({'image_url': image_url}) |
|
except Exception as e: |
|
return jsonify({'error': str(e)}), 500 |
|
|
|
|
|
@app.route('/proxy.html', methods=['POST']) |
|
def proxy(): |
|
html_content = request.form.get('htmlContent', '') |
|
|
|
|
|
return render_template('proxy.html', html_content=html_content) |
|
|
|
|
|
if __name__ == "__main__": |
|
app.run(host="0.0.0.0", port=7860, debug=True) |
|
|