Spaces:
Sleeping
Sleeping
wjm55
commited on
Commit
·
bd8c1d3
1
Parent(s):
fdcc3a0
init
Browse files- app.py +84 -4
- requirements.txt +4 -0
app.py
CHANGED
@@ -1,7 +1,87 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from sentence_transformers import SentenceTransformer
|
3 |
+
import weaviate
|
4 |
+
from weaviate.classes.init import Auth
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
import os
|
7 |
|
8 |
+
# Load environment variables
|
9 |
+
load_dotenv()
|
10 |
|
11 |
+
# Initialize the model
|
12 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
13 |
+
|
14 |
+
# Initialize Weaviate client
|
15 |
+
client = weaviate.connect_to_weaviate_cloud(
|
16 |
+
cluster_url=os.getenv("WEAVIATE_URL"),
|
17 |
+
auth_credentials=Auth.api_key(os.getenv("WEAVIATE_API_KEY"))
|
18 |
+
)
|
19 |
+
|
20 |
+
lux_collection = client.collections.get("LuxData")
|
21 |
+
|
22 |
+
def search_images(query):
|
23 |
+
# Encode the query and search
|
24 |
+
query_vector = model.encode(query)
|
25 |
+
results = lux_collection.query.near_vector(
|
26 |
+
near_vector=query_vector.tolist(),
|
27 |
+
limit=20
|
28 |
+
)
|
29 |
+
|
30 |
+
# Format results for display
|
31 |
+
formatted_results = []
|
32 |
+
for result in results.objects:
|
33 |
+
formatted_results.append({
|
34 |
+
"label": result.properties['label'],
|
35 |
+
"lux_url": result.properties['lux_url'],
|
36 |
+
"image_url": result.properties.get('image_url', "No image available")
|
37 |
+
})
|
38 |
+
|
39 |
+
return formatted_results
|
40 |
+
|
41 |
+
# Create Gradio interface
|
42 |
+
def create_interface():
|
43 |
+
with gr.Blocks() as demo:
|
44 |
+
gr.Markdown("# Lux Image Search")
|
45 |
+
|
46 |
+
with gr.Row():
|
47 |
+
query_input = gr.Textbox(label="Enter your search query")
|
48 |
+
search_button = gr.Button("Search")
|
49 |
+
|
50 |
+
results_table = gr.HTML(label="Search Results")
|
51 |
+
|
52 |
+
def on_search(query):
|
53 |
+
results = search_images(query)
|
54 |
+
# Create HTML table with images
|
55 |
+
html = """
|
56 |
+
<table style="width:100%; border-collapse: collapse;">
|
57 |
+
<tr>
|
58 |
+
<th style="border: 1px solid #ddd; padding: 8px;">Label</th>
|
59 |
+
<th style="border: 1px solid #ddd; padding: 8px;">Lux URL</th>
|
60 |
+
<th style="border: 1px solid #ddd; padding: 8px;">Image</th>
|
61 |
+
</tr>
|
62 |
+
"""
|
63 |
+
|
64 |
+
for r in results:
|
65 |
+
image_cell = f'<img src="{r["image_url"]}" style="max-width:200px; max-height:200px;">' if r["image_url"] and r["image_url"] != "No image available" else "No image available"
|
66 |
+
html += f"""
|
67 |
+
<tr>
|
68 |
+
<td style="border: 1px solid #ddd; padding: 8px;">{r["label"]}</td>
|
69 |
+
<td style="border: 1px solid #ddd; padding: 8px;"><a href="{r["lux_url"]}" target="_blank">{r["lux_url"]}</a></td>
|
70 |
+
<td style="border: 1px solid #ddd; padding: 8px;">{image_cell}</td>
|
71 |
+
</tr>
|
72 |
+
"""
|
73 |
+
|
74 |
+
html += "</table>"
|
75 |
+
return html
|
76 |
+
|
77 |
+
search_button.click(
|
78 |
+
fn=on_search,
|
79 |
+
inputs=query_input,
|
80 |
+
outputs=results_table
|
81 |
+
)
|
82 |
+
|
83 |
+
return demo
|
84 |
+
|
85 |
+
if __name__ == "__main__":
|
86 |
+
demo = create_interface()
|
87 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
weaviate-client
|
2 |
+
python-dotenv
|
3 |
+
tqdm
|
4 |
+
srsly
|