Spaces:
No application file
No application file
Commit
·
305a7f0
1
Parent(s):
dfa700f
first
Browse files- DockerFile +11 -0
- app.py +38 -0
- requirements.txt +10 -0
- static/css/styles.css +63 -0
- templates/index.html +34 -0
DockerFile
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
COPY ./requirements.txt /code/requirements.txt
|
6 |
+
|
7 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
8 |
+
|
9 |
+
COPY . .
|
10 |
+
|
11 |
+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify
|
2 |
+
from rank_bm25 import BM25Okapi
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
app = Flask(__name__)
|
6 |
+
|
7 |
+
corpus = [
|
8 |
+
"Hello there good man!",
|
9 |
+
"It is quite windy in London",
|
10 |
+
"How is the weather today?"
|
11 |
+
|
12 |
+
]
|
13 |
+
|
14 |
+
tokenized_corpus = [doc.split(" ") for doc in corpus]
|
15 |
+
|
16 |
+
bm25 = BM25Okapi(tokenized_corpus)
|
17 |
+
|
18 |
+
@app.route('/')
|
19 |
+
def home():
|
20 |
+
return render_template('index.html')
|
21 |
+
|
22 |
+
@app.route('/search', methods=['POST'])
|
23 |
+
def search():
|
24 |
+
query = request.form['query']
|
25 |
+
|
26 |
+
if not query:
|
27 |
+
return render_template('index.html', error='Query is required')
|
28 |
+
|
29 |
+
tokenized_query = query.split(" ")
|
30 |
+
doc_scores = bm25.get_scores(tokenized_query)
|
31 |
+
doc_scores = -np.sort(-np.array(doc_scores))
|
32 |
+
doc_scores=doc_scores[:20]
|
33 |
+
top_n_docs = bm25.get_top_n(tokenized_query, corpus, n=3)
|
34 |
+
|
35 |
+
return render_template('index.html', query=query, doc_scores=doc_scores, top_n_docs=top_n_docs)
|
36 |
+
|
37 |
+
if __name__ == '__main__':
|
38 |
+
app.run(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
faiss-cpu
|
2 |
+
Flask
|
3 |
+
gensim
|
4 |
+
numpy
|
5 |
+
pandas
|
6 |
+
python-dateutil
|
7 |
+
pytz
|
8 |
+
rank-bm25
|
9 |
+
scipy
|
10 |
+
packaging
|
static/css/styles.css
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
body {
|
2 |
+
background-color: skyblue;
|
3 |
+
font-family: Arial, sans-serif;
|
4 |
+
}
|
5 |
+
|
6 |
+
.container {
|
7 |
+
width: 80%;
|
8 |
+
margin: auto;
|
9 |
+
padding: 20px;
|
10 |
+
background-color: white;
|
11 |
+
border-radius: 10px;
|
12 |
+
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
13 |
+
}
|
14 |
+
|
15 |
+
h1 {
|
16 |
+
text-align: center;
|
17 |
+
}
|
18 |
+
|
19 |
+
form {
|
20 |
+
display: flex;
|
21 |
+
align-items: center;
|
22 |
+
justify-content: center;
|
23 |
+
margin-bottom: 20px;
|
24 |
+
}
|
25 |
+
|
26 |
+
label {
|
27 |
+
margin-right: 10px;
|
28 |
+
}
|
29 |
+
|
30 |
+
input[type="text"] {
|
31 |
+
width: 300px;
|
32 |
+
padding: 5px;
|
33 |
+
font-size: 16px;
|
34 |
+
border-radius: 5px;
|
35 |
+
border: 1px solid #ccc;
|
36 |
+
}
|
37 |
+
|
38 |
+
input[type="submit"] {
|
39 |
+
padding: 5px 10px;
|
40 |
+
font-size: 16px;
|
41 |
+
background-color: #007bff;
|
42 |
+
color: white;
|
43 |
+
border: none;
|
44 |
+
border-radius: 5px;
|
45 |
+
cursor: pointer;
|
46 |
+
}
|
47 |
+
|
48 |
+
.results {
|
49 |
+
max-height: 200px;
|
50 |
+
overflow-y: auto;
|
51 |
+
border: 1px solid #ccc;
|
52 |
+
padding: 10px;
|
53 |
+
border-radius: 5px;
|
54 |
+
}
|
55 |
+
|
56 |
+
ul {
|
57 |
+
list-style-type: none;
|
58 |
+
padding: 0;
|
59 |
+
}
|
60 |
+
|
61 |
+
ul li {
|
62 |
+
margin-bottom: 10px;
|
63 |
+
}
|
templates/index.html
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>BM25 Search App</title>
|
7 |
+
<link rel="stylesheet" href="static\css\styles.css">
|
8 |
+
</head>
|
9 |
+
<body>
|
10 |
+
<div class="container">
|
11 |
+
<h1>BM25 Search App</h1>
|
12 |
+
<form action="/search" method="post">
|
13 |
+
<label for="query">Enter your query:</label>
|
14 |
+
<input type="text" id="query" name="query">
|
15 |
+
<input type="submit" value="Search">
|
16 |
+
</form>
|
17 |
+
{% if error %}
|
18 |
+
<p>{{ error }}</p>
|
19 |
+
{% endif %}
|
20 |
+
{% if query %}
|
21 |
+
<h2>Results for "{{ query }}":</h2>
|
22 |
+
<div class="results">
|
23 |
+
<p>BM25 scores: {{ doc_scores }}</p>
|
24 |
+
<h3>Top Documents:</h3>
|
25 |
+
<ul>
|
26 |
+
{% for doc in top_n_docs %}
|
27 |
+
<li>{{ doc }}</li>
|
28 |
+
{% endfor %}
|
29 |
+
</ul>
|
30 |
+
</div>
|
31 |
+
{% endif %}
|
32 |
+
</div>
|
33 |
+
</body>
|
34 |
+
</html>
|