wongshennan commited on
Commit
e827598
0 Parent(s):
Files changed (6) hide show
  1. .gitignore +1 -0
  2. README.md +1 -0
  3. app.py +94 -0
  4. requirements.txt +77 -0
  5. src/blip +1 -0
  6. src/clip +1 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .env
README.md ADDED
@@ -0,0 +1 @@
 
 
1
+ ## Same Same But Different
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ import warnings
3
+ from io import BytesIO
4
+ from clip_interrogator import Interrogator, Config
5
+ import torch
6
+ import gradio as gr
7
+ import clip
8
+ import stability_sdk.interfaces.gooseai.generation.generation_pb2 as generation
9
+ from stability_sdk import client
10
+ from PIL import Image
11
+ from IPython.display import display
12
+
13
+ import argparse
14
+ import subprocess
15
+ import sys
16
+ import time
17
+
18
+
19
+ def setup():
20
+ install_cmds = [
21
+ ['pip', 'install', 'ftfy', 'gradio', 'regex', 'tqdm', 'stability-sdk',
22
+ 'transformers==4.21.2', 'timm', 'fairscale', 'requests'],
23
+ ['pip', 'install', '-e', 'git+https://github.com/openai/CLIP.git@main#egg=clip'],
24
+ ['pip', 'install', '-e',
25
+ 'git+https://github.com/pharmapsychotic/BLIP.git@lib#egg=blip'],
26
+ ['git', 'clone', 'https://github.com/pharmapsychotic/clip-interrogator.git']
27
+ ]
28
+ for cmd in install_cmds:
29
+ print(subprocess.run(cmd, stdout=subprocess.PIPE).stdout.decode('utf-8'))
30
+
31
+
32
+ setup()
33
+
34
+ sys.path.append('src/blip')
35
+ sys.path.append('src/clip')
36
+ sys.path.append('clip-interrogator')
37
+
38
+
39
+ ci = Interrogator(Config())
40
+
41
+
42
+ stability_api = client.StabilityInference(
43
+ key=os.environ['STABILITY_KEY'],
44
+ verbose=True
45
+ )
46
+
47
+
48
+ def inferAndRebuild(image, mode):
49
+ image = image.convert('RGB')
50
+ output = ''
51
+ if (mode == 'best'):
52
+ output = ci.interrogate(image)
53
+ elif (mode == 'classic'):
54
+ output = ci.interrogate_classic(image)
55
+ else:
56
+ output = ci.interrogate_fast(image)
57
+
58
+ answers = stability_api.generate(
59
+ prompt=str(output),
60
+ seed=34567,
61
+ steps=30,
62
+ samples=5
63
+ )
64
+
65
+ imglist = []
66
+ for resp in answers:
67
+ for artifact in resp.artifacts:
68
+ if artifact.finish_reason == generate.FILTER:
69
+ warnings.warn(
70
+ "Your request activated the API's safety filters and could not be processed. Please modify the prompt and try again.")
71
+ if artifact.type == generation.ARTIFACT_IMAGE:
72
+ img = Image.open(BytesIO(artifact.binary))
73
+ imglist.append(img)
74
+ return [imglist, output]
75
+
76
+
77
+ inputs = [
78
+ gr.inputs.Image(type='pil'),
79
+ gr.Radio(['best', 'classic', 'fast'], label='Models', value='fast')
80
+ ]
81
+
82
+ outputs = [
83
+ gr.Gallery(),
84
+ gr.outputs.Textbox(label='Prompt')
85
+ ]
86
+
87
+ io = gr.Interface(
88
+ inferAndRebuild,
89
+ inputs,
90
+ outputs,
91
+ allow_flagging=False,
92
+ )
93
+
94
+ io.launch(share=True, debug=True)
requirements.txt ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiohttp==3.8.3
2
+ aiosignal==1.3.1
3
+ anyio==3.6.2
4
+ async-timeout==4.0.2
5
+ attrs==22.1.0
6
+ bcrypt==4.0.1
7
+ certifi @ file:///private/var/folders/sy/f16zz6x50xz3113nwtb9bvq00000gp/T/abs_0ek9yztvu3/croot/certifi_1665076692562/work/certifi
8
+ cffi==1.15.1
9
+ charset-normalizer==2.1.1
10
+ click==8.1.3
11
+ -e git+https://github.com/openai/CLIP.git@d50d76daa670286dd6cacf3bcd80b5e4823fc8e1#egg=clip
12
+ contourpy==1.0.6
13
+ cryptography==38.0.3
14
+ cycler==0.11.0
15
+ fairscale==0.4.12
16
+ fastapi==0.86.0
17
+ ffmpy==0.3.0
18
+ filelock==3.8.0
19
+ fonttools==4.38.0
20
+ frozenlist==1.3.3
21
+ fsspec==2022.11.0
22
+ ftfy==6.1.1
23
+ gradio==3.9.1
24
+ grpcio==1.48.1
25
+ grpcio-tools==1.48.1
26
+ h11==0.12.0
27
+ httpcore==0.15.0
28
+ httpx==0.23.0
29
+ huggingface-hub==0.10.1
30
+ idna==3.4
31
+ Jinja2==3.1.2
32
+ kiwisolver==1.4.4
33
+ linkify-it-py==1.0.3
34
+ markdown-it-py==2.1.0
35
+ MarkupSafe==2.1.1
36
+ matplotlib==3.6.2
37
+ mdit-py-plugins==0.3.1
38
+ mdurl==0.1.2
39
+ multidict==6.0.2
40
+ numpy==1.23.4
41
+ orjson==3.8.1
42
+ packaging==21.3
43
+ pandas==1.5.1
44
+ paramiko==2.12.0
45
+ Pillow==9.3.0
46
+ protobuf==3.19.5
47
+ pycparser==2.21
48
+ pycryptodome==3.15.0
49
+ pydantic==1.10.2
50
+ pydub==0.25.1
51
+ PyNaCl==1.5.0
52
+ pyparsing==3.0.9
53
+ python-dateutil==2.8.2
54
+ python-dotenv==0.21.0
55
+ python-multipart==0.0.5
56
+ pytz==2022.6
57
+ PyYAML==6.0
58
+ regex==2022.10.31
59
+ requests==2.28.1
60
+ rfc3986==1.5.0
61
+ six==1.16.0
62
+ sniffio==1.3.0
63
+ stability-sdk==0.2.7
64
+ starlette==0.20.4
65
+ timm==0.6.11
66
+ tokenizers==0.12.1
67
+ torch==1.13.0
68
+ torchvision==0.14.0
69
+ tqdm==4.64.1
70
+ transformers==4.21.2
71
+ typing_extensions==4.4.0
72
+ uc-micro-py==1.0.1
73
+ urllib3==1.26.12
74
+ uvicorn==0.19.0
75
+ wcwidth==0.2.5
76
+ websockets==10.4
77
+ yarl==1.8.1
src/blip ADDED
@@ -0,0 +1 @@
 
 
1
+ Subproject commit 5b58f59454039295a87c6628b1392e6b8752e911
src/clip ADDED
@@ -0,0 +1 @@
 
 
1
+ Subproject commit d50d76daa670286dd6cacf3bcd80b5e4823fc8e1