Spaces:
Runtime error
Runtime error
chore: init
Browse files- app.py +45 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from datasets import load_dataset
|
4 |
+
from sentence_transformers import SentenceTransformer
|
5 |
+
from sentence_transformers.util import semantic_search
|
6 |
+
|
7 |
+
title_dataset = load_dataset("pyimagesearch/blog-title", data_files="bp-title.csv")
|
8 |
+
|
9 |
+
title_embeddings = load_dataset("pyimagesearch/blog-title", data_files="embeddings.csv")
|
10 |
+
title_embeddings = torch.from_numpy(title_embeddings["train"].to_pandas().to_numpy()).to(torch.float)
|
11 |
+
|
12 |
+
model = SentenceTransformer("paraphrase-MiniLM-L6-v2")
|
13 |
+
|
14 |
+
title="Title Semantic Search"
|
15 |
+
description="Provide a blog post title, and we'll find the most similar titles from our already written blog posts."
|
16 |
+
|
17 |
+
examples=[
|
18 |
+
"Introduction to Keras",
|
19 |
+
"Conditional GANs with Keras",
|
20 |
+
"A Gentle Introduction to PyTorch with Deep Learning",
|
21 |
+
]
|
22 |
+
|
23 |
+
def get_titles(query):
|
24 |
+
query_embed = model.encode(query)
|
25 |
+
hits = semantic_search(query_embed, title_embeddings, top_k=5)[0]
|
26 |
+
|
27 |
+
titles = list()
|
28 |
+
|
29 |
+
for hit in hits:
|
30 |
+
index = hit["corpus_id"]
|
31 |
+
selected_title = title_dataset["train"]["title"][index]
|
32 |
+
# score = hit["score"]
|
33 |
+
titles.append(selected_title)
|
34 |
+
return "\n".join(titles)
|
35 |
+
|
36 |
+
space = gr.Interface(
|
37 |
+
fn=get_titles,
|
38 |
+
inputs=gr.Textbox(label="Input Title"),
|
39 |
+
outputs=gr.Textbox(label="Similar Titles"),
|
40 |
+
title=title,
|
41 |
+
description=description,
|
42 |
+
examples=examples,
|
43 |
+
)
|
44 |
+
|
45 |
+
space.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
sentence-transformers
|
2 |
+
datasets
|
3 |
+
torch
|