Create lorify.py
Browse files
lorify.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import random
|
3 |
+
import numpy as np
|
4 |
+
from huggingface_hub import AsyncInferenceClient
|
5 |
+
from translatepy import Translator
|
6 |
+
from gradio_client import Client, handle_file
|
7 |
+
from PIL import Image
|
8 |
+
|
9 |
+
# Constants
|
10 |
+
MAX_SEED = np.iinfo(np.int32).max
|
11 |
+
HF_TOKEN = os.getenv('HF_TOKEN') # Set the environment variable for HF_TOKEN
|
12 |
+
HF_TOKEN_UPSCALER = os.getenv('HF_TOKEN_UPSCALER') # Set the environment variable for HF_TOKEN_UPSCALER
|
13 |
+
|
14 |
+
|
15 |
+
class Lorify:
|
16 |
+
def __init__(self, hf_token=None, hf_token_upscaler=None):
|
17 |
+
# Optionally load tokens from environment if not passed
|
18 |
+
self.hf_token = hf_token or HF_TOKEN
|
19 |
+
self.hf_token_upscaler = hf_token_upscaler or HF_TOKEN_UPSCALER
|
20 |
+
|
21 |
+
# Initialize clients
|
22 |
+
self.qwen_client = Client("K00B404/HugChatWrap", hf_token=self.hf_token)
|
23 |
+
self.client = AsyncInferenceClient()
|
24 |
+
|
25 |
+
# List of available LoRAs (replace with your LoRA repo names or paths)
|
26 |
+
self.loaded_loras = []
|
27 |
+
self.loras = [
|
28 |
+
"Shakker-Labs/FLUX.1-dev-LoRA-add-details",
|
29 |
+
"XLabs-AI/flux-RealismLora",
|
30 |
+
"enhanceaiteam/Flux-uncensored"
|
31 |
+
]
|
32 |
+
self.loaded_loras.extend(self.loras)
|
33 |
+
|
34 |
+
# Enable or disable LoRA
|
35 |
+
def enable_lora(self, lora_add, basemodel):
|
36 |
+
return basemodel if not lora_add else lora_add
|
37 |
+
|
38 |
+
# Generate image function
|
39 |
+
async def generate_image(self, prompt, model, lora_word, width, height, scales, steps, seed):
|
40 |
+
try:
|
41 |
+
if seed == -1:
|
42 |
+
seed = random.randint(0, MAX_SEED)
|
43 |
+
seed = int(seed)
|
44 |
+
|
45 |
+
# Translate prompt
|
46 |
+
text = str(Translator().translate(prompt, 'English')) + "," + lora_word
|
47 |
+
|
48 |
+
# Generate image
|
49 |
+
image = await self.client.text_to_image(
|
50 |
+
prompt=text,
|
51 |
+
height=height,
|
52 |
+
width=width,
|
53 |
+
guidance_scale=scales,
|
54 |
+
num_inference_steps=steps,
|
55 |
+
model=model
|
56 |
+
)
|
57 |
+
|
58 |
+
return image, seed
|
59 |
+
except Exception as e:
|
60 |
+
print(f"Error generating image: {e}")
|
61 |
+
return None, None
|
62 |
+
|
63 |
+
# Upscale image function
|
64 |
+
def upscale_image(self, prompt, img_path, upscale_factor):
|
65 |
+
try:
|
66 |
+
# Initialize the upscale client
|
67 |
+
upscale_client = Client("finegrain/finegrain-image-enhancer", hf_token=self.hf_token_upscaler)
|
68 |
+
result = upscale_client.predict(
|
69 |
+
input_image=handle_file(img_path),
|
70 |
+
prompt=prompt,
|
71 |
+
negative_prompt="worst quality, low quality, normal quality",
|
72 |
+
upscale_factor=upscale_factor,
|
73 |
+
controlnet_scale=0.6,
|
74 |
+
controlnet_decay=1,
|
75 |
+
condition_scale=6,
|
76 |
+
denoise_strength=0.35,
|
77 |
+
num_inference_steps=18,
|
78 |
+
solver="DDIM",
|
79 |
+
api_name="/process"
|
80 |
+
)
|
81 |
+
return result[1] # Return upscale image path
|
82 |
+
except Exception as e:
|
83 |
+
print(f"Error scaling image: {e}")
|
84 |
+
return None
|
85 |
+
|
86 |
+
# Main method to generate and optionally upscale image
|
87 |
+
async def gen_image(self, prompt, basemodel, width, height, scales, steps, seed, upscale_factor, process_upscale, lora_model, process_lora):
|
88 |
+
model = self.enable_lora(lora_model, basemodel) if process_lora else basemodel
|
89 |
+
|
90 |
+
image, seed = await self.generate_image(prompt, model, "", width, height, scales, steps, seed)
|
91 |
+
|
92 |
+
if image is None:
|
93 |
+
print("Image generation failed.")
|
94 |
+
return []
|
95 |
+
|
96 |
+
image_path = "temp_image.jpg"
|
97 |
+
image.save(image_path, format="JPEG")
|
98 |
+
|
99 |
+
upscale_image_path = None
|
100 |
+
if process_upscale:
|
101 |
+
upscale_image_path = self.upscale_image(prompt, image_path, upscale_factor)
|
102 |
+
if upscale_image_path and os.path.exists(upscale_image_path):
|
103 |
+
return [image_path, upscale_image_path]
|
104 |
+
|
105 |
+
return [image_path]
|