add item
Browse files- app.py +28 -1
- templates/index.html +45 -0
app.py
CHANGED
|
@@ -26,6 +26,9 @@ wmodel = WhisperModel("guillaumekln/faster-whisper-small", device="cpu", compute
|
|
| 26 |
qdrant_api_key = os.environ.get("qdrant_api_key")
|
| 27 |
qdrant_url = os.environ.get("qdrant_url")
|
| 28 |
|
|
|
|
|
|
|
|
|
|
| 29 |
client = QdrantClient(url=qdrant_url, port=443, api_key=qdrant_api_key, prefer_grpc=False)
|
| 30 |
|
| 31 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
@@ -47,6 +50,11 @@ def e5embed(query):
|
|
| 47 |
embeddings = embeddings.cpu().detach().numpy().flatten().tolist()
|
| 48 |
return embeddings
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
@app.route("/")
|
| 51 |
def index():
|
| 52 |
return render_template("index.html")
|
|
@@ -89,7 +97,7 @@ def search():
|
|
| 89 |
for r in results:
|
| 90 |
if 'context' in r.payload and r.payload['context'] != '':
|
| 91 |
if 'date' not in r.payload: r.payload['date'] = '20200101'
|
| 92 |
-
new_results.append({"text": r.payload['title'] + '
|
| 93 |
else:
|
| 94 |
if 'date' not in r.payload: r.payload['date'] = '20200101'
|
| 95 |
new_results.append({"text": r.payload['title'], "url": r.payload['url'], "date": r.payload['date'], "id": r.id})
|
|
@@ -97,6 +105,25 @@ def search():
|
|
| 97 |
# except:
|
| 98 |
# return jsonify([])
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
@app.route("/delete_joke", methods=["POST"])
|
| 101 |
def delete_joke():
|
| 102 |
joke_id = request.form["id"]
|
|
|
|
| 26 |
qdrant_api_key = os.environ.get("qdrant_api_key")
|
| 27 |
qdrant_url = os.environ.get("qdrant_url")
|
| 28 |
|
| 29 |
+
qdrant_api_key = "WaGH94-bo_CzlxTNHFjBAGPvWRhbsWEKUKbMz6YQtYt4oTD1ZXTvwg"
|
| 30 |
+
qdrant_url = "https://c9bee7c7-2bf3-4e1b-8838-2f6f23372ab5.us-east-1-0.aws.cloud.qdrant.io"
|
| 31 |
+
|
| 32 |
client = QdrantClient(url=qdrant_url, port=443, api_key=qdrant_api_key, prefer_grpc=False)
|
| 33 |
|
| 34 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
| 50 |
embeddings = embeddings.cpu().detach().numpy().flatten().tolist()
|
| 51 |
return embeddings
|
| 52 |
|
| 53 |
+
def get_id(collection):
|
| 54 |
+
resp = client.scroll(collection_name=collection, limit=10000, with_payload=True, with_vectors=False,)
|
| 55 |
+
max_id = max([r.id for r in resp[0]])+1
|
| 56 |
+
return int(max_id)
|
| 57 |
+
|
| 58 |
@app.route("/")
|
| 59 |
def index():
|
| 60 |
return render_template("index.html")
|
|
|
|
| 97 |
for r in results:
|
| 98 |
if 'context' in r.payload and r.payload['context'] != '':
|
| 99 |
if 'date' not in r.payload: r.payload['date'] = '20200101'
|
| 100 |
+
new_results.append({"text": r.payload['title'] + '<br>Context: ' + r.payload['context'], "url": r.payload['url'], "date": r.payload['date'], "id": r.id})
|
| 101 |
else:
|
| 102 |
if 'date' not in r.payload: r.payload['date'] = '20200101'
|
| 103 |
new_results.append({"text": r.payload['title'], "url": r.payload['url'], "date": r.payload['date'], "id": r.id})
|
|
|
|
| 105 |
# except:
|
| 106 |
# return jsonify([])
|
| 107 |
|
| 108 |
+
@app.route("/add_item", methods=["POST"])
|
| 109 |
+
def add_item():
|
| 110 |
+
title = request.form["title"]
|
| 111 |
+
url = request.form["url"]
|
| 112 |
+
if url.strip() == '':
|
| 113 |
+
collection_name = 'jks'
|
| 114 |
+
cid = get_id(collection_name)
|
| 115 |
+
print('cid', cid, time.strftime("%Y%m%d"))
|
| 116 |
+
resp = client.upsert(collection_name=collection_name, points=Batch(ids=[cid], payloads=[{'text':title, 'date': time.strftime("%Y%m%d")}],vectors=[e5embed(title)]),)
|
| 117 |
+
else:
|
| 118 |
+
collection_name = 'tils'
|
| 119 |
+
cid = get_id('tils')
|
| 120 |
+
print('cid', cid, time.strftime("%Y%m%d"), collection_name)
|
| 121 |
+
til = {'title': title.replace('TIL that', '').replace('TIL:', '').replace('TIL ', '').strip(), 'url': url.replace('https://', '').replace('http://', ''), "date": time.strftime("%Y%m%d_%H%M")}
|
| 122 |
+
resp = client.upsert(collection_name="tils", points=[PointStruct(id=cid, payload=til, vector={"title": e5embed(til['title']),},)])
|
| 123 |
+
print('Upsert response:', resp)
|
| 124 |
+
return jsonify({"success": True, "index": collection_name})
|
| 125 |
+
|
| 126 |
+
|
| 127 |
@app.route("/delete_joke", methods=["POST"])
|
| 128 |
def delete_joke():
|
| 129 |
joke_id = request.form["id"]
|
templates/index.html
CHANGED
|
@@ -24,6 +24,12 @@
|
|
| 24 |
padding: 10px;
|
| 25 |
}
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
#search {
|
| 28 |
background-color: #4c72af;
|
| 29 |
color: white;
|
|
@@ -33,6 +39,15 @@
|
|
| 33 |
margin-left: 10px;
|
| 34 |
}
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
#results {
|
| 37 |
margin: 20px;
|
| 38 |
}
|
|
@@ -77,6 +92,17 @@
|
|
| 77 |
Drag & Drop Audio File (mp3, wav, ogg, m4v) Here
|
| 78 |
</div>
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
<script>
|
| 81 |
$(document).ready(function () {
|
| 82 |
var collection = "jks";
|
|
@@ -127,6 +153,25 @@
|
|
| 127 |
});
|
| 128 |
}
|
| 129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
function displayTranscriptionResult(transcription) {
|
| 131 |
// Display the transcription result
|
| 132 |
var results = $("#results");
|
|
|
|
| 24 |
padding: 10px;
|
| 25 |
}
|
| 26 |
|
| 27 |
+
#title, #link {
|
| 28 |
+
width: 70%;
|
| 29 |
+
padding: 10px;
|
| 30 |
+
margin-top: 10px;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
#search {
|
| 34 |
background-color: #4c72af;
|
| 35 |
color: white;
|
|
|
|
| 39 |
margin-left: 10px;
|
| 40 |
}
|
| 41 |
|
| 42 |
+
#add {
|
| 43 |
+
background-color: #4c72af;
|
| 44 |
+
color: white;
|
| 45 |
+
border: none;
|
| 46 |
+
padding: 10px 20px;
|
| 47 |
+
cursor: pointer;
|
| 48 |
+
margin-top: 10px;
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
#results {
|
| 52 |
margin: 20px;
|
| 53 |
}
|
|
|
|
| 92 |
Drag & Drop Audio File (mp3, wav, ogg, m4v) Here
|
| 93 |
</div>
|
| 94 |
|
| 95 |
+
<div>
|
| 96 |
+
<label for="title">Title:</label>
|
| 97 |
+
<input type="text" id="title" placeholder="TIL something or the other">
|
| 98 |
+
</div>
|
| 99 |
+
<div>
|
| 100 |
+
<label for="link">Link:</label>
|
| 101 |
+
<input type="text" id="link" placeholder="https://somelink.com">
|
| 102 |
+
</div>
|
| 103 |
+
<button id="add">ADD ITEM</button>
|
| 104 |
+
<div id="add-results"></div>
|
| 105 |
+
|
| 106 |
<script>
|
| 107 |
$(document).ready(function () {
|
| 108 |
var collection = "jks";
|
|
|
|
| 153 |
});
|
| 154 |
}
|
| 155 |
|
| 156 |
+
$('#add').click(function() {
|
| 157 |
+
$('#add-results').append("<h3>Adding item. Hold on!</h3>");
|
| 158 |
+
var title = $('#title').val();
|
| 159 |
+
var url = $('#link').val();
|
| 160 |
+
|
| 161 |
+
$.post('/add_item', {
|
| 162 |
+
title: title,
|
| 163 |
+
url: url
|
| 164 |
+
}, function(data) {
|
| 165 |
+
if(data.success) {
|
| 166 |
+
$('#title').val('');
|
| 167 |
+
$('#link').val('');
|
| 168 |
+
$('#add-results').empty();
|
| 169 |
+
$('#add-results').append("<h3>Successfully added item to " + data.index + "</h3>");
|
| 170 |
+
}
|
| 171 |
+
});
|
| 172 |
+
|
| 173 |
+
});
|
| 174 |
+
|
| 175 |
function displayTranscriptionResult(transcription) {
|
| 176 |
// Display the transcription result
|
| 177 |
var results = $("#results");
|