Upload app (3).py
Browse files- app (3).py +122 -0
app (3).py
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import sys
|
4 |
+
import random
|
5 |
+
import string
|
6 |
+
import time
|
7 |
+
from queue import Queue
|
8 |
+
from threading import Thread
|
9 |
+
|
10 |
+
|
11 |
+
proc1 = gr.Interface.load("models/segmind/SSD-1B")
|
12 |
+
|
13 |
+
def restart_script_periodically():
|
14 |
+
while True:
|
15 |
+
random_time = random.randint(540, 600)
|
16 |
+
time.sleep(random_time)
|
17 |
+
os.execl(sys.executable, sys.executable, *sys.argv)
|
18 |
+
|
19 |
+
restart_thread = Thread(target=restart_script_periodically, daemon=True)
|
20 |
+
restart_thread.start()
|
21 |
+
|
22 |
+
queue = Queue()
|
23 |
+
queue_threshold = 100
|
24 |
+
|
25 |
+
def add_random_noise(prompt, noise_level=0.00):
|
26 |
+
if noise_level == 0:
|
27 |
+
noise_level = 0.00
|
28 |
+
percentage_noise = noise_level * 5
|
29 |
+
num_noise_chars = int(len(prompt) * (percentage_noise / 100))
|
30 |
+
noise_indices = random.sample(range(len(prompt)), num_noise_chars)
|
31 |
+
prompt_list = list(prompt)
|
32 |
+
noise_chars = list(string.ascii_letters + string.punctuation + ' ' + string.digits)
|
33 |
+
noise_chars.extend(['๐', '๐ฉ', '๐', '๐ค', '๐', '๐ค', '๐ญ', '๐', '๐ท', '๐คฏ', '๐คซ', '๐ฅด', '๐ด', '๐คฉ', '๐ฅณ', '๐', '๐ฉ', '๐คช', '๐', '๐คข', '๐', '๐น', '๐ป', '๐ค', '๐ฝ', '๐', '๐', '๐
', '๐', '๐', '๐', '๐', '๐', '๐', '๐ฎ', 'โค๏ธ', '๐', '๐', '๐', '๐', '๐ถ', '๐ฑ', '๐ญ', '๐น', '๐ฆ', '๐ป', '๐จ', '๐ฏ', '๐ฆ', '๐', '๐ฅ', '๐ง๏ธ', '๐', '๐', '๐ฅ', '๐ด', '๐', '๐บ', '๐ป', '๐ธ', '๐จ', '๐
', '๐', 'โ๏ธ', 'โ๏ธ', 'โ๏ธ', 'โ๏ธ', '๐ค๏ธ', 'โ
๏ธ', '๐ฅ๏ธ', '๐ฆ๏ธ', '๐ง๏ธ', '๐ฉ๏ธ', '๐จ๏ธ', '๐ซ๏ธ', 'โ๏ธ', '๐ฌ๏ธ', '๐จ', '๐ช๏ธ', '๐'])
|
34 |
+
for index in noise_indices:
|
35 |
+
prompt_list[index] = random.choice(noise_chars)
|
36 |
+
return "".join(prompt_list)
|
37 |
+
|
38 |
+
# Existing code...
|
39 |
+
|
40 |
+
import uuid # Import the UUID library
|
41 |
+
|
42 |
+
# Existing code...
|
43 |
+
|
44 |
+
# Existing code...
|
45 |
+
|
46 |
+
request_counter = 0 # Global counter to track requests
|
47 |
+
# Defining the image styles and default style
|
48 |
+
IMAGE_STYLES = [
|
49 |
+
"No style",
|
50 |
+
"Cinematic",
|
51 |
+
"Photographic",
|
52 |
+
"Anime",
|
53 |
+
"Manga",
|
54 |
+
"Digital Art",
|
55 |
+
"Pixel art",
|
56 |
+
"Fantasy art",
|
57 |
+
"Neonpunk",
|
58 |
+
"3D Model"
|
59 |
+
]
|
60 |
+
|
61 |
+
DEFAULT_IMAGE_STYLE = "Cinematic"
|
62 |
+
|
63 |
+
|
64 |
+
def send_it1(inputs, noise_level, style, proc=proc1):
|
65 |
+
global request_counter
|
66 |
+
request_counter += 1
|
67 |
+
timestamp = f"{time.time()}_{request_counter}"
|
68 |
+
advanced_options = f"\nStyle: {style}\n"
|
69 |
+
prompt_with_noise = add_random_noise(inputs, noise_level) + f" - {timestamp}"+ advanced_options
|
70 |
+
while queue.qsize() >= queue_threshold:
|
71 |
+
time.sleep(2)
|
72 |
+
queue.put(prompt_with_noise)
|
73 |
+
output = proc(prompt_with_noise)
|
74 |
+
return output
|
75 |
+
|
76 |
+
|
77 |
+
# ... (existing code)
|
78 |
+
|
79 |
+
# ... (existing code)
|
80 |
+
|
81 |
+
with gr.Blocks(css=".gradio-container {background-color: #fdf7e6;} footer{display:none !important;}",) as demo:
|
82 |
+
|
83 |
+
|
84 |
+
|
85 |
+
with gr.Column(elem_id="col-container"):
|
86 |
+
|
87 |
+
with gr.Row(variant="compact"):
|
88 |
+
prompt = gr.Textbox(
|
89 |
+
lines=8,
|
90 |
+
label="Enter your prompt",
|
91 |
+
show_label=False,
|
92 |
+
max_lines=10,
|
93 |
+
placeholder="Full Prompt",
|
94 |
+
).style(
|
95 |
+
container=False,
|
96 |
+
textarea={'height': '400px'}
|
97 |
+
)
|
98 |
+
|
99 |
+
run = gr.Button("Generate Images").style(full_width=False)
|
100 |
+
with gr.Accordion("Advanced options", open=False):
|
101 |
+
with gr.Row():
|
102 |
+
style_selection = gr.Radio(
|
103 |
+
show_label=True, container=True, interactive=True,
|
104 |
+
choices=IMAGE_STYLES,
|
105 |
+
value=DEFAULT_IMAGE_STYLE,
|
106 |
+
label='Image Style'
|
107 |
+
)
|
108 |
+
|
109 |
+
with gr.Row():
|
110 |
+
with gr.Row():
|
111 |
+
noise_level = gr.Slider(minimum=0.0, maximum=3, step=0.1, label="Noise Level")
|
112 |
+
|
113 |
+
with gr.Row():
|
114 |
+
with gr.Row():
|
115 |
+
output1 = gr.Image(label="", show_label=False, show_share_button=False)
|
116 |
+
output2 = gr.Image(label="", show_label=False, show_share_button=False)
|
117 |
+
|
118 |
+
# Adjust the click event to include the style_selection Radio component
|
119 |
+
run.click(send_it1, inputs=[prompt, noise_level, style_selection], outputs=[output1])
|
120 |
+
run.click(send_it1, inputs=[prompt, noise_level, style_selection], outputs=[output2])
|
121 |
+
|
122 |
+
demo.launch(enable_queue=True, inline=True)
|