Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from sentence_transformers import SentenceTransformer
|
| 4 |
+
import faiss
|
| 5 |
+
|
| 6 |
+
# 1. Dataset — replace or load from CSV
|
| 7 |
+
texts = [
|
| 8 |
+
"Creativity is intelligence having fun.",
|
| 9 |
+
"Simplicity is the ultimate sophistication.",
|
| 10 |
+
"Innovation distinguishes between a leader and a follower.",
|
| 11 |
+
# Add more...
|
| 12 |
+
]
|
| 13 |
+
df = pd.DataFrame({"text": texts})
|
| 14 |
+
|
| 15 |
+
# 2. Embeddings + FAISS index
|
| 16 |
+
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 17 |
+
emb = model.encode(df["text"].tolist(), convert_to_numpy=True).astype("float32")
|
| 18 |
+
faiss.normalize_L2(emb)
|
| 19 |
+
index = faiss.IndexFlatIP(emb.shape[1])
|
| 20 |
+
index.add(emb)
|
| 21 |
+
|
| 22 |
+
# 3. Search function
|
| 23 |
+
def search(query):
|
| 24 |
+
qv = model.encode([query], convert_to_numpy=True).astype("float32")
|
| 25 |
+
faiss.normalize_L2(qv)
|
| 26 |
+
D, I = index.search(qv, 3) # top 3
|
| 27 |
+
return "\n".join([df.iloc[i]["text"] for i in I[0]])
|
| 28 |
+
|
| 29 |
+
# 4. Gradio UI
|
| 30 |
+
demo = gr.Interface(
|
| 31 |
+
fn=search,
|
| 32 |
+
inputs=gr.Textbox(lines=2, placeholder="Type something..."),
|
| 33 |
+
outputs="text",
|
| 34 |
+
title="Mini Quote Finder",
|
| 35 |
+
description="Type a phrase, get similar quotes."
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
demo.launch()
|