Ashrafb commited on
Commit
109feab
ยท
1 Parent(s): d624cb7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -57
app.py CHANGED
@@ -1,34 +1,75 @@
1
  import gradio as gr
2
  import os
3
- from share_btn import community_icon_html, loading_icon_html, share_js
 
 
 
 
 
 
4
 
5
- text_gen = gr.load(name="spaces/Ashrafb/MagicPrompt-Stable-Diffusiongust")
6
- stable_diffusion = gr.load(name="spaces/runwayml/stable-diffusion-v1-5")
7
 
8
- def get_images(prompt):
9
- gallery_dir = stable_diffusion(prompt, fn_index=2)
10
- sd_output = [os.path.join(gallery_dir, image) for image in os.listdir(gallery_dir)]
11
- return sd_output, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
12
 
13
  def get_prompts(prompt_text):
14
  return text_gen(prompt_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- css = '''
17
- .animate-spin {
18
- animation: spin 1s linear infinite;
19
- }
20
- @keyframes spin {
21
- from {
22
- transform: rotate(0deg);
23
- }
24
- to {
25
- transform: rotate(360deg);
26
- }
27
- }
28
- footer{display:none !important}
29
- '''
30
-
31
- with gr.Blocks(css=css) as demo:
 
 
 
 
 
 
32
  gr.HTML("""<div style="text-align: center; max-width: 700px; margin: 0 auto;">
33
  <div
34
  style="
@@ -48,41 +89,50 @@ with gr.Blocks(css=css) as demo:
48
  </p>
49
  </div>""")
50
 
51
- with gr.Row():
52
- with gr.Column():
53
- input_text = gr.Textbox(label="Short text prompt",
54
- lines=4, elem_id="input-text")
55
- with gr.Row():
56
- see_prompts = gr.Button("Feed in your text!")
57
-
58
- with gr.Column():
59
- text_output = gr.Textbox(
60
- label="Prettified text prompt",
61
- lines=4,
62
- elem_id="translated"
63
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  with gr.Row():
65
- diffuse_btn = gr.Button(value="Diffuse the Prompt!")
66
- with gr.Column(elem_id="generated-gallery"):
67
- sd_output = gr.Gallery()
68
- with gr.Group(elem_id="share-btn-container"):
69
- community_icon = gr.HTML(community_icon_html, visible=False)
70
- loading_icon = gr.HTML(loading_icon_html, visible=False)
71
-
72
-
73
- see_prompts.click(get_prompts,
74
- inputs = [input_text],
75
- outputs = [
76
- text_output
77
- ])
78
- diffuse_btn.click(get_images,
79
- inputs = [
80
- text_output
81
- ],
82
- outputs = [sd_output, loading_icon]
83
- )
84
-
85
 
86
 
 
 
 
 
 
 
 
 
87
 
88
- demo.launch(debug=True)
 
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
 
12
+ text_gen = gr.load(name="spaces/Ashrafb/MagicPrompt-Stable-Diffusiongust")
13
+ proc1 = gr.Interface.load("models/runwayml/stable-diffusion-v1-5")
 
 
14
 
15
  def get_prompts(prompt_text):
16
  return text_gen(prompt_text)
17
+
18
+ def restart_script_periodically():
19
+ while True:
20
+ random_time = random.randint(540, 600)
21
+ time.sleep(random_time)
22
+ os.execl(sys.executable, sys.executable, *sys.argv)
23
+
24
+ restart_thread = Thread(target=restart_script_periodically, daemon=True)
25
+ restart_thread.start()
26
+
27
+ queue = Queue()
28
+ queue_threshold = 100
29
+
30
+ def add_random_noise(prompt, noise_level=0.00):
31
+ if noise_level == 0:
32
+ noise_level = 0.00
33
+ percentage_noise = noise_level * 5
34
+ num_noise_chars = int(len(prompt) * (percentage_noise / 100))
35
+ noise_indices = random.sample(range(len(prompt)), num_noise_chars)
36
+ prompt_list = list(prompt)
37
+ noise_chars = list(string.ascii_letters + string.punctuation + ' ' + string.digits)
38
+ noise_chars.extend(['๐Ÿ˜', '๐Ÿ’ฉ', '๐Ÿ˜‚', '๐Ÿค”', '๐Ÿ˜Š', '๐Ÿค—', '๐Ÿ˜ญ', '๐Ÿ™„', '๐Ÿ˜ท', '๐Ÿคฏ', '๐Ÿคซ', '๐Ÿฅด', '๐Ÿ˜ด', '๐Ÿคฉ', '๐Ÿฅณ', '๐Ÿ˜”', '๐Ÿ˜ฉ', '๐Ÿคช', '๐Ÿ˜‡', '๐Ÿคข', '๐Ÿ˜ˆ', '๐Ÿ‘น', '๐Ÿ‘ป', '๐Ÿค–', '๐Ÿ‘ฝ', '๐Ÿ’€', '๐ŸŽƒ', '๐ŸŽ…', '๐ŸŽ„', '๐ŸŽ', '๐ŸŽ‚', '๐ŸŽ‰', '๐ŸŽˆ', '๐ŸŽŠ', '๐ŸŽฎ', 'โค๏ธ', '๐Ÿ’”', '๐Ÿ’•', '๐Ÿ’–', '๐Ÿ’—', '๐Ÿถ', '๐Ÿฑ', '๐Ÿญ', '๐Ÿน', '๐ŸฆŠ', '๐Ÿป', '๐Ÿจ', '๐Ÿฏ', '๐Ÿฆ', '๐Ÿ˜', '๐Ÿ”ฅ', '๐ŸŒง๏ธ', '๐ŸŒž', '๐ŸŒˆ', '๐Ÿ’ฅ', '๐ŸŒด', '๐ŸŒŠ', '๐ŸŒบ', '๐ŸŒป', '๐ŸŒธ', '๐ŸŽจ', '๐ŸŒ…', '๐ŸŒŒ', 'โ˜๏ธ', 'โ›ˆ๏ธ', 'โ„๏ธ', 'โ˜€๏ธ', '๐ŸŒค๏ธ', 'โ›…๏ธ', '๐ŸŒฅ๏ธ', '๐ŸŒฆ๏ธ', '๐ŸŒง๏ธ', '๐ŸŒฉ๏ธ', '๐ŸŒจ๏ธ', '๐ŸŒซ๏ธ', 'โ˜”๏ธ', '๐ŸŒฌ๏ธ', '๐Ÿ’จ', '๐ŸŒช๏ธ', '๐ŸŒˆ'])
39
+ for index in noise_indices:
40
+ prompt_list[index] = random.choice(noise_chars)
41
+ return "".join(prompt_list)
42
+
43
+ # Existing code...
44
+
45
+ import uuid # Import the UUID library
46
+
47
+ # Existing code...
48
+
49
+ # Existing code...
50
 
51
+ request_counter = 0 # Global counter to track requests
52
+
53
+ def send_it1(inputs, noise_level, proc=proc1):
54
+ global request_counter
55
+ request_counter += 1
56
+ timestamp = f"{time.time()}_{request_counter}"
57
+ prompt_with_noise = add_random_noise(inputs, noise_level) + f" - {timestamp}"
58
+
59
+ try:
60
+ while queue.qsize() >= queue_threshold:
61
+ time.sleep(2)
62
+ queue.put(prompt_with_noise)
63
+ output = proc(prompt_with_noise)
64
+ return output
65
+ except Exception as e:
66
+ # Display a generic error message to the user
67
+ raise gr.Error("Experiencing high demand. Please retry shortly. Thank you for your patience.")
68
+
69
+
70
+
71
+
72
+ with gr.Blocks(css="footer{display:none !important;}",) as demo:
73
  gr.HTML("""<div style="text-align: center; max-width: 700px; margin: 0 auto;">
74
  <div
75
  style="
 
89
  </p>
90
  </div>""")
91
 
92
+ with gr.Column(elem_id="col-container"):
93
+ with gr.Row(variant="compact"):
94
+ input_text = gr.Textbox(
95
+ lines=4,
96
+ label="Short text prompt",
97
+
98
+ max_lines=8,
99
+ placeholder="",
100
+ ).style(
101
+
102
+ textarea={'height': '400px'}
103
+ )
104
+ see_prompts = gr.Button("โœจ Feed in your text! โœจ")
105
+
106
+ with gr.Row(variant="compact"):
107
+ prompt = gr.Textbox(
108
+ lines=4,
109
+ label="Prettified text prompt",
110
+
111
+ max_lines=10,
112
+ placeholder="Full Prompt",
113
+ ).style(
114
+
115
+ textarea={'height': '400px'}
116
+ )
117
+ run = gr.Button("Diffuse the Prompt!")
118
+
119
  with gr.Row():
120
+ with gr.Row():
121
+ noise_level = gr.Slider(minimum=0.0, maximum=3, step=0.1, label="Noise Level")
122
+
123
+ with gr.Row():
124
+ with gr.Row():
125
+ output1 = gr.Image(label="Dreamlike Diffusion 1.0", show_label=False, show_share_button=False)
126
+ output2 = gr.Image(label="Dreamlike Diffusion 1.0", show_label=False, show_share_button=False)
127
+ output3 = gr.Image(label="Dreamlike Diffusion 1.0", show_label=False, show_share_button=False)
 
 
 
 
 
 
 
 
 
 
 
 
128
 
129
 
130
+ see_prompts.click(get_prompts, inputs=[input_text], outputs=[prompt], queue=False)
131
+ run.click(send_it1, inputs=[prompt, noise_level], outputs=[output1])
132
+ run.click(send_it1, inputs=[prompt, noise_level], outputs=[output2])
133
+ run.click(send_it1, inputs=[prompt, noise_level], outputs=[output3])
134
+
135
+
136
+
137
+ demo.launch(enable_queue=True, inline=True)
138