Spaces:
Running
on
Zero
Running
on
Zero
File size: 9,295 Bytes
d241648 e7137c8 d241648 e7137c8 1023e7a 2a1d36f f657ff8 2a1d36f 6a587db 59e2f6a b4dc422 f4342d5 fbec79f e7137c8 f4342d5 204f693 c96b67e 204f693 0f1e529 204f693 ce0d343 204f693 ce0d343 4e7618b ce0d343 47ca329 2a1d36f ce0d343 1023e7a bee5346 1023e7a 28ab779 1023e7a 2a1d36f f4342d5 2a1d36f 983c55a 2a1d36f a62e14b d7a467f 3b986ef d7a467f 1023e7a e7137c8 d241648 e7137c8 d241648 e7137c8 7ac4025 e7137c8 c96b67e 86de7ac c96b67e e7137c8 c96b67e e7137c8 c96b67e 204f693 23ef31b 204f693 e7137c8 71224a5 f2bbaef 71224a5 1023e7a b4dc422 1023e7a b4dc422 1023e7a b4dc422 1023e7a b4dc422 1023e7a b4dc422 1023e7a b4dc422 9aa2220 b4dc422 9aa2220 b4dc422 f4342d5 b4dc422 ef096c2 1023e7a 555d0b9 81aac09 555d0b9 1023e7a ba7ccc0 f4342d5 ba7ccc0 e7137c8 1023e7a |
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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
from PIL import Image
import base64
from io import BytesIO
import os
from mistralai import Mistral
import gradio as gr
import numpy as np
import random
import spaces
import torch
from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, AutoencoderTiny, AutoencoderKL
from huggingface_hub import hf_hub_download
from transformers import CLIPTextModel, CLIPTokenizer,T5EncoderModel, T5TokenizerFast
from openai import OpenAI
import config
from extras.expansion import FooocusExpansion
import re
expansion = FooocusExpansion()
api_key = os.getenv("MISTRAL_API_KEY")
client = Mistral(api_key=api_key)
client_open_ai = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key=os.getenv('HF_TOKEN')
)
dtype = torch.bfloat16
device = "cuda" if torch.cuda.is_available() else "cpu"
taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
good_vae = AutoencoderKL.from_pretrained("shuttleai/shuttle-3-diffusion", subfolder="vae", torch_dtype=dtype).to(device)
pipe = DiffusionPipeline.from_pretrained("shuttleai/shuttle-3-diffusion", torch_dtype=dtype, vae=taef1).to(device)
pipe.load_lora_weights(hf_hub_download("aifeifei798/feifei-flux-lora-v1", "feifei.safetensors"), adapter_name = "feifei")
pipe.load_lora_weights(hf_hub_download("aifeifei798/feifei-flux-lora-v1", "FLUX-dev-lora-add_details.safetensors"), adapter_name = "FLUX-dev-lora-add_details")
pipe.load_lora_weights(hf_hub_download("aifeifei798/feifei-flux-lora-v1", "Shadow-Projection.safetensors"), adapter_name = "Shadow-Projection")
pipe.set_adapters(["feifei","FLUX-dev-lora-add_details","Shadow-Projection"], adapter_weights=[0.65,0.35,0.35])
pipe.fuse_lora(adapter_name=["feifei","FLUX-dev-lora-add_details","Shadow-Projection"], lora_scale=1.0)
pipe.unload_lora_weights()
torch.cuda.empty_cache()
MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 4096
css="""
#col-container {
width: auto;
height: 750px;
}
"""
@spaces.GPU()
def infer(prompt, quality_select, styles_Radio, FooocusExpansion_select, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True), guidance_scale=3.5):
if randomize_seed:
seed = random.randint(0, MAX_SEED)
generator = torch.Generator().manual_seed(seed)
if not prompt:
prompt = "the photo is a 18 yo jpop girl is looking absolutely adorable and gorgeous, with a playful and mischievous grin, her eyes twinkling with joy."
if quality_select:
prompt += ", masterpiece, best quality, very aesthetic, absurdres"
if styles_Radio:
for style_name in styles_Radio:
for style in config.style_list:
if style["name"] == style_name:
prompt += style["prompt"].replace("{prompt}", "the ")
if FooocusExpansion_select:
prompt = expansion(prompt, seed)
image = pipe(
prompt = "",
prompt_2 = prompt,
width = width,
height = height,
num_inference_steps = num_inference_steps,
generator = generator,
guidance_scale=guidance_scale,
output_type="pil",
).images[0]
return image, seed
def encode_image(image_path):
"""Encode the image to base64."""
try:
# 打开图片文件
image = Image.open(image_path).convert("RGB")
# 将图片转换为字节流
buffered = BytesIO()
image.save(buffered, format="JPEG")
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
return img_str
except FileNotFoundError:
print(f"Error: The file {image_path} was not found.")
return None
except Exception as e: # 添加通用异常处理
print(f"Error: {e}")
return None
def predict(message, history, additional_dropdown):
message_text = message.get("text", "")
message_files = message.get("files", [])
if message_files:
# Getting the base64 string
message_file = message_files[0]
base64_image = encode_image(message_file)
if base64_image is None:
yield "Error: Failed to encode the image."
return
# Specify model
model = "pixtral-large-2411"
# Define the messages for the chat
messages = [
{
"role": "user",
"content": [
{"type": "text", "text": message_text},
{
"type": "image_url",
"image_url": f"data:image/jpeg;base64,{base64_image}",
},
],
}
]
partial_message = ""
for chunk in client.chat.stream(model=model, messages=messages):
if chunk.data.choices[0].delta.content is not None:
partial_message = partial_message + chunk.data.choices[0].delta.content
yield partial_message
else:
stream = client_open_ai.chat.completions.create(
model=additional_dropdown,
messages=[{"role": "user", "content": str(message_text)}],
temperature=0.5,
max_tokens=1024,
top_p=0.7,
stream=True
)
partial_message = ""
temp = ""
for chunk in stream:
if chunk.choices[0].delta.content is not None:
temp += chunk.choices[0].delta.content
yield temp
with gr.Blocks(css=css) as demo:
with gr.Row():
with gr.Column(scale=1):
with gr.Tab("Generator"):
prompt = gr.Text(
label="Prompt",
show_label=False,
placeholder="Enter your prompt",
max_lines = 12,
container=False
)
run_button = gr.Button("Run")
result = gr.Image(label="Result", show_label=False, interactive=False)
with gr.Accordion("Advanced Settings", open=False):
seed = gr.Slider(
label="Seed",
minimum=0,
maximum=MAX_SEED,
step=1,
value=0,
)
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
with gr.Row():
width = gr.Slider(
label="Width",
minimum=256,
maximum=MAX_IMAGE_SIZE,
step=64,
value=896,
)
height = gr.Slider(
label="Height",
minimum=256,
maximum=MAX_IMAGE_SIZE,
step=64,
value=1152,
)
with gr.Row():
num_inference_steps = gr.Slider(
label="Number of inference steps",
minimum=1,
maximum=50,
step=1,
value=4,
)
guidancescale = gr.Slider(
label="Guidance scale",
minimum=0,
maximum=10,
step=0.1,
value=3.5,
)
with gr.Tab("Styles"):
quality_select = gr.Checkbox(label="high quality")
FooocusExpansion_select = gr.Checkbox(label="FooocusExpansion",value=True)
styles_name = [style["name"] for style in config.style_list]
styles_Radio = gr.Dropdown(styles_name,label="Styles",multiselect=True)
with gr.Column(scale=3,elem_id="col-container"):
gr.ChatInterface(
predict,
type="messages",
multimodal=True,
additional_inputs =[gr.Dropdown(
["CohereForAI/c4ai-command-r-plus-08-2024",
"meta-llama/Meta-Llama-3.1-70B-Instruct",
"Qwen/Qwen2.5-72B-Instruct",
"nvidia/Llama-3.1-Nemotron-70B-Instruct-HF",
"NousResearch/Hermes-3-Llama-3.1-8B",
"mistralai/Mistral-Nemo-Instruct-2407",
"microsoft/Phi-3.5-mini-instruct"],
value="meta-llama/Meta-Llama-3.1-70B-Instruct",
show_label=False,
)]
)
gr.on(
triggers=[run_button.click, prompt.submit],
fn = infer,
inputs = [prompt, quality_select, styles_Radio, FooocusExpansion_select, seed, randomize_seed, width, height, num_inference_steps, guidancescale],
outputs = [result, seed]
)
if __name__ == "__main__":
demo.queue().launch() |