Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,17 +8,20 @@ import io
|
|
8 |
import requests
|
9 |
from huggingface_hub import HfApi, login
|
10 |
|
11 |
-
# Initialize session state
|
12 |
if 'hf_token' not in st.session_state:
|
13 |
-
st.session_state
|
14 |
if 'is_authenticated' not in st.session_state:
|
15 |
-
st.session_state
|
16 |
|
17 |
class ModelGenerator:
|
18 |
@staticmethod
|
19 |
def generate_midjourney(prompt):
|
|
|
|
|
|
|
20 |
try:
|
21 |
-
client = Client("mukaist/Midjourney", hf_token=st.session_state
|
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,11 +51,13 @@ class ModelGenerator:
|
|
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
|
56 |
result = client.predict(
|
57 |
prompt=prompt,
|
58 |
negative_prompt=prompt,
|
@@ -72,8 +77,11 @@ class ModelGenerator:
|
|
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
|
77 |
result = client.predict(
|
78 |
prompt=prompt,
|
79 |
negative_prompt=prompt,
|
@@ -91,8 +99,11 @@ class ModelGenerator:
|
|
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
|
96 |
result = client.predict(
|
97 |
prompt=prompt,
|
98 |
negative_prompt=prompt,
|
@@ -110,9 +121,12 @@ class ModelGenerator:
|
|
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
|
116 |
result = client.predict(
|
117 |
prompt,
|
118 |
prompt, # negative prompt
|
@@ -152,7 +166,7 @@ def generate_images(prompt, selected_models):
|
|
152 |
return results
|
153 |
|
154 |
def handle_prompt_click(prompt_text, key):
|
155 |
-
if not st.session_state.is_authenticated:
|
156 |
st.error("Please login with your HuggingFace account first!")
|
157 |
return
|
158 |
|
@@ -175,11 +189,11 @@ def main():
|
|
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
|
182 |
-
st.session_state
|
183 |
st.rerun()
|
184 |
else:
|
185 |
token = st.text_input("Enter HuggingFace Token", type="password",
|
@@ -190,8 +204,9 @@ def main():
|
|
190 |
# Verify token is valid
|
191 |
api = HfApi(token=token)
|
192 |
api.whoami()
|
193 |
-
|
194 |
-
st.session_state
|
|
|
195 |
st.success("Successfully logged in!")
|
196 |
st.rerun()
|
197 |
except Exception as e:
|
@@ -199,7 +214,7 @@ def main():
|
|
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(
|
@@ -224,7 +239,7 @@ def main():
|
|
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 = {
|
|
|
8 |
import requests
|
9 |
from huggingface_hub import HfApi, login
|
10 |
|
11 |
+
# Initialize session state - must be first
|
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 |
+
if not st.session_state.get('hf_token'):
|
21 |
+
return ("Midjourney", "Error: No authentication token found")
|
22 |
+
|
23 |
try:
|
24 |
+
client = Client("mukaist/Midjourney", hf_token=st.session_state['hf_token'])
|
25 |
result = client.predict(
|
26 |
prompt=prompt,
|
27 |
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",
|
|
|
51 |
except Exception as e:
|
52 |
return ("Midjourney", f"Error: {str(e)}")
|
53 |
|
|
|
54 |
@staticmethod
|
55 |
def generate_stable_cascade(prompt):
|
56 |
+
if not st.session_state.get('hf_token'):
|
57 |
+
return ("Stable Cascade", "Error: No authentication token found")
|
58 |
+
|
59 |
try:
|
60 |
+
client = Client("multimodalart/stable-cascade", hf_token=st.session_state['hf_token'])
|
61 |
result = client.predict(
|
62 |
prompt=prompt,
|
63 |
negative_prompt=prompt,
|
|
|
77 |
|
78 |
@staticmethod
|
79 |
def generate_stable_diffusion_3(prompt):
|
80 |
+
if not st.session_state.get('hf_token'):
|
81 |
+
return ("SD 3 Medium", "Error: No authentication token found")
|
82 |
+
|
83 |
try:
|
84 |
+
client = Client("stabilityai/stable-diffusion-3-medium", hf_token=st.session_state['hf_token'])
|
85 |
result = client.predict(
|
86 |
prompt=prompt,
|
87 |
negative_prompt=prompt,
|
|
|
99 |
|
100 |
@staticmethod
|
101 |
def generate_stable_diffusion_35(prompt):
|
102 |
+
if not st.session_state.get('hf_token'):
|
103 |
+
return ("SD 3.5 Large", "Error: No authentication token found")
|
104 |
+
|
105 |
try:
|
106 |
+
client = Client("stabilityai/stable-diffusion-3.5-large", hf_token=st.session_state['hf_token'])
|
107 |
result = client.predict(
|
108 |
prompt=prompt,
|
109 |
negative_prompt=prompt,
|
|
|
121 |
|
122 |
@staticmethod
|
123 |
def generate_playground_v2_5(prompt):
|
124 |
+
if not st.session_state.get('hf_token'):
|
125 |
+
return ("Playground v2.5", "Error: No authentication token found")
|
126 |
+
|
127 |
try:
|
128 |
client = Client("https://playgroundai-playground-v2-5.hf.space/--replicas/ji5gy/",
|
129 |
+
hf_token=st.session_state['hf_token'])
|
130 |
result = client.predict(
|
131 |
prompt,
|
132 |
prompt, # negative prompt
|
|
|
166 |
return results
|
167 |
|
168 |
def handle_prompt_click(prompt_text, key):
|
169 |
+
if not st.session_state.get('is_authenticated') or not st.session_state.get('hf_token'):
|
170 |
st.error("Please login with your HuggingFace account first!")
|
171 |
return
|
172 |
|
|
|
189 |
# Handle authentication in sidebar
|
190 |
with st.sidebar:
|
191 |
st.header("π Authentication")
|
192 |
+
if st.session_state.get('is_authenticated') and st.session_state.get('hf_token'):
|
193 |
st.success("β Logged in to HuggingFace")
|
194 |
if st.button("Logout"):
|
195 |
+
st.session_state['hf_token'] = None
|
196 |
+
st.session_state['is_authenticated'] = False
|
197 |
st.rerun()
|
198 |
else:
|
199 |
token = st.text_input("Enter HuggingFace Token", type="password",
|
|
|
204 |
# Verify token is valid
|
205 |
api = HfApi(token=token)
|
206 |
api.whoami()
|
207 |
+
# Use dictionary style access to ensure proper setting
|
208 |
+
st.session_state['hf_token'] = token
|
209 |
+
st.session_state['is_authenticated'] = True
|
210 |
st.success("Successfully logged in!")
|
211 |
st.rerun()
|
212 |
except Exception as e:
|
|
|
214 |
else:
|
215 |
st.error("Please enter your HuggingFace token")
|
216 |
|
217 |
+
if st.session_state.get('is_authenticated') and st.session_state.get('hf_token'):
|
218 |
st.markdown("---")
|
219 |
st.header("Model Selection")
|
220 |
st.session_state['selected_models'] = st.multiselect(
|
|
|
239 |
""")
|
240 |
|
241 |
# Only show the main interface if authenticated
|
242 |
+
if st.session_state.get('is_authenticated') and st.session_state.get('hf_token'):
|
243 |
st.markdown("### Select a prompt style to generate artwork:")
|
244 |
|
245 |
prompt_emojis = {
|