llinahosna commited on
Commit
18d7e79
verified
1 Parent(s): 1a423e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -102
app.py CHANGED
@@ -1,109 +1,54 @@
1
- import gradio as gr
 
2
  import os
3
- import requests
4
- import random
5
- import time
6
- name2 = "stablediffusionapi/opendalle"
7
 
8
- models=[
9
- gr.Interface.load(f"models/{name2}"),
10
- gr.Interface.load(f"models/{name2}"),
11
- gr.Interface.load(f"models/{name2}"),
12
- gr.Interface.load(f"models/{name2}"),
13
- gr.Interface.load(f"models/{name2}"),
14
- gr.Interface.load(f"models/{name2}"),
15
- gr.Interface.load(f"models/{name2}"),
16
- gr.Interface.load(f"models/{name2}"),
17
- gr.Interface.load(f"models/{name2}"),
18
- gr.Interface.load(f"models/{name2}"),
19
- gr.Interface.load(f"models/{name2}"),
20
- gr.Interface.load(f"models/{name2}"),
21
- gr.Interface.load(f"models/{name2}"),
22
- gr.Interface.load(f"models/{name2}"),
23
- gr.Interface.load(f"models/{name2}"),
24
- gr.Interface.load(f"models/{name2}"),
25
- gr.Interface.load(f"models/{name2}"),
26
- gr.Interface.load(f"models/{name2}"),
27
- gr.Interface.load(f"models/{name2}"),
28
- gr.Interface.load(f"models/{name2}"),
29
- ]
30
- #o = os.getenv("P")
31
- o = "V"
32
 
33
- m_out = ("""
34
- <div id="restart">
35
- <h4 id="head">Loading Time Limit Reached. Please choose a Simpler Prompt</h4><br>
36
- </div>
37
- """)
38
- loading=("""
39
- <div class="lds-ellipsis"><div></div><div></div><div></div><div></div></div>""")
 
40
 
41
- def ac():
42
- def clear():
43
- return gr.update(value=0),gr.update(value=0)
44
- def start():
45
- stamp = time.time()
46
- return gr.update(value=stamp),gr.update(value=0)
47
- def end(stamp):
48
- ts = stamp + 120
49
- ti = time.time()
50
- if ti > ts and stamp != 0:
51
- return gr.update(value=1),gr.HTML.update(f"{m_out}",visible=True)
52
- else:
53
- return gr.update(value=0),None
54
- def im_fn(put,fac="",h=None):
55
- try:
56
- if h == o:
57
- put = f"{put}{fac}"
58
- fac = f"{fac} "
59
- rn = random.randint(0, 19)
60
- model=models[rn]
61
- return model(put),fac
62
- elif h != o:
63
- return(None,None)
64
- except Exception:
65
- return None, None
66
- def cl_fac():
67
- return "",gr.HTML.update(f"{loading}")
68
- with gr.Blocks() as b:
69
- with gr.Row():
70
- with gr.Column():
71
- put = gr.Textbox()
72
- with gr.Column():
73
- with gr.Row():
74
- btn1 = gr.Button("Run")
75
- btn2 = gr.Button("Clear")
76
- message=gr.HTML("<div></div>")
77
- message2=gr.HTML("",visible=False)
78
 
79
- with gr.Row():
80
- out1 = gr.Image()
81
- out2 = gr.Image()
82
- with gr.Row():
83
- out3 = gr.Image()
84
- out4 = gr.Image()
 
 
85
 
86
- with gr.Row(visible=False):
87
- h=gr.Textbox(value="V")
88
- t_state=gr.Number()
89
- t_switch=gr.Textbox(value=0)
90
- def clear_all():
91
- return "",None,None,None,None,None,None,1,gr.HTML.update("<div></div>")
92
- fac_b = gr.Textbox(value="", visible=False)
93
 
94
- def noth():
95
- return gr.HTML.update("<div></div>")
96
- #a1=btn1.click(noth,None,btn1,every=1)
97
- btn1.click(cl_fac,None,[fac_b,message],show_progress=False)
98
- b1=btn1.click(start,None,[t_state,t_switch],show_progress=True)
99
- sta = t_state.change(end,t_state,[t_switch,message2],every=1,show_progress=True)
100
- b2=btn1.click(im_fn,[put,fac_b,h],[out1,fac_b], show_progress=True)
101
- b3=out1.change(im_fn,[put,fac_b,h],[out2,fac_b], show_progress=True)
102
- b4=out2.change(im_fn,[put,fac_b,h],[out3,fac_b], show_progress=True)
103
- b5=out3.change(im_fn,[put,fac_b,h],[out4,fac_b], show_progress=True)
104
- b6=out4.change(noth,None,message, show_progress=False)
105
- swi=t_switch.change(clear,None,[t_switch,fac_b], cancels=[sta,b2,b3,b4,b5],show_progress=False)
106
- #btn2.click(noth,None,message,cancels=[b1,sta,b2,b3,b4,b5,swi],show_progress=False)
107
- btn2.click(clear_all, None,[fac_b,put,out1,out2,out3,out4,t_state,t_switch,message],cancels=[b1,sta,b2,b3,b4,b5,swi],show_progress=False)
108
- b.queue(concurrency_count=100).launch(show_api=False)
109
- ac()
 
1
+ #!/usr/bin/env python
2
+ # coding: utf-8
3
  import os
 
 
 
 
4
 
5
+ import gradio as gr
6
+ from backend import get_images_from_backend
7
+
8
+ block = gr.Blocks(css=".container { max-width: 800px; margin: auto; }")
9
+ backend_url = os.environ["BACKEND_SERVER"] + "/generate"
10
+
11
+
12
+ def infer(prompt):
13
+ response = get_images_from_backend(prompt, backend_url)
14
+ return response["images"]
15
+
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ with block:
18
+ gr.Markdown("<h1><center>DALL路E mini</center></h1>")
19
+ gr.Markdown(
20
+ "DALL路E mini is an AI model that generates images from any prompt you give!"
21
+ )
22
+ with gr.Group():
23
+ with gr.Box():
24
+ with gr.Row().style(mobile_collapse=False, equal_height=True):
25
 
26
+ text = gr.Textbox(
27
+ label="Enter your prompt", show_label=False, max_lines=1
28
+ ).style(
29
+ border=(True, False, True, True),
30
+ margin=False,
31
+ rounded=(True, False, False, True),
32
+ container=False,
33
+ )
34
+ btn = gr.Button("Run").style(
35
+ margin=False,
36
+ rounded=(False, True, True, False),
37
+ )
38
+ gallery = gr.Gallery(label="Generated images", show_label=False).style(
39
+ grid=[3], height="auto"
40
+ )
41
+ text.submit(infer, inputs=text, outputs=gallery)
42
+ btn.click(infer, inputs=text, outputs=gallery)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
+ gr.Markdown(
45
+ """___
46
+ <p style='text-align: center'>
47
+ Created by <a href="https://twitter.com/borisdayma" target="_blank">Boris Dayma</a> et al. 2021-2022
48
+ <br/>
49
+ <a href="https://github.com/borisdayma/dalle-mini" target="_blank">GitHub</a> | <a href="https://wandb.ai/dalle-mini/dalle-mini/reports/DALL-E-mini-Generate-images-from-any-text-prompt--VmlldzoyMDE4NDAy" target="_blank">Project Report</a>
50
+ </p>"""
51
+ )
52
 
 
 
 
 
 
 
 
53
 
54
+ block.launch(enable_queue=False)