jaydeep123423 commited on
Commit
f3080da
·
verified ·
1 Parent(s): 98cae54

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -0
app.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Semantic Quote Search Engine
3
+ AIPI 510 - Deployed on Hugging Face Spaces, Jaideep
4
+ """
5
+
6
+ import gradio as gr
7
+ from sentence_transformers import SentenceTransformer
8
+ import chromadb
9
+ import os
10
+
11
+ # intialization
12
+
13
+ # Load embedding model
14
+
15
+ model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
16
+ print("Model loaded!")
17
+
18
+ # Load existing ChromaDB (pre-built, not created on the fly)
19
+
20
+ chroma_path = "./chromadb"
21
+ client = chromadb.PersistentClient(path=chroma_path)
22
+ collection = client.get_collection("quotes_collection")
23
+ print(f"Loaded collection with {collection.count()} documents!")
24
+
25
+
26
+ # SEARCH FUNCTION
27
+ def semantic_search(query, n_results=5):
28
+ """
29
+ Perform semantic search over the quotes collection.
30
+ """
31
+ # Encode query using the same model
32
+ query_embedding = model.encode([query])
33
+
34
+ # Query ChromaDB for similar documents
35
+ results = collection.query(
36
+ query_embeddings=query_embedding.tolist(),
37
+ n_results=n_results,
38
+ include=['documents', 'metadatas', 'distances']
39
+ )
40
+
41
+ # Format results nicely
42
+ output = []
43
+ for i in range(len(results['documents'][0])):
44
+ meta = results['metadatas'][0][i]
45
+ distance = results['distances'][0][i]
46
+ similarity = 1 - (distance / 2) # Convert distance to similarity score
47
+
48
+ result_text = f"""
49
+ ### Result {i+1} (Similarity: {similarity:.1%})
50
+
51
+ > "{meta['quote']}"
52
+
53
+ **— {meta['author']}**
54
+
55
+ 🏷️ *Tags: {meta['tags']}*
56
+ """
57
+ output.append(result_text)
58
+
59
+ return "\n---\n".join(output)
60
+
61
+
62
+ def search_quotes(query, num_results):
63
+ """Wrapper function for Gradio interface"""
64
+ if not query.strip():
65
+ return "Please enter a search query!"
66
+ return semantic_search(query, n_results=int(num_results))
67
+
68
+
69
+ # gradio interface
70
+ demo = gr.Interface(
71
+ fn=search_quotes,
72
+ inputs=[
73
+ gr.Textbox(
74
+ label="🔍 Search Query",
75
+ placeholder="Try: 'love', 'success', 'wisdom', 'courage'...",
76
+ lines=2
77
+ ),
78
+ gr.Slider(
79
+ minimum=1,
80
+ maximum=10,
81
+ value=5,
82
+ step=1,
83
+ label=" Number of Results"
84
+ )
85
+ ],
86
+ outputs=gr.Markdown(label=" Search Results"),
87
+ title=" Semantic Quote Search Engine",
88
+ description="""
89
+ ## Search through famous quotes using AI-powered semantic similarity!
90
+
91
+ Unlike traditional keyword search, this understands the **meaning** of your query.
92
+
93
+ **How it works:**
94
+ 1. Your query is converted to a vector using a transformer model
95
+ 2. We find quotes with the most similar meaning in our database
96
+ 3. Results are ranked by semantic similarity
97
+
98
+ *Built for AIPI 510: Data Sourcing for Analytics | Duke University*
99
+ """,
100
+ examples=[
101
+ ["finding happiness in life", 5],
102
+ ["overcoming fear and challenges", 5],
103
+ ["the importance of friendship", 3],
104
+ ["learning from mistakes", 5],
105
+ ["believing in yourself", 3]
106
+ ]
107
+ )
108
+
109
+ # Launch the app
110
+ if __name__ == "__main__":
111
+ demo.launch()