-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
101 lines (80 loc) · 3.31 KB
/
app.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from flask import Flask, request, jsonify, render_template
import psycopg2
from transformers import AutoTokenizer, AutoModel
import torch
from dotenv import load_dotenv
import os
load_dotenv()
app = Flask(__name__)
# Load the pre-trained transformer model for embeddings
tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/all-MiniLM-L6-v2")
model = AutoModel.from_pretrained("sentence-transformers/all-MiniLM-L6-v2")
# Function to generate embeddings
def get_embedding(text):
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
with torch.no_grad():
outputs = model(**inputs)
embedding = outputs.last_hidden_state.mean(dim=1).squeeze().numpy()
return embedding
# Connect to the PostgreSQL database
def get_db_connection():
conn = psycopg2.connect(
dbname=os.getenv("dbname"),
user=os.getenv("user"),
password=os.getenv("password"),
host=os.getenv("host")
)
return conn
@app.route('/insert', methods=['GET', 'POST'])
def insert_documents():
if request.method == 'POST':
# Handle form submission and insert documents
content = request.form['content']
documents = content.splitlines() # Split the input by line breaks to get multiple documents
conn = get_db_connection()
cur = conn.cursor()
# Insert each document with its embedding
for document in documents:
if document.strip(): # Only process non-empty lines
embedding = get_embedding(document)
cur.execute(
"INSERT INTO documents (text, embedding) VALUES (%s, %s)",
(document, embedding.tolist())
)
conn.commit()
cur.close()
conn.close()
return render_template('insert.html', message="Documents inserted successfully!")
# If it's a GET request, show the form
return render_template('insert.html')
# Home route (for the search page and results)
@app.route('/', methods=['GET', 'POST'])
def home():
if request.method == 'POST':
query = request.form.get('query')
if not query:
return render_template('index.html', message="No query provided.")
# Generate the embedding for the query
query_embedding = get_embedding(query)
query_embedding_str = '[' + ','.join(map(str, query_embedding.tolist())) + ']'
# Connect to the PostgreSQL database and search for similar documents
conn = get_db_connection()
cur = conn.cursor()
cur.execute(f"""
SELECT id, text, embedding <=> '{query_embedding_str}'::vector AS distance
FROM documents
ORDER BY embedding <=> '{query_embedding_str}'::vector
LIMIT 5
""")
results = cur.fetchall()
cur.close()
conn.close()
# If no results are found, render the same page with a message
if len(results) == 0:
return render_template('index.html', query=query, message="No similar documents found.")
# Render the page with search results
return render_template('index.html', query=query, results=results)
# If it's a GET request, just render the search form
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)