Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,43 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
-
import numpy as np
|
| 3 |
-
from sentence_transformers import SentenceTransformer
|
| 4 |
-
import faiss
|
| 5 |
import gradio as gr
|
| 6 |
-
from
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
quotes = [
|
| 11 |
-
df = pd.DataFrame({"text": quotes})
|
| 12 |
|
| 13 |
-
#
|
| 14 |
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
demo = gr.Interface(
|
| 29 |
fn=get_top3,
|
| 30 |
inputs=gr.Textbox(lines=2, placeholder="Type something..."),
|
| 31 |
outputs="text",
|
| 32 |
title="Quote Finder (1,000+ quotes)",
|
| 33 |
-
description="Enter any
|
| 34 |
-
)
|
| 35 |
-
|
| 36 |
-
if __name__ == "__main__":
|
| 37 |
-
demo = gr.Interface(
|
| 38 |
-
fn=get_top3,
|
| 39 |
-
inputs=gr.Textbox(lines=2, placeholder="Type something..."),
|
| 40 |
-
outputs="text",
|
| 41 |
-
title="Quote Finder (1,000+ quotes)",
|
| 42 |
-
description="Enter any sentence to find 3 semantically similar quotes.",
|
| 43 |
examples=[
|
| 44 |
["happiness"],
|
| 45 |
-
["overcoming
|
| 46 |
-
["
|
| 47 |
]
|
| 48 |
)
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
|
|
|
| 1 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
from sentence_transformers import SentenceTransformer, util
|
| 4 |
|
| 5 |
+
# Load dataset
|
| 6 |
+
df = pd.read_csv("quotes.csv")
|
| 7 |
+
quotes = df["quote"].tolist()
|
|
|
|
| 8 |
|
| 9 |
+
# Load embedding model
|
| 10 |
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 11 |
+
quote_embeddings = model.encode(quotes, convert_to_tensor=True)
|
| 12 |
+
|
| 13 |
+
# Function to find top 3 similar quotes
|
| 14 |
+
def get_top3(user_input):
|
| 15 |
+
if not user_input.strip():
|
| 16 |
+
return "Please enter a search phrase."
|
| 17 |
+
|
| 18 |
+
input_embedding = model.encode(user_input, convert_to_tensor=True)
|
| 19 |
+
similarities = util.pytorch_cos_sim(input_embedding, quote_embeddings)[0]
|
| 20 |
+
top_results = similarities.argsort(descending=True)[:3]
|
| 21 |
+
|
| 22 |
+
output_quotes = []
|
| 23 |
+
for idx in top_results:
|
| 24 |
+
output_quotes.append(f"- {quotes[idx]}")
|
| 25 |
+
|
| 26 |
+
return "\n".join(output_quotes)
|
| 27 |
+
|
| 28 |
+
# Create Gradio interface
|
| 29 |
demo = gr.Interface(
|
| 30 |
fn=get_top3,
|
| 31 |
inputs=gr.Textbox(lines=2, placeholder="Type something..."),
|
| 32 |
outputs="text",
|
| 33 |
title="Quote Finder (1,000+ quotes)",
|
| 34 |
+
description="Enter any topic or phrase to find 3 semantically similar quotes from our dataset.",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
examples=[
|
| 36 |
["happiness"],
|
| 37 |
+
["overcoming failure"],
|
| 38 |
+
["friendship"]
|
| 39 |
]
|
| 40 |
)
|
| 41 |
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
demo.launch()
|