albhu commited on
Commit
496acc7
·
verified ·
1 Parent(s): 2ce8295

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -42
app.py CHANGED
@@ -24,56 +24,43 @@ model = AutoModelForCausalLM.from_pretrained(generator_name, trust_remote_code=T
24
  document_file = st.file_uploader("Húzd ide a dokumentumot vagy kattints a feltöltéshez", type=["pdf", "docx", "doc"])
25
 
26
  if document_file is not None:
27
- # Dokumentum méretének ellenőrzése és szeletekre bontása
28
- document_chunks = []
29
- if document_file.type == "application/pdf":
30
- with pdfplumber.open(document_file) as pdf:
31
- for page in pdf.pages:
32
- document_chunks.append(page.extract_text())
33
- elif document_file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
34
- docx_file = docx.Document(document_file)
35
- document_text = ""
36
- for paragraph in docx_file.paragraphs:
37
- document_chunks.append(paragraph.text)
38
- elif document_file.type == "application/msword":
39
- doc_file = docx.Document(document_file)
40
  document_text = ""
41
- for paragraph in doc_file.paragraphs:
42
- document_chunks.append(paragraph.text)
43
- else:
44
- st.error("A fájltípus nem támogatott. Kérlek válassz ki egy PDF, DOCX vagy DOC fájlt!")
45
 
46
- # Előző beszélgetésekhez csatolható kontextus
47
- context = st.text_area("Korábbi Beszélgetéshez Tartozó Kontextus", "")
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- # Kérdés mező hozzáadása
50
- question = st.text_input("Kérdés a Dokumentumból", "")
51
-
52
- # Válaszgenerálás
53
- if st.button("Generálj Választ"):
54
- generated_responses = []
55
- for chunk in document_chunks:
56
- if context:
57
- input_text = f"{context} {chunk}"
58
- else:
59
- input_text = chunk
60
 
61
- # Ha van kérdés, azt is hozzáadjuk a bemenethez
62
- if question:
63
- input_text += f" {question}"
64
 
65
- # Model használata a válasz generálásához
66
- response = generate_response(input_text, tokenizer, model)
67
- generated_responses.append(response)
 
 
68
 
69
- # Összefésüljük a válaszokat egyetlen szöveggé
70
- final_response = " ".join(generated_responses)
71
 
72
  # Válasz megjelenítése
73
  st.subheader("Generált Válasz:")
74
- st.write(final_response)
75
 
76
  # Aktuális beszélgetés hozzáadása az előző beszélgetésekhez
77
- st.session_state.previous_conversations.append({"input_text": input_text, "response": final_response})
78
- else:
79
- st.warning("Kérlek válassz ki egy dokumentumfájlt!")
 
24
  document_file = st.file_uploader("Húzd ide a dokumentumot vagy kattints a feltöltéshez", type=["pdf", "docx", "doc"])
25
 
26
  if document_file is not None:
27
+ # Válaszgenerálás
28
+ if st.button("Generálj Választ"):
 
 
 
 
 
 
 
 
 
 
 
29
  document_text = ""
 
 
 
 
30
 
31
+ if document_file.type == "application/pdf":
32
+ with pdfplumber.open(document_file) as pdf:
33
+ for page in pdf.pages:
34
+ document_text += page.extract_text()
35
+ elif document_file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
36
+ docx_file = docx.Document(document_file)
37
+ for paragraph in docx_file.paragraphs:
38
+ document_text += paragraph.text
39
+ elif document_file.type == "application/msword":
40
+ doc_file = docx.Document(document_file)
41
+ for paragraph in doc_file.paragraphs:
42
+ document_text += paragraph.text
43
+ else:
44
+ st.error("A fájltípus nem támogatott. Kérlek válassz ki egy PDF, DOCX vagy DOC fájlt!")
45
 
46
+ # Előző beszélgetésekhez csatolható kontextus
47
+ context = st.text_area("Korábbi Beszélgetéshez Tartozó Kontextus", "")
 
 
 
 
 
 
 
 
 
48
 
49
+ # Kérdés mező hozzáadása
50
+ question = st.text_input("Kérdés a Dokumentumból", "")
 
51
 
52
+ # Ha van kérdés, azt is hozzáadjuk a bemenethez
53
+ if question:
54
+ input_text = f"{context} {document_text} {question}" if context else f"{document_text} {question}"
55
+ else:
56
+ input_text = f"{context} {document_text}" if context else document_text
57
 
58
+ # Model használata a válasz generálásához
59
+ response = generate_response(input_text, tokenizer, model)
60
 
61
  # Válasz megjelenítése
62
  st.subheader("Generált Válasz:")
63
+ st.write(response)
64
 
65
  # Aktuális beszélgetés hozzáadása az előző beszélgetésekhez
66
+ st.session_state.previous_conversations.append({"input_text": input_text, "response": response})