Spaces:
Running
Running
File size: 7,043 Bytes
e547b24 09e6956 79927aa e547b24 df85d56 4a22dd6 e547b24 149734c 103daba 3079d5a 130e6a7 6f5a32e e547b24 9be63af e547b24 130e6a7 e547b24 7281220 e547b24 6f5a32e e547b24 e92be6e 2f8eff5 38f6f0f 3079d5a e92be6e 2f8eff5 149734c 24b81c2 2f8eff5 bef47e0 e92be6e 6f5a32e e547b24 6f5a32e e547b24 02f8cfa bc84ac0 02f8cfa 73f7edc e547b24 3079d5a 8886d40 4a22dd6 02f8cfa 3079d5a 130e6a7 02f8cfa e547b24 130e6a7 e547b24 d82c582 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
import gradio as gr
import requests
import io
import random
import os
import time
import numpy as np
from PIL import Image, ImageEnhance
from deep_translator import GoogleTranslator
import json
# Project by Nymbo
alto_api= "https://api-inference.huggingface.co/models/enhanceaiteam/Flux-uncensored"
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
API_TOKEN = os.getenv("HF_READ_TOKEN")
headers = {"Authorization": f"Bearer {API_TOKEN}"}
timeout = 100
def add_noise(image, intensity=25):
"""
Добавляет цифровой шум к изображению.
:param image: Изображение (PIL.Image)
:param intensity: Интенсивность шума (от 0 до 255)
:return: Изображение с шумом (PIL.Image)
"""
# Преобразуем изображение в массив NumPy
img_array = np.array(image)
# Генерируем шум
noise = np.random.randint(-intensity, intensity, img_array.shape, dtype=np.int32)
# Добавляем шум к изображению
noisy_array = np.clip(img_array + noise, 0, 255).astype(np.uint8)
# Преобразуем массив обратно в изображение
noisy_image = Image.fromarray(noisy_array)
return noisy_image
def resize_and_crop(image, target_width=768, target_height=1024):
"""
Подгоняет изображение под размер target_width x target_height с сохранением пропорций.
Если изображение не соответствует соотношению сторон, обрезает его по бокам.
:param image: Изображение (PIL.Image)
:param target_width: Целевая ширина
:param target_height: Целевая высота
:return: Изображение с измененным размером (PIL.Image)
"""
# Сохраняем исходные пропорции
original_width, original_height = image.size
target_ratio = target_width / target_height
original_ratio = original_width / original_height
# Масштабируем изображение с сохранением пропорций
if original_ratio > target_ratio:
# Если изображение шире, чем нужно, обрезаем по бокам
new_height = target_height
new_width = int(original_width * (target_height / original_height))
resized_image = image.resize((new_width, new_height), Image.Resampling.LANCZOS)
left = (new_width - target_width) / 2
top = 0
right = (new_width + target_width) / 2
bottom = target_height
else:
# Если изображение уже, чем нужно, обрезаем сверху и снизу
new_width = target_width
new_height = int(original_height * (target_width / original_width))
resized_image = image.resize((new_width, new_height), Image.Resampling.LANCZOS)
left = 0
top = (new_height - target_height) / 2
right = target_width
bottom = (new_height + target_height) / 2
# Обрезаем изображение до целевого размера
cropped_image = resized_image.crop((left, top, right, bottom))
return cropped_image
def compress_image(image, quality=50):
"""
Сжимает изображение и понижает его качество в формате JPEG.
:param image: Изображение (PIL.Image)
:param quality: Качество JPEG (от 1 до 100)
:return: Сжатое изображение (PIL.Image)
"""
# Сохраняем изображение в буфер с пониженным качеством
buffer = io.BytesIO()
image.save(buffer, format="JPEG", quality=quality)
buffer.seek(0)
# Загружаем изображение обратно из буфера
compressed_image = Image.open(buffer)
return compressed_image
def query(prompt):
if prompt == "" or prompt == None:
return None
key = random.randint(0, 999)
API_TOKEN = random.choice([os.getenv("HF_READ_TOKEN")])
headers = {"Authorization": f"Bearer {API_TOKEN}"}
payload = {
"inputs": prompt
}
response = requests.post(API_URL, headers=headers, json=payload, timeout=timeout)
if response.status_code != 200:
print(f"Error: Failed to get image. Response status: {response.status_code}")
print(f"Response content: {response.text}")
if response.status_code == 503:
raise gr.Error(f"{response.status_code} : The model is being loaded")
raise gr.Error(f"{response.status_code}")
try:
image_bytes = response.content
image = Image.open(io.BytesIO(image_bytes))
image = add_noise(image, intensity=12.5)
image = resize_and_crop(image, 360, 640)
image = compress_image(image, 75)
# Изменение насыщенности
enhancer = ImageEnhance.Color(image)
image = enhancer.enhance(0.7)
# Изменение экспозиции (яркости)
enhancer = ImageEnhance.Brightness(image)
image = enhancer.enhance(1.05)
enhancer = ImageEnhance.Contrast(image)
image = enhancer.enhance(0.85)
print(f'\033[1mGeneration {key} completed!\033[0m ({prompt})')
return image
except Exception as e:
print(f"Error when trying to open the image: {e}")
return None
css = """
#app-container {
max-width: 600px;
margin-left: auto;
margin-right: auto;
}
"""
test_prompt = """(
[russian athletic female with brown eyes and gold loose long hair (her clothes: with gray cotton sweater, with pink mini-skirt, with black cotton short socks, with beige sandals. Clothes and female may no visible or hidden), smiling and laying on something]
[realistic photo with poor pale lighting]
[detailed living room composition (may be people)]
[morning light, pale tones]
[home russian atmosphere]
[no mutation glitches, natural anatomy]
[amateur one hand-selfie pov side angle]
)"""
with gr.Blocks(theme='gstaff/xkcd', css=css) as app:
gr.HTML("<center><h1>FLUX.1-Schnell</h1></center>")
with gr.Column(elem_id="app-container"):
with gr.Row():
with gr.Column(elem_id="prompt-container"):
with gr.Row():
text_prompt = gr.Textbox(label="Prompt", placeholder="Enter a prompt here", lines=2, elem_id="prompt-text-input", value=test_prompt)
with gr.Row():
text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
with gr.Row():
image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")
text_button.click(query, inputs=[text_prompt], outputs=image_output)
app.launch(show_api=True, share=True) |