Spaces:
Runtime error
Runtime error
saylee-m
commited on
Commit
•
bd4a4f5
1
Parent(s):
ca11711
basic setup
Browse files- app.py +61 -2
- embeddings_plot.png +0 -0
app.py
CHANGED
@@ -1,7 +1,66 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
def greet(name):
|
4 |
return "Hello " + name + "!!"
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
from sklearn.decomposition import PCA
|
5 |
+
from sklearn.manifold import TSNE
|
6 |
|
7 |
+
def greet(name, name2, name3):
|
8 |
return "Hello " + name + "!!"
|
9 |
|
10 |
+
# Dummy function to simulate getting embeddings from different models
|
11 |
+
def get_embeddings(model_name, data):
|
12 |
+
np.random.seed(0) # For reproducibility
|
13 |
+
return np.random.rand(len(data), 128) # Simulate 128-dimensional embeddings
|
14 |
+
|
15 |
+
def visualize_embeddings(model1, model2, data):
|
16 |
+
# Convert input data to list
|
17 |
+
data = data.split(',')
|
18 |
+
data = [item.strip() for item in data]
|
19 |
+
|
20 |
+
# Get embeddings
|
21 |
+
embeddings1 = get_embeddings(model1, data)
|
22 |
+
embeddings2 = get_embeddings(model2, data)
|
23 |
+
|
24 |
+
# Combine embeddings
|
25 |
+
combined_embeddings = np.concatenate((embeddings1, embeddings2), axis=0)
|
26 |
+
|
27 |
+
# Reduce dimensions using PCA for initial dimensionality reduction
|
28 |
+
pca = PCA(n_components=2) #, svd_solver='randomized')
|
29 |
+
pca_embeddings = pca.fit_transform(combined_embeddings)
|
30 |
+
tsne_embeddings = pca_embeddings
|
31 |
+
|
32 |
+
# Further reduce dimensions using t-SNE
|
33 |
+
# tsne = TSNE(n_components=2, random_state=0)
|
34 |
+
# tsne_embeddings = tsne.fit_transform(pca_embeddings)
|
35 |
+
|
36 |
+
# Plot the embeddings
|
37 |
+
plt.figure(figsize=(10, 5))
|
38 |
+
plt.scatter(tsne_embeddings[:len(data), 0], tsne_embeddings[:len(data), 1], label=model1, alpha=0.5)
|
39 |
+
plt.scatter(tsne_embeddings[len(data):, 0], tsne_embeddings[len(data):, 1], label=model2, alpha=0.5)
|
40 |
+
plt.legend()
|
41 |
+
plt.title('Embeddings Visualization')
|
42 |
+
plt.xlabel('Dimension 1')
|
43 |
+
plt.ylabel('Dimension 2')
|
44 |
+
|
45 |
+
# Save the plot to a file and return the file path
|
46 |
+
plt.savefig('embeddings_plot.png')
|
47 |
+
plt.close()
|
48 |
+
|
49 |
+
return 'embeddings_plot.png'
|
50 |
+
|
51 |
+
# demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
52 |
+
# Define Gradio interface
|
53 |
+
# Model 1 - sentence-transformers/sentence-t5-large
|
54 |
+
# Model 2 - nomic-ai/nomic-embed-text-v1
|
55 |
+
demo = gr.Interface(
|
56 |
+
fn=visualize_embeddings,
|
57 |
+
inputs=[
|
58 |
+
gr.Textbox(label="Model 1 Name"),
|
59 |
+
gr.Textbox(label="Model 2 Name"),
|
60 |
+
gr.Textbox(lines=2, label="Data (comma-separated)")
|
61 |
+
],
|
62 |
+
outputs="image",
|
63 |
+
title="Embeddings Visualizer",
|
64 |
+
description="Visualize embeddings from different models"
|
65 |
+
)
|
66 |
demo.launch()
|
embeddings_plot.png
ADDED