albhu commited on
Commit
6e0319a
·
verified ·
1 Parent(s): d1efd0b
Files changed (1) hide show
  1. app.py +31 -0
app.py CHANGED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, AutoModelForQuestionAnswering
2
+ from flask import Flask, request, jsonify
3
+ import torch
4
+
5
+ # Modell betöltése
6
+ tokenizer = AutoTokenizer.from_pretrained("nlpaueb/legal-bert-base-uncased")
7
+ model = AutoModelForQuestionAnswering.from_pretrained("nlpaueb/legal-bert-base-uncased")
8
+
9
+ app = Flask(__name__)
10
+
11
+ @app.route("/answer", methods=["POST"])
12
+ def answer():
13
+ data = request.json
14
+ context = data.get("context")
15
+ question = data.get("question")
16
+
17
+ # Tokenizálás és válasz előállítás
18
+ inputs = tokenizer.encode_plus(question, context, return_tensors="pt")
19
+ answer_start_scores, answer_end_scores = model(**inputs).values()
20
+
21
+ # Legjobb válasz kiválasztása
22
+ answer_start = torch.argmax(answer_start_scores)
23
+ answer_end = torch.argmax(answer_end_scores) + 1
24
+ answer = tokenizer.convert_tokens_to_string(
25
+ tokenizer.convert_ids_to_tokens(inputs["input_ids"][0][answer_start:answer_end])
26
+ )
27
+
28
+ return jsonify({"answer": answer})
29
+
30
+ if __name__ == "__main__":
31
+ app.run()