Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,80 +1,107 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
-
from datetime import datetime
|
5 |
-
import json
|
6 |
|
7 |
-
# Initialize
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
os.makedirs("outputs/images", exist_ok=True)
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
|
|
|
|
|
17 |
messages = [
|
18 |
{
|
19 |
"role": "system",
|
20 |
"content": (
|
21 |
-
"You are
|
22 |
-
"
|
23 |
-
"to achieve this.\n\nYou will receive a user description, and you must rephrase it to consist of "
|
24 |
-
"short phrases separated by periods, adding detail to everything the user provides.\n\nAdd describe "
|
25 |
-
"the color of all parts or components of the emoji. Unless otherwise specified by the user, do not "
|
26 |
-
"describe people. Do not describe the background of the image. Your output should be in the format:\n\n"
|
27 |
-
"```emoji of {description}. {addon phrases}. 3D lighting. no cast shadows.```\n\nThe description "
|
28 |
-
"should be a 1 sentence of your interpretation of the emoji. Then, you may choose to add addon phrases."
|
29 |
-
" You must use the following in the given scenarios:\n\n- \"cute.\": If generating anything that's not "
|
30 |
-
"an object, and also not a human\n- \"enlarged head in cartoon style.\": ONLY animals\n- \"head is "
|
31 |
-
"turned towards viewer.\": ONLY humans or animals\n- \"detailed texture.\": ONLY objects\n\nFurther "
|
32 |
-
"addon phrases may be added to ensure the clarity of the emoji."
|
33 |
),
|
34 |
},
|
35 |
-
{"role": "user", "content":
|
36 |
]
|
|
|
|
|
|
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
print(f"
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
45 |
|
46 |
-
|
47 |
-
|
48 |
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
-
# Create Gradio interface
|
52 |
with gr.Blocks() as demo:
|
53 |
-
gr.Markdown("#
|
54 |
-
|
55 |
-
#
|
56 |
with gr.Row():
|
57 |
-
prompt_input = gr.Textbox(label="Enter a
|
58 |
-
|
|
|
|
|
59 |
with gr.Row():
|
60 |
-
|
61 |
-
|
|
|
|
|
62 |
with gr.Row():
|
63 |
-
|
64 |
-
width_input = gr.Slider(label="Width", minimum=256, maximum=2048, value=
|
65 |
-
height_input = gr.Slider(label="Height", minimum=256, maximum=2048, value=640, step=64)
|
66 |
|
67 |
-
# Output
|
|
|
68 |
image_output = gr.Image(label="Generated Image")
|
69 |
-
|
70 |
# Button to generate the image
|
71 |
generate_button = gr.Button("Generate Image")
|
72 |
-
|
73 |
# Define button click behavior
|
74 |
generate_button.click(
|
75 |
-
fn=process,
|
76 |
-
inputs=[
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
)
|
79 |
|
80 |
# Launch the app
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from diffusers import FluxPipeline
|
4 |
from huggingface_hub import InferenceClient
|
5 |
import os
|
|
|
|
|
6 |
|
7 |
+
# Initialize the Flux pipeline
|
8 |
+
def initialize_flux_pipeline():
|
9 |
+
pipe = FluxPipeline.from_pretrained(
|
10 |
+
"black-forest-labs/FLUX.1-dev",
|
11 |
+
torch_dtype=torch.bfloat16
|
12 |
+
)
|
13 |
+
pipe.enable_model_cpu_offload()
|
14 |
+
pipe.load_lora_weights("EvanZhouDev/open-genmoji")
|
15 |
+
return pipe
|
16 |
|
17 |
+
flux_pipeline = initialize_flux_pipeline()
|
|
|
18 |
|
19 |
+
# Initialize the language model client
|
20 |
+
llm_client = InferenceClient("Qwen/Qwen2.5-72B-Instruct", token=os.getenv("HUGGINGFACE_API_TOKEN"))
|
21 |
+
|
22 |
+
# Function to refine the prompt
|
23 |
+
def refine_prompt(original_prompt):
|
24 |
messages = [
|
25 |
{
|
26 |
"role": "system",
|
27 |
"content": (
|
28 |
+
"You are refining a user-provided description for generating images. The output should focus on clarity, "
|
29 |
+
"detail, and vivid descriptions. The format should be concise and effective for image generation."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
),
|
31 |
},
|
32 |
+
{"role": "user", "content": original_prompt},
|
33 |
]
|
34 |
+
completion = llm_client.chat_completion(messages, max_tokens=100)
|
35 |
+
refined = completion["choices"][0]["message"]["content"].strip()
|
36 |
+
return refined
|
37 |
|
38 |
+
# Define the process function
|
39 |
+
def process(prompt, guidance_scale, num_inference_steps, height, width, seed):
|
40 |
+
print(f"Original Prompt: {prompt}")
|
41 |
|
42 |
+
# Refine the prompt
|
43 |
+
try:
|
44 |
+
refined_prompt = refine_prompt(prompt)
|
45 |
+
print(f"Refined Prompt: {refined_prompt}")
|
46 |
+
except Exception as e:
|
47 |
+
return f"Error refining prompt: {str(e)}"
|
48 |
|
49 |
+
# Set the random generator seed
|
50 |
+
generator = torch.Generator(device="cuda").manual_seed(seed)
|
51 |
|
52 |
+
try:
|
53 |
+
# Generate the image
|
54 |
+
output = flux_pipeline(
|
55 |
+
prompt=refined_prompt,
|
56 |
+
guidance_scale=guidance_scale,
|
57 |
+
num_inference_steps=num_inference_steps,
|
58 |
+
height=height,
|
59 |
+
width=width,
|
60 |
+
generator=generator,
|
61 |
+
)
|
62 |
+
image = output.images[0]
|
63 |
+
return image
|
64 |
+
except Exception as e:
|
65 |
+
return f"Error generating image: {str(e)}"
|
66 |
|
67 |
+
# Create the Gradio interface
|
68 |
with gr.Blocks() as demo:
|
69 |
+
gr.Markdown("# Flux Text-to-Image Generator with Prompt Refinement")
|
70 |
+
|
71 |
+
# User inputs
|
72 |
with gr.Row():
|
73 |
+
prompt_input = gr.Textbox(label="Enter a Prompt", placeholder="Describe your image")
|
74 |
+
guidance_scale_input = gr.Slider(
|
75 |
+
label="Guidance Scale", minimum=1.0, maximum=10.0, value=3.5, step=0.1
|
76 |
+
)
|
77 |
with gr.Row():
|
78 |
+
num_inference_steps_input = gr.Slider(
|
79 |
+
label="Inference Steps", minimum=1, maximum=100, value=50, step=1
|
80 |
+
)
|
81 |
+
seed_input = gr.Number(label="Seed", value=42, precision=0)
|
82 |
with gr.Row():
|
83 |
+
height_input = gr.Slider(label="Height", minimum=256, maximum=2048, value=768, step=64)
|
84 |
+
width_input = gr.Slider(label="Width", minimum=256, maximum=2048, value=1360, step=64)
|
|
|
85 |
|
86 |
+
# Output components
|
87 |
+
refined_prompt_output = gr.Textbox(label="Refined Prompt", interactive=False)
|
88 |
image_output = gr.Image(label="Generated Image")
|
89 |
+
|
90 |
# Button to generate the image
|
91 |
generate_button = gr.Button("Generate Image")
|
92 |
+
|
93 |
# Define button click behavior
|
94 |
generate_button.click(
|
95 |
+
fn=lambda prompt, *args: (refine_prompt(prompt), process(prompt, *args)),
|
96 |
+
inputs=[
|
97 |
+
prompt_input,
|
98 |
+
guidance_scale_input,
|
99 |
+
num_inference_steps_input,
|
100 |
+
height_input,
|
101 |
+
width_input,
|
102 |
+
seed_input,
|
103 |
+
],
|
104 |
+
outputs=[refined_prompt_output, image_output],
|
105 |
)
|
106 |
|
107 |
# Launch the app
|