shweaung commited on
Commit
6158581
•
1 Parent(s): 9d14420

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -26
app.py CHANGED
@@ -3,8 +3,10 @@ import requests
3
  import io
4
  import random
5
  import os
 
6
  from PIL import Image
7
  from deep_translator import GoogleTranslator
 
8
 
9
  # Project by Nymbo
10
 
@@ -13,27 +15,20 @@ API_TOKEN = os.getenv("HF_READ_TOKEN")
13
  headers = {"Authorization": f"Bearer {API_TOKEN}"}
14
  timeout = 100
15
 
16
- MAX_IMAGE_SIZE = 1024 # Define the maximum image size
17
-
18
- def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7, width=512, height=512):
19
- if not prompt:
20
  return None
21
 
22
  key = random.randint(0, 999)
23
-
24
- if API_TOKEN is None:
25
- raise gr.Error("API token is missing. Please set the HF_READ_TOKEN environment variable.")
26
 
 
27
  headers = {"Authorization": f"Bearer {API_TOKEN}"}
28
 
29
- try:
30
- prompt = GoogleTranslator(source='my', target='en').translate(prompt)
31
- except Exception as e:
32
- print(f"Translation error: {e}")
33
- raise gr.Error("Failed to translate the prompt.")
34
 
35
  prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
36
- print(f'Generation {key}: {prompt}')
37
 
38
  payload = {
39
  "inputs": prompt,
@@ -41,21 +36,22 @@ def query(prompt, is_negative=False, steps=30, cfg_scale=7, sampler="DPM++ 2M Ka
41
  "steps": steps,
42
  "cfg_scale": cfg_scale,
43
  "seed": seed if seed != -1 else random.randint(1, 1000000000),
44
- "strength": strength,
45
- "width": width,
46
- "height": height
47
  }
48
 
 
 
 
 
 
 
 
 
49
  try:
50
- response = requests.post(API_URL, headers=headers, json=payload, timeout=timeout)
51
- response.raise_for_status()
52
  image_bytes = response.content
53
  image = Image.open(io.BytesIO(image_bytes))
54
- print(f'Generation {key} completed! ({prompt})')
55
  return image
56
- except requests.exceptions.RequestException as e:
57
- print(f"Error: Failed to get image. {e}")
58
- raise gr.Error(f"Failed to get image: {e}")
59
  except Exception as e:
60
  print(f"Error when trying to open the image: {e}")
61
  return None
@@ -73,7 +69,8 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
73
  with gr.Column(elem_id="app-container"):
74
  with gr.Row():
75
  with gr.Column(elem_id="prompt-container"):
76
- text_prompt = gr.Textbox(label="Prompt ရေးပါ", placeholder="Enter a prompt here", lines=2, elem_id="prompt-text-input")
 
77
  with gr.Row():
78
  with gr.Accordion("အဆင့်မြင့် Settings", open=False):
79
  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")
@@ -82,14 +79,12 @@ with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
82
  method = gr.Radio(label="Sampling method", value="DPM++ 2M Karras", choices=["DPM++ 2M Karras", "DPM++ SDE Karras", "Euler", "Euler a", "Heun", "DDIM"])
83
  strength = gr.Slider(label="Strength", value=0.7, minimum=0, maximum=1, step=0.001)
84
  seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=1000000000, step=1)
85
- width = gr.Slider(label="Width", value=512, minimum=256, maximum=MAX_IMAGE_SIZE, step=32)
86
- height = gr.Slider(label="Height", value=512, minimum=256, maximum=MAX_IMAGE_SIZE, step=32)
87
 
88
  with gr.Row():
89
  text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
90
  with gr.Row():
91
  image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")
92
 
93
- text_button.click(query, inputs=[text_prompt, negative_prompt, steps, cfg, method, seed, strength, width, height], outputs=image_output)
94
 
95
  app.launch(show_api=False, share=False)
 
3
  import io
4
  import random
5
  import os
6
+ import time
7
  from PIL import Image
8
  from deep_translator import GoogleTranslator
9
+ import json
10
 
11
  # Project by Nymbo
12
 
 
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,
 
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
 
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="Enter a prompt here", 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")
 
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=False)