bhlewis's picture
Create app.py
663d1ad verified
raw
history blame
1.75 kB
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()