ppbrown commited on
Commit
40cd7fc
1 Parent(s): 7a9b925

Upload graph-embeddings-XL.py

Browse files
Files changed (1) hide show
  1. graph-embeddings-XL.py +84 -0
graph-embeddings-XL.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/env python
2
+
3
+ """
4
+ (Similar to graph-embeddings, but for SDXL)
5
+
6
+ This program requires two files as arguments:
7
+ A text encoder model (SDXL style), and matching config.json
8
+
9
+ You can get the fancy SDXL "vit-bigg" based text encoding model and config, from
10
+ https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/tree/main/text_encoder_2
11
+ Take the config.json and one of the .safetensors files
12
+
13
+ The sd1.5 encoding model resides at
14
+ https://huggingface.co/runwayml/stable-diffusion-v1-5/tree/main/text_encoder
15
+
16
+ Once it has read those files in, it asks for 1-2 text prompts, and then graphs them.
17
+ (and pops up a prog to display the output)
18
+
19
+ """
20
+ import sys
21
+ import torch
22
+ from transformers import CLIPProcessor, CLIPTextModel
23
+
24
+ if len(sys.argv) <3:
25
+ print("Error: require clipmodel file and config file as arguments")
26
+ exit(1)
27
+
28
+ # 1. Load the pretrained model
29
+ # Note that it doesnt like a leading "/" in the name!!
30
+ #
31
+ model_path = sys.argv[1]
32
+ model_config = sys.argv[2]
33
+ print("loading",model_path)
34
+ model = CLIPTextModel.from_pretrained(
35
+ model_path,config=model_config,local_files_only=True,use_safetensors=True)
36
+
37
+
38
+ # This is the tokenizer for sd1 and sdxl
39
+ CLIPname = "openai/clip-vit-large-patch14"
40
+ print("getting processor",CLIPname)
41
+ processor = CLIPProcessor.from_pretrained(CLIPname)
42
+
43
+ def embed_from_text(text):
44
+ print("getting tokens for",text)
45
+ inputs = processor(text=text, return_tensors="pt")
46
+ outputs = model(**inputs)
47
+ embeddings = outputs.pooler_output
48
+ return embeddings
49
+
50
+
51
+ import PyQt5
52
+ import matplotlib
53
+ matplotlib.use('QT5Agg') # Set the backend to TkAgg
54
+
55
+ import matplotlib.pyplot as plt
56
+
57
+
58
+ fig, ax = plt.subplots()
59
+
60
+
61
+ text1 = input("First prompt: ")
62
+ text2 = input("Second prompt(or leave blank): ")
63
+
64
+
65
+ emb1 = embed_from_text(text1)
66
+ print("shape of emb1:",emb1.shape)
67
+
68
+ graph1=emb1[0].tolist()
69
+ ax.plot(graph1, label=text1[:20])
70
+
71
+ if len(text2) >0:
72
+ emb2 = embed_from_text(text2)
73
+ graph2=emb2[0].tolist()
74
+ ax.plot(graph2, label=text2[:20])
75
+
76
+ # Add labels, title, and legend
77
+ #ax.set_xlabel('Index')
78
+ ax.set_ylabel('Values')
79
+ ax.set_title(f"Graph of Embeddings in {model_path}")
80
+ ax.legend()
81
+
82
+ # Display the graph
83
+ print("Pulling up the graph")
84
+ plt.show()