Commit
·
71a5076
1
Parent(s):
bdd2c52
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,8 +12,13 @@ API_URL = "https://api-inference.huggingface.co/models/openskyml/open-diffusion-
|
|
| 12 |
API_TOKEN = os.getenv("HF_READ_TOKEN") # it is free
|
| 13 |
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
| 14 |
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def query(prompt, is_negative=False, image_style="None style", steps=8, cfg_scale=7, seed=None, num_images=4):
|
|
|
|
|
|
|
|
|
|
| 17 |
images = []
|
| 18 |
for _ in range(num_images):
|
| 19 |
if image_style == "None style":
|
|
@@ -51,14 +56,6 @@ def query(prompt, is_negative=False, image_style="None style", steps=8, cfg_scal
|
|
| 51 |
|
| 52 |
image_bytes = requests.post(API_URL, headers=headers, json=payload).content
|
| 53 |
image = Image.open(io.BytesIO(image_bytes))
|
| 54 |
-
rand_num = random.randint(1, 2147483647)
|
| 55 |
-
api = HfApi()
|
| 56 |
-
api.upload_file(
|
| 57 |
-
path_or_fileobj=image,
|
| 58 |
-
path_in_repo=f"images/img_{rand_num}.png",
|
| 59 |
-
repo_id="openskyml/prompts",
|
| 60 |
-
repo_type="dataset",
|
| 61 |
-
)
|
| 62 |
|
| 63 |
images.append(image)
|
| 64 |
|
|
|
|
| 12 |
API_TOKEN = os.getenv("HF_READ_TOKEN") # it is free
|
| 13 |
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
| 14 |
|
| 15 |
+
word_list_dataset = load_dataset("openskyml/bad-words-prompt-list", data_files="en.txt", use_auth_token=True)
|
| 16 |
+
word_list = word_list_dataset["train"]['text']
|
| 17 |
|
| 18 |
def query(prompt, is_negative=False, image_style="None style", steps=8, cfg_scale=7, seed=None, num_images=4):
|
| 19 |
+
for filter in word_list:
|
| 20 |
+
if re.search(rf"\b{filter}\b", prompt):
|
| 21 |
+
raise gr.Error("Unsafe content found. Please try again with different prompts.")
|
| 22 |
images = []
|
| 23 |
for _ in range(num_images):
|
| 24 |
if image_style == "None style":
|
|
|
|
| 56 |
|
| 57 |
image_bytes = requests.post(API_URL, headers=headers, json=payload).content
|
| 58 |
image = Image.open(io.BytesIO(image_bytes))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
images.append(image)
|
| 61 |
|