Update app.py
Browse files
app.py
CHANGED
|
@@ -24,43 +24,39 @@ 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
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
else:
|
| 31 |
-
|
| 32 |
-
# PDF fájl feldolgozása részletekben
|
| 33 |
-
with pdfplumber.open(document_file) as pdf:
|
| 34 |
-
document_text = ""
|
| 35 |
-
for page in pdf.pages:
|
| 36 |
-
document_text += page.extract_text()
|
| 37 |
-
elif document_file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
| 38 |
-
# DOCX fájl feldolgozása részletekben
|
| 39 |
-
docx_file = docx.Document(document_file)
|
| 40 |
-
document_text = ""
|
| 41 |
-
for paragraph in docx_file.paragraphs:
|
| 42 |
-
document_text += paragraph.text
|
| 43 |
-
elif document_file.type == "application/msword":
|
| 44 |
-
# DOC fájl feldolgozása részletekben
|
| 45 |
-
doc_file = docx.Document(document_file)
|
| 46 |
-
document_text = ""
|
| 47 |
-
for paragraph in doc_file.paragraphs:
|
| 48 |
-
document_text += paragraph.text
|
| 49 |
-
else:
|
| 50 |
-
st.error("A fájltípus nem támogatott. Kérlek válassz ki egy PDF, DOCX vagy DOC fájlt!")
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
| 60 |
if context:
|
| 61 |
-
input_text = f"{context} {
|
| 62 |
else:
|
| 63 |
-
input_text =
|
| 64 |
|
| 65 |
# Ha van kérdés, azt is hozzáadjuk a bemenethez
|
| 66 |
if question:
|
|
@@ -68,12 +64,16 @@ if document_file is not None:
|
|
| 68 |
|
| 69 |
# Model használata a válasz generálásához
|
| 70 |
response = generate_response(input_text, tokenizer, model)
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
st.write(response)
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# 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:
|
|
|
|
| 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!")
|