Spaces:
Paused
Paused
| from flask import Flask, request, jsonify, send_file, render_template_string | |
| import requests | |
| import io | |
| import os | |
| import random | |
| from PIL import Image | |
| from deep_translator import GoogleTranslator | |
| app = Flask(__name__) | |
| API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev" | |
| API_TOKEN = os.getenv("HF_READ_TOKEN") | |
| headers = {"Authorization": f"Bearer {API_TOKEN}"} | |
| timeout = 100 | |
| # Function to query the API and return the generated image | |
| def query(prompt, negative_prompt="", steps=35, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7, width=1024, height=1024): | |
| if not prompt: | |
| return None, "Prompt is required" | |
| key = random.randint(0, 999) | |
| # Translate the prompt from Russian to English if necessary | |
| prompt = GoogleTranslator(source='ru', target='en').translate(prompt) | |
| print(f'Generation {key} translation: {prompt}') | |
| # Add some extra flair to the prompt | |
| prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect." | |
| print(f'Generation {key}: {prompt}') | |
| payload = { | |
| "inputs": prompt, | |
| "is_negative": False, | |
| "steps": steps, | |
| "cfg_scale": cfg_scale, | |
| "seed": seed if seed != -1 else random.randint(1, 1000000000), | |
| "strength": strength, | |
| "parameters": { | |
| "width": width, | |
| "height": height | |
| } | |
| } | |
| # Send the request to the API | |
| try: | |
| response = requests.post(API_URL, headers=headers, json=payload, timeout=timeout) | |
| if response.status_code != 200: | |
| return None, f"Error: Failed to get image. Status code: {response.status_code}, Details: {response.text}" | |
| image_bytes = response.content | |
| image = Image.open(io.BytesIO(image_bytes)) | |
| return image, None | |
| except Exception as e: | |
| return None, f"Error when trying to open the image: {e}" | |
| # HTML template for the index page | |
| index_html = """ | |
| <!DOCTYPE html> | |
| <html lang="ja"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>FLUX.1-Dev Image Generator</title> | |
| </head> | |
| <body> | |
| <h1>FLUX.1-Dev Image Generator</h1> | |
| <form action="/generate" method="get"> | |
| <label for="prompt">Prompt:</label> | |
| <input type="text" id="prompt" name="prompt" placeholder="Enter your prompt" required><br><br> | |
| <label for="negative_prompt">Negative Prompt:</label> | |
| <input type="text" id="negative_prompt" name="negative_prompt" value="(deformed, distorted, disfigured), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, misspellings, typos"><br><br> | |
| <label for="width">Width:</label> | |
| <input type="number" id="width" name="width" value="1024"><br><br> | |
| <label for="height">Height:</label> | |
| <input type="number" id="height" name="height" value="1024"><br><br> | |
| <label for="steps">Sampling Steps:</label> | |
| <input type="number" id="steps" name="steps" value="35"><br><br> | |
| <label for="cfgs">CFG Scale:</label> | |
| <input type="number" id="cfgs" name="cfgs" value="7"><br><br> | |
| <label for="sampler">Sampling Method:</label> | |
| <select id="sampler" name="sampler"> | |
| <option value="DPM++ 2M Karras">DPM++ 2M Karras</option> | |
| <option value="DPM++ SDE Karras">DPM++ SDE Karras</option> | |
| <option value="Euler">Euler</option> | |
| <option value="Euler a">Euler a</option> | |
| <option value="Heun">Heun</option> | |
| <option value="DDIM">DDIM</option> | |
| </select><br><br> | |
| <label for="strength">Strength:</label> | |
| <input type="number" id="strength" name="strength" value="0.7" step="0.01" min="0" max="1"><br><br> | |
| <label for="seed">Seed:</label> | |
| <input type="number" id="seed" name="seed" value="-1" step="1"><br><br> | |
| <button type="submit">Generate Image</button> | |
| </form> | |
| </body> | |
| </html> | |
| """ | |
| def index(): | |
| return render_template_string(index_html) | |
| def generate_image(): | |
| # Retrieve query parameters | |
| prompt = request.args.get("prompt", "") | |
| negative_prompt = request.args.get("negative_prompt", "") | |
| steps = int(request.args.get("steps", 35)) | |
| cfg_scale = float(request.args.get("cfgs", 7)) | |
| sampler = request.args.get("sampler", "DPM++ 2M Karras") | |
| seed = int(request.args.get("seed", -1)) | |
| strength = float(request.args.get("strength", 0.7)) | |
| width = int(request.args.get("width", 1024)) | |
| height = int(request.args.get("height", 1024)) | |
| # Call the query function to generate the image | |
| image, error = query(prompt, negative_prompt, steps, cfg_scale, sampler, seed, strength, width, height) | |
| if error: | |
| return jsonify({"error": error}), 500 | |
| # Save the image to a buffer | |
| img_io = io.BytesIO() | |
| image.save(img_io, 'PNG') | |
| img_io.seek(0) | |
| # Return the image | |
| return send_file(img_io, mimetype='image/png') | |
| if __name__ == '__main__': | |
| app.run(host='0.0.0.0', port=7860) | |