Jaswanth-0821 commited on
Commit
dab52ad
·
verified ·
1 Parent(s): 9b9bf8e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from sentence_transformers import SentenceTransformer
3
+ import torch
4
+
5
+ # Load the model
6
+ model = SentenceTransformer("Tarka-AIR/Tarka-Embedding-150M-V1")
7
+
8
+ def find_similar_documents(query, documents):
9
+ if not query.strip():
10
+ return "Please enter a query."
11
+ if not documents.strip():
12
+ return "Please enter documents (one per line)."
13
+
14
+ # Split documents by lines
15
+ doc_list = [doc.strip() for doc in documents.split('\n') if doc.strip()]
16
+
17
+ if not doc_list:
18
+ return "Please enter at least one document."
19
+
20
+ # Encode query and documents
21
+ query_embeddings = model.encode_query(query)
22
+ document_embeddings = model.encode_document(doc_list)
23
+
24
+ # Compute similarities
25
+ similarities = model.similarity(query_embeddings, document_embeddings)
26
+
27
+ # Sort documents by similarity score
28
+ sorted_indices = torch.argsort(similarities[0], descending=True)
29
+
30
+ # Format results
31
+ results = []
32
+ for i, idx in enumerate(sorted_indices):
33
+ score = similarities[0][idx].item()
34
+ doc = doc_list[idx]
35
+ results.append(f"{i+1}. Score: {score:.4f}\n Document: {doc}")
36
+
37
+ return "\n\n".join(results)
38
+
39
+ with gr.Blocks(title="Document Similarity Search", theme=gr.themes.Default(primary_hue="blue", secondary_hue="indigo", neutral_hue="zinc", font=[gr.themes.GoogleFont("Roboto"), "Arial", "sans-serif"])) as demo:
40
+ gr.Markdown("# Document Similarity Search")
41
+ gr.Markdown("Find the most relevant documents for your query using Google's Embedding Gemma model.")
42
+
43
+ with gr.Row():
44
+ with gr.Column(scale=1):
45
+ query_input = gr.Textbox(
46
+ label="Query",
47
+ placeholder="Enter your search query...",
48
+ lines=2
49
+ )
50
+
51
+ documents_input = gr.Textbox(
52
+ label="Documents",
53
+ placeholder="Enter documents (one per line)...",
54
+ lines=10
55
+ )
56
+
57
+ search_btn = gr.Button("Search", variant="primary")
58
+
59
+ with gr.Column(scale=1):
60
+ output = gr.Textbox(
61
+ label="Results",
62
+ lines=15,
63
+ show_copy_button=True
64
+ )
65
+
66
+ # Example usage
67
+ gr.Examples(
68
+ examples=[
69
+ [
70
+ "Which planet is known as the Red Planet?",
71
+ "Venus is often called Earth's twin because of its similar size and proximity.\nMars, known for its reddish appearance, is often referred to as the Red Planet.\nJupiter, the largest planet in our solar system, has a prominent red spot.\nSaturn, famous for its rings, is sometimes mistaken for the Red Planet."
72
+ ],
73
+ [
74
+ "What causes seasons on Earth?",
75
+ "The tilt of Earth's axis causes different parts of the planet to receive varying amounts of sunlight throughout the year.\nThe moon's gravitational pull affects ocean tides but not seasons.\nEarth's orbit around the sun is slightly elliptical, but this has minimal effect on seasons.\nThe rotation of Earth on its axis causes day and night cycles."
76
+ ]
77
+ ],
78
+ inputs=[query_input, documents_input]
79
+ )
80
+
81
+ search_btn.click(
82
+ fn=find_similar_documents,
83
+ inputs=[query_input, documents_input],
84
+ outputs=output
85
+ )
86
+
87
+ # Allow Enter key to trigger search
88
+ query_input.submit(
89
+ fn=find_similar_documents,
90
+ inputs=[query_input, documents_input],
91
+ outputs=output
92
+ )
93
+
94
+ demo.launch()