File size: 1,709 Bytes
252da2a 7eddabc 252da2a 7eddabc 252da2a 7eddabc |
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 |
#!/bin/env python
""" Work in progress
Plan:
Generate two embeddings, from text prompts.
Create comparative graph of their values
"""
import sys
import json
import torch
from transformers import CLIPProcessor,CLIPModel
import PyQt5
import matplotlib
matplotlib.use('QT5Agg') # Set the backend to TkAgg
import matplotlib.pyplot as plt
clipsrc="openai/clip-vit-large-patch14"
processor=None
model=None
device=torch.device("cuda")
def init():
global processor
global model
# Load the processor and model
print("loading processor from "+clipsrc,file=sys.stderr)
processor = CLIPProcessor.from_pretrained(clipsrc)
print("done",file=sys.stderr)
print("loading model from "+clipsrc,file=sys.stderr)
model = CLIPModel.from_pretrained(clipsrc)
print("done",file=sys.stderr)
model = model.to(device)
# Expect SINGLE WORD ONLY
def standard_embed_calc(text):
inputs = processor(text=text, return_tensors="pt")
inputs.to(device)
with torch.no_grad():
text_features = model.get_text_features(**inputs)
embedding = text_features[0]
return embedding
init()
fig, ax = plt.subplots()
text1 = input("First word or prompt: ")
text2 = input("Second prompt(or leave blank): ")
print("generating embeddings for each now")
emb1 = standard_embed_calc(text1)
graph1=emb1.tolist()
ax.plot(graph1, label=text1[:20])
if len(text2) >0:
emb2 = standard_embed_calc(text2)
graph2=emb2.tolist()
ax.plot(graph2, label=text2[:20])
# Add labels, title, and legend
#ax.set_xlabel('Index')
ax.set_ylabel('Values')
ax.set_title('Comparative Graph of Two Embeddings')
ax.legend()
# Display the graph
print("Pulling up the graph")
plt.show()
|