Spaces:
Sleeping
Sleeping
| import requests | |
| import random | |
| from typing import Tuple | |
| import random | |
| import requests | |
| import time | |
| from PIL import Image | |
| from io import BytesIO | |
| from typing import Tuple | |
| from diffusers import DiffusionPipeline | |
| import torch | |
| from flask import Flask, request, jsonify | |
| app = Flask(__name__) | |
| style_list = [ | |
| { | |
| "name": "(No style)", | |
| "prompt": "{prompt}", | |
| "negative_prompt": "", | |
| }, | |
| { | |
| "name": "Cinematic", | |
| "prompt": "cinematic still {prompt} . emotional, harmonious, vignette, highly detailed, high budget, bokeh,realistic,textured, cinemascope, moody, epic, gorgeous, film grain, grainy", | |
| "negative_prompt": "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured", | |
| }, | |
| { | |
| "name": "Photographic", | |
| "prompt": "cinematic photo {prompt} . 35mm photograph, film, bokeh, professional, 4k, highly detailed", | |
| "negative_prompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly", | |
| }, | |
| { | |
| "name": "Anime", | |
| "prompt": "anime artwork {prompt} . anime style, key visual, vibrant, studio anime, highly detailed", | |
| "negative_prompt": "photo, deformed, black and white, realism, disfigured, low contrast", | |
| }, | |
| { | |
| "name": "Manga", | |
| "prompt": "manga style {prompt} . vibrant, high-energy, detailed, iconic, Japanese comic style", | |
| "negative_prompt": "ugly, deformed, noisy, blurry, low contrast, realism, photorealistic, Western comic style", | |
| }, | |
| { | |
| "name": "Digital Art", | |
| "prompt": "concept art {prompt} . digital artwork, illustrative, painterly, matte painting, highly detailed", | |
| "negative_prompt": "photo, photorealistic, realism, ugly", | |
| }, | |
| { | |
| "name": "Pixel art", | |
| "prompt": "pixel-art {prompt} . low-res, blocky, pixel art style, 8-bit graphics", | |
| "negative_prompt": "sloppy, messy, blurry, noisy, highly detailed, ultra textured, photo, realistic", | |
| }, | |
| { | |
| "name": "Fantasy art", | |
| "prompt": "ethereal fantasy concept art of {prompt} . magnificent, celestial, ethereal, painterly, epic, majestic, magical, fantasy art, cover art, dreamy", | |
| "negative_prompt": "photographic, realistic, realism, 35mm film, dslr, cropped, frame, text, deformed, glitch, noise, noisy, off-center, deformed, cross-eyed, closed eyes, bad anatomy, ugly, disfigured, sloppy, duplicate, mutated, black and white", | |
| }, | |
| { | |
| "name": "Neonpunk", | |
| "prompt": "neonpunk style {prompt} . cyberpunk, vaporwave, neon, vibes, vibrant, stunningly beautiful, crisp, detailed, sleek, ultramodern, magenta highlights, dark purple shadows, high contrast, cinematic, ultra detailed, intricate, professional", | |
| "negative_prompt": "painting, drawing, illustration, glitch, deformed, mutated, cross-eyed, ugly, disfigured", | |
| }, | |
| { | |
| "name": "3D Model", | |
| "prompt": "professional 3d model {prompt} . octane render, highly detailed, volumetric, dramatic lighting", | |
| "negative_prompt": "ugly, deformed, noisy, low poly, blurry, painting", | |
| }, | |
| { | |
| "name":"Portrait", | |
| "prompt":"Portrait Art {prompt}.soft light, sharp, exposure blend, medium shot, bokeh, (hdr:1.4), high contrast, (cinematic, teal and orange:0.85), (muted colors, dim colors, soothing tones:1.3), low saturation, (hyperdetailed:1.2), (noir:0.4), (natural skin texture, hyperrealism, soft light, sharp:1.2)", | |
| "negative_prompt":"" | |
| } | |
| ] | |
| styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list} | |
| STYLE_NAMES = list(styles.keys()) | |
| DEFAULT_STYLE_NAME = "(No style)" | |
| def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]: | |
| p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME]) | |
| return p.replace("{prompt}", positive), n + negative | |
| def text_to_image(prompt,guidance,style_name): | |
| url = "https://api.getimg.ai/v1/stable-diffusion-xl/text-to-image" | |
| #seed = random.randint(0,4294967295) | |
| prompts, negative = apply_style(style_name, prompt) | |
| print(prompts) | |
| print(negative) | |
| payload = { | |
| "model": "stable-diffusion-xl-v1-0", | |
| "prompt": prompts, | |
| "width": 1024, | |
| "height": 1024, | |
| "steps": 90, | |
| "guidance": guidance, | |
| "output_format": "jpeg", | |
| "response_format": "url", | |
| "negative_prompt": negative, | |
| } | |
| headers = { | |
| "accept": "application/json", | |
| "content-type": "application/json", | |
| "authorization": "Bearer key-3ryTG9YlTHzB0hkjgFHoL7yhsOQkWUbodn1sOU7WwOkhDXNSWVL8heMXWwNqCliAbdpm9xNtrITY3WInbRdC98bZ5QVdfOoi" | |
| } | |
| response = requests.post(url, json=payload, headers=headers) | |
| #image_url =response.json()['url'] | |
| # Fetch the image data directly from the URL | |
| #image_datas = requests.get(image_url).content | |
| # Save the image to a local file (e.g., 'my_image.jpg') | |
| #with open('my_image.jpg', 'wb') as f: | |
| # f.write(image_datas) | |
| #print(response.text) | |
| return response.text | |
| def generate_image(): | |
| data = request.get_json() | |
| if 'prompt' in data and 'style_name' in data and 'guidance_scale' in data: | |
| prompt = data['prompt'] | |
| style_name = data['style_name'] | |
| guidance_scale = data['guidance_scale'] | |
| image_urls = text_to_image(prompt=prompt, style_name=style_name, guidance=guidance_scale) | |
| # Convert the first generated image to base64 | |
| #image_b64 = image_urls[0].split(",")[1] | |
| #return jsonify({'image_base64': image_b64}) | |
| return jsonify({'image_urls':image_urls}) | |
| else: | |
| return jsonify({'error': 'Missing required parameters'}), 400 | |
| if __name__ == '__main__': | |
| app.run(host='0.0.0.0',port=7860,debug=True) |