Spaces:
Runtime error
Runtime error
Download autoexpress/views.py from damientheodore/AutoExpress: direct link, hf CLI and curl.
- Browser
- Download file 5.55 kB
-
https://huggingface.co/spaces/damientheodore/AutoExpress/resolve/main/autoexpress/views.py
- Command line
-
hf download hf://spaces/damientheodore/AutoExpress/autoexpress/views.py
-
curl -L -o views.py https://huggingface.co/spaces/damientheodore/AutoExpress/resolve/main/autoexpress/views.py
5.55 kB
| from flask import Flask, request, jsonify, render_template, send_from_directory | |
| from werkzeug.utils import secure_filename | |
| import os | |
| import pathlib | |
| from autoexpress.modules import ( | |
| a1111_client, | |
| image_parser, | |
| expression_generator, | |
| ) | |
| from loguru import logger as log | |
| import requests | |
| import re | |
| autoexpress = Flask(__name__) | |
| sd = a1111_client.A1111Client() | |
| uploaded = False | |
| filepath = None | |
| is_realistic = False | |
| # Assuming you want to save uploaded files in a folder called 'uploads' | |
| UPLOAD_FOLDER = "uploads" | |
| MAX_FILES = 10 | |
| autoexpress.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER | |
| if not os.path.exists(UPLOAD_FOLDER): | |
| os.makedirs(UPLOAD_FOLDER) | |
| # AutoExpress UI | |
| def index(): | |
| return render_template("index.html") | |
| # Stable diffusion API Calls | |
| def get_models(): | |
| # Simulate fetching models from an API | |
| try: | |
| models = sd.models | |
| except requests.exceptions.ConnectionError: | |
| models = [] | |
| return jsonify(models) | |
| def get_samplers(): | |
| # Simulate fetching models from an API | |
| try: | |
| samplers = sd.samplers | |
| except requests.exceptions.ConnectionError: | |
| samplers = [] | |
| return jsonify(samplers) | |
| def get_loras(): | |
| # Simulate fetching models from an API | |
| try: | |
| loras = sd.loras | |
| except requests.exceptions.ConnectionError: | |
| loras = [] | |
| return jsonify(loras) | |
| # End of Stable diffusion API Calls | |
| # Image uploaded | |
| def upload_file(): | |
| if "file" not in request.files: | |
| return jsonify({"error": "No file part"}), 400 | |
| file = request.files["file"] | |
| if file.filename == "": | |
| return jsonify({"error": "No selected file"}), 400 | |
| if file and allowed_file(file.filename): | |
| filename = secure_filename(file.filename) | |
| filepath = os.path.join(autoexpress.config["UPLOAD_FOLDER"], filename) | |
| file.save(filepath) | |
| full_image_data = {"cleaned_data": None, "uncleaned_data": None} | |
| full_image_data["cleaned_data"] = image_parser.generate_parameters(filepath) | |
| full_image_data["uncleaned_data"] = image_parser.generate_uncleaned_params(filepath) | |
| return ( | |
| jsonify(full_image_data), | |
| 200, | |
| ) | |
| def allowed_file(filename): | |
| return "." in filename and filename.rsplit(".", 1)[1].lower() in { | |
| "png", | |
| "jpg", | |
| "jpeg", | |
| "gif", | |
| } | |
| # Try connecting to SD | |
| def receive_data(): | |
| data = request.json | |
| url = data["text"] | |
| if url in [""]: | |
| sd.setURL("http://127.0.0.1:7860") | |
| log.info(f"No url found.") | |
| elif url[-1] in ["/"]: | |
| sd.setURL(url[:-1]) | |
| elif "http" in url: | |
| sd.setURL(url) | |
| else: | |
| sd.setURL("http://" + url) | |
| log.info("SD URL set to: " + sd.getURL()) | |
| return jsonify({"status": "success"}) | |
| # Generate Images | |
| def generate(): | |
| data = request.json | |
| adetailer_exists = sd.is_extension() | |
| if not adetailer_exists: | |
| return jsonify({"status": "Failed", "message": "Could not find adetailer"}) | |
| matches = get_lora_from_prompt(data.get("ad_prompt")) | |
| img_str = data.get("init_images") | |
| output_dir = data.get("output_dir") or "New_Character" | |
| if not matches and data.get("lora") not in [""]: | |
| data["ad_prompt"] += f" <lora: {data.get('lora')}: 0.8>" | |
| data.pop("output_dir") | |
| data.pop("lora") | |
| data.pop("init_images") | |
| log.info("Using the following generation parameters:\n" + str(data)) | |
| try: | |
| expression_generator.generate_expressions( | |
| sd=sd, | |
| image_str=img_str, | |
| output_path=f"Output/{output_dir}", | |
| settings=data, | |
| is_realistic=is_realistic, | |
| ) | |
| except KeyboardInterrupt: | |
| sd.interrupt() | |
| # Process data here, e.g., generate text based on the model and prompt | |
| return jsonify({"status": "success", "message": "Data processed successfully"}) | |
| def get_lora_from_prompt(text): | |
| if not text: | |
| return [] | |
| # Regular expression pattern to find text and strength | |
| pattern = r"<lora:(.*?):(.*?)>" | |
| # Find all matches | |
| matches = re.findall(pattern, text) | |
| return matches | |
| def list_images(subpath): | |
| root = pathlib.Path(autoexpress.root_path).parent | |
| directory = os.path.join(root, "Output", subpath) | |
| log.info("Attempting to list images from:", directory) # Debugging statement | |
| try: | |
| files = [ | |
| f | |
| for f in os.listdir(directory) | |
| if os.path.isfile(os.path.join(directory, f)) | |
| ] | |
| return jsonify(files) | |
| except FileNotFoundError: | |
| log.info("Directory not found:", directory) # Debugging statement | |
| return jsonify({"error": "Directory not found"}), 404 | |
| def get_image(filename): | |
| """Endpoint to serve images from the entire 'Output' directory.""" | |
| root_path = pathlib.Path(autoexpress.root_path).parent | |
| return send_from_directory(os.path.join(root_path, "Output"), filename) | |
| def handle_toggle(): | |
| global is_realistic | |
| data = request.get_json() | |
| is_realistic = data.get("isRealistic") | |
| return jsonify({"message": f"Is realistic status set to {is_realistic}"}) | |