Spaces:
Running
Running
Commit
·
b99f9a1
1
Parent(s):
0ce02f7
Delete app.py
Browse files
app.py
DELETED
@@ -1,159 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import requests
|
3 |
-
import json
|
4 |
-
import PIL.Image
|
5 |
-
from io import BytesIO
|
6 |
-
import os
|
7 |
-
import random
|
8 |
-
import datetime
|
9 |
-
import urllib3
|
10 |
-
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
11 |
-
|
12 |
-
def generate_image(prompt, negative_prompt, use_negative_embedding, scheduler, steps, width, height, cfg, restore_faces, seed):
|
13 |
-
forbidden_words = os.getenv("FORBIDDEN_WORDS").split(", ")
|
14 |
-
# Check if the prompt contains any of the forbidden words
|
15 |
-
for word in forbidden_words:
|
16 |
-
if word in prompt:
|
17 |
-
raise Exception(f"The prompt contains a forbidden word: {word}")
|
18 |
-
request_time = datetime.datetime.now()
|
19 |
-
restore_faces = bool(restore_faces)
|
20 |
-
use_negative_embedding = bool(use_negative_embedding)
|
21 |
-
print(f"restore_faces: {restore_faces}, type: {type(restore_faces)}")
|
22 |
-
print(f"use_negative_embedding: {use_negative_embedding}, type: {type(use_negative_embedding)}")
|
23 |
-
if use_negative_embedding:
|
24 |
-
negative_prompt += ", rz-neg-general"
|
25 |
-
# Define the API endpoint
|
26 |
-
apiUrl = os.getenv("API_URL")
|
27 |
-
# Define the request headers
|
28 |
-
headers = {
|
29 |
-
"Content-Type": "application/json",
|
30 |
-
"token": os.getenv("API_TOKEN")
|
31 |
-
}
|
32 |
-
|
33 |
-
# Define the request body
|
34 |
-
body = {
|
35 |
-
"mode": "url",
|
36 |
-
"model": "Freedom.safetensors",
|
37 |
-
"tiling": False,
|
38 |
-
"batch_size": 1,
|
39 |
-
"prompt": prompt,
|
40 |
-
"negative_prompt": negative_prompt,
|
41 |
-
"seed":random.randint(0, 999999999),
|
42 |
-
"scheduler": scheduler,
|
43 |
-
"n_iter": 1,
|
44 |
-
"steps": steps,
|
45 |
-
"cfg": cfg,
|
46 |
-
"offset_noise": 0.0,
|
47 |
-
"width": width,
|
48 |
-
"height": height,
|
49 |
-
"clip_skip": 1,
|
50 |
-
"embeddings": [
|
51 |
-
{
|
52 |
-
"name": "rz-neg-general",
|
53 |
-
"strength": 1.0 if use_negative_embedding else 0
|
54 |
-
},
|
55 |
-
],
|
56 |
-
"vae": "vae-ft-mse-840000-ema-pruned.ckpt",
|
57 |
-
"restore_faces": restore_faces,
|
58 |
-
"fr_model": "CodeFormer",
|
59 |
-
"codeformer_weight": 0.5,
|
60 |
-
"enable_hr": False,
|
61 |
-
"denoising_strength": 0.75,
|
62 |
-
"hr_scale": 2,
|
63 |
-
"hr_upscale": "None",
|
64 |
-
"img2img_ref_img_type": "piece",
|
65 |
-
"img2img_resize_mode": 0,
|
66 |
-
"img2img_denoising_strength": 0.75,
|
67 |
-
}
|
68 |
-
|
69 |
-
# Send the request
|
70 |
-
response = requests.post(apiUrl, headers=headers, data=json.dumps(body), verify=False)
|
71 |
-
# Print the response body if the status code is not 200
|
72 |
-
if response.status_code != 200:
|
73 |
-
print(response.text)
|
74 |
-
|
75 |
-
# Check the response status
|
76 |
-
if response.status_code == 200:
|
77 |
-
|
78 |
-
# Get the image URL from the response
|
79 |
-
response_json = response.json()
|
80 |
-
if 'results' in response_json and isinstance(response_json['results'], list) and len(response_json['results']) > 0:
|
81 |
-
image_url = response_json['results'][0]
|
82 |
-
|
83 |
-
# Get the image from the URL
|
84 |
-
image_response = requests.get(image_url)
|
85 |
-
image = PIL.Image.open(BytesIO(image_response.content))
|
86 |
-
|
87 |
-
# Log the information together
|
88 |
-
print(f"Request time: {request_time}\n"
|
89 |
-
f"Prompt: {prompt}\n"
|
90 |
-
f"Negative Prompt: {negative_prompt}\n"
|
91 |
-
f"Seed: {seed}\n"
|
92 |
-
f"Res(width x height): {width} x {height}\n"
|
93 |
-
f"Image URL: {image_url}")
|
94 |
-
|
95 |
-
return image
|
96 |
-
else:
|
97 |
-
raise Exception("Unexpected API response format")
|
98 |
-
else:
|
99 |
-
raise Exception("API request failed with status code " + str(response.status_code))
|
100 |
-
|
101 |
-
# Define the Gradio interface
|
102 |
-
iface = gr.Interface(
|
103 |
-
fn=generate_image,
|
104 |
-
inputs=[
|
105 |
-
gr.components.Textbox(label="Prompt"),
|
106 |
-
gr.components.Textbox(value="ugly, tiling, poorly drawn hands, poorly drawn feet, poorly drawn face, out of frame, extra limbs, disfigured, deformed, body out of frame, blurry, bad anatomy, blurred, watermark, grainy, signature, cut off, draft", label="Negative Prompt"),
|
107 |
-
gr.inputs.Checkbox(label="Use Negative Embedding", default=True),
|
108 |
-
gr.components.Dropdown(choices=[
|
109 |
-
"Euler a",
|
110 |
-
"Euler",
|
111 |
-
"LMS",
|
112 |
-
"Heun",
|
113 |
-
"DPM2",
|
114 |
-
"DPM2 a",
|
115 |
-
"DPM++ 2S a",
|
116 |
-
"DPM++ 2M",
|
117 |
-
"DPM++ SDE",
|
118 |
-
"DPM fast",
|
119 |
-
"DPM adaptive",
|
120 |
-
"LMS Karras",
|
121 |
-
"DPM2 Karras",
|
122 |
-
"DPM2 a Karras",
|
123 |
-
"DPM++ 2S a Karras",
|
124 |
-
"DPM++ 2M Karras",
|
125 |
-
"DPM++ SDE Karras",
|
126 |
-
"DDIM",
|
127 |
-
"PLMS"
|
128 |
-
], label="Scheduler", value="DPM++ SDE Karras"),
|
129 |
-
gr.components.Slider(minimum=10, maximum=100, step=1.0,value=30, label="Steps"),
|
130 |
-
gr.components.Slider(minimum=512, maximum=1600, value=1024, label="Width"),
|
131 |
-
gr.components.Slider(minimum=512, maximum=1600, value=1024, label="Height"),
|
132 |
-
gr.components.Slider(minimum=4, maximum=12, step=0.5, value=7.0, label="CFG"),
|
133 |
-
gr.inputs.Checkbox(label="Restore Faces", default=False),
|
134 |
-
],
|
135 |
-
outputs=gr.components.Image(),
|
136 |
-
title="Freedom.Redmond Demonstration",
|
137 |
-
description = """
|
138 |
-
## Finetuned model of SD 2.1 768X produced by [@artificialguybr](https://twitter.com/artificialguybr).
|
139 |
-
|
140 |
-
## Resources
|
141 |
-
- The weights were released [here](https://civitai.com/models/87288/freedomredmond) with example prompts in CIVITAI and [here in HF](https://huggingface.co/artificialguybr/freedom).
|
142 |
-
|
143 |
-
## Demonstration
|
144 |
-
This demonstration is running on the [makeai.run API](https://www.makeai.run/).
|
145 |
-
|
146 |
-
## Acknowledgements
|
147 |
-
Thanks to [Redmond.ai](https://redmond.ai/) for providing GPU Time and sponsoring this model.
|
148 |
-
|
149 |
-
## Test my 1.5 Finetuned Model (Liberte) [here](https://huggingface.co/spaces/artificialguybr/liberte).
|
150 |
-
|
151 |
-
""",
|
152 |
-
allow_flagging='never'
|
153 |
-
)
|
154 |
-
|
155 |
-
#Adding queue
|
156 |
-
iface.queue(concurrency_count=12)
|
157 |
-
|
158 |
-
# Launch the app
|
159 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|