marahmerah commited on
Commit
753d150
·
verified ·
1 Parent(s): e2b312e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -25
app.py CHANGED
@@ -15,7 +15,7 @@ API_TOKEN = os.getenv("HF_READ_TOKEN")
15
  headers = {"Authorization": f"Bearer {API_TOKEN}"}
16
  timeout = 100
17
 
18
- # Function to query the API and return the generated image
19
  def query(prompt, is_negative=False, steps=35, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7, width=1024, height=1024):
20
  if prompt == "" or prompt is None:
21
  return None
@@ -25,15 +25,15 @@ def query(prompt, is_negative=False, steps=35, cfg_scale=7, sampler="DPM++ 2M Ka
25
  API_TOKEN = random.choice([os.getenv("HF_READ_TOKEN")])
26
  headers = {"Authorization": f"Bearer {API_TOKEN}"}
27
 
28
- # Translate the prompt from Russian to English if necessary
29
  prompt = GoogleTranslator(source='id', target='en').translate(prompt)
30
  print(f'\033[1mGeneration {key} translation:\033[0m {prompt}')
31
 
32
- # Add some extra flair to the prompt
33
  prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
34
  print(f'\033[1mGeneration {key}:\033[0m {prompt}')
35
 
36
- # Prepare the payload for the API call, including width and height
37
  payload = {
38
  "inputs": prompt,
39
  "is_negative": is_negative,
@@ -42,12 +42,12 @@ def query(prompt, is_negative=False, steps=35, cfg_scale=7, sampler="DPM++ 2M Ka
42
  "seed": seed if seed != -1 else random.randint(1, 1000000000),
43
  "strength": strength,
44
  "parameters": {
45
- "width": width, # Pass the width to the API
46
- "height": height # Pass the height to the API
47
  }
48
  }
49
 
50
- # Send the request to the API and handle the response
51
  response = requests.post(API_URL, headers=headers, json=payload, timeout=timeout)
52
  if response.status_code != 200:
53
  print(f"Error: Failed to get image. Response status: {response.status_code}")
@@ -57,16 +57,23 @@ def query(prompt, is_negative=False, steps=35, cfg_scale=7, sampler="DPM++ 2M Ka
57
  raise gr.Error(f"{response.status_code}")
58
 
59
  try:
60
- # Convert the response content into an image
61
  image_bytes = response.content
62
- image = Image.open(io.BytesIO(image_bytes))
 
 
 
 
 
 
 
63
  print(f'\033[1mGeneration {key} completed!\033[0m ({prompt})')
64
- return image
65
  except Exception as e:
66
  print(f"Error when trying to open the image: {e}")
67
  return None
68
 
69
- # CSS to style the app
70
  css = """
71
  #app-container {
72
  max-width: 800px;
@@ -75,42 +82,54 @@ css = """
75
  }
76
  """
77
 
78
- # Build the Gradio UI with Blocks
79
  with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
80
- # Add a title to the app
81
  gr.HTML("<center><h1>FLUX.1-Dev</h1></center>")
82
 
83
- # Container for all the UI elements
84
  with gr.Column(elem_id="app-container"):
85
- # Add a text input for the main prompt
86
  with gr.Row():
87
  with gr.Column(elem_id="prompt-container"):
88
  with gr.Row():
89
- text_prompt = gr.Textbox(label="Prompt", placeholder="Enter a prompt here", lines=2, elem_id="prompt-text-input")
 
 
 
 
 
90
 
91
- # Accordion for advanced settings
92
  with gr.Row():
93
  with gr.Accordion("Advanced Settings", open=False):
94
- negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="What should not be in the image", value="(deformed, distorted, disfigured), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, misspellings, typos", lines=3, elem_id="negative-prompt-text-input")
 
 
 
 
 
 
95
  with gr.Row():
96
  width = gr.Slider(label="Width", value=1024, minimum=64, maximum=1216, step=32)
97
  height = gr.Slider(label="Height", value=1024, minimum=64, maximum=1216, step=32)
98
  steps = gr.Slider(label="Sampling steps", value=35, minimum=1, maximum=100, step=1)
99
  cfg = gr.Slider(label="CFG Scale", value=7, minimum=1, maximum=20, step=1)
100
  strength = gr.Slider(label="Strength", value=0.7, minimum=0, maximum=1, step=0.001)
101
- seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=1000000000, step=1) # Setting the seed to -1 will make it random
102
- method = gr.Radio(label="Sampling method", value="DPM++ 2M Karras", choices=["DPM++ 2M Karras", "DPM++ SDE Karras", "Euler", "Euler a", "Heun", "DDIM"])
 
 
 
 
103
 
104
- # Add a button to trigger the image generation
105
  with gr.Row():
106
  text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
107
 
108
- # Image output area to display the generated image
109
  with gr.Row():
110
  image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")
111
 
112
- # Bind the button to the query function with the added width and height inputs
113
- text_button.click(query, inputs=[text_prompt, negative_prompt, steps, cfg, method, seed, strength, width, height], outputs=image_output)
 
 
 
114
 
115
- # Launch the Gradio app
116
  app.launch(show_api=True, share=True)
 
15
  headers = {"Authorization": f"Bearer {API_TOKEN}"}
16
  timeout = 100
17
 
18
+ # Function to query the API and return the generated image as PNG
19
  def query(prompt, is_negative=False, steps=35, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7, width=1024, height=1024):
20
  if prompt == "" or prompt is None:
21
  return None
 
25
  API_TOKEN = random.choice([os.getenv("HF_READ_TOKEN")])
26
  headers = {"Authorization": f"Bearer {API_TOKEN}"}
27
 
28
+ # Translate the prompt from Indonesian to English
29
  prompt = GoogleTranslator(source='id', target='en').translate(prompt)
30
  print(f'\033[1mGeneration {key} translation:\033[0m {prompt}')
31
 
32
+ # Enhance the prompt
33
  prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
34
  print(f'\033[1mGeneration {key}:\033[0m {prompt}')
35
 
36
+ # Prepare the payload
37
  payload = {
38
  "inputs": prompt,
39
  "is_negative": is_negative,
 
42
  "seed": seed if seed != -1 else random.randint(1, 1000000000),
43
  "strength": strength,
44
  "parameters": {
45
+ "width": width,
46
+ "height": height
47
  }
48
  }
49
 
50
+ # API request
51
  response = requests.post(API_URL, headers=headers, json=payload, timeout=timeout)
52
  if response.status_code != 200:
53
  print(f"Error: Failed to get image. Response status: {response.status_code}")
 
57
  raise gr.Error(f"{response.status_code}")
58
 
59
  try:
60
+ # Convert to PNG
61
  image_bytes = response.content
62
+ img = Image.open(io.BytesIO(image_bytes))
63
+
64
+ # Create PNG in memory
65
+ png_buffer = io.BytesIO()
66
+ img.save(png_buffer, format="PNG")
67
+ png_buffer.seek(0)
68
+ png_image = Image.open(png_buffer)
69
+
70
  print(f'\033[1mGeneration {key} completed!\033[0m ({prompt})')
71
+ return png_image
72
  except Exception as e:
73
  print(f"Error when trying to open the image: {e}")
74
  return None
75
 
76
+ # CSS styling
77
  css = """
78
  #app-container {
79
  max-width: 800px;
 
82
  }
83
  """
84
 
85
+ # Gradio UI
86
  with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
 
87
  gr.HTML("<center><h1>FLUX.1-Dev</h1></center>")
88
 
 
89
  with gr.Column(elem_id="app-container"):
 
90
  with gr.Row():
91
  with gr.Column(elem_id="prompt-container"):
92
  with gr.Row():
93
+ text_prompt = gr.Textbox(
94
+ label="Prompt",
95
+ placeholder="Enter a prompt here",
96
+ lines=2,
97
+ elem_id="prompt-text-input"
98
+ )
99
 
 
100
  with gr.Row():
101
  with gr.Accordion("Advanced Settings", open=False):
102
+ negative_prompt = gr.Textbox(
103
+ label="Negative Prompt",
104
+ placeholder="What should not be in the image",
105
+ value="(deformed, distorted, disfigured), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, misspellings, typos",
106
+ lines=3,
107
+ elem_id="negative-prompt-text-input"
108
+ )
109
  with gr.Row():
110
  width = gr.Slider(label="Width", value=1024, minimum=64, maximum=1216, step=32)
111
  height = gr.Slider(label="Height", value=1024, minimum=64, maximum=1216, step=32)
112
  steps = gr.Slider(label="Sampling steps", value=35, minimum=1, maximum=100, step=1)
113
  cfg = gr.Slider(label="CFG Scale", value=7, minimum=1, maximum=20, step=1)
114
  strength = gr.Slider(label="Strength", value=0.7, minimum=0, maximum=1, step=0.001)
115
+ seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=1000000000, step=1)
116
+ method = gr.Radio(
117
+ label="Sampling method",
118
+ value="DPM++ 2M Karras",
119
+ choices=["DPM++ 2M Karras", "DPM++ SDE Karras", "Euler", "Euler a", "Heun", "DDIM"]
120
+ )
121
 
 
122
  with gr.Row():
123
  text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
124
 
 
125
  with gr.Row():
126
  image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")
127
 
128
+ text_button.click(
129
+ query,
130
+ inputs=[text_prompt, negative_prompt, steps, cfg, method, seed, strength, width, height],
131
+ outputs=image_output
132
+ )
133
 
134
+ # Launch the app
135
  app.launch(show_api=True, share=True)