albhu commited on
Commit
e00a4eb
·
verified ·
1 Parent(s): 6e0319a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -3
app.py CHANGED
@@ -1,11 +1,15 @@
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"])
@@ -27,5 +31,55 @@ def answer():
27
 
28
  return jsonify({"answer": answer})
29
 
30
- if __name__ == "__main__":
31
- app.run()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import threading
4
  from flask import Flask, request, jsonify
5
+ from transformers import AutoTokenizer, AutoModelForQuestionAnswering
6
  import torch
7
 
8
  # Modell betöltése
9
  tokenizer = AutoTokenizer.from_pretrained("nlpaueb/legal-bert-base-uncased")
10
  model = AutoModelForQuestionAnswering.from_pretrained("nlpaueb/legal-bert-base-uncased")
11
 
12
+ # Flask API létrehozása háttérfolyamatként
13
  app = Flask(__name__)
14
 
15
  @app.route("/answer", methods=["POST"])
 
31
 
32
  return jsonify({"answer": answer})
33
 
34
+ def run_flask_app():
35
+ app.run(port=5000)
36
+
37
+ # Flask szerver indítása háttérszálon
38
+ flask_thread = threading.Thread(target=run_flask_app)
39
+ flask_thread.start()
40
+
41
+ # Streamlit alkalmazás
42
+ def split_text_into_chunks(text, max_length=512):
43
+ paragraphs = text.split("\n\n")
44
+ chunks = []
45
+ current_chunk = ""
46
+
47
+ for paragraph in paragraphs:
48
+ if len(current_chunk) + len(paragraph) <= max_length:
49
+ current_chunk += paragraph + "\n\n"
50
+ else:
51
+ chunks.append(current_chunk)
52
+ current_chunk = paragraph + "\n\n"
53
+
54
+ if current_chunk:
55
+ chunks.append(current_chunk)
56
+
57
+ return chunks
58
+
59
+ st.title("AI Jogi Chatbot")
60
+
61
+ # Dokumentum feltöltése
62
+ uploaded_file = st.file_uploader("Töltsön fel egy dokumentumot", type=["txt", "pdf"])
63
+
64
+ if uploaded_file:
65
+ context = uploaded_file.read().decode("utf-8") # Szöveg kinyerése
66
+ chunks = split_text_into_chunks(context)
67
+ st.write(f"A dokumentum {len(chunks)} részre bontva.")
68
+
69
+ # Felhasználói kérdés
70
+ question = st.text_input("Írja be a kérdését a dokumentumról:")
71
+
72
+ if question:
73
+ answers = []
74
+ for i, chunk in enumerate(chunks):
75
+ response = requests.post(
76
+ "http://localhost:5000/answer",
77
+ json={"context": chunk, "question": question},
78
+ )
79
+ if response.status_code == 200:
80
+ answer = response.json().get("answer")
81
+ answers.append(f"Rész {i+1}: {answer}")
82
+
83
+ st.write("Válaszok:")
84
+ for ans in answers:
85
+ st.write(ans)