KoonJamesZ commited on
Commit
8d89dbc
·
verified ·
1 Parent(s): 5d50d0b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import faiss
4
+ import numpy as np
5
+ import os
6
+ from sentence_transformers import SentenceTransformer
7
+
8
+ # Load the pre-trained embedding model
9
+ model = SentenceTransformer("Alibaba-NLP/gte-Qwen2-1.5B-instruct", trust_remote_code=True)
10
+
11
+ # Set the max sequence length
12
+ model.max_seq_length = 512
13
+
14
+ # Load the JSON data into a DataFrame
15
+ df = pd.read_json('White-Stride-Red-68.json')
16
+ df['embeding_context'] = df['embeding_context'].astype(str).fillna('')
17
+
18
+ # Filter out any rows where 'embeding_context' might be empty or invalid
19
+ df = df[df['embeding_context'] != '']
20
+
21
+ index = faiss.read_index('vector_store.index')
22
+
23
+
24
+ # Function to perform search and return all columns
25
+ def search_query(query_text):
26
+ num_records = 50
27
+
28
+ # Encode the input query text
29
+ embeddings_query = model.encode([query_text], prompt_name="query")
30
+ embeddings_query_np = np.array(embeddings_query).astype('float32')
31
+
32
+ # Search in FAISS index for nearest neighbors
33
+ distances, indices = index.search(embeddings_query_np, num_records)
34
+
35
+ # Get the top results based on FAISS indices
36
+ result_df = df.iloc[indices[0]].drop(columns=['embeding_context']).drop_duplicates().reset_index(drop=True)
37
+
38
+ return result_df
39
+
40
+ # Gradio interface function
41
+ def gradio_interface(query_text):
42
+ search_results = search_query(query_text)
43
+ return search_results
44
+
45
+ # Gradio interface setup
46
+ with gr.Blocks() as app:
47
+ gr.Markdown("<h1>White Stride Red Search (GTE-Qwen2)</h1>")
48
+
49
+ # Input text box for the search query
50
+ search_input = gr.Textbox(label="Search Query", placeholder="Enter search text", interactive=True)
51
+
52
+ # Output table for displaying results
53
+ search_output = gr.DataFrame(label="Search Results")
54
+
55
+ # Search button
56
+ search_button = gr.Button("Search")
57
+
58
+ # Link button click to action
59
+ search_button.click(fn=gradio_interface, inputs=search_input, outputs=search_output)
60
+
61
+ # Launch the Gradio app
62
+ app.launch()
63
+