|
import gradio as gr |
|
import pandas as pd |
|
import faiss |
|
import numpy as np |
|
import os |
|
from sentence_transformers import SentenceTransformer |
|
|
|
|
|
model = SentenceTransformer("Alibaba-NLP/gte-Qwen2-1.5B-instruct", trust_remote_code=True) |
|
|
|
|
|
model.max_seq_length = 512 |
|
|
|
|
|
df = pd.read_json('White-Stride-Red-68.json') |
|
df['embeding_context'] = df['embeding_context'].astype(str).fillna('') |
|
|
|
|
|
df = df[df['embeding_context'] != ''] |
|
|
|
index = faiss.read_index('vector_store.index') |
|
|
|
|
|
|
|
def search_query(query_text): |
|
num_records = 50 |
|
|
|
|
|
embeddings_query = model.encode([query_text], prompt_name="query") |
|
embeddings_query_np = np.array(embeddings_query).astype('float32') |
|
|
|
|
|
distances, indices = index.search(embeddings_query_np, num_records) |
|
|
|
|
|
result_df = df.iloc[indices[0]].drop(columns=['embeding_context']).drop_duplicates().reset_index(drop=True) |
|
|
|
return result_df |
|
|
|
|
|
def gradio_interface(query_text): |
|
search_results = search_query(query_text) |
|
return search_results |
|
|
|
|
|
with gr.Blocks() as app: |
|
gr.Markdown("<h1>White Stride Red Search (GTE-Qwen2)</h1>") |
|
|
|
|
|
search_input = gr.Textbox(label="Search Query", placeholder="Enter search text", interactive=True) |
|
|
|
|
|
search_output = gr.DataFrame(label="Search Results") |
|
|
|
|
|
search_button = gr.Button("Search") |
|
|
|
|
|
search_button.click(fn=gradio_interface, inputs=search_input, outputs=search_output) |
|
|
|
|
|
app.launch() |
|
|
|
|