Update app.py
Browse files
app.py
CHANGED
@@ -1,132 +1,93 @@
|
|
1 |
import streamlit as st
|
2 |
-
import
|
3 |
-
import
|
4 |
-
from io import BytesIO
|
5 |
-
from PIL import Image
|
6 |
-
import time
|
7 |
|
8 |
# Streamlit app title and description
|
9 |
-
st.title("LoraTheExplorer
|
10 |
st.markdown("""
|
11 |
-
This Streamlit app
|
12 |
-
|
13 |
""")
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
|
18 |
-
#
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
# Function to
|
24 |
-
def
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
),
|
31 |
-
"Anime": (
|
32 |
-
"anime artwork {prompt}, vibrant, studio anime, highly detailed",
|
33 |
-
"photo, realism, blurry"
|
34 |
-
),
|
35 |
-
"Digital Art": (
|
36 |
-
"digital artwork {prompt}, illustrative, painterly, highly detailed",
|
37 |
-
"photo, low quality"
|
38 |
-
),
|
39 |
-
"Fantasy Art": (
|
40 |
-
"fantasy concept art of {prompt}, ethereal, epic, magical",
|
41 |
-
"realistic, blurry, text"
|
42 |
-
)
|
43 |
-
}
|
44 |
-
positive, neg = style_dict.get(style_name, style_dict["(No style)"])
|
45 |
-
return positive.replace("{prompt}", prompt), neg + (negative or "")
|
46 |
-
|
47 |
-
# Function to call the Gradio backend
|
48 |
-
def generate_images(prompt, negative_prompt, guidance_scale, style_name):
|
49 |
-
try:
|
50 |
-
# Apply style to prompt and negative prompt
|
51 |
-
styled_prompt, styled_negative = apply_style(style_name, prompt, negative_prompt)
|
52 |
-
|
53 |
-
# Prepare payload (based on typical Gradio API structure)
|
54 |
-
payload = {
|
55 |
-
"data": [
|
56 |
-
styled_prompt,
|
57 |
-
styled_negative,
|
58 |
-
guidance_scale,
|
59 |
-
style_name
|
60 |
-
]
|
61 |
-
}
|
62 |
-
|
63 |
-
# Send request to Gradio backend
|
64 |
-
start_time = time.time()
|
65 |
-
response = requests.post(BACKEND_URL, json=payload, timeout=60)
|
66 |
-
elapsed_time = time.time() - start_time
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
|
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
img = Image.open(BytesIO(img_bytes))
|
87 |
-
images.append(img)
|
88 |
-
elif img_data.startswith("http"):
|
89 |
-
response = requests.get(img_data, timeout=5)
|
90 |
-
img = Image.open(BytesIO(response.content))
|
91 |
-
images.append(img)
|
92 |
-
else:
|
93 |
-
st.warning(f"Unexpected image data format: {img_data}")
|
94 |
-
else:
|
95 |
-
st.warning("Unexpected image data type.")
|
96 |
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
110 |
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
|
119 |
-
#
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
# Display images in a gallery
|
128 |
-
st.subheader("Generated Images")
|
129 |
-
cols = st.columns(min(len(images), 3))
|
130 |
-
for idx, img in enumerate(images):
|
131 |
-
with cols[idx % 3]:
|
132 |
-
st.image(img, use_column_width=True)
|
|
|
1 |
import streamlit as st
|
2 |
+
import streamlit.components.v1 as components
|
3 |
+
import urllib.parse
|
|
|
|
|
|
|
4 |
|
5 |
# Streamlit app title and description
|
6 |
+
st.title("LoraTheExplorer Gradio App Wrapper")
|
7 |
st.markdown("""
|
8 |
+
This Streamlit app embeds the MultimodalArt LoraTheExplorer Gradio app in an iframe.
|
9 |
+
Use the controls below to interact with the app or prefill prompts (if supported).
|
10 |
""")
|
11 |
|
12 |
+
# Gradio app URL
|
13 |
+
GRADIO_URL = "https://multimodalart-loratheexplorer.hf.space"
|
14 |
|
15 |
+
# Function to generate iframe HTML
|
16 |
+
def render_gradio_iframe(url, width=850, height=600):
|
17 |
+
iframe_html = f"""
|
18 |
+
<iframe
|
19 |
+
src="{url}"
|
20 |
+
frameborder="0"
|
21 |
+
width="{width}"
|
22 |
+
height="{height}"
|
23 |
+
style="border: none;"
|
24 |
+
></iframe>
|
25 |
+
"""
|
26 |
+
return iframe_html
|
27 |
|
28 |
+
# Function to attempt to prefill prompt via URL parameters (if Gradio app supports it)
|
29 |
+
def generate_url_with_prompt(base_url, prompt):
|
30 |
+
# URL-encode the prompt
|
31 |
+
encoded_prompt = urllib.parse.quote(prompt)
|
32 |
+
# Append as a query parameter (assumption: Gradio app might accept ?prompt=...)
|
33 |
+
# This is speculative; adjust based on actual app behavior
|
34 |
+
return f"{base_url}?prompt={encoded_prompt}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
# Sidebar for controls
|
37 |
+
st.sidebar.header("Controls")
|
38 |
+
prompt = st.sidebar.text_input(
|
39 |
+
"Enter a prompt",
|
40 |
+
placeholder="A magical forest with glowing creatures",
|
41 |
+
help="Enter a prompt to prefill in the Gradio app (if supported)."
|
42 |
+
)
|
43 |
+
apply_prompt = st.sidebar.button("Apply Prompt")
|
44 |
|
45 |
+
# Handle prompt application
|
46 |
+
if apply_prompt and prompt:
|
47 |
+
# Attempt to modify URL with prompt
|
48 |
+
modified_url = generate_url_with_prompt(GRADIO_URL, prompt)
|
49 |
+
st.sidebar.info(f"Attempting to load with prompt: {prompt}")
|
50 |
+
else:
|
51 |
+
modified_url = GRADIO_URL
|
52 |
|
53 |
+
# Embed Gradio app
|
54 |
+
st.subheader("Gradio App")
|
55 |
+
components.html(
|
56 |
+
render_gradio_iframe(modified_url),
|
57 |
+
height=600,
|
58 |
+
scrolling=True
|
59 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
# JavaScript injection to set prompt (alternative approach, may be blocked by CORS)
|
62 |
+
st.markdown("""
|
63 |
+
<script>
|
64 |
+
function setPrompt(prompt) {
|
65 |
+
try {
|
66 |
+
const iframe = document.querySelector('iframe');
|
67 |
+
iframe.contentWindow.postMessage({
|
68 |
+
type: 'set_prompt',
|
69 |
+
prompt: prompt
|
70 |
+
}, '*');
|
71 |
+
} catch (e) {
|
72 |
+
console.error('Error sending prompt:', e);
|
73 |
+
}
|
74 |
+
}
|
75 |
+
</script>
|
76 |
+
""", unsafe_allow_html=True)
|
77 |
|
78 |
+
if prompt and apply_prompt:
|
79 |
+
# Attempt to send prompt via JavaScript (requires Gradio app to listen for postMessage)
|
80 |
+
st.markdown(f"""
|
81 |
+
<script>
|
82 |
+
setPrompt("{prompt}");
|
83 |
+
</script>
|
84 |
+
""", unsafe_allow_html=True)
|
85 |
|
86 |
+
# Notes and limitations
|
87 |
+
st.markdown("""
|
88 |
+
**Notes:**
|
89 |
+
- The Gradio app is embedded in an iframe, preserving its original interface.
|
90 |
+
- Prefilling prompts via URL parameters or JavaScript may not work if the Gradio app doesn't support these methods.
|
91 |
+
- If the app doesn't respond to the prompt, interact directly with the iframe below.
|
92 |
+
- To fully control the app, the Gradio app's API or source code would need to be inspected for supported endpoints.
|
93 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|