Opd / app.py
Ashrafb's picture
Update app.py
e39414c
Raw History Blame Contribute Delete
2.25 kB
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 # Import the UUID library
# Existing code...
# Existing code...
request_counter = 0 # Global counter to track requests
# Modify your send_it1 function to handle any errors generically
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:
# Display a generic error message to the user
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)