Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,210 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import random
|
3 |
+
import time
|
4 |
+
|
5 |
+
# Define dummy loras list for the gallery
|
6 |
+
loras = [
|
7 |
+
{"repo": "stabilityai/stable-diffusion-xl-base-1.0", "image": "https://huggingface.co/spaces/reach-vb/Blazingly-fast-LoRA/resolve/main/flux_lora.png", "title": "SDXL Base 1.0"},
|
8 |
+
{"repo": "stabilityai/sdxl-turbo", "image": "https://huggingface.co/stabilityai/sdxl-turbo/resolve/main/banner.png", "title": "SDXL Turbo"},
|
9 |
+
{"repo": "runwayml/stable-diffusion-v1-5", "image": "https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/sd-v1-5.png", "title": "SD 1.5"},
|
10 |
+
{"repo": "SG161222/Realistic_Vision_V5.1_noVAE", "image": "https://huggingface.co/SG161222/Realistic_Vision_V5.1_noVAE/resolve/main/realistic_vision_v5.1.png", "title": "Realistic Vision V5.1"},
|
11 |
+
{"repo": "gsdf/Counterfeit-V3.0", "image": "https://huggingface.co/gsdf/Counterfeit-V3.0/resolve/main/cf3.jpg", "title": "Counterfeit V3.0"},
|
12 |
+
{"repo": "digiplay/AbsoluteReality_v1.8.1", "image": "https://huggingface.co/digiplay/AbsoluteReality_v1.8.1/resolve/main/ar.jpg", "title": "Absolute Reality v1.8.1"}
|
13 |
+
]
|
14 |
+
|
15 |
+
# Custom CSS
|
16 |
+
css = """
|
17 |
+
#title { text-align: center; margin-bottom: 10px; }
|
18 |
+
#gallery {min-height: 450px; max-height: 650px; overflow-y: auto;}
|
19 |
+
#gen_column {display: flex; align-items: flex-end; margin-bottom: 0.5rem;}
|
20 |
+
#gen_btn {margin-bottom: 0.5rem; max-width: 100%;}
|
21 |
+
#lora_list {font-size: 0.8em; margin-top: 0.5rem;}
|
22 |
+
#progress {text-align: center; margin-top: 0.8rem;}
|
23 |
+
"""
|
24 |
+
|
25 |
+
font = "Montserrat"
|
26 |
+
|
27 |
+
# Dummy function to update selection
|
28 |
+
def update_selection(evt: gr.SelectData):
|
29 |
+
"""Update the UI when a model is selected from the gallery"""
|
30 |
+
selected_index = evt.index
|
31 |
+
selected_lora = loras[selected_index]
|
32 |
+
# Get the model name to display
|
33 |
+
model_title = selected_lora.get("title", selected_lora["repo"].split('/')[-1])
|
34 |
+
|
35 |
+
# Create an informative text about the selected model
|
36 |
+
info_text = f"Selected model: **{model_title}**\n\nModel ID: `{selected_lora['repo']}`"
|
37 |
+
|
38 |
+
# Return with appropriate placeholder text for the prompt
|
39 |
+
return (
|
40 |
+
gr.update(placeholder=f"Enter your prompt for {model_title}..."),
|
41 |
+
info_text,
|
42 |
+
selected_index,
|
43 |
+
1024, # Default width
|
44 |
+
1024 # Default height
|
45 |
+
)
|
46 |
+
|
47 |
+
# Dummy function to add a custom model
|
48 |
+
def add_custom_lora(lora_id):
|
49 |
+
"""Add a custom model from a Hugging Face ID or URL"""
|
50 |
+
if not lora_id or lora_id.strip() == "":
|
51 |
+
return gr.update(), gr.update(visible=False), gr.update(), gr.update(), None, gr.update()
|
52 |
+
|
53 |
+
# Create a dummy entry for the custom model
|
54 |
+
custom_entry = {
|
55 |
+
"repo": lora_id,
|
56 |
+
"image": "https://huggingface.co/spaces/reach-vb/Blazingly-fast-LoRA/resolve/main/flux_lora.png", # Placeholder image
|
57 |
+
"title": f"Custom: {lora_id.split('/')[-1]}"
|
58 |
+
}
|
59 |
+
|
60 |
+
# Add the custom entry to the gallery list
|
61 |
+
updated_loras = loras.copy()
|
62 |
+
updated_loras.append(custom_entry)
|
63 |
+
|
64 |
+
# Create info HTML for the custom model
|
65 |
+
info_html = f"""
|
66 |
+
<div style="padding: 10px; border: 1px solid #ddd; border-radius: 5px; margin-top: 10px;">
|
67 |
+
<p><b>Custom model added:</b> {lora_id}</p>
|
68 |
+
</div>
|
69 |
+
"""
|
70 |
+
|
71 |
+
# Create info text for selection
|
72 |
+
info_text = f"Using custom model: **{lora_id}**"
|
73 |
+
|
74 |
+
# Return with updates
|
75 |
+
return (
|
76 |
+
gr.update(value=info_html, visible=True),
|
77 |
+
gr.update(visible=True),
|
78 |
+
gr.update(value=[(item.get("image"), item.get("title", item["repo"].split('/')[-1])) for item in updated_loras]),
|
79 |
+
info_text,
|
80 |
+
len(updated_loras) - 1, # Index of the newly added model
|
81 |
+
gr.update(placeholder=f"Enter your prompt for custom model {lora_id.split('/')[-1]}...")
|
82 |
+
)
|
83 |
+
|
84 |
+
# Dummy function to remove custom model info
|
85 |
+
def remove_custom_lora():
|
86 |
+
"""Remove custom model information and reset UI"""
|
87 |
+
return (
|
88 |
+
gr.update(visible=False),
|
89 |
+
gr.update(visible=False),
|
90 |
+
gr.update(value=[(item.get("image"), item.get("title", item["repo"].split('/')[-1])) for item in loras]),
|
91 |
+
"Select a base model or add a custom one below.",
|
92 |
+
None,
|
93 |
+
gr.update(value="")
|
94 |
+
)
|
95 |
+
|
96 |
+
# Dummy function to generate images
|
97 |
+
def run_lora(prompt, selected_index, seed, width, height):
|
98 |
+
"""Simulate image generation with the selected model"""
|
99 |
+
if selected_index is None:
|
100 |
+
return gr.update(value=None), seed, gr.update(value="Please select a model first.", visible=True)
|
101 |
+
|
102 |
+
if not prompt or prompt.strip() == "":
|
103 |
+
return gr.update(value=None), seed, gr.update(value="Please enter a prompt.", visible=True)
|
104 |
+
|
105 |
+
# Show progress bar
|
106 |
+
progress = gr.update(value="Generating your image...", visible=True)
|
107 |
+
|
108 |
+
# Simulate generation delay
|
109 |
+
time.sleep(2)
|
110 |
+
|
111 |
+
# If seed is 0, randomize it
|
112 |
+
if seed == 0:
|
113 |
+
seed = random.randint(1, 2147483647)
|
114 |
+
|
115 |
+
# Get the selected model info
|
116 |
+
model_info = loras[selected_index] if selected_index < len(loras) else {"repo": "custom_model", "title": "Custom Model"}
|
117 |
+
|
118 |
+
# For dummy purposes, we'll just return the model's image or a placeholder
|
119 |
+
# In a real implementation, you would call an API to generate the image
|
120 |
+
result_image = model_info.get("image", "https://huggingface.co/spaces/reach-vb/Blazingly-fast-LoRA/resolve/main/flux_lora.png")
|
121 |
+
|
122 |
+
# Hide progress bar
|
123 |
+
progress = gr.update(visible=False)
|
124 |
+
|
125 |
+
return result_image, seed, progress
|
126 |
+
|
127 |
+
# Now you can run the app with this code:
|
128 |
+
if __name__ == "__main__":
|
129 |
+
with gr.Blocks(theme=gr.themes.Soft(font=font), css=css) as app:
|
130 |
+
title = gr.HTML(
|
131 |
+
"""<h1><a href="https://huggingface.co/docs/inference-providers/en/index">Blazingly Fast LoRA by Fal & HF (this is a dummy app)</a> 🤗</h1>""",
|
132 |
+
elem_id="title",
|
133 |
+
)
|
134 |
+
title = gr.HTML(
|
135 |
+
"""<h3>This is just a dummy app</h3>""",
|
136 |
+
elem_id="subtitle",
|
137 |
+
)
|
138 |
+
# --- States for parameters previously in Advanced Settings ---
|
139 |
+
selected_index = gr.State(None)
|
140 |
+
width = gr.State(1024) # Default width
|
141 |
+
height = gr.State(1024) # Default height
|
142 |
+
seed = gr.State(0) # Default seed (will be randomized by run_lora)
|
143 |
+
|
144 |
+
with gr.Row():
|
145 |
+
with gr.Column(scale=3):
|
146 |
+
prompt = gr.Textbox(label="Prompt", lines=1, placeholder="Type a prompt after selecting a LoRA/Model")
|
147 |
+
with gr.Column(scale=1, elem_id="gen_column"):
|
148 |
+
generate_button = gr.Button("Generate", variant="primary", elem_id="gen_btn")
|
149 |
+
with gr.Row():
|
150 |
+
with gr.Column():
|
151 |
+
selected_info = gr.Markdown("Select a base model or add a custom one below.") # Updated initial text
|
152 |
+
gallery = gr.Gallery(
|
153 |
+
# Ensure items have 'image' and 'title' keys, provide fallbacks if needed
|
154 |
+
[(item.get("image"), item.get("title", item["repo"].split('/')[-1])) for item in loras],
|
155 |
+
label="Model Gallery", # Changed label
|
156 |
+
allow_preview=False,
|
157 |
+
columns=3,
|
158 |
+
elem_id="gallery",
|
159 |
+
show_share_button=False
|
160 |
+
)
|
161 |
+
with gr.Group():
|
162 |
+
custom_lora = gr.Textbox(label="Custom Model", info="Hugging Face model ID (e.g., user/model-name) or URL", placeholder="stabilityai/stable-diffusion-xl-base-1.0") # Updated label/placeholder
|
163 |
+
gr.Markdown("[Check Hugging Face Models](https://huggingface.co/models?inference_provider=fal-ai&pipeline_tag=text-to-image&sort=trending)", elem_id="lora_list") # Updated link/text
|
164 |
+
custom_lora_info = gr.HTML(visible=False)
|
165 |
+
custom_lora_button = gr.Button("Clear custom model info", visible=False) # Changed button text
|
166 |
+
with gr.Column():
|
167 |
+
# Keep progress bar element, but it will only be shown briefly if API is slow, then hidden by run_lora return
|
168 |
+
progress_bar = gr.Markdown(elem_id="progress", visible=False, value="Generating...")
|
169 |
+
result = gr.Image(label="Generated Image")
|
170 |
+
# Display the seed used for the generation
|
171 |
+
used_seed_display = gr.Textbox(label="Seed Used", value=0, interactive=False) # Display seed used
|
172 |
+
|
173 |
+
gallery.select(
|
174 |
+
update_selection,
|
175 |
+
inputs=[], # No direct inputs needed, uses evt
|
176 |
+
# Update prompt placeholder, selection text, selected index state, and width/height states
|
177 |
+
outputs=[prompt, selected_info, selected_index, width, height],
|
178 |
+
api_name=False,
|
179 |
+
)
|
180 |
+
# Use submit event for Textbox to trigger add_custom_lora
|
181 |
+
custom_lora.submit(
|
182 |
+
add_custom_lora,
|
183 |
+
inputs=[custom_lora],
|
184 |
+
# Outputs: info card, remove button, gallery, selection text, selected index state, prompt placeholder
|
185 |
+
outputs=[custom_lora_info, custom_lora_button, gallery, selected_info, selected_index, prompt],
|
186 |
+
api_name=False,
|
187 |
+
)
|
188 |
+
custom_lora_button.click(
|
189 |
+
remove_custom_lora,
|
190 |
+
outputs=[custom_lora_info, custom_lora_button, gallery, selected_info, selected_index, custom_lora], # Clear textbox too
|
191 |
+
api_name=False,
|
192 |
+
)
|
193 |
+
gr.on(
|
194 |
+
triggers=[generate_button.click, prompt.submit],
|
195 |
+
fn=run_lora,
|
196 |
+
# Inputs now use state variables for width, height, seed
|
197 |
+
inputs=[prompt, selected_index, seed, width, height],
|
198 |
+
# Outputs: result image, seed state (updated with used seed), progress bar update
|
199 |
+
outputs=[result, seed, progress_bar],
|
200 |
+
api_name=False,
|
201 |
+
).then(
|
202 |
+
# Update the displayed seed value after run_lora completes
|
203 |
+
lambda s: gr.update(value=s),
|
204 |
+
inputs=[seed],
|
205 |
+
outputs=[used_seed_display],
|
206 |
+
api_name=False,
|
207 |
+
)
|
208 |
+
|
209 |
+
app.queue()
|
210 |
+
app.launch(debug=True, show_api=False)
|