| import gradio as gr |
| import os |
| import sys |
| import random |
| import string |
| import time |
| from queue import Queue |
| from threading import Thread |
|
|
| proc1 = gr.Interface.load("models/playgroundai/playground-v2-1024px-aesthetic") |
|
|
| def restart_script_periodically(): |
| while True: |
| random_time = random.randint(540, 600) |
| time.sleep(random_time) |
| os.execl(sys.executable, sys.executable, *sys.argv) |
|
|
| restart_thread = Thread(target=restart_script_periodically, daemon=True) |
| restart_thread.start() |
|
|
| queue = Queue() |
| queue_threshold = 100 |
|
|
|
|
| import uuid |
|
|
| |
|
|
| |
|
|
| request_counter = 0 |
|
|
| |
| def send_it1(inputs, proc=proc1): |
| global request_counter |
| |
|
|
| request_counter += 1 |
| timestamp = f"{time.time()}_{request_counter}" |
| prompt_with_noise = (inputs) + f" - {timestamp}" |
| |
| try: |
| while queue.qsize() >= queue_threshold: |
| time.sleep(2) |
| queue.put(prompt_with_noise) |
| output = proc(prompt_with_noise) |
| return output |
| except Exception as e: |
| |
| raise gr.Error("Experiencing high demand. Please retry shortly. Thank you for your patience.") |
|
|
|
|
|
|
|
|
|
|
|
|
| with gr.Blocks(css=".gradio-container {background-color: #FDF5E6;} .dark .gradio-container {background-color: black;} footer{display:none !important;}",) as demo: |
| with gr.Column(elem_id="col-container"): |
| with gr.Row(variant="compact"): |
| input_text = gr.Textbox( |
| lines=8, |
| label="Enter your prompt", |
| show_label=False, |
| max_lines=10, |
| placeholder="Enter your text prompt here", |
| ).style( |
| container=False, |
| textarea={'height': '400px'} |
| ) |
| run = gr.Button("Generate Image").style(full_width=False) |
|
|
| with gr.Row(): |
| with gr.Row(): |
| output1 = gr.Image(label="", show_label=False, show_share_button=False) |
| |
| run.click(send_it1, inputs=[input_text], outputs=[output1]) |
|
|
| demo.launch(enable_queue=True, inline=True) |
|
|