SpyC0der77 commited on
Commit
ee7865a
·
verified ·
1 Parent(s): 34f8991

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -37
app.py CHANGED
@@ -4,55 +4,79 @@ import os
4
  from datetime import datetime
5
  import json
6
 
7
-
8
  client = InferenceClient("EvanZhouDev/open-genmoji", token=os.getenv("HUGGINGFACE_API_TOKEN"))
9
  llm = InferenceClient("Qwen/Qwen2.5-72B-Instruct")
10
- try:
11
- os.mkdir("outputs")
12
- except FileExistsError:
13
- print("Folder already exists.")
14
-
15
- try:
16
- os.mkdir("outputs/images")
17
- except FileExistsError:
18
- print("Folder already exists.")
19
-
20
- # Define the process function that takes a text prompt and returns an image
21
- def process(prompt):
22
- print(prompt)
23
  messages = [
24
- { "role": "system", "content": "You are helping create a prompt for a Emoji generation image model. An emoji must be easily interpreted when small so details must be exaggerated to be clear. Your goal is to use descriptions to achieve this.\n\nYou will receive a user description, and you must rephrase it to consist of short phrases separated by periods, adding detail to everything the user provides.\n\nAdd describe the color of all parts or components of the emoji. Unless otherwise specified by the user, do not describe people. Do not describe the background of the image. Your output should be in the format:\n\n```\nemoji of {description}. {addon phrases}. 3D lighting. no cast shadows.\n```\n\nThe description should be a 1 sentence of your interpretation of the emoji.\nThen, you may choose to add addon phrases. You must use the following in the given scenarios:\n\n- \"cute.\": If generating anything that's not an object, and also not a human\n- \"enlarged head in cartoon style.\": ONLY animals\n- \"head is turned towards viewer.\": ONLY humans or animals\n- \"detailed texture.\": ONLY objects\n\nFurther addon phrases may be added to ensure the clarity of the emoji." },
25
- { "role": "user", "content": prompt }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  ]
27
 
28
  completion = llm.chat_completion(messages, max_tokens=100)
29
-
30
  response = completion.get("choices")[0].get("message").get("content").replace("```", "").replace("\n", "")
31
- print(response)
32
- time = str(datetime.now())
33
- print(time)
34
- time = time.replace(".", "").replace(":","")
35
- image = client.text_to_image(response)
36
- image.save("outputs/images/" + time + ".png")
37
- with open("outputs/" + time + ".json", "w") as f:
38
- f.write(json.dumps({"prompt": prompt, "refined_prompt": response, "image": "outputs/images/" + time + ".png"}))
 
39
  return image
40
 
41
- # Create a Gradio Blocks app
42
  with gr.Blocks() as demo:
43
- # Create a Textbox for the input prompt
44
- prompt_input = gr.Textbox(label="Enter a prompt")
45
- # Create an Image component for the output image
 
 
 
 
 
 
 
 
 
 
 
 
46
  image_output = gr.Image(label="Generated Image")
47
- # Create a Button to trigger the image generation
48
- generate_button = gr.Button("Generate Image")
49
 
50
- # Define the event listener for the button click
51
- generate_button.click(fn=process, inputs=prompt_input, outputs=image_output)
52
 
53
- # Define the event listener for the Enter key press
54
- prompt_input.submit(fn=process, inputs=prompt_input, outputs=image_output)
 
 
 
 
55
 
56
- # Launch the interface
57
  if __name__ == "__main__":
58
- demo.launch(show_error=True)
 
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-72B-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
81
  if __name__ == "__main__":
82
+ demo.launch(show_error=True)