-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
39 lines (32 loc) · 1.05 KB
/
app.py
File metadata and controls
39 lines (32 loc) · 1.05 KB
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
from flask import Flask, request, render_template
from sentence_transformers import SentenceTransformer
import faiss
import numpy as np
from werkzeug.utils import secure_filename
import os
app = Flask(__name__)
# Load model and documents
model = SentenceTransformer('all-MiniLM-L6-v2')
documents = [
"Legal contract about property rights",
"Court ruling on employment disputes",
"Patent law regarding intellectual property",
"Legal framework for data protection"
]
# Encode and index documents
vectors = model.encode(documents)
dimension = vectors.shape[1]
index = faiss.IndexFlatL2(dimension)
index.add(np.array(vectors))
@app.route("/", methods=["GET", "POST"])
def search():
results = []
query = ""
if request.method == "POST":
query = request.form["query"]
query_vector = model.encode([query])
D, I = index.search(np.array(query_vector), k=3)
results = [documents[i] for i in I[0]]
return render_template("index.html", query=query, results=results)
if __name__ == "__main__":
app.run(debug=True)