Spaces:
Sleeping
Sleeping
import gradio as gr | |
import requests | |
import time | |
custom_css = """ | |
#generate_button { | |
background: linear-gradient(to bottom, #ffffff 0%, #e6e6e6 50%, #cccccc 100%); | |
border: 1px solid #b3b3b3; | |
border-radius: 12px; | |
color: #333333; | |
font-size: 16px; | |
font-weight: bold; | |
padding: 10px 20px; | |
text-align: center; | |
text-shadow: 0px 1px 1px #ffffff; | |
cursor: pointer; | |
} | |
#generate_button:hover { | |
background: linear-gradient(to bottom, #f2f2f2 0%, #d9d9d9 50%, #bfbfbf 100%); | |
} | |
""" | |
get_window_url_params = """ | |
function(url_params) { | |
console.log(url_params); | |
const params = new URLSearchParams(window.location.search); | |
url_params = Object.fromEntries(params); | |
return [url_params.id]; | |
} | |
""" | |
# Function to generate images | |
def generate_image(prompt, style, size, enhance_prompt, old_images, url_params): | |
tempStyle = "" if style is None else style | |
# API request payload with base64 disabled | |
#payload = { | |
# "key": url_params, | |
# "prompt": prompt if style is None or style == "No-Style" else "((" + tempStyle + " Style)) " + prompt + " as " + tempStyle + " Style, ", | |
#} | |
payload = { | |
"key": url_params, | |
"model_id": "flux", | |
"prompt": prompt if style is None or style == "No-Style" else "((" + tempStyle + " Style)) " + prompt + " as " + tempStyle + " Style, ", | |
"negative_prompt": "", # you can modify this as per your needs | |
"width": size.split('X')[0], | |
"height": size.split('X')[1], | |
"samples": "2", | |
"num_inference_steps": "3", | |
"safety_checker": "no", | |
"enhance_prompt": "no", | |
"seed": None, | |
"guidance_scale": 7.5, | |
"panorama": "no", | |
"self_attention": "no", | |
"upscale": "no", | |
"embeddings_model": None, | |
"lora_model": None, | |
"tomesd": "yes", | |
"clip_skip": "2", | |
"use_karras_sigmas": "yes", | |
"vae": None, | |
"lora_strength": None, | |
"scheduler": "UniPCMultistepScheduler", | |
"webhook": None, | |
"track_id": None, | |
"watermark": "no" | |
} | |
# Send the POST request to your API | |
#response = requests.post("https://developersoft.in/api/v3-img", json=payload) | |
response = requests.post("https://developersoft.in/api/v1-generate-image", json=payload) | |
if response.status_code == 200: | |
data = response.json() | |
image_urls = data["future_links"] | |
# Polling to check if images are available | |
for url in image_urls: | |
for _ in range(20): | |
image_response = requests.get(url) | |
if image_response.status_code == 200: | |
break | |
time.sleep(1) | |
old_images.extend(url for url in image_urls if url not in old_images) | |
return image_urls, old_images | |
else: | |
return ["Error generating image"], old_images | |
def toggle_advanced(advanced): | |
return gr.update(visible=advanced) | |
def predict(url_params): | |
return url_params | |
# Create the Gradio interface | |
with gr.Blocks(css=custom_css) as demo: | |
url_params = gr.Text(visible=False, label="URL Params") | |
prompt = gr.Textbox(label="Prompt") | |
advanced_option = gr.Checkbox(label="Advanced Options", value=False) | |
with gr.Group(visible=False, elem_id="advance") as advanced_group: | |
style = gr.Dropdown( | |
["No-Style", "Realism", "Anime", "3D Cartoon", "Vivid", "Comic", "Cyberpunk", "Pixel Art", "Watercolor", "Abstract", "Impressionism", "Surrealism", "Photorealistic", "Minimalism", "Vintage", "Steampunk", "Fantasy"], | |
label="Style", | |
value="No-Style" | |
) | |
size = gr.Dropdown( | |
["800X800", "800X1200", "1080X1600", "1200X800", "1600X900", "1440X1080", "1280X800", "1280X720", "1500X500", "1584X800"], | |
label="Size", | |
value="800X800" | |
) | |
enhance_prompt = gr.Dropdown( | |
["no", "yes"], | |
label="Enhance Prompt", | |
value="yes" | |
) | |
advanced_option.change(fn=toggle_advanced, inputs=advanced_option, outputs=advanced_group) | |
old_images = gr.State([]) | |
generate_button = gr.Button("Generate", elem_id="generate_button") | |
generated_gallery = gr.Gallery(label="Generated Images", columns=4) | |
history_gallery = gr.Gallery(label="History Images", columns=8) | |
prompt.submit( | |
generate_image, | |
concurrency_limit=None, | |
inputs=[prompt, style, size, enhance_prompt, old_images, url_params], | |
outputs=[generated_gallery, old_images] | |
) | |
generate_button.click( | |
generate_image, | |
concurrency_limit=None, | |
inputs=[prompt, style, size, enhance_prompt, old_images, url_params], | |
outputs=[generated_gallery, old_images] | |
) | |
old_images.change( | |
fn=lambda old_images: old_images, | |
concurrency_limit=None, | |
inputs=old_images, | |
outputs=history_gallery, | |
show_progress=False | |
) | |
demo.load( | |
fn=predict, | |
inputs=url_params, | |
outputs=url_params, | |
js=get_window_url_params | |
) | |
demo.launch() | |