forked from crux82/LocalBioRag
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
76 lines (55 loc) · 1.81 KB
/
app.py
File metadata and controls
76 lines (55 loc) · 1.81 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
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
import os
from functools import wraps
from flask import Flask, render_template, request, jsonify, Response
from retrieval_logic import search_docs, grounded_answ_gen
app = Flask(__name__)
APP_USERNAME = os.getenv("APP_USERNAME", "sagdemo")
APP_PASSWORD = os.getenv("APP_PASSWORD", "Demo2026!")
def check_auth(username, password):
return username == APP_USERNAME and password == APP_PASSWORD
def authenticate():
return Response(
"Authentication required.",
401,
{"WWW-Authenticate": 'Basic realm="LocalBioRAG"'}
)
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if not auth or not check_auth(auth.username, auth.password):
return authenticate()
return f(*args, **kwargs)
return decorated
@app.route('/')
@requires_auth
def index():
return render_template('index.html')
@app.route('/api/search', methods=['POST'])
@requires_auth
def search():
data = request.get_json(silent=True) or {}
query_text = data.get('query', '').strip()
if not query_text:
return jsonify({"error": "Empty query"}), 400
risultati = search_docs(query_text)
return jsonify(risultati)
@app.route('/api/generate_answer', methods=['POST'])
@requires_auth
def generate_answer():
data = request.get_json(silent=True) or {}
query_text = data.get('query', '').strip()
documents = data.get('documents', [])
if not query_text or not documents:
return jsonify({"error": "Missing input data"}), 400
risposta_ai = grounded_answ_gen(query_text, documents)
return jsonify({"answer": risposta_ai})
if __name__ == '__main__':
app.run(
host='0.0.0.0',
port=5000,
debug=False,
use_reloader=False,
threaded=False,
processes=1
)