Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,49 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import io
|
| 4 |
import random
|
| 5 |
import os
|
| 6 |
import time
|
| 7 |
from PIL import Image
|
| 8 |
from deep_translator import GoogleTranslator
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
# Project by Nymbo
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
def convert_to_png(image):
|
| 18 |
"""Convert any image format to true PNG format"""
|
| 19 |
png_buffer = io.BytesIO()
|
| 20 |
if image.mode == 'RGBA':
|
| 21 |
-
# If image has alpha channel, save as PNG with transparency
|
| 22 |
image.save(png_buffer, format='PNG', optimize=True)
|
| 23 |
else:
|
| 24 |
-
# Convert to RGB first if not in RGB/RGBA mode
|
| 25 |
if image.mode != 'RGB':
|
| 26 |
image = image.convert('RGB')
|
| 27 |
image.save(png_buffer, format='PNG', optimize=True)
|
|
@@ -34,8 +56,6 @@ def query(prompt, is_negative=False, steps=35, cfg_scale=7, sampler="DPM++ 2M Ka
|
|
| 34 |
return None
|
| 35 |
|
| 36 |
key = random.randint(0, 999)
|
| 37 |
-
API_TOKEN = random.choice([os.getenv("HF_READ_TOKEN")])
|
| 38 |
-
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
| 39 |
|
| 40 |
# Translate prompt
|
| 41 |
try:
|
|
@@ -47,39 +67,47 @@ def query(prompt, is_negative=False, steps=35, cfg_scale=7, sampler="DPM++ 2M Ka
|
|
| 47 |
|
| 48 |
print(f'\033[1mGeneration {key}:\033[0m {prompt}')
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
"
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
"
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
}
|
| 59 |
-
|
| 60 |
try:
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
|
|
|
| 68 |
print(f'\033[1mGeneration {key} completed as PNG!\033[0m')
|
| 69 |
return png_img
|
| 70 |
|
| 71 |
-
except requests.exceptions.RequestException as e:
|
| 72 |
-
print(f"API Error: {e}")
|
| 73 |
-
if hasattr(e, 'response') and e.response:
|
| 74 |
-
if e.response.status_code == 503:
|
| 75 |
-
raise gr.Error("503: Model is loading, please try again later")
|
| 76 |
-
raise gr.Error(f"{e.response.status_code}: {e.response.text}")
|
| 77 |
-
raise gr.Error("Network error occurred")
|
| 78 |
except Exception as e:
|
| 79 |
-
print(f"
|
| 80 |
-
raise gr.Error(f"Image
|
| 81 |
|
| 82 |
-
# Light theme CSS
|
| 83 |
css = """
|
| 84 |
#app-container {
|
| 85 |
max-width: 800px;
|
|
@@ -111,7 +139,7 @@ h1 {
|
|
| 111 |
"""
|
| 112 |
|
| 113 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="green"), css=css) as app:
|
| 114 |
-
gr.HTML("<center><h1>FLUX.1-Dev (PNG Output)</h1></center>")
|
| 115 |
|
| 116 |
with gr.Column(elem_id="app-container"):
|
| 117 |
with gr.Row():
|
|
@@ -151,7 +179,7 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="green"), css=css) as app:
|
|
| 151 |
output_image = gr.Image(
|
| 152 |
type="pil",
|
| 153 |
label="Generated PNG Image",
|
| 154 |
-
format="png",
|
| 155 |
elem_id="gallery"
|
| 156 |
)
|
| 157 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
|
|
|
| 3 |
import random
|
| 4 |
import os
|
| 5 |
import time
|
| 6 |
from PIL import Image
|
| 7 |
from deep_translator import GoogleTranslator
|
| 8 |
+
from diffusers import DiffusionPipeline
|
| 9 |
+
from huggingface_hub import hf_hub_download
|
| 10 |
|
| 11 |
+
# Project by Nymbo with LoRA integration
|
| 12 |
|
| 13 |
+
# Model and LoRA configuration
|
| 14 |
+
BASE_MODEL = "black-forest-labs/FLUX.1-dev"
|
| 15 |
+
LORA_REPO = "burhansyam/uncen"
|
| 16 |
+
LORA_WEIGHTS_NAME = "uncen.safetensors" # Adjust if different
|
| 17 |
+
torch_dtype = torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float16
|
| 18 |
+
|
| 19 |
+
# Initialize the pipeline with LoRA
|
| 20 |
+
def init_pipeline():
|
| 21 |
+
pipe = DiffusionPipeline.from_pretrained(
|
| 22 |
+
BASE_MODEL,
|
| 23 |
+
torch_dtype=torch_dtype
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# Load LoRA weights
|
| 27 |
+
pipe.load_lora_weights(
|
| 28 |
+
hf_hub_download(repo_id=LORA_REPO, filename=LORA_WEIGHTS_NAME),
|
| 29 |
+
adapter_name="uncen"
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Enable model offloading if needed
|
| 33 |
+
if torch.cuda.is_available():
|
| 34 |
+
pipe.to("cuda")
|
| 35 |
+
pipe.enable_xformers_memory_efficient_attention()
|
| 36 |
+
|
| 37 |
+
return pipe
|
| 38 |
+
|
| 39 |
+
pipe = init_pipeline()
|
| 40 |
|
| 41 |
def convert_to_png(image):
|
| 42 |
"""Convert any image format to true PNG format"""
|
| 43 |
png_buffer = io.BytesIO()
|
| 44 |
if image.mode == 'RGBA':
|
|
|
|
| 45 |
image.save(png_buffer, format='PNG', optimize=True)
|
| 46 |
else:
|
|
|
|
| 47 |
if image.mode != 'RGB':
|
| 48 |
image = image.convert('RGB')
|
| 49 |
image.save(png_buffer, format='PNG', optimize=True)
|
|
|
|
| 56 |
return None
|
| 57 |
|
| 58 |
key = random.randint(0, 999)
|
|
|
|
|
|
|
| 59 |
|
| 60 |
# Translate prompt
|
| 61 |
try:
|
|
|
|
| 67 |
|
| 68 |
print(f'\033[1mGeneration {key}:\033[0m {prompt}')
|
| 69 |
|
| 70 |
+
# Set random seed if not specified
|
| 71 |
+
generator = None
|
| 72 |
+
if seed != -1:
|
| 73 |
+
generator = torch.Generator(device="cuda" if torch.cuda.is_available() else "cpu").manual_seed(seed)
|
| 74 |
+
else:
|
| 75 |
+
seed = random.randint(1, 1000000000)
|
| 76 |
+
generator = torch.Generator(device="cuda" if torch.cuda.is_available() else "cpu").manual_seed(seed)
|
| 77 |
+
|
| 78 |
+
# Map sampler names to Diffusers scheduler names
|
| 79 |
+
sampler_map = {
|
| 80 |
+
"DPM++ 2M Karras": "dpmsolver++",
|
| 81 |
+
"DPM++ SDE Karras": "dpmsolver++",
|
| 82 |
+
"Euler": "euler",
|
| 83 |
+
"Euler a": "euler_a",
|
| 84 |
+
"Heun": "heun",
|
| 85 |
+
"DDIM": "ddim"
|
| 86 |
}
|
| 87 |
+
|
| 88 |
try:
|
| 89 |
+
# Generate image with LoRA
|
| 90 |
+
image = pipe(
|
| 91 |
+
prompt=prompt,
|
| 92 |
+
negative_prompt=is_negative if is_negative else None,
|
| 93 |
+
num_inference_steps=steps,
|
| 94 |
+
guidance_scale=cfg_scale,
|
| 95 |
+
generator=generator,
|
| 96 |
+
strength=strength,
|
| 97 |
+
width=width,
|
| 98 |
+
height=height,
|
| 99 |
+
cross_attention_kwargs={"scale": 0.8}, # LoRA strength adjustment
|
| 100 |
+
).images[0]
|
| 101 |
|
| 102 |
+
png_img = convert_to_png(image)
|
| 103 |
print(f'\033[1mGeneration {key} completed as PNG!\033[0m')
|
| 104 |
return png_img
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
except Exception as e:
|
| 107 |
+
print(f"Generation error: {e}")
|
| 108 |
+
raise gr.Error(f"Image generation failed: {str(e)}")
|
| 109 |
|
| 110 |
+
# Light theme CSS (same as before)
|
| 111 |
css = """
|
| 112 |
#app-container {
|
| 113 |
max-width: 800px;
|
|
|
|
| 139 |
"""
|
| 140 |
|
| 141 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="green"), css=css) as app:
|
| 142 |
+
gr.HTML("<center><h1>FLUX.1-Dev with LoRA (PNG Output)</h1></center>")
|
| 143 |
|
| 144 |
with gr.Column(elem_id="app-container"):
|
| 145 |
with gr.Row():
|
|
|
|
| 179 |
output_image = gr.Image(
|
| 180 |
type="pil",
|
| 181 |
label="Generated PNG Image",
|
| 182 |
+
format="png",
|
| 183 |
elem_id="gallery"
|
| 184 |
)
|
| 185 |
|