Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,88 +4,185 @@ import numpy as np
|
|
| 4 |
import random
|
| 5 |
from huggingface_hub import AsyncInferenceClient
|
| 6 |
from translatepy import Translator
|
| 7 |
-
import requests
|
| 8 |
-
import re
|
| 9 |
-
import asyncio
|
| 10 |
-
from PIL import Image
|
| 11 |
from gradio_client import Client, handle_file
|
|
|
|
| 12 |
from huggingface_hub import login
|
| 13 |
-
from
|
| 14 |
|
| 15 |
MAX_SEED = np.iinfo(np.int32).max
|
| 16 |
-
HF_TOKEN =
|
| 17 |
-
HF_TOKEN_UPSCALER =
|
| 18 |
|
|
|
|
| 19 |
def enable_lora(lora_add, basemodel):
|
|
|
|
| 20 |
return basemodel if not lora_add else lora_add
|
| 21 |
|
|
|
|
| 22 |
async def generate_image(prompt, model, lora_word, width, height, scales, steps, seed):
|
| 23 |
try:
|
| 24 |
if seed == -1:
|
| 25 |
seed = random.randint(0, MAX_SEED)
|
| 26 |
seed = int(seed)
|
|
|
|
|
|
|
| 27 |
text = str(Translator().translate(prompt, 'English')) + "," + lora_word
|
|
|
|
|
|
|
| 28 |
client = AsyncInferenceClient()
|
| 29 |
image = await client.text_to_image(prompt=text, height=height, width=width, guidance_scale=scales, num_inference_steps=steps, model=model)
|
| 30 |
return image, seed
|
| 31 |
except Exception as e:
|
| 32 |
-
print(f"Error
|
| 33 |
return None, None
|
| 34 |
|
|
|
|
| 35 |
def get_upscale_finegrain(prompt, img_path, upscale_factor):
|
| 36 |
try:
|
|
|
|
| 37 |
client = Client("finegrain/finegrain-image-enhancer", hf_token=HF_TOKEN_UPSCALER)
|
| 38 |
-
result = client.predict(
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
-
print(f"Error
|
| 42 |
return None
|
| 43 |
-
|
|
|
|
| 44 |
async def gen(prompt, basemodel, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model, process_lora):
|
|
|
|
|
|
|
| 45 |
model = enable_lora(lora_model, basemodel) if process_lora else basemodel
|
|
|
|
|
|
|
| 46 |
image, seed = await generate_image(prompt, model, "", width, height, scales, steps, seed)
|
| 47 |
-
if image is None:
|
| 48 |
-
return [None, None]
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
image_path = "temp_image.jpg"
|
|
|
|
| 51 |
image.save(image_path, format="JPEG")
|
| 52 |
-
|
|
|
|
| 53 |
if process_upscale:
|
|
|
|
| 54 |
upscale_image_path = get_upscale_finegrain(prompt, image_path, upscale_factor)
|
| 55 |
-
if upscale_image_path is not None:
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
return [image_path, "upscale_image.jpg"]
|
| 59 |
else:
|
| 60 |
-
print("
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
return [image_path, image_path]
|
| 64 |
|
|
|
|
| 65 |
css = """
|
| 66 |
-
#col-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
"""
|
| 68 |
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
with gr.Column(elem_id="col-container"):
|
|
|
|
| 71 |
with gr.Row():
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import random
|
| 5 |
from huggingface_hub import AsyncInferenceClient
|
| 6 |
from translatepy import Translator
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from gradio_client import Client, handle_file
|
| 8 |
+
from PIL import Image
|
| 9 |
from huggingface_hub import login
|
| 10 |
+
from themes import IndonesiaTheme # Import custom IndonesiaTheme
|
| 11 |
|
| 12 |
MAX_SEED = np.iinfo(np.int32).max
|
| 13 |
+
HF_TOKEN = "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"
|
| 14 |
+
HF_TOKEN_UPSCALER = "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"
|
| 15 |
|
| 16 |
+
# Function to enable LoRA if selected
|
| 17 |
def enable_lora(lora_add, basemodel):
|
| 18 |
+
print(f"[-] Menentukan model: LoRA {'diaktifkan' if lora_add else 'tidak diaktifkan'}, model dasar: {basemodel}")
|
| 19 |
return basemodel if not lora_add else lora_add
|
| 20 |
|
| 21 |
+
# Function to generate image
|
| 22 |
async def generate_image(prompt, model, lora_word, width, height, scales, steps, seed):
|
| 23 |
try:
|
| 24 |
if seed == -1:
|
| 25 |
seed = random.randint(0, MAX_SEED)
|
| 26 |
seed = int(seed)
|
| 27 |
+
|
| 28 |
+
print(f"[-] Menerjemahkan prompt: {prompt}")
|
| 29 |
text = str(Translator().translate(prompt, 'English')) + "," + lora_word
|
| 30 |
+
|
| 31 |
+
print(f"[-] Generating image with prompt: {text}, model: {model}")
|
| 32 |
client = AsyncInferenceClient()
|
| 33 |
image = await client.text_to_image(prompt=text, height=height, width=width, guidance_scale=scales, num_inference_steps=steps, model=model)
|
| 34 |
return image, seed
|
| 35 |
except Exception as e:
|
| 36 |
+
print(f"[-] Error generating image: {e}")
|
| 37 |
return None, None
|
| 38 |
|
| 39 |
+
# Function to upscale image
|
| 40 |
def get_upscale_finegrain(prompt, img_path, upscale_factor):
|
| 41 |
try:
|
| 42 |
+
print(f"[-] Memulai proses upscaling dengan faktor {upscale_factor} untuk gambar {img_path}")
|
| 43 |
client = Client("finegrain/finegrain-image-enhancer", hf_token=HF_TOKEN_UPSCALER)
|
| 44 |
+
result = client.predict(
|
| 45 |
+
input_image=handle_file(img_path),
|
| 46 |
+
prompt=prompt,
|
| 47 |
+
negative_prompt="worst quality, low quality, normal quality",
|
| 48 |
+
upscale_factor=upscale_factor,
|
| 49 |
+
controlnet_scale=0.6,
|
| 50 |
+
controlnet_decay=1,
|
| 51 |
+
condition_scale=6,
|
| 52 |
+
denoise_strength=0.35,
|
| 53 |
+
num_inference_steps=18,
|
| 54 |
+
solver="DDIM",
|
| 55 |
+
api_name="/process"
|
| 56 |
+
)
|
| 57 |
+
print(f"[-] Proses upscaling berhasil.")
|
| 58 |
+
return result[1] # Return upscale image path
|
| 59 |
except Exception as e:
|
| 60 |
+
print(f"[-] Error scaling image: {e}")
|
| 61 |
return None
|
| 62 |
+
|
| 63 |
+
# Main function to generate images and optionally upscale
|
| 64 |
async def gen(prompt, basemodel, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model, process_lora):
|
| 65 |
+
print(f"[-] Memulai generasi gambar dengan prompt: {prompt}")
|
| 66 |
+
|
| 67 |
model = enable_lora(lora_model, basemodel) if process_lora else basemodel
|
| 68 |
+
print(f"[-] Menggunakan model: {model}")
|
| 69 |
+
|
| 70 |
image, seed = await generate_image(prompt, model, "", width, height, scales, steps, seed)
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
if image is None:
|
| 73 |
+
print("[-] Image generation failed.")
|
| 74 |
+
return []
|
| 75 |
+
|
| 76 |
image_path = "temp_image.jpg"
|
| 77 |
+
print(f"[-] Menyimpan gambar sementara di: {image_path}")
|
| 78 |
image.save(image_path, format="JPEG")
|
| 79 |
+
|
| 80 |
+
upscale_image_path = None
|
| 81 |
if process_upscale:
|
| 82 |
+
print(f"[-] Memproses upscaling dengan faktor: {upscale_factor}")
|
| 83 |
upscale_image_path = get_upscale_finegrain(prompt, image_path, upscale_factor)
|
| 84 |
+
if upscale_image_path is not None and os.path.exists(upscale_image_path):
|
| 85 |
+
print(f"[-] Proses upscaling selesai. Gambar tersimpan di: {upscale_image_path}")
|
| 86 |
+
return [image_path, upscale_image_path] # Return both images
|
|
|
|
| 87 |
else:
|
| 88 |
+
print("[-] Upscaling gagal, jalur gambar upscale tidak ditemukan.")
|
| 89 |
+
|
| 90 |
+
return [image_path]
|
|
|
|
| 91 |
|
| 92 |
+
# CSS for styling the interface
|
| 93 |
css = """
|
| 94 |
+
#col-left, #col-mid, #col-right {
|
| 95 |
+
margin: 0 auto;
|
| 96 |
+
max-width: 400px;
|
| 97 |
+
padding: 10px;
|
| 98 |
+
border-radius: 15px;
|
| 99 |
+
background-color: #f9f9f9;
|
| 100 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
| 101 |
+
}
|
| 102 |
+
#banner {
|
| 103 |
+
width: 100%;
|
| 104 |
+
text-align: center;
|
| 105 |
+
margin-bottom: 20px;
|
| 106 |
+
}
|
| 107 |
+
#run-button {
|
| 108 |
+
background-color: #ff4b5c;
|
| 109 |
+
color: white;
|
| 110 |
+
font-weight: bold;
|
| 111 |
+
padding: 10px;
|
| 112 |
+
border-radius: 10px;
|
| 113 |
+
cursor: pointer;
|
| 114 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
|
| 115 |
+
}
|
| 116 |
+
#footer {
|
| 117 |
+
text-align: center;
|
| 118 |
+
margin-top: 20px;
|
| 119 |
+
color: silver;
|
| 120 |
+
}
|
| 121 |
"""
|
| 122 |
|
| 123 |
+
# Creating Gradio interface
|
| 124 |
+
with gr.Blocks(css=css, theme=IndonesiaTheme()) as WallpaperFluxMaker:
|
| 125 |
+
# Displaying the application title
|
| 126 |
+
gr.HTML('<div id="banner">β¨ Flux MultiMode Generator + Upscaler β¨</div>')
|
| 127 |
+
|
| 128 |
with gr.Column(elem_id="col-container"):
|
| 129 |
+
# Output section (replacing ImageSlider with gr.Gallery)
|
| 130 |
with gr.Row():
|
| 131 |
+
output_res = gr.Gallery(label="β‘ Flux / Upscaled Image β‘", elem_id="output-res", columns=2, height="auto")
|
| 132 |
+
|
| 133 |
+
# User input section split into two columns
|
| 134 |
+
with gr.Row():
|
| 135 |
+
# Column 1: Input prompt, LoRA, and base model
|
| 136 |
+
with gr.Column(scale=1, elem_id="col-left"):
|
| 137 |
+
prompt = gr.Textbox(
|
| 138 |
+
label="π Deskripsi Gambar",
|
| 139 |
+
placeholder="Tuliskan prompt Anda dalam bahasa apapun, yang akan langsung diterjemahkan ke bahasa Inggris.",
|
| 140 |
+
elem_id="textbox-prompt"
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
basemodel_choice = gr.Dropdown(
|
| 144 |
+
label="πΌοΈ Pilih Model",
|
| 145 |
+
choices=[
|
| 146 |
+
"black-forest-labs/FLUX.1-schnell",
|
| 147 |
+
"black-forest-labs/FLUX.1-DEV",
|
| 148 |
+
"enhanceaiteam/Flux-uncensored",
|
| 149 |
+
"Shakker-Labs/FLUX.1-dev-ControlNet-Union-Pro",
|
| 150 |
+
"Shakker-Labs/FLUX.1-dev-LoRA-add-details",
|
| 151 |
+
"city96/FLUX.1-dev-gguf"
|
| 152 |
+
],
|
| 153 |
+
value="black-forest-labs/FLUX.1-schnell"
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
lora_model_choice = gr.Dropdown(
|
| 157 |
+
label="π¨ Pilih LoRA",
|
| 158 |
+
choices=[
|
| 159 |
+
"Shakker-Labs/FLUX.1-dev-LoRA-add-details",
|
| 160 |
+
"XLabs-AI/flux-RealismLora",
|
| 161 |
+
"enhanceaiteam/Flux-uncensored"
|
| 162 |
+
],
|
| 163 |
+
value="XLabs-AI/flux-RealismLora"
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
process_lora = gr.Checkbox(label="π¨ Aktifkan LoRA")
|
| 167 |
+
process_upscale = gr.Checkbox(label="π Aktifkan Peningkatan Resolusi")
|
| 168 |
+
upscale_factor = gr.Radio(label="π Faktor Peningkatan Resolusi", choices=[2, 4, 8], value=2)
|
| 169 |
+
|
| 170 |
+
# Column 2: Advanced options (always open)
|
| 171 |
+
with gr.Column(scale=1, elem_id="col-right"):
|
| 172 |
+
with gr.Accordion(label="βοΈ Opsi Lanjutan", open=True):
|
| 173 |
+
width = gr.Slider(label="Lebar", minimum=512, maximum=1280, step=8, value=1280)
|
| 174 |
+
height = gr.Slider(label="Tinggi", minimum=512, maximum=1280, step=8, value=768)
|
| 175 |
+
scales = gr.Slider(label="Skala", minimum=1, maximum=20, step=1, value=8)
|
| 176 |
+
steps = gr.Slider(label="Langkah", minimum=1, maximum=100, step=1, value=8)
|
| 177 |
+
seed = gr.Number(label="Seed", value=-1)
|
| 178 |
+
|
| 179 |
+
# Button to generate image
|
| 180 |
+
btn = gr.Button("π Buat Gambar", elem_id="generate-btn")
|
| 181 |
+
|
| 182 |
+
# Running the `gen` function when "Generate" button is pressed
|
| 183 |
+
btn.click(fn=gen, inputs=[
|
| 184 |
+
prompt, basemodel_choice, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model_choice, process_lora
|
| 185 |
+
], outputs=output_res)
|
| 186 |
+
|
| 187 |
+
# Launching the Gradio app
|
| 188 |
+
WallpaperFluxMaker.queue(api_open=False).launch(show_api=False)
|