Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -36,7 +36,6 @@ sponsor_html = """
|
|
36 |
</div>
|
37 |
"""
|
38 |
|
39 |
-
|
40 |
# more servers coming soon...
|
41 |
|
42 |
|
@@ -50,8 +49,6 @@ SERVER_NAMES = {
|
|
50 |
"nsfw_core_2" : "NSFW-Core: Uncensored Server 2",
|
51 |
"nsfw_core_3" : "NSFW-Core: Uncensored Server 3",
|
52 |
"nsfw_core_4" : "NSFW-Core: Uncensored Server 4",
|
53 |
-
|
54 |
-
|
55 |
}
|
56 |
|
57 |
|
@@ -65,14 +62,12 @@ SERVER_SOCKETS = {
|
|
65 |
"nsfw_core_2" : "FLUX-Pro-NSFW-LocalCoreProcessor-v2",
|
66 |
"nsfw_core_3" : "FLUX-Pro-NSFW-LocalCoreProcessor-v3",
|
67 |
"nsfw_core_4" : "FLUX-Pro-NSFW-LocalCoreProcessor-v4",
|
68 |
-
|
69 |
}
|
70 |
|
71 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
72 |
FLUX_URL = os.environ.get("FLUX_URL")
|
73 |
|
74 |
|
75 |
-
|
76 |
def _open_image_from_str(s: str):
|
77 |
# base64 decoding
|
78 |
if s.startswith("http"):
|
@@ -89,22 +84,20 @@ def _open_image_from_str(s: str):
|
|
89 |
raise ValueError(f"Can't parse image string: {s[:30]}β¦")
|
90 |
|
91 |
|
92 |
-
|
93 |
def generate_image(prompt, width, height, seed, randomize, server_choice):
|
94 |
-
|
95 |
-
print("\f\n\n\n\n"+prompt+"\n\n\n\n")
|
96 |
-
|
97 |
if randomize:
|
98 |
seed = random.randint(0, 9_999_999)
|
|
|
99 |
|
100 |
-
#
|
101 |
key = next(k for k, v in SERVER_NAMES.items() if v == server_choice)
|
102 |
socket = SERVER_SOCKETS.get(key)
|
103 |
|
104 |
-
|
105 |
if socket is None:
|
106 |
if not FLUX_URL:
|
107 |
-
return "Error: FLUX_URL not set."
|
108 |
url = (
|
109 |
FLUX_URL
|
110 |
.replace("[prompt]", prompt)
|
@@ -113,34 +106,34 @@ def generate_image(prompt, width, height, seed, randomize, server_choice):
|
|
113 |
.replace("[seed]", str(seed))
|
114 |
)
|
115 |
r = requests.get(url)
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
space_id = f"NihalGazi/{socket}"
|
120 |
-
client = Client(space_id, hf_token=HF_TOKEN)
|
121 |
-
res = client.predict(
|
122 |
-
prompt=prompt,
|
123 |
-
width=width,
|
124 |
-
height=height,
|
125 |
-
seed=seed,
|
126 |
-
randomize=randomize,
|
127 |
-
api_name="/predict"
|
128 |
-
)
|
129 |
-
|
130 |
-
# handle dict or str
|
131 |
-
if isinstance(res, dict):
|
132 |
-
if res.get("path"):
|
133 |
-
return Image.open(res["path"])
|
134 |
-
if res.get("url"):
|
135 |
-
return _open_image_from_str(res["url"])
|
136 |
-
return "No image found in response."
|
137 |
-
elif isinstance(res, str):
|
138 |
-
return _open_image_from_str(res)
|
139 |
else:
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
|
142 |
# βββ GRADIO INTERFACE βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
143 |
-
|
144 |
with gr.Blocks(theme=gr.themes.Default()) as demo:
|
145 |
gr.Markdown(
|
146 |
"""
|
@@ -153,8 +146,8 @@ with gr.Blocks(theme=gr.themes.Default()) as demo:
|
|
153 |
- **Google US Server**
|
154 |
- **Azure Lite Supercomputer Server**
|
155 |
- **Artemis GPU Super cluster**
|
156 |
-
- **NebulaDrive Tensor Server**
|
157 |
-
- **PixelNet NPU Server**
|
158 |
- **NSFWβCore: Uncensored Servers** (for explicit content; use responsibly)
|
159 |
- **Suggestions** β have ideas? Iβm open to them!
|
160 |
|
@@ -172,8 +165,8 @@ Click **Generate** and enjoy unlimited AI art!
|
|
172 |
β’ Discord:β―nihal_gazi_io
|
173 |
"""
|
174 |
)
|
175 |
-
|
176 |
-
|
177 |
prompt = gr.Textbox(label="Prompt", placeholder="Enter your image promptβ¦", lines=4)
|
178 |
width = gr.Slider(512, 1280, step=16, value=1280, label="Width")
|
179 |
height = gr.Slider(512, 1280, step=16, value=1280, label="Height")
|
@@ -182,20 +175,19 @@ Click **Generate** and enjoy unlimited AI art!
|
|
182 |
server = gr.Dropdown(label="Server", choices=list(SERVER_NAMES.values()),
|
183 |
value=list(SERVER_NAMES.values())[0])
|
184 |
|
185 |
-
|
186 |
generate_btn = gr.Button("Generate")
|
187 |
|
188 |
-
|
189 |
-
|
|
|
|
|
190 |
generate_btn.click(
|
191 |
generate_image,
|
192 |
inputs=[prompt, width, height, seed, rand, server],
|
193 |
-
outputs=output
|
194 |
)
|
195 |
|
196 |
-
|
197 |
-
|
198 |
-
# Now inject your raw HTML sponsor wall
|
199 |
gr.HTML(sponsor_html)
|
200 |
|
201 |
-
demo.launch()
|
|
|
36 |
</div>
|
37 |
"""
|
38 |
|
|
|
39 |
# more servers coming soon...
|
40 |
|
41 |
|
|
|
49 |
"nsfw_core_2" : "NSFW-Core: Uncensored Server 2",
|
50 |
"nsfw_core_3" : "NSFW-Core: Uncensored Server 3",
|
51 |
"nsfw_core_4" : "NSFW-Core: Uncensored Server 4",
|
|
|
|
|
52 |
}
|
53 |
|
54 |
|
|
|
62 |
"nsfw_core_2" : "FLUX-Pro-NSFW-LocalCoreProcessor-v2",
|
63 |
"nsfw_core_3" : "FLUX-Pro-NSFW-LocalCoreProcessor-v3",
|
64 |
"nsfw_core_4" : "FLUX-Pro-NSFW-LocalCoreProcessor-v4",
|
|
|
65 |
}
|
66 |
|
67 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
68 |
FLUX_URL = os.environ.get("FLUX_URL")
|
69 |
|
70 |
|
|
|
71 |
def _open_image_from_str(s: str):
|
72 |
# base64 decoding
|
73 |
if s.startswith("http"):
|
|
|
84 |
raise ValueError(f"Can't parse image string: {s[:30]}β¦")
|
85 |
|
86 |
|
|
|
87 |
def generate_image(prompt, width, height, seed, randomize, server_choice):
|
88 |
+
# determine seed
|
|
|
|
|
89 |
if randomize:
|
90 |
seed = random.randint(0, 9_999_999)
|
91 |
+
used_seed = seed
|
92 |
|
93 |
+
# pick server key and socket
|
94 |
key = next(k for k, v in SERVER_NAMES.items() if v == server_choice)
|
95 |
socket = SERVER_SOCKETS.get(key)
|
96 |
|
97 |
+
# generate image via FLUX or HF space
|
98 |
if socket is None:
|
99 |
if not FLUX_URL:
|
100 |
+
return "Error: FLUX_URL not set.", used_seed
|
101 |
url = (
|
102 |
FLUX_URL
|
103 |
.replace("[prompt]", prompt)
|
|
|
106 |
.replace("[seed]", str(seed))
|
107 |
)
|
108 |
r = requests.get(url)
|
109 |
+
img = Image.open(BytesIO(r.content)) if r.ok else f"FLUX-Pro failed ({r.status_code})"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
else:
|
111 |
+
space_id = f"NihalGazi/{socket}"
|
112 |
+
client = Client(space_id, hf_token=HF_TOKEN)
|
113 |
+
res = client.predict(
|
114 |
+
prompt=prompt,
|
115 |
+
width=width,
|
116 |
+
height=height,
|
117 |
+
seed=seed,
|
118 |
+
randomize=randomize,
|
119 |
+
api_name="/predict"
|
120 |
+
)
|
121 |
+
if isinstance(res, dict):
|
122 |
+
if res.get("path"):
|
123 |
+
img = Image.open(res["path"])
|
124 |
+
elif res.get("url"):
|
125 |
+
img = _open_image_from_str(res["url"])
|
126 |
+
else:
|
127 |
+
img = "No image found in response."
|
128 |
+
elif isinstance(res, str):
|
129 |
+
img = _open_image_from_str(res)
|
130 |
+
else:
|
131 |
+
img = f"Unexpected response type: {type(res)}"
|
132 |
+
|
133 |
+
# return both image and used seed
|
134 |
+
return img, used_seed
|
135 |
|
136 |
# βββ GRADIO INTERFACE βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
137 |
with gr.Blocks(theme=gr.themes.Default()) as demo:
|
138 |
gr.Markdown(
|
139 |
"""
|
|
|
146 |
- **Google US Server**
|
147 |
- **Azure Lite Supercomputer Server**
|
148 |
- **Artemis GPU Super cluster**
|
149 |
+
- **NebulaDrive Tensor Server**
|
150 |
+
- **PixelNet NPU Server**
|
151 |
- **NSFWβCore: Uncensored Servers** (for explicit content; use responsibly)
|
152 |
- **Suggestions** β have ideas? Iβm open to them!
|
153 |
|
|
|
165 |
β’ Discord:β―nihal_gazi_io
|
166 |
"""
|
167 |
)
|
168 |
+
|
169 |
+
# Inputs
|
170 |
prompt = gr.Textbox(label="Prompt", placeholder="Enter your image promptβ¦", lines=4)
|
171 |
width = gr.Slider(512, 1280, step=16, value=1280, label="Width")
|
172 |
height = gr.Slider(512, 1280, step=16, value=1280, label="Height")
|
|
|
175 |
server = gr.Dropdown(label="Server", choices=list(SERVER_NAMES.values()),
|
176 |
value=list(SERVER_NAMES.values())[0])
|
177 |
|
|
|
178 |
generate_btn = gr.Button("Generate")
|
179 |
|
180 |
+
# Outputs: image and seed display
|
181 |
+
output = gr.Image(type="pil", label="Generated Image")
|
182 |
+
seed_display = gr.Textbox(label="Used Seed", interactive=False)
|
183 |
+
|
184 |
generate_btn.click(
|
185 |
generate_image,
|
186 |
inputs=[prompt, width, height, seed, rand, server],
|
187 |
+
outputs=[output, seed_display]
|
188 |
)
|
189 |
|
190 |
+
# Sponsor wall
|
|
|
|
|
191 |
gr.HTML(sponsor_html)
|
192 |
|
193 |
+
demo.launch()
|