Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
import chromadb
|
3 |
from transformers import AutoTokenizer, AutoModel
|
4 |
-
import faiss
|
5 |
import numpy as np
|
6 |
import torch
|
7 |
|
@@ -18,56 +17,7 @@ collection = client.create_collection(name="tree_images")
|
|
18 |
|
19 |
# Custom dataset of tree descriptions (both decorated and undecorated)
|
20 |
content = [
|
21 |
-
|
22 |
-
"Tree 2: Undecorated, only a bare tree with no lights or ornaments",
|
23 |
-
"Tree 3: Decorated with silver tinsel and baubles",
|
24 |
-
"Tree 4: Undecorated, only green branches",
|
25 |
-
"Tree 5: Decorated with red ribbons and golden ornaments",
|
26 |
-
"Tree 6: Undecorated, a plain pine tree",
|
27 |
-
"Tree 7: Decorated with multicolored lights and silver bells",
|
28 |
-
"Tree 8: Undecorated, just a tree with no decorations",
|
29 |
-
"Tree 9: Decorated with handmade ornaments and garlands",
|
30 |
-
"Tree 10: Undecorated, a simple tree without any adornment",
|
31 |
-
"Tree 11: Decorated with blue and white lights and a snowflake theme",
|
32 |
-
"Tree 12: Undecorated, only branches with no adornments",
|
33 |
-
"Tree 13: Decorated with red and green ornaments and candy canes",
|
34 |
-
"Tree 14: Undecorated, just a tall and natural-looking tree",
|
35 |
-
"Tree 15: Decorated with silver garlands and a star topper",
|
36 |
-
"Tree 16: Undecorated, a bare spruce tree",
|
37 |
-
"Tree 17: Decorated with gold and red ornaments, and a snowman figure",
|
38 |
-
"Tree 18: Undecorated, a simple fir tree with no extras",
|
39 |
-
"Tree 19: Decorated with colorful LED lights and a bow on top",
|
40 |
-
"Tree 20: Undecorated, just a bare tree with no lights or baubles",
|
41 |
-
"Tree 21: Decorated with small fairy lights and a red ribbon",
|
42 |
-
"Tree 22: Undecorated, just a simple tree with green branches",
|
43 |
-
"Tree 23: Decorated with golden stars and white snowflakes",
|
44 |
-
"Tree 24: Undecorated, just a natural green pine tree",
|
45 |
-
"Tree 25: Decorated with pink ornaments and a gold topper",
|
46 |
-
"Tree 26: Undecorated, no decorations, just a plain tree",
|
47 |
-
"Tree 27: Decorated with silver and blue ornaments and a festive ribbon",
|
48 |
-
"Tree 28: Undecorated, just a fresh pine tree",
|
49 |
-
"Tree 29: Decorated with white fairy lights and a Christmas angel on top",
|
50 |
-
"Tree 30: Undecorated, only the green foliage of the tree",
|
51 |
-
"Tree 31: Decorated with bright red ornaments and golden tinsel",
|
52 |
-
"Tree 32: Undecorated, no lights or decorations, just branches",
|
53 |
-
"Tree 33: Decorated with silver tinsel, green ribbons, and a star",
|
54 |
-
"Tree 34: Undecorated, plain with no adornments",
|
55 |
-
"Tree 35: Decorated with red and white ornaments, and a Santa figurine",
|
56 |
-
"Tree 36: Undecorated, just a bare tree",
|
57 |
-
"Tree 37: Decorated with rainbow lights and colorful ornaments",
|
58 |
-
"Tree 38: Undecorated, a simple evergreen tree with no additions",
|
59 |
-
"Tree 39: Decorated with small golden bells and a red bow",
|
60 |
-
"Tree 40: Undecorated, no ornaments or lights",
|
61 |
-
"Tree 41: Decorated with silver baubles, white snowflakes, and a red star",
|
62 |
-
"Tree 42: Undecorated, just a natural tree with no accessories",
|
63 |
-
"Tree 43: Decorated with multicolor ribbons and white angel decorations",
|
64 |
-
"Tree 44: Undecorated, no adornments, just the tree itself",
|
65 |
-
"Tree 45: Decorated with large silver baubles and a golden star",
|
66 |
-
"Tree 46: Undecorated, no decorations, just a green tree",
|
67 |
-
"Tree 47: Decorated with white snowflakes, red ribbons, and a Santa hat",
|
68 |
-
"Tree 48: Undecorated, a bare tree with no lights or ornaments",
|
69 |
-
"Tree 49: Decorated with small lights and star-shaped ornaments",
|
70 |
-
"Tree 50: Undecorated, only the tree, no adornments or lights"
|
71 |
]
|
72 |
|
73 |
# Function to generate embeddings using the pre-trained model
|
@@ -83,33 +33,24 @@ def generate_embeddings(texts):
|
|
83 |
# Generate embeddings for the content
|
84 |
embeddings = generate_embeddings(content)
|
85 |
|
86 |
-
# Add the embeddings to Chroma
|
87 |
for idx, text in enumerate(content):
|
88 |
-
collection.
|
89 |
documents=[text], # the document (text) itself
|
90 |
metadatas=[{"id": idx}], # metadata associated with the document
|
91 |
embeddings=[embeddings[idx]] # the corresponding embeddings for the document
|
92 |
)
|
93 |
|
94 |
-
# Build FAISS index for efficient retrieval
|
95 |
-
embeddings_np = np.array(embeddings).astype('float32')
|
96 |
-
faiss_index = faiss.IndexFlatL2(embeddings_np.shape[1])
|
97 |
-
faiss_index.add(embeddings_np)
|
98 |
-
|
99 |
# Define the search function for Gradio interface
|
100 |
def search(query):
|
101 |
# Generate embedding for the query
|
102 |
query_embedding = generate_embeddings([query])[0].reshape(1, -1)
|
103 |
|
104 |
-
# FAISS-based search
|
105 |
-
distances, indices = faiss_index.search(query_embedding, 3)
|
106 |
-
faiss_results = [content[i] for i in indices[0]]
|
107 |
-
|
108 |
# Chroma-based search
|
109 |
chroma_results = collection.query(query_embeddings=query_embedding, n_results=3)["documents"]
|
110 |
|
111 |
# Return results
|
112 |
-
return "
|
113 |
|
114 |
# Create the Gradio interface
|
115 |
interface = gr.Interface(fn=search, inputs="text", outputs="text")
|
|
|
1 |
import gradio as gr
|
2 |
import chromadb
|
3 |
from transformers import AutoTokenizer, AutoModel
|
|
|
4 |
import numpy as np
|
5 |
import torch
|
6 |
|
|
|
17 |
|
18 |
# Custom dataset of tree descriptions (both decorated and undecorated)
|
19 |
content = [
|
20 |
+
# Your tree descriptions here...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
]
|
22 |
|
23 |
# Function to generate embeddings using the pre-trained model
|
|
|
33 |
# Generate embeddings for the content
|
34 |
embeddings = generate_embeddings(content)
|
35 |
|
36 |
+
# Add the embeddings to Chroma using upsert
|
37 |
for idx, text in enumerate(content):
|
38 |
+
collection.upsert(
|
39 |
documents=[text], # the document (text) itself
|
40 |
metadatas=[{"id": idx}], # metadata associated with the document
|
41 |
embeddings=[embeddings[idx]] # the corresponding embeddings for the document
|
42 |
)
|
43 |
|
|
|
|
|
|
|
|
|
|
|
44 |
# Define the search function for Gradio interface
|
45 |
def search(query):
|
46 |
# Generate embedding for the query
|
47 |
query_embedding = generate_embeddings([query])[0].reshape(1, -1)
|
48 |
|
|
|
|
|
|
|
|
|
49 |
# Chroma-based search
|
50 |
chroma_results = collection.query(query_embeddings=query_embedding, n_results=3)["documents"]
|
51 |
|
52 |
# Return results
|
53 |
+
return "Chroma Results: " + ", ".join(chroma_results)
|
54 |
|
55 |
# Create the Gradio interface
|
56 |
interface = gr.Interface(fn=search, inputs="text", outputs="text")
|