Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -43,74 +43,80 @@ def enhance_prompt_with_qwen(prompt):
|
|
43 |
|
44 |
# Image generation query function
|
45 |
def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7, huggingface_api_key=None, use_dev=False):
|
46 |
-
# Set system prompt first
|
47 |
-
set_system_prompt()
|
48 |
-
|
49 |
-
# Enhance the prompt before translation
|
50 |
-
enhanced_prompt = enhance_prompt_with_qwen(prompt)
|
51 |
-
|
52 |
-
# Determine which API URL to use
|
53 |
-
api_url = API_URL_DEV if use_dev else API_URL
|
54 |
-
|
55 |
-
# Check if the request is an API call by checking for the presence of the huggingface_api_key
|
56 |
-
is_api_call = huggingface_api_key is not None
|
57 |
-
|
58 |
-
if is_api_call:
|
59 |
-
# Use the environment variable for the API key in GUI mode
|
60 |
-
API_TOKEN = os.getenv("HF_READ_TOKEN")
|
61 |
-
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
62 |
-
else:
|
63 |
-
# Validate the API key if it's an API call
|
64 |
-
if huggingface_api_key == "":
|
65 |
-
raise gr.Error("API key is required for API calls.")
|
66 |
-
headers = {"Authorization": f"Bearer {huggingface_api_key}"}
|
67 |
-
|
68 |
-
if enhanced_prompt == "" or enhanced_prompt is None:
|
69 |
-
return None
|
70 |
-
|
71 |
-
key = random.randint(0, 999)
|
72 |
-
|
73 |
-
# Translate the enhanced prompt
|
74 |
-
enhanced_prompt = GoogleTranslator(source='ru', target='en').translate(enhanced_prompt)
|
75 |
-
print(f'\033[1mGeneration {key} translation:\033[0m {enhanced_prompt}')
|
76 |
-
|
77 |
-
enhanced_prompt = f"{enhanced_prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
|
78 |
-
print(f'\033[1mGeneration {key}:\033[0m {enhanced_prompt}')
|
79 |
-
|
80 |
-
# If seed is -1, generate a random seed and use it
|
81 |
-
if seed == -1:
|
82 |
-
seed = random.randint(1, 1000000000)
|
83 |
-
|
84 |
-
payload = {
|
85 |
-
"inputs": enhanced_prompt,
|
86 |
-
"is_negative": is_negative,
|
87 |
-
"steps": steps,
|
88 |
-
"cfg_scale": cfg_scale,
|
89 |
-
"seed": seed,
|
90 |
-
"strength": strength
|
91 |
-
}
|
92 |
-
|
93 |
-
response = requests.post(api_url, headers=headers, json=payload, timeout=timeout)
|
94 |
-
if response.status_code != 200:
|
95 |
-
print(f"Error: Failed to get image. Response status: {response.status_code}")
|
96 |
-
print(f"Response content: {response.text}")
|
97 |
-
if response.status_code == 503:
|
98 |
-
raise gr.Error(f"{response.status_code} : The model is being loaded")
|
99 |
-
raise gr.Error(f"{response.status_code}")
|
100 |
-
|
101 |
try:
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
css = """
|
116 |
#app-container {
|
|
|
43 |
|
44 |
# Image generation query function
|
45 |
def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7, huggingface_api_key=None, use_dev=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
try:
|
47 |
+
# Set system prompt first
|
48 |
+
set_system_prompt()
|
49 |
+
|
50 |
+
# Enhance the prompt before translation
|
51 |
+
enhanced_prompt = enhance_prompt_with_qwen(prompt)
|
52 |
+
|
53 |
+
# Determine which API URL to use
|
54 |
+
api_url = API_URL_DEV if use_dev else API_URL
|
55 |
+
|
56 |
+
# Check if the request is an API call by checking for the presence of the huggingface_api_key
|
57 |
+
is_api_call = huggingface_api_key is not None
|
58 |
+
|
59 |
+
if is_api_call:
|
60 |
+
# Use the environment variable for the API key in GUI mode
|
61 |
+
API_TOKEN = os.getenv("HF_READ_TOKEN")
|
62 |
+
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
63 |
+
else:
|
64 |
+
# Validate the API key if it's an API call
|
65 |
+
if huggingface_api_key == "":
|
66 |
+
raise gr.Error("API key is required for API calls.")
|
67 |
+
headers = {"Authorization": f"Bearer {huggingface_api_key}"}
|
68 |
+
|
69 |
+
if enhanced_prompt == "" or enhanced_prompt is None:
|
70 |
+
return None, None
|
71 |
+
|
72 |
+
key = random.randint(0, 999)
|
73 |
+
|
74 |
+
# Translate the enhanced prompt
|
75 |
+
enhanced_prompt = GoogleTranslator(source='ru', target='en').translate(enhanced_prompt)
|
76 |
+
print(f'\033[1mGeneration {key} translation:\033[0m {enhanced_prompt}')
|
77 |
+
|
78 |
+
enhanced_prompt = f"{enhanced_prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
|
79 |
+
print(f'\033[1mGeneration {key}:\033[0m {enhanced_prompt}')
|
80 |
+
|
81 |
+
# If seed is -1, generate a random seed and use it
|
82 |
+
if seed == -1:
|
83 |
+
seed = random.randint(1, 1000000000)
|
84 |
+
|
85 |
+
payload = {
|
86 |
+
"inputs": enhanced_prompt,
|
87 |
+
"is_negative": is_negative,
|
88 |
+
"steps": steps,
|
89 |
+
"cfg_scale": cfg_scale,
|
90 |
+
"seed": seed,
|
91 |
+
"strength": strength
|
92 |
+
}
|
93 |
+
|
94 |
+
response = requests.post(api_url, headers=headers, json=payload, timeout=timeout)
|
95 |
+
if response.status_code != 200:
|
96 |
+
print(f"Error: Failed to get image. Response status: {response.status_code}")
|
97 |
+
print(f"Response content: {response.text}")
|
98 |
+
if response.status_code == 503:
|
99 |
+
raise gr.Error(f"{response.status_code} : The model is being loaded")
|
100 |
+
raise gr.Error(f"{response.status_code}")
|
101 |
|
102 |
+
try:
|
103 |
+
# Attempt to open the image
|
104 |
+
image_bytes = response.content
|
105 |
+
image = Image.open(io.BytesIO(image_bytes))
|
106 |
+
print(f'\033[1mGeneration {key} completed!\033[0m ({enhanced_prompt})')
|
107 |
+
|
108 |
+
# Save the image to a file and return the file path and seed
|
109 |
+
output_path = f"./output_{key}.png"
|
110 |
+
image.save(output_path)
|
111 |
+
|
112 |
+
return output_path, seed
|
113 |
+
except Exception as e:
|
114 |
+
print(f"Error when trying to open the image: {e}")
|
115 |
+
return None, seed # If the image fails, return None for image, seed is still returned
|
116 |
+
|
117 |
+
except Exception as ex:
|
118 |
+
print(f"Error in query execution: {ex}")
|
119 |
+
return None, None # If the entire process fails, return None for both
|
120 |
|
121 |
css = """
|
122 |
#app-container {
|