File size: 1,754 Bytes
663d1ad |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import gradio as gr
import numpy as np
import h5py
import faiss
import json
from sentence_transformers import SentenceTransformer
# Load embeddings and metadata
def load_data():
with h5py.File('patent_embeddings.h5', 'r') as f:
embeddings = f['embeddings'][:]
patent_numbers = f['patent_numbers'][:]
metadata = {}
with open('patent_metadata.jsonl', 'r') as f:
for line in f:
data = json.loads(line)
metadata[data['patent_number']] = data
return embeddings, patent_numbers, metadata
embeddings, patent_numbers, metadata = load_data()
# Create FAISS index
index = faiss.IndexFlatL2(embeddings.shape[1])
index.add(embeddings)
# Load BERT model for encoding search queries
model = SentenceTransformer('all-MiniLM-L6-v2')
def search(query, top_k=5):
# Encode the query
query_embedding = model.encode([query])[0]
# Perform similarity search
distances, indices = index.search(np.array([query_embedding]), top_k)
results = []
for i, idx in enumerate(indices[0]):
patent_number = patent_numbers[idx]
patent_data = metadata[patent_number]
result = f"Patent Number: {patent_number}\n"
result += f"Abstract: {patent_data['abstract'][:200]}...\n"
result += f"Similarity Score: {1 - distances[0][i]:.4f}\n\n"
results.append(result)
return "\n".join(results)
# Create Gradio interface
iface = gr.Interface(
fn=search,
inputs=gr.Textbox(lines=2, placeholder="Enter your search query here..."),
outputs=gr.Textbox(lines=10, label="Search Results"),
title="Patent Similarity Search",
description="Enter a query to find similar patents based on their embeddings."
)
iface.launch()
|