Spaces:
Running
on
Zero
Running
on
Zero
zR
commited on
Commit
•
4ddbeae
1
Parent(s):
5bf3f79
test_glm
Browse files- app.py +97 -4
- requirements.txt +2 -1
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import os
|
|
|
2 |
import threading
|
3 |
import time
|
4 |
from datetime import datetime, timedelta
|
@@ -9,6 +10,7 @@ import random
|
|
9 |
import spaces # [uncomment to use ZeroGPU]
|
10 |
from diffusers import FluxPipeline
|
11 |
import torch
|
|
|
12 |
|
13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
14 |
model_repo_id = "black-forest-labs/FLUX.1-dev"
|
@@ -24,10 +26,95 @@ pipe = pipe.to(device)
|
|
24 |
MAX_SEED = np.iinfo(np.int32).max
|
25 |
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
def delete_old_files():
|
28 |
while True:
|
29 |
now = datetime.now()
|
30 |
-
cutoff = now - timedelta(minutes=
|
31 |
directories = ["./gradio_tmp"]
|
32 |
|
33 |
for directory in directories:
|
@@ -102,12 +189,18 @@ with gr.Blocks(css=css) as demo:
|
|
102 |
prompt = gr.Text(
|
103 |
label="Prompt",
|
104 |
show_label=False,
|
105 |
-
max_lines=
|
106 |
placeholder="Enter your prompt",
|
107 |
container=False,
|
108 |
)
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
result = gr.Image(label="Result", show_label=False)
|
112 |
|
113 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
1 |
import os
|
2 |
+
import re
|
3 |
import threading
|
4 |
import time
|
5 |
from datetime import datetime, timedelta
|
|
|
10 |
import spaces # [uncomment to use ZeroGPU]
|
11 |
from diffusers import FluxPipeline
|
12 |
import torch
|
13 |
+
from openai import OpenAI
|
14 |
|
15 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
16 |
model_repo_id = "black-forest-labs/FLUX.1-dev"
|
|
|
26 |
MAX_SEED = np.iinfo(np.int32).max
|
27 |
|
28 |
|
29 |
+
def clean_string(s):
|
30 |
+
s = s.replace("\n", " ")
|
31 |
+
s = s.strip()
|
32 |
+
s = re.sub(r"\s{2,}", " ", s)
|
33 |
+
return s
|
34 |
+
|
35 |
+
|
36 |
+
def convert_prompt(
|
37 |
+
prompt: str,
|
38 |
+
retry_times: int = 5,
|
39 |
+
) -> str:
|
40 |
+
if not os.environ.get("OPENAI_API_KEY"):
|
41 |
+
return prompt
|
42 |
+
client = OpenAI()
|
43 |
+
system_instruction = """
|
44 |
+
You are part of a team of bots that creates images . You work with an assistant bot that will draw anything you say.
|
45 |
+
For example , outputting " a beautiful morning in the woods with the sun peaking through the trees " will trigger your partner bot to output an image of a forest morning , as described.
|
46 |
+
You will be prompted by people looking to create detailed , amazing images. The way to accomplish this is to take their short prompts and make them extremely detailed and descriptive.
|
47 |
+
There are a few rules to follow :
|
48 |
+
- Prompt should always be written in English, regardless of the input language. Please provide the prompts in English.
|
49 |
+
- You will only ever output a single image description per user request.
|
50 |
+
- Image descriptions must be detailed and specific, including keyword categories such as subject, medium, style, additional details, color, and lighting.
|
51 |
+
- When generating descriptions, focus on portraying the visual elements rather than delving into abstract psychological and emotional aspects. Provide clear and concise details that vividly depict the scene and its composition, capturing the tangible elements that make up the setting.
|
52 |
+
- Do not provide the process and explanation, just return the modified English description . Image descriptions must be between 100-200 words. Extra words will be ignored.
|
53 |
+
"""
|
54 |
+
|
55 |
+
text = prompt.strip()
|
56 |
+
for i in range(retry_times):
|
57 |
+
try:
|
58 |
+
response = client.chat.completions.create(
|
59 |
+
messages=[
|
60 |
+
{"role": "system", "content": f"{system_instruction}"},
|
61 |
+
{
|
62 |
+
"role": "user",
|
63 |
+
"content": 'Create an imaginative image descriptive caption for the user input : "一个头发花白的老人"',
|
64 |
+
},
|
65 |
+
{
|
66 |
+
"role": "assistant",
|
67 |
+
"content": "A seasoned male with white hair and a neatly groomed beard stands confidently, donning a dark vest over a striped shirt. His hands are clasped together in front, one adorned with a ring, as he looks directly at the viewer with a composed expression. The soft lighting accentuates his features and the subtle textures of his attire, creating a portrait that exudes sophistication and a timeless elegance.",
|
68 |
+
},
|
69 |
+
{
|
70 |
+
"role": "user",
|
71 |
+
"content": 'Create an imaginative image descriptive caption for the user input : "画一只老鹰"',
|
72 |
+
},
|
73 |
+
{
|
74 |
+
"role": "assistant",
|
75 |
+
"content": "A majestic eagle with expansive brown and white wings glides through the air, its sharp yellow eyes focused intently ahead. The eagle's talons are poised and ready for hunting, as it soars over a rugged mountainous terrain dusted with snow, under a soft blue sky.",
|
76 |
+
},
|
77 |
+
{
|
78 |
+
"role": "user",
|
79 |
+
"content": 'Create an imaginative image descriptive caption for the user input : "画一辆摩托车"',
|
80 |
+
},
|
81 |
+
{
|
82 |
+
"role": "assistant",
|
83 |
+
"content": "Parked on a wet city street at night, a sleek motorcycle with a black and green design stands out. Its headlights cast a soft glow, reflecting off the puddles and highlighting its aerodynamic shape. The design is marked by sharp lines and angular features, with gold accents that shine against the dark backdrop. The motorcycle exudes an air of performance and luxury, ready to slice through the urban landscape.",
|
84 |
+
},
|
85 |
+
{
|
86 |
+
"role": "user",
|
87 |
+
"content": 'Create an imaginative image descriptive caption for the user input : "穿着金色盔甲的人"',
|
88 |
+
},
|
89 |
+
{
|
90 |
+
"role": "assistant",
|
91 |
+
"content": "A figure clad in meticulously crafted, golden armor stands with an air of quiet confidence. The armor, reminiscent of medieval knight attire, features a scalloped design with leaf-like patterns and is complemented by a black, form-fitting undergarment. The helmet, with its angular visor, adds to the intimidating presence. This armor, with its rich gold tones and intricate details, suggests a character of nobility or mythical origin, poised for valorous endeavors.",
|
92 |
+
},
|
93 |
+
{
|
94 |
+
"role": "user",
|
95 |
+
"content": f'Create an imaginative image descriptive caption for the user input : "{text}"',
|
96 |
+
},
|
97 |
+
],
|
98 |
+
model="glm-4-plus",
|
99 |
+
temperature=0.01,
|
100 |
+
top_p=0.7,
|
101 |
+
stream=False,
|
102 |
+
max_tokens=300,
|
103 |
+
)
|
104 |
+
prompt = response.choices[0].message.content
|
105 |
+
if prompt:
|
106 |
+
prompt = clean_string(prompt)
|
107 |
+
break
|
108 |
+
except Exception as e:
|
109 |
+
pass
|
110 |
+
|
111 |
+
return prompt
|
112 |
+
|
113 |
+
|
114 |
def delete_old_files():
|
115 |
while True:
|
116 |
now = datetime.now()
|
117 |
+
cutoff = now - timedelta(minutes=5)
|
118 |
directories = ["./gradio_tmp"]
|
119 |
|
120 |
for directory in directories:
|
|
|
189 |
prompt = gr.Text(
|
190 |
label="Prompt",
|
191 |
show_label=False,
|
192 |
+
max_lines=3,
|
193 |
placeholder="Enter your prompt",
|
194 |
container=False,
|
195 |
)
|
196 |
+
with gr.Row():
|
197 |
+
enhance = gr.Button("Enhance Prompt (Strongly Suggest)", scale=1)
|
198 |
+
enhance.click(
|
199 |
+
convert_prompt,
|
200 |
+
inputs=[prompt],
|
201 |
+
outputs=[prompt]
|
202 |
+
)
|
203 |
+
run_button = gr.Button("Run", scale=1)
|
204 |
result = gr.Image(label="Result", show_label=False)
|
205 |
|
206 |
with gr.Accordion("Advanced Settings", open=False):
|
requirements.txt
CHANGED
@@ -3,4 +3,5 @@ diffusers>=0.30.3
|
|
3 |
torch
|
4 |
gradio>=4.44
|
5 |
transformers>=4.45.2
|
6 |
-
xformers
|
|
|
|
3 |
torch
|
4 |
gradio>=4.44
|
5 |
transformers>=4.45.2
|
6 |
+
xformers
|
7 |
+
openai
|