waloneai commited on
Commit
8b1e228
·
verified ·
1 Parent(s): db53b0f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -52
app.py CHANGED
@@ -15,98 +15,76 @@ 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, negative_prompt, steps=35, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7, width=1024, height=1024):
20
- if not prompt:
21
- raise gr.Error("Prompt cannot be empty.")
22
 
23
  key = random.randint(0, 999)
24
-
25
- # Translate the prompt from Russian to English if necessary
 
 
26
  prompt = GoogleTranslator(source='my', target='en').translate(prompt)
27
  print(f'\033[1mGeneration {key} translation:\033[0m {prompt}')
28
 
29
- # Add some extra flair to the prompt
30
  prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
31
  print(f'\033[1mGeneration {key}:\033[0m {prompt}')
32
-
33
- # Prepare the payload for the API call, including width and height
34
  payload = {
35
  "inputs": prompt,
36
- "parameters": {
37
- "negative_prompt": negative_prompt,
38
- "steps": steps,
39
- "cfg_scale": cfg_scale,
40
- "sampler": sampler,
41
- "seed": seed if seed != -1 else random.randint(1, 1000000000),
42
- "strength": strength,
43
- "width": width,
44
- "height": height
45
- }
46
  }
47
 
48
- # Send the request to the API and handle the response
49
  response = requests.post(API_URL, headers=headers, json=payload, timeout=timeout)
50
  if response.status_code != 200:
51
- error_message = f"Error: Failed to get image. Response status: {response.status_code}"
 
52
  if response.status_code == 503:
53
- error_message += " - The model is being loaded"
54
- raise gr.Error(error_message)
55
-
56
  try:
57
- # Convert the response content into an image
58
  image_bytes = response.content
59
  image = Image.open(io.BytesIO(image_bytes))
60
  print(f'\033[1mGeneration {key} completed!\033[0m ({prompt})')
61
  return image
62
  except Exception as e:
63
- raise gr.Error(f"Error when trying to open the image: {e}")
 
64
 
65
- # CSS to style the app
66
  css = """
67
  #app-container {
68
- max-width: 800px;
69
  margin-left: auto;
70
  margin-right: auto;
71
  }
72
  """
73
 
74
- # Build the Gradio UI with Blocks
75
  with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
76
- # Add a title to the app
77
- gr.HTML("<center><h1>Walone AI Image Pro Large</h1></center>")
78
-
79
- # Container for all the UI elements
80
  with gr.Column(elem_id="app-container"):
81
- # Add a text input for the main prompt
82
  with gr.Row():
83
  with gr.Column(elem_id="prompt-container"):
84
  with gr.Row():
85
- text_prompt = gr.Textbox(label="Prompt", placeholder="Prompt စာသားရေးပါ", lines=2, elem_id="prompt-text-input")
86
-
87
- # Accordion for advanced settings
88
  with gr.Row():
89
- with gr.Accordion("Advanced Settings", open=False):
90
  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")
91
- with gr.Row():
92
- width = gr.Slider(label="Width", value=1024, minimum=64, maximum=1216, step=32)
93
- height = gr.Slider(label="Height", value=1024, minimum=64, maximum=1216, step=32)
94
- steps = gr.Slider(label="Sampling steps", value=35, minimum=1, maximum=100, step=1)
95
  cfg = gr.Slider(label="CFG Scale", value=7, minimum=1, maximum=20, step=1)
96
- strength = gr.Slider(label="Strength", value=0.7, minimum=0, maximum=1, step=0.001)
97
- seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=1000000000, step=1) # Setting the seed to -1 will make it random
98
  method = gr.Radio(label="Sampling method", value="DPM++ 2M Karras", choices=["DPM++ 2M Karras", "DPM++ SDE Karras", "Euler", "Euler a", "Heun", "DDIM"])
 
 
99
 
100
- # Add a button to trigger the image generation
101
  with gr.Row():
102
  text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
