Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

app.py 5.7 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
  1. import os, subprocess
  2. import torch
  3. def setup():
  4. install_cmds = [
  5. ['pip', 'install', 'ftfy', 'gradio', 'regex', 'tqdm', 'transformers==4.21.2', 'timm', 'fairscale', 'requests'],
  6. ['pip', 'install', 'open_clip_torch'],
  7. ['pip', 'install', '-e', 'git+https://github.com/pharmapsychotic/BLIP.git@lib#egg=blip'],
  8. ['git', 'clone', '-b', 'open-clip', 'https://github.com/pharmapsychotic/clip-interrogator.git']
  9. ]
  10. for cmd in install_cmds:
  11. print(subprocess.run(cmd, stdout=subprocess.PIPE).stdout.decode('utf-8'))
  12. setup()
  13. # download cache files
  14. print("Download preprocessed cache files...")
  15. CACHE_URLS = [
  16. 'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_artists.pkl',
  17. 'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_flavors.pkl',
  18. 'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_mediums.pkl',
  19. 'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_movements.pkl',
  20. 'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_trendings.pkl',
  21. ]
  22. os.makedirs('cache', exist_ok=True)
  23. for url in CACHE_URLS:
  24. print(subprocess.run(['wget', url, '-P', 'cache'], stdout=subprocess.PIPE).stdout.decode('utf-8'))
  25. import sys
  26. sys.path.append('src/blip')
  27. sys.path.append('clip-interrogator')
  28. import gradio as gr
  29. from clip_interrogator import Config, Interrogator
  30. config = Config()
  31. config.device = 'cuda' if torch.cuda.is_available() else 'cpu'
  32. config.blip_offload = False if torch.cuda.is_available() else True
  33. config.chunk_size = 2048
  34. config.flavor_intermediate_count = 512
  35. config.blip_num_beams = 64
  36. ci = Interrogator(config)
  37. def inference(image, mode, best_max_flavors):
  38. """
  39. Generate a descriptive prompt from an input image using different interrogation modes.
  40. Args:
  41. image: A PIL Image object representing the input image to be analyzed.
  42. mode: A string specifying the interrogation mode to use.
  43. Can be one of ['best', 'classic', 'fast']:
  44. - 'best': Produces a prompt using the 'best' interrogation mode with max flavors control.
  45. - 'classic': Uses the classic interrogation method.
  46. - 'fast': Uses a faster but less detailed interrogation method.
  47. best_max_flavors: An integer controlling the maximum number of flavor descriptors
  48. when using 'best' mode (ignored in other modes).
  49. Returns:
  50. A string containing the generated prompt describing the image.
  51. """
  52. image = image.convert('RGB')
  53. if mode == 'best':
  54. prompt_result = ci.interrogate(image, max_flavors=int(best_max_flavors))
  55. print("mode best: " + prompt_result)
  56. return prompt_result
  57. elif mode == 'classic':
  58. prompt_result = ci.interrogate_classic(image)
  59. print("mode classic: " + prompt_result)
  60. return prompt_result
  61. else:
  62. prompt_result = ci.interrogate_fast(image)
  63. print("mode fast: " + prompt_result)
  64. return prompt_result
  65. title = """
  66. <div style="text-align: center; max-width: 500px; margin: 0 auto;">
  67. <div
  68. style="
  69. display: inline-flex;
  70. align-items: center;
  71. gap: 0.8rem;
  72. font-size: 1.75rem;
  73. margin-bottom: 10px;
  74. "
  75. >
  76. <h1 style="font-weight: 600; margin-bottom: 7px;">
  77. CLIP Interrogator 2.1
  78. </h1>
  79. </div>
  80. <p style="margin-bottom: 10px;font-size: 94%;font-weight: 100;line-height: 1.5em;">
  81. Want to figure out what a good prompt might be to create new images like an existing one?
  82. <br />The CLIP Interrogator is here to get you answers!
  83. <br />This version is specialized for producing nice prompts for use with Stable Diffusion 2.0 using the ViT-H-14 OpenCLIP model!
  84. </p>
  85. </div>
  86. """
  87. article = """
  88. <div style="text-align: center; max-width: 500px; margin: 0 auto;font-size: 94%;">
  89. <p>
  90. 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>
  91. </p>
  92. <p>
  93. Has this been helpful to you? Follow Pharma on twitter
  94. <a href="https://twitter.com/pharmapsychotic">@pharmapsychotic</a>
  95. and check out more tools at his
  96. <a href="https://pharmapsychotic.com/tools.html">Ai generative art tools list</a>
  97. </p>
  98. </div>
  99. """
  100. css = '''
  101. #col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
  102. a {text-decoration-line: underline; font-weight: 600;}
  103. '''
  104. with gr.Blocks(css=css) as demo:
  105. with gr.Column(elem_id="col-container"):
  106. gr.HTML(title)
  107. input_image = gr.Image(type='pil', elem_id="input-img")
  108. with gr.Row():
  109. mode_input = gr.Radio(['best', 'classic', 'fast'], label='Select mode', value='best')
  110. flavor_input = gr.Slider(minimum=2, maximum=24, step=2, value=4, label='best mode max flavors')
  111. submit_btn = gr.Button("Submit")
  112. output_text = gr.Textbox(label="Description Output", elem_id="output-txt")
  113. examples=[['27E894C4-9375-48A1-A95D-CB2425416B4B.png', "best",4], ['DB362F56-BA98-4CA1-A999-A25AA94B723B.png',"fast",4]]
  114. ex = gr.Examples(examples=examples, fn=inference, inputs=[input_image, mode_input, flavor_input], outputs=[output_text], cache_examples=False, run_on_click=True)
  115. gr.HTML(article)
  116. submit_btn.click(fn=inference, inputs=[input_image,mode_input,flavor_input], outputs=[output_text], api_name="clipi2")
  117. demo.queue(max_size=32).launch(show_api=True, ssr_mode=False, mcp_server=True)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...