SpyC0der77 commited on
Commit
db7de4c
·
verified ·
1 Parent(s): 5c0781f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -49
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 clients
8
- client = InferenceClient("EvanZhouDev/open-genmoji", token=os.getenv("HUGGINGFACE_API_TOKEN"))
9
- llm = InferenceClient("Qwen/Qwen2.5-3B-Instruct")
 
 
 
 
 
 
10
 
11
- # Ensure output directories exist
12
- os.makedirs("outputs/images", exist_ok=True)
13
 
14
- # Define the process function
15
- def process(prompt, steps, seed, quantize, guidance, width, height):
16
- print(f"Prompt: {prompt}")
 
 
17
  messages = [
18
  {
19
  "role": "system",
20
  "content": (
21
- "You are helping create a prompt for a Emoji generation image model. An emoji must be easily "
22
- "interpreted when small so details must be exaggerated to be clear. Your goal is to use descriptions "
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": prompt},
36
  ]
 
 
 
37
 
38
- completion = llm.chat_completion(messages, max_tokens=100)
39
- response = completion.get("choices")[0].get("message").get("content").replace("```", "").replace("\n", "")
40
- print(f"Refined Prompt: {response}")
41
 
42
- time = datetime.now().strftime("%Y%m%d%H%M%S")
43
- image = client.text_to_image(response, steps=steps, seed=seed, quantize=quantize, guidance=guidance, width=width, height=height)
44
- image.save(f"outputs/images/{time}.png")
 
 
 
45
 
46
- with open(f"outputs/{time}.json", "w") as f:
47
- json.dump({"prompt": prompt, "refined_prompt": response, "image": f"outputs/images/{time}.png"}, f)
48
 
49
- return image
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
- # Create Gradio interface
52
  with gr.Blocks() as demo:
53
- gr.Markdown("# Emoji Generator with Customizable Parameters")
54
-
55
- # Input fields
56
  with gr.Row():
57
- prompt_input = gr.Textbox(label="Enter a prompt")
58
- steps_input = gr.Slider(label="Steps", minimum=1, maximum=50, value=20, step=1)
 
 
59
  with gr.Row():
60
- seed_input = gr.Number(label="Seed", value=2, precision=0)
61
- quantize_input = gr.Slider(label="Quantize", minimum=1, maximum=16, value=8, step=1)
 
 
62
  with gr.Row():
63
- guidance_input = gr.Slider(label="Guidance", minimum=1.0, maximum=10.0, value=5.0, step=0.1)
64
- width_input = gr.Slider(label="Width", minimum=256, maximum=2048, value=1280, step=64)
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=[prompt_input, steps_input, seed_input, quantize_input, guidance_input, width_input, height_input],
77
- outputs=image_output,
 
 
 
 
 
 
 
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