Makhinur commited on
Commit
87556c9
·
verified ·
1 Parent(s): 26d6779

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import io
3
+ from PIL import Image
4
+ import torch
5
+ from clip_interrogator import Config, Interrogator
6
+
7
+ config = Config()
8
+ config.device = 'cuda' if torch.cuda.is_available() else 'cpu'
9
+ config.blip_offload = False if torch.cuda.is_available() else True
10
+ config.chunk_size = 2048
11
+ config.flavor_intermediate_count = 512
12
+ config.blip_num_beams = 64
13
+
14
+ ci = Interrogator(config)
15
+
16
+ def inference(input_images, mode, best_max_flavors):
17
+ prompt_results = []
18
+ for image_bytes in input_images:
19
+ image = Image.open(io.BytesIO(image_bytes)).convert('RGB')
20
+ if mode == 'best':
21
+ prompt_result = ci.interrogate(image, max_flavors=int(best_max_flavors))
22
+ elif mode == 'classic':
23
+ prompt_result = ci.interrogate_classic(image)
24
+ else:
25
+ prompt_result = ci.interrogate_fast(image)
26
+ prompt_results.append(prompt_result)
27
+ return "\n\n".join(prompt_results)
28
+
29
+ title = """
30
+ <div style="text-align: center; max-width: 500px; margin: 0 auto;">
31
+ <h1 style="font-weight: 600; margin-bottom: 7px;">
32
+ CLIP Interrogator 2.1
33
+ </h1>
34
+ <p style="margin-bottom: 10px;font-size: 94%;font-weight: 100;line-height: 1.5em;">
35
+ Want to figure out what a good prompt might be to create new images like an existing one?
36
+ <br />The CLIP Interrogator is here to get you answers!
37
+ <br />This version is specialized for producing nice prompts for use with Stable Diffusion 2.0 using the ViT-H-14 OpenCLIP model!
38
+ </p>
39
+ </div>
40
+ """
41
+
42
+ article = """
43
+ <div style="text-align: center; max-width: 500px; margin: 0 auto;font-size: 94%;">
44
+ <p>
45
+ Server busy? You can also run on <a href="https://colab.research.google.com/github/pharmapsychotic/clip-interrogator/blob/open-clip/clip_interrogator.ipynb">Google Colab</a>
46
+ </p>
47
+ <p>
48
+ Has this been helpful to you? Follow Pharma on twitter
49
+ <a href="https://twitter.com/pharmapsychotic">@pharmapsychotic</a>
50
+ and check out more tools at his
51
+ <a href="https://pharmapsychotic.com/tools.html">Ai generative art tools list</a>
52
+ </p>
53
+ </div>
54
+ """
55
+
56
+ css = '''
57
+ #col-container {width: 80%; margin-left: auto; margin-right: auto;}
58
+ a {text-decoration-line: underline; font-weight: 600;}
59
+ '''
60
+
61
+ with gr.Blocks(css=css) as block:
62
+ with gr.Column(elem_id="col-container"):
63
+ gr.HTML(title)
64
+
65
+ input_image = gr.Files(label="Inputs", file_count="multiple", type='binary', elem_id='inputs')
66
+ with gr.Row():
67
+ mode_input = gr.Radio(['best', 'classic', 'fast'], label='Select mode', value='best')
68
+ flavor_input = gr.Slider(minimum=2, maximum=24, step=2, value=4, label='Best mode max flavors')
69
+
70
+ submit_btn = gr.Button("Submit")
71
+
72
+ output_text = gr.Textbox(label="Output Prompts", lines=10, elem_id="output_text")
73
+
74
+ with gr.Group(elem_id="share-btn-container"):
75
+ gr.HTML(article)
76
+
77
+ submit_btn.click(fn=inference, inputs=[input_image, mode_input, flavor_input], outputs=[output_text], api_name="clipi2")
78
+
79
+ block.queue().launch()