103
-
104
- # Image output area to display the generated image
105
  with gr.Row():
106
  image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")
 
 
107
 
108
- # Bind the button to the query function with the added width and height inputs
109
- text_button.click(query, inputs=[text_prompt, negative_prompt, steps, cfg, method, seed, strength, width, height], outputs=image_output)
110
-
111
- # Launch the Gradio app
112
- app.launch(show_api=False, share=False)
 
15
  headers = {"Authorization": f"Bearer {API_TOKEN}"}
16
  timeout = 100
17
 
18
+ def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7):
19
+ if prompt == "" or prompt == None:
20
+ return None
 
21
 
22
  key = random.randint(0, 999)
23
+
24
+ API_TOKEN = random.choice([os.getenv("HF_READ_TOKEN")])
25
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
26
+
27
  prompt = GoogleTranslator(source='my', target='en').translate(prompt)
28
  print(f'\033[1mGeneration {key} translation:\033[0m {prompt}')
29
 
 
30
  prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
31
  print(f'\033[1mGeneration {key}:\033[0m {prompt}')
32
+
 
33
  payload = {
34
  "inputs": prompt,
35
+ "is_negative": is_negative,
36
+ "steps": steps,
37
+ "cfg_scale": cfg_scale,
38
+ "seed": seed if seed != -1 else random.randint(1, 1000000000),
39
+ "strength": strength
 
 
 
 
 
40
  }
41
 
 
42
  response = requests.post(API_URL, headers=headers, json=payload, timeout=timeout)
43
  if response.status_code != 200:
44
+ print(f"Error: Failed to get image. Response status: {response.status_code}")
45
+ print(f"Response content: {response.text}")
46
  if response.status_code == 503:
47
+ raise gr.Error(f"{response.status_code} : The model is being loaded")
48
+ raise gr.Error(f"{response.status_code}")
49
+
50
  try:
 
51
  image_bytes = response.content
52
  image = Image.open(io.BytesIO(image_bytes))
53
  print(f'\033[1mGeneration {key} completed!\033[0m ({prompt})')
54
  return image
55
  except Exception as e:
56
+ print(f"Error when trying to open the image: {e}")
57
+ return None
58
 
 
59
  css = """
60
  #app-container {
61
+ max-width: 600px;
62
  margin-left: auto;
63
  margin-right: auto;
64
  }
65
  """
66
 
 
67
  with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
68
+ gr.HTML("<center><h1>Walone AI Image Stable</h1></center>")
 
 
 
69
  with gr.Column(elem_id="app-container"):
 
70
  with gr.Row():
71
  with gr.Column(elem_id="prompt-container"):
72
  with gr.Row():
73
+ text_prompt = gr.Textbox(label="Prompt ရေးရန်", placeholder="ဒီနေရာမှာ prompt ရေးပါ", lines=2, elem_id="prompt-text-input")
 
 
74
  with gr.Row():
75
+ with gr.Accordion("အဆင့်မြင့် Settings", open=False):
76
  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")
77
+ steps = gr.Slider(label="Sampling steps", value=4, minimum=1, maximum=100, step=1)
 
 
 
78
  cfg = gr.Slider(label="CFG Scale", value=7, minimum=1, maximum=20, step=1)
 
 
79
  method = gr.Radio(label="Sampling method", value="DPM++ 2M Karras", choices=["DPM++ 2M Karras", "DPM++ SDE Karras", "Euler", "Euler a", "Heun", "DDIM"])
80
+ strength = gr.Slider(label="Strength", value=0.7, minimum=0, maximum=1, step=0.001)
81
+ seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=1000000000, step=1)
82
 
 
83
  with gr.Row():
84
  text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
 
 
85
  with gr.Row():
86
  image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")
87
+
88
+ text_button.click(query, inputs=[text_prompt, negative_prompt, steps, cfg, method, seed, strength], outputs=image_output)
89
 
90
+ app.launch(show_api=False, share=True)