Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,55 +7,18 @@ from PIL import Image
|
|
7 |
import io
|
8 |
import requests
|
9 |
from huggingface_hub import HfApi, login
|
10 |
-
from pathlib import Path
|
11 |
-
import json
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
st.session_state.is_authenticated = False
|
19 |
-
|
20 |
-
def save_token(token):
|
21 |
-
"""Save token to session state"""
|
22 |
-
st.session_state.hf_token = token
|
23 |
-
st.session_state.is_authenticated = True
|
24 |
-
|
25 |
-
def authenticate_user():
|
26 |
-
"""Handle user authentication with HuggingFace"""
|
27 |
-
st.sidebar.markdown("## π Authentication")
|
28 |
-
|
29 |
-
if st.session_state.is_authenticated:
|
30 |
-
st.sidebar.success("β Logged in to HuggingFace")
|
31 |
-
if st.sidebar.button("Logout"):
|
32 |
-
st.session_state.hf_token = None
|
33 |
-
st.session_state.is_authenticated = False
|
34 |
-
st.rerun()
|
35 |
-
else:
|
36 |
-
token = st.sidebar.text_input("Enter HuggingFace Token", type="password",
|
37 |
-
help="Get your token from https://huggingface.co/settings/tokens")
|
38 |
-
if st.sidebar.button("Login"):
|
39 |
-
if token:
|
40 |
-
try:
|
41 |
-
# Verify token is valid
|
42 |
-
api = HfApi(token=token)
|
43 |
-
api.whoami()
|
44 |
-
save_token(token)
|
45 |
-
st.sidebar.success("Successfully logged in!")
|
46 |
-
st.rerun()
|
47 |
-
except Exception as e:
|
48 |
-
st.sidebar.error(f"Authentication failed: {str(e)}")
|
49 |
-
else:
|
50 |
-
st.sidebar.error("Please enter your HuggingFace token")
|
51 |
|
52 |
class ModelGenerator:
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
def generate_midjourney(self, prompt):
|
57 |
try:
|
58 |
-
client = Client("mukaist/Midjourney", hf_token=
|
59 |
result = client.predict(
|
60 |
prompt=prompt,
|
61 |
negative_prompt="(deformed iris, deformed pupils, semi-realistic, cgi, 3d, render, sketch, cartoon, drawing, anime:1.4), text, close up, cropped, out of frame, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck",
|
@@ -85,9 +48,11 @@ class ModelGenerator:
|
|
85 |
except Exception as e:
|
86 |
return ("Midjourney", f"Error: {str(e)}")
|
87 |
|
88 |
-
|
|
|
|
|
89 |
try:
|
90 |
-
client = Client("multimodalart/stable-cascade", hf_token=
|
91 |
result = client.predict(
|
92 |
prompt=prompt,
|
93 |
negative_prompt=prompt,
|
@@ -105,9 +70,10 @@ class ModelGenerator:
|
|
105 |
except Exception as e:
|
106 |
return ("Stable Cascade", f"Error: {str(e)}")
|
107 |
|
108 |
-
|
|
|
109 |
try:
|
110 |
-
client = Client("stabilityai/stable-diffusion-3-medium", hf_token=
|
111 |
result = client.predict(
|
112 |
prompt=prompt,
|
113 |
negative_prompt=prompt,
|
@@ -123,9 +89,10 @@ class ModelGenerator:
|
|
123 |
except Exception as e:
|
124 |
return ("SD 3 Medium", f"Error: {str(e)}")
|
125 |
|
126 |
-
|
|
|
127 |
try:
|
128 |
-
client = Client("stabilityai/stable-diffusion-3.5-large", hf_token=
|
129 |
result = client.predict(
|
130 |
prompt=prompt,
|
131 |
negative_prompt=prompt,
|
@@ -141,9 +108,11 @@ class ModelGenerator:
|
|
141 |
except Exception as e:
|
142 |
return ("SD 3.5 Large", f"Error: {str(e)}")
|
143 |
|
144 |
-
|
|
|
145 |
try:
|
146 |
-
client = Client("https://playgroundai-playground-v2-5.hf.space/--replicas/ji5gy/",
|
|
|
147 |
result = client.predict(
|
148 |
prompt,
|
149 |
prompt, # negative prompt
|
@@ -161,17 +130,16 @@ class ModelGenerator:
|
|
161 |
except Exception as e:
|
162 |
return ("Playground v2.5", f"Error: {str(e)}")
|
163 |
|
164 |
-
def generate_images(prompt, selected_models
|
165 |
results = []
|
166 |
with concurrent.futures.ThreadPoolExecutor() as executor:
|
167 |
futures = []
|
168 |
-
generator = ModelGenerator(token)
|
169 |
model_map = {
|
170 |
-
"Midjourney":
|
171 |
-
"Stable Cascade":
|
172 |
-
"SD 3 Medium":
|
173 |
-
"SD 3.5 Large":
|
174 |
-
"Playground v2.5":
|
175 |
}
|
176 |
|
177 |
for model in selected_models:
|
@@ -188,6 +156,8 @@ def handle_prompt_click(prompt_text, key):
|
|
188 |
st.error("Please login with your HuggingFace account first!")
|
189 |
return
|
190 |
|
|
|
|
|
191 |
selected_models = st.session_state.get('selected_models', [])
|
192 |
|
193 |
if not selected_models:
|
@@ -195,22 +165,42 @@ def handle_prompt_click(prompt_text, key):
|
|
195 |
return
|
196 |
|
197 |
with st.spinner('Generating artwork...'):
|
198 |
-
results = generate_images(prompt_text, selected_models
|
199 |
st.session_state[f'generated_images_{key}'] = results
|
200 |
st.success("Artwork generated successfully!")
|
201 |
|
202 |
def main():
|
203 |
st.title("π¨ Multi-Model Art Generator")
|
204 |
-
|
205 |
-
# Initialize session state
|
206 |
-
init_session_state()
|
207 |
-
|
208 |
-
# Handle authentication
|
209 |
-
authenticate_user()
|
210 |
|
211 |
-
#
|
212 |
-
|
213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
214 |
st.header("Model Selection")
|
215 |
st.session_state['selected_models'] = st.multiselect(
|
216 |
"Choose AI Models",
|
@@ -233,6 +223,8 @@ def main():
|
|
233 |
- **Playground v2.5**: Advanced model with high customization
|
234 |
""")
|
235 |
|
|
|
|
|
236 |
st.markdown("### Select a prompt style to generate artwork:")
|
237 |
|
238 |
prompt_emojis = {
|
|
|
7 |
import io
|
8 |
import requests
|
9 |
from huggingface_hub import HfApi, login
|
|
|
|
|
10 |
|
11 |
+
# Initialize session state for authentication
|
12 |
+
if 'hf_token' not in st.session_state:
|
13 |
+
st.session_state.hf_token = None
|
14 |
+
if 'is_authenticated' not in st.session_state:
|
15 |
+
st.session_state.is_authenticated = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
class ModelGenerator:
|
18 |
+
@staticmethod
|
19 |
+
def generate_midjourney(prompt):
|
|
|
|
|
20 |
try:
|
21 |
+
client = Client("mukaist/Midjourney", hf_token=st.session_state.hf_token)
|
22 |
result = client.predict(
|
23 |
prompt=prompt,
|
24 |
negative_prompt="(deformed iris, deformed pupils, semi-realistic, cgi, 3d, render, sketch, cartoon, drawing, anime:1.4), text, close up, cropped, out of frame, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck",
|
|
|
48 |
except Exception as e:
|
49 |
return ("Midjourney", f"Error: {str(e)}")
|
50 |
|
51 |
+
# Other model generators remain exactly the same, just changing HF_TOKEN to st.session_state.hf_token
|
52 |
+
@staticmethod
|
53 |
+
def generate_stable_cascade(prompt):
|
54 |
try:
|
55 |
+
client = Client("multimodalart/stable-cascade", hf_token=st.session_state.hf_token)
|
56 |
result = client.predict(
|
57 |
prompt=prompt,
|
58 |
negative_prompt=prompt,
|
|
|
70 |
except Exception as e:
|
71 |
return ("Stable Cascade", f"Error: {str(e)}")
|
72 |
|
73 |
+
@staticmethod
|
74 |
+
def generate_stable_diffusion_3(prompt):
|
75 |
try:
|
76 |
+
client = Client("stabilityai/stable-diffusion-3-medium", hf_token=st.session_state.hf_token)
|
77 |
result = client.predict(
|
78 |
prompt=prompt,
|
79 |
negative_prompt=prompt,
|
|
|
89 |
except Exception as e:
|
90 |
return ("SD 3 Medium", f"Error: {str(e)}")
|
91 |
|
92 |
+
@staticmethod
|
93 |
+
def generate_stable_diffusion_35(prompt):
|
94 |
try:
|
95 |
+
client = Client("stabilityai/stable-diffusion-3.5-large", hf_token=st.session_state.hf_token)
|
96 |
result = client.predict(
|
97 |
prompt=prompt,
|
98 |
negative_prompt=prompt,
|
|
|
108 |
except Exception as e:
|
109 |
return ("SD 3.5 Large", f"Error: {str(e)}")
|
110 |
|
111 |
+
@staticmethod
|
112 |
+
def generate_playground_v2_5(prompt):
|
113 |
try:
|
114 |
+
client = Client("https://playgroundai-playground-v2-5.hf.space/--replicas/ji5gy/",
|
115 |
+
hf_token=st.session_state.hf_token)
|
116 |
result = client.predict(
|
117 |
prompt,
|
118 |
prompt, # negative prompt
|
|
|
130 |
except Exception as e:
|
131 |
return ("Playground v2.5", f"Error: {str(e)}")
|
132 |
|
133 |
+
def generate_images(prompt, selected_models):
|
134 |
results = []
|
135 |
with concurrent.futures.ThreadPoolExecutor() as executor:
|
136 |
futures = []
|
|
|
137 |
model_map = {
|
138 |
+
"Midjourney": ModelGenerator.generate_midjourney,
|
139 |
+
"Stable Cascade": ModelGenerator.generate_stable_cascade,
|
140 |
+
"SD 3 Medium": ModelGenerator.generate_stable_diffusion_3,
|
141 |
+
"SD 3.5 Large": ModelGenerator.generate_stable_diffusion_35,
|
142 |
+
"Playground v2.5": ModelGenerator.generate_playground_v2_5
|
143 |
}
|
144 |
|
145 |
for model in selected_models:
|
|
|
156 |
st.error("Please login with your HuggingFace account first!")
|
157 |
return
|
158 |
|
159 |
+
st.session_state[f'selected_prompt_{key}'] = prompt_text
|
160 |
+
|
161 |
selected_models = st.session_state.get('selected_models', [])
|
162 |
|
163 |
if not selected_models:
|
|
|
165 |
return
|
166 |
|
167 |
with st.spinner('Generating artwork...'):
|
168 |
+
results = generate_images(prompt_text, selected_models)
|
169 |
st.session_state[f'generated_images_{key}'] = results
|
170 |
st.success("Artwork generated successfully!")
|
171 |
|
172 |
def main():
|
173 |
st.title("π¨ Multi-Model Art Generator")
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
|
175 |
+
# Handle authentication in sidebar
|
176 |
+
with st.sidebar:
|
177 |
+
st.header("π Authentication")
|
178 |
+
if st.session_state.is_authenticated:
|
179 |
+
st.success("β Logged in to HuggingFace")
|
180 |
+
if st.button("Logout"):
|
181 |
+
st.session_state.hf_token = None
|
182 |
+
st.session_state.is_authenticated = False
|
183 |
+
st.rerun()
|
184 |
+
else:
|
185 |
+
token = st.text_input("Enter HuggingFace Token", type="password",
|
186 |
+
help="Get your token from https://huggingface.co/settings/tokens")
|
187 |
+
if st.button("Login"):
|
188 |
+
if token:
|
189 |
+
try:
|
190 |
+
# Verify token is valid
|
191 |
+
api = HfApi(token=token)
|
192 |
+
api.whoami()
|
193 |
+
st.session_state.hf_token = token
|
194 |
+
st.session_state.is_authenticated = True
|
195 |
+
st.success("Successfully logged in!")
|
196 |
+
st.rerun()
|
197 |
+
except Exception as e:
|
198 |
+
st.error(f"Authentication failed: {str(e)}")
|
199 |
+
else:
|
200 |
+
st.error("Please enter your HuggingFace token")
|
201 |
+
|
202 |
+
if st.session_state.is_authenticated:
|
203 |
+
st.markdown("---")
|
204 |
st.header("Model Selection")
|
205 |
st.session_state['selected_models'] = st.multiselect(
|
206 |
"Choose AI Models",
|
|
|
223 |
- **Playground v2.5**: Advanced model with high customization
|
224 |
""")
|
225 |
|
226 |
+
# Only show the main interface if authenticated
|
227 |
+
if st.session_state.is_authenticated:
|
228 |
st.markdown("### Select a prompt style to generate artwork:")
|
229 |
|
230 |
prompt_emojis = {
|