nikolltt commited on
Commit
b7e9319
·
verified ·
1 Parent(s): c931d9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -36
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 datasets import load_dataset
7
 
8
- # 1) Load from Hugging Face dataset
9
- ds = load_dataset("Abirate/english_quotes", split="train")
10
- quotes = [q["quote"] for q in ds.select(range(1000))]
11
- df = pd.DataFrame({"text": quotes})
12
 
13
- # 2) Embeddings + FAISS setup
14
  model = SentenceTransformer("all-MiniLM-L6-v2")
15
- embeddings = model.encode(df["text"].tolist(), convert_to_numpy=True).astype("float32")
16
- faiss.normalize_L2(embeddings)
17
- index = faiss.IndexFlatIP(embeddings.shape[1])
18
- index.add(embeddings)
19
-
20
- # 3) Query function
21
- def get_top3(query):
22
- qv = model.encode([query], convert_to_numpy=True).astype("float32")
23
- faiss.normalize_L2(qv)
24
- D, I = index.search(qv, 3)
25
- return "\n\n".join(f"{i+1}. {df.iloc[idx]['text']}" for i, idx in enumerate(I[0]))
26
-
27
- # 4) Gradio UI
 
 
 
 
 
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 sentence to find 3 semantically similar quotes."
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 fear"],
46
- ["success in life"]
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()