K00B404 commited on
Commit
4195937
1 Parent(s): fe8891a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -7
app.py CHANGED
@@ -5,6 +5,7 @@ import random
5
  import os
6
  from PIL import Image
7
  from deep_translator import GoogleTranslator
 
8
 
9
  # os.makedirs('assets', exist_ok=True)
10
  if not os.path.exists('icon.jpg'):
@@ -13,7 +14,20 @@ API_URL_DEV = "https://api-inference.huggingface.co/models/black-forest-labs/FLU
13
  API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
14
  timeout = 100
15
 
 
 
 
 
 
 
 
 
 
 
16
  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):
 
 
 
17
  # Determine which API URL to use
18
  api_url = API_URL_DEV if use_dev else API_URL
19
 
@@ -30,23 +44,24 @@ def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Ka
30
  raise gr.Error("API key is required for API calls.")
31
  headers = {"Authorization": f"Bearer {huggingface_api_key}"}
32
 
33
- if prompt == "" or prompt is None:
34
  return None
35
 
36
  key = random.randint(0, 999)
37
 
38
- prompt = GoogleTranslator(source='ru', target='en').translate(prompt)
39
- print(f'\033[1mGeneration {key} translation:\033[0m {prompt}')
 
40
 
41
- prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
42
- print(f'\033[1mGeneration {key}:\033[0m {prompt}')
43
 
44
  # If seed is -1, generate a random seed and use it
45
  if seed == -1:
46
  seed = random.randint(1, 1000000000)
47
 
48
  payload = {
49
- "inputs": prompt,
50
  "is_negative": is_negative,
51
  "steps": steps,
52
  "cfg_scale": cfg_scale,
@@ -65,7 +80,7 @@ def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Ka
65
  try:
66
  image_bytes = response.content
67
  image = Image.open(io.BytesIO(image_bytes))
68
- print(f'\033[1mGeneration {key} completed!\033[0m ({prompt})')
69
 
70
  # Save the image to a file and return the file path and seed
71
  output_path = f"./output_{key}.png"
 
5
  import os
6
  from PIL import Image
7
  from deep_translator import GoogleTranslator
8
+ from gradio_client import Client # Import the gradio client for prompt enhancement
9
 
10
  # os.makedirs('assets', exist_ok=True)
11
  if not os.path.exists('icon.jpg'):
 
14
  API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
15
  timeout = 100
16
 
17
+ def enhance_prompt_with_qwen(prompt):
18
+ client = Client("Qwen/Qwen2.5-72B-Instruct")
19
+ result = client.predict(
20
+ query=prompt,
21
+ history=[],
22
+ system="You are Qwen, an image generation prompt enhancer",
23
+ api_name="/model_chat"
24
+ )
25
+ return result['output'] # Assuming the enhanced prompt is under 'output'
26
+
27
  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):
28
+ # Enhance the prompt before translation
29
+ enhanced_prompt = enhance_prompt_with_qwen(prompt)
30
+
31
  # Determine which API URL to use
32
  api_url = API_URL_DEV if use_dev else API_URL
33
 
 
44
  raise gr.Error("API key is required for API calls.")
45
  headers = {"Authorization": f"Bearer {huggingface_api_key}"}
46
 
47
+ if enhanced_prompt == "" or enhanced_prompt is None:
48
  return None
49
 
50
  key = random.randint(0, 999)
51
 
52
+ # Translate the enhanced prompt
53
+ enhanced_prompt = GoogleTranslator(source='ru', target='en').translate(enhanced_prompt)
54
+ print(f'\033[1mGeneration {key} translation:\033[0m {enhanced_prompt}')
55
 
56
+ enhanced_prompt = f"{enhanced_prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
57
+ print(f'\033[1mGeneration {key}:\033[0m {enhanced_prompt}')
58
 
59
  # If seed is -1, generate a random seed and use it
60
  if seed == -1:
61
  seed = random.randint(1, 1000000000)
62
 
63
  payload = {
64
+ "inputs": enhanced_prompt,
65
  "is_negative": is_negative,
66
  "steps": steps,
67
  "cfg_scale": cfg_scale,
 
80
  try:
81
  image_bytes = response.content
82
  image = Image.open(io.BytesIO(image_bytes))
83
+ print(f'\033[1mGeneration {key} completed!\033[0m ({enhanced_prompt})')
84
 
85
  # Save the image to a file and return the file path and seed
86
  output_path = f"./output_{key}.png"