#!/usr/bin/python3 """ 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() text1 = input("First word or prompt? ") text2 = input("Second word or prompt? ") print("generating embeddings for each now") emb1 = standard_embed_calc(text1) emb2 = standard_embed_calc(text2) graph1=emb1.tolist() graph2=emb2.tolist() fig, ax = plt.subplots() # Plot the two lists on the same graph using the read labels ax.plot(graph1, label=text1[:20]) 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()