albhu commited on
Commit
4d48a61
·
verified ·
1 Parent(s): 112973e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -30
app.py CHANGED
@@ -17,53 +17,51 @@ if st.checkbox("Korábbi Beszélgetések Törlése"):
17
  tokenizer = AutoTokenizer.from_pretrained("pile-of-law/legalbert-large-1.7M-2")
18
  model = AutoModelForCausalLM.from_pretrained("apple/OpenELM-3B-Instruct", trust_remote_code=True)
19
 
20
- # Definiáljuk a válaszgeneráló függvényt
21
- def generate_response(input_text, tokenizer, model):
22
- # Bemeneti szöveg tisztítása
23
- cleaned_input = input_text.strip() # Eltávolítjuk a felesleges szóközöket és sortöréseket
24
-
25
- # Tokenizálás
26
- inputs = tokenizer(cleaned_input, return_tensors="pt", max_length=1024)
27
-
28
- # Modell használata a válasz generálásához
29
- outputs = model(**inputs)
30
-
31
- # Válasz visszaadása
32
- response = tokenizer.decode(outputs.logits.argmax(dim=1)[0])
33
- return response
34
-
35
- # Dokumentum feltöltése drag and drop segítségével
36
- document_file = st.file_uploader("Húzd ide a dokumentumot vagy kattints a feltöltéshez", type=["pdf", "docx", "doc"])
37
-
38
- if document_file is not None:
39
  document_text = ""
40
-
41
  if document_file.type == "application/pdf":
42
- # PDF fájl feldolgozása
43
  with pdfplumber.open(document_file) as pdf:
44
  for page in pdf.pages:
45
  text = page.extract_text()
46
  if text:
47
- document_text += text.strip() # Tisztítjuk a kinyert szöveget
48
- document_text += "\n\n" # Új sor hozzáadása a bekezdések közé
49
  elif document_file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
50
- # DOCX fájl feldolgozása
51
  docx_file = docx.Document(document_file)
52
  for paragraph in docx_file.paragraphs:
53
  text = paragraph.text
54
  if text:
55
- document_text += text.strip() # Tisztítjuk a kinyert szöveget
56
- document_text += "\n\n" # Új sor hozzáadása a bekezdések közé
57
  elif document_file.type == "application/msword":
58
- # DOC fájl feldolgozása
59
  doc_file = docx.Document(document_file)
60
  for paragraph in doc_file.paragraphs:
61
  text = paragraph.text
62
  if text:
63
- document_text += text.strip() # Tisztítjuk a kinyert szöveget
64
- document_text += "\n\n" # Új sor hozzáadása a bekezdések közé
65
  else:
66
  st.error("A fájltípus nem támogatott. Kérlek válassz ki egy PDF, DOCX vagy DOC fájlt!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
  # Előző beszélgetésekhez csatolható kontextus
69
  context = st.text_area("Korábbi Beszélgetéshez Tartozó Kontextus", "")
@@ -79,7 +77,7 @@ if document_file is not None:
79
 
80
  # Válasz generálása csak akkor, ha a felhasználó elküldi a promptot
81
  if input_text.strip() != "":
82
- response = generate_response(input_text, tokenizer, model)
83
  st.subheader("Generált Válasz:")
84
  st.write(response)
85
 
 
17
  tokenizer = AutoTokenizer.from_pretrained("pile-of-law/legalbert-large-1.7M-2")
18
  model = AutoModelForCausalLM.from_pretrained("apple/OpenELM-3B-Instruct", trust_remote_code=True)
19
 
20
+ # Dokumentumfeldolgozó függvény
21
+ def process_document(document_file):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  document_text = ""
 
23
  if document_file.type == "application/pdf":
 
24
  with pdfplumber.open(document_file) as pdf:
25
  for page in pdf.pages:
26
  text = page.extract_text()
27
  if text:
28
+ document_text += text.strip() + "\n\n"
 
29
  elif document_file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
 
30
  docx_file = docx.Document(document_file)
31
  for paragraph in docx_file.paragraphs:
32
  text = paragraph.text
33
  if text:
34
+ document_text += text.strip() + "\n\n"
 
35
  elif document_file.type == "application/msword":
 
36
  doc_file = docx.Document(document_file)
37
  for paragraph in doc_file.paragraphs:
38
  text = paragraph.text
39
  if text:
40
+ document_text += text.strip() + "\n\n"
 
41
  else:
42
  st.error("A fájltípus nem támogatott. Kérlek válassz ki egy PDF, DOCX vagy DOC fájlt!")
43
+ return document_text
44
+
45
+ # Válaszgeneráló függvény
46
+ def generate_response(input_text):
47
+ # Bemeneti szöveg tisztítása
48
+ cleaned_input = input_text.strip()
49
+
50
+ # Tokenizálás
51
+ inputs = tokenizer(cleaned_input, return_tensors="pt", max_length=1024)
52
+
53
+ # Modell használata a válasz generálásához
54
+ outputs = model(**inputs)
55
+
56
+ # Válasz visszaadása
57
+ response = tokenizer.decode(outputs.logits.argmax(dim=1)[0])
58
+ return response
59
+
60
+ # Dokumentum feltöltése drag and drop segítségével
61
+ document_file = st.file_uploader("Húzd ide a dokumentumot vagy kattints a feltöltéshez", type=["pdf", "docx", "doc"])
62
+
63
+ if document_file is not None:
64
+ document_text = process_document(document_file)
65
 
66
  # Előző beszélgetésekhez csatolható kontextus
67
  context = st.text_area("Korábbi Beszélgetéshez Tartozó Kontextus", "")
 
77
 
78
  # Válasz generálása csak akkor, ha a felhasználó elküldi a promptot
79
  if input_text.strip() != "":
80
+ response = generate_response(input_text)
81
  st.subheader("Generált Válasz:")
82
  st.write(response)
83