Spaces:
Sleeping
Sleeping
shweaung
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,22 +24,18 @@ article_text = """
|
|
| 24 |
"""
|
| 25 |
|
| 26 |
def query(lora_id, prompt, steps=28, cfg_scale=3.5, randomize_seed=True, seed=-1, width=1024, height=1024, output_format="PNG"):
|
| 27 |
-
if
|
| 28 |
return None, None, None
|
| 29 |
|
| 30 |
-
if lora_id.strip()
|
| 31 |
lora_id = "black-forest-labs/FLUX.1-dev"
|
| 32 |
|
| 33 |
key = random.randint(0, 999)
|
| 34 |
-
|
| 35 |
API_URL = "https://api-inference.huggingface.co/models/" + lora_id.strip()
|
| 36 |
|
| 37 |
-
API_TOKEN = random.choice([os.getenv("HF_READ_TOKEN")])
|
| 38 |
-
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
| 39 |
-
|
| 40 |
prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
|
| 41 |
|
| 42 |
-
#
|
| 43 |
if randomize_seed:
|
| 44 |
seed = random.randint(1, 4294967296)
|
| 45 |
|
|
@@ -54,6 +50,7 @@ def query(lora_id, prompt, steps=28, cfg_scale=3.5, randomize_seed=True, seed=-1
|
|
| 54 |
}
|
| 55 |
}
|
| 56 |
|
|
|
|
| 57 |
response = requests.post(API_URL, headers=headers, json=payload, timeout=timeout)
|
| 58 |
if response.status_code != 200:
|
| 59 |
print(f"Error: Failed to get image. Response status: {response.status_code}")
|
|
@@ -63,16 +60,17 @@ def query(lora_id, prompt, steps=28, cfg_scale=3.5, randomize_seed=True, seed=-1
|
|
| 63 |
raise gr.Error(f"{response.status_code}")
|
| 64 |
|
| 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 |
-
# Convert to specified format (JPEG or PNG)
|
| 71 |
img_byte_arr = io.BytesIO()
|
| 72 |
image.save(img_byte_arr, format=output_format)
|
| 73 |
img_byte_arr.seek(0)
|
| 74 |
-
|
| 75 |
-
|
|
|
|
| 76 |
except Exception as e:
|
| 77 |
print(f"Error when trying to open the image: {e}")
|
| 78 |
return None, None, None
|
|
|
|
| 24 |
"""
|
| 25 |
|
| 26 |
def query(lora_id, prompt, steps=28, cfg_scale=3.5, randomize_seed=True, seed=-1, width=1024, height=1024, output_format="PNG"):
|
| 27 |
+
if not prompt:
|
| 28 |
return None, None, None
|
| 29 |
|
| 30 |
+
if not lora_id.strip():
|
| 31 |
lora_id = "black-forest-labs/FLUX.1-dev"
|
| 32 |
|
| 33 |
key = random.randint(0, 999)
|
|
|
|
| 34 |
API_URL = "https://api-inference.huggingface.co/models/" + lora_id.strip()
|
| 35 |
|
|
|
|
|
|
|
|
|
|
| 36 |
prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
|
| 37 |
|
| 38 |
+
# Generate a random seed if needed
|
| 39 |
if randomize_seed:
|
| 40 |
seed = random.randint(1, 4294967296)
|
| 41 |
|
|
|
|
| 50 |
}
|
| 51 |
}
|
| 52 |
|
| 53 |
+
# Send the request and get the image data
|
| 54 |
response = requests.post(API_URL, headers=headers, json=payload, timeout=timeout)
|
| 55 |
if response.status_code != 200:
|
| 56 |
print(f"Error: Failed to get image. Response status: {response.status_code}")
|
|
|
|
| 60 |
raise gr.Error(f"{response.status_code}")
|
| 61 |
|
| 62 |
try:
|
| 63 |
+
# Load and convert the image to the specified format (JPEG or PNG)
|
| 64 |
image_bytes = response.content
|
| 65 |
image = Image.open(io.BytesIO(image_bytes))
|
| 66 |
print(f'\033[1mGeneration {key} completed!\033[0m ({prompt})')
|
| 67 |
|
|
|
|
| 68 |
img_byte_arr = io.BytesIO()
|
| 69 |
image.save(img_byte_arr, format=output_format)
|
| 70 |
img_byte_arr.seek(0)
|
| 71 |
+
|
| 72 |
+
# Prepare for Gradio's download
|
| 73 |
+
return image, seed, img_byte_arr
|
| 74 |
except Exception as e:
|
| 75 |
print(f"Error when trying to open the image: {e}")
|
| 76 |
return None, None, None
|