Shortly before finishing
This commit is contained in:
parent
6c1d6762b5
commit
8ec5eb69ab
15 changed files with 511 additions and 15 deletions
server-implementation
51
server-implementation/app.py
Normal file
51
server-implementation/app.py
Normal file
|
@ -0,0 +1,51 @@
|
|||
import pathlib
|
||||
from gevent.pywsgi import WSGIServer
|
||||
from flask import Flask, request, render_template
|
||||
from tabulate import tabulate
|
||||
from wtforms import Form, StringField, SubmitField
|
||||
from wtforms.validators import DataRequired
|
||||
|
||||
from backend import AIBackend
|
||||
|
||||
app = Flask("xkcd_retriever", template_folder=pathlib.Path(__file__).parent / "templates")
|
||||
|
||||
print("Initializing backend")
|
||||
backend = AIBackend()
|
||||
|
||||
# backend.warmup() # Only needed when there is no data in the pgvector database
|
||||
backend._ready = True
|
||||
print("AI backend initialized ...")
|
||||
|
||||
class BasicForm(Form):
|
||||
ids = StringField("ID",validators=[DataRequired()])
|
||||
submit = SubmitField("Submit")
|
||||
|
||||
@app.route("/",methods =['POST','GET'])
|
||||
def main():
|
||||
form = BasicForm()
|
||||
return render_template("index.html", form = form)
|
||||
|
||||
@app.route("/search",methods =['POST','GET'])
|
||||
def results():
|
||||
topic = request.form.get('ids')
|
||||
|
||||
result = backend.query(f"Give an example for {topic}")
|
||||
|
||||
headers = ["ID", "Title", "Link"]
|
||||
print(tabulate(backend.format_result(result), headers=headers, tablefmt="github"))
|
||||
|
||||
res = backend.format_for_api(result)
|
||||
return render_template("results.html", results=res, topic=topic)
|
||||
|
||||
@app.route("/find")
|
||||
def query_backend():
|
||||
topic = request.args.get('topic')
|
||||
result = backend.query(f"Give an example for {topic}")
|
||||
|
||||
headers = ["ID", "Title", "Link"]
|
||||
print(tabulate(backend.format_result(result), headers=headers, tablefmt="github"))
|
||||
|
||||
return backend.format_for_api(result)
|
||||
|
||||
http_server = WSGIServer(("0.0.0.0", 8000), app)
|
||||
http_server.serve_forever()
|
Loading…
Add table
Add a link
Reference in a new issue