Nymbo commited on
Commit
e547b24
1 Parent(s): c6227e1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import io
4
+ import random
5
+ import os
6
+ import time
7
+ from PIL import Image
8
+ from deep_translator import GoogleTranslator
9
+ import json
10
+
11
+ # Project by Nymbo
12
+
13
+ API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl"
14
+ API_TOKEN = os.getenv("HF_READ_TOKEN")
15
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
16
+ timeout = 100
17
+
18
+ def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7):
19
+ if prompt == "" or prompt is None:
20
+ return None
21
+
22
+ key = random.randint(0, 999)
23
+
24
+ API_TOKEN = random.choice([os.getenv("HF_READ_TOKEN"), os.getenv("HF_READ_TOKEN_2"), os.getenv("HF_READ_TOKEN_3"), os.getenv("HF_READ_TOKEN_4"), os.getenv("HF_READ_TOKEN_5")])
25
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
26
+
27
+ prompt = GoogleTranslator(source='ru', target='en').translate(prompt)
28
+ print(f'\033[1mГенерация {key} перевод:\033[0m {prompt}')
29
+
30
+ prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
31
+ print(f'\033[1mГенерация {key}:\033[0m {prompt}')
32
+
33
+ payload = {
34
+ "inputs": prompt,
35
+ "is_negative": is_negative,
36
+ "steps": steps,
37
+ "cfg_scale": cfg_scale,
38
+ "seed": seed if seed != -1 else random.randint(1, 1000000000),
39
+ "strength": strength
40
+ }
41
+
42
+ response = requests.post(API_URL, headers=headers, json=payload, timeout=timeout)
43
+ if response.status_code != 200:
44
+ print(f"Ошибка: Не удалось получить изображение. Статус ответа: {response.status_code}")
45
+ print(f"Содержимое ответа: {response.text}")
46
+ if response.status_code == 503:
47
+ raise gr.Error(f"{response.status_code} : The model is being loaded")
48
+ raise gr.Error(f"{response.status_code}")
49
+ return None
50
+
51
+ try:
52
+ image_bytes = response.content
53
+ image = Image.open(io.BytesIO(image_bytes))
54
+ print(f'\033[1mГенерация {key} завершена!\033[0m ({prompt})')
55
+ return image
56
+ except Exception as e:
57
+ print(f"Ошибка при попытке открыть изображение: {e}")
58
+ return None
59
+
60
+ css = """
61
+ * {}
62
+ footer {visibility: hidden !important;}
63
+ """
64
+
65
+ with gr.Blocks(theme='Nymbo/Nymbo_Theme') as dalle:
66
+ with gr.Tab("Basic Settings"):
67
+ with gr.Row():
68
+ with gr.Column(elem_id="prompt-container"):
69
+ with gr.Row():
70
+ text_prompt = gr.Textbox(label="Prompt", placeholder="Enter a prompt here", lines=3, elem_id="prompt-text-input")
71
+
72
+ with gr.Tab("Advanced Settings"):
73
+ with gr.Accordion("Advanced Settings", open=True):
74
+ with gr.Row():
75
+ negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="What should not be in the image", value="[deformed | disfigured], poorly drawn, [bad : wrong] anatomy, [extra | missing | floating | disconnected] limb, (mutated hands and fingers), blurry, text, fuzziness", lines=3, elem_id="negative-prompt-text-input")
76
+ with gr.Row():
77
+ steps = gr.Slider(label="Sampling steps", value=35, minimum=1, maximum=100, step=1)
78
+ with gr.Row():
79
+ cfg = gr.Slider(label="CFG Scale", value=7, minimum=1, maximum=20, step=1)
80
+ with gr.Row():
81
+ method = gr.Radio(label="Sampling method", value="DPM++ 2M Karras", choices=["DPM++ 2M Karras", "DPM++ SDE Karras", "Euler", "Euler a", "Heun", "DDIM"])
82
+ with gr.Row():
83
+ strength = gr.Slider(label="Strength", value=0.7, minimum=0, maximum=1, step=0.001)
84
+ with gr.Row():
85
+ seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=1000000000, step=1)
86
+
87
+ with gr.Row():
88
+ text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
89
+ with gr.Row():
90
+ image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")
91
+
92
+ text_button.click(query, inputs=[text_prompt, negative_prompt, steps, cfg, method, seed, strength], outputs=image_output)
93
+
94
+ dalle.launch(show_api=False, share=False)