Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,30 +5,30 @@ from langchain_community.document_loaders import PyPDFLoader
|
|
5 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
6 |
from langchain.chains import RetrievalQA
|
7 |
from langchain.prompts import PromptTemplate
|
8 |
-
|
9 |
-
from transformers import LayoutLMv3Processor, AutoModelForTokenClassification
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
text_pages.append(text)
|
24 |
-
return text_pages
|
25 |
-
|
26 |
-
ocr_tool = LayoutLMv3OCR()
|
27 |
|
|
|
28 |
def process_pdf_and_query(pdf_path, question):
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
31 |
|
|
|
32 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
33 |
vectordb = Chroma.from_documents(documents, embeddings)
|
34 |
|
@@ -36,25 +36,23 @@ def process_pdf_and_query(pdf_path, question):
|
|
36 |
prompt_template = "Beantworte die folgende Frage basierend auf dem Dokument: {context}\nFrage: {question}\nAntwort:"
|
37 |
prompt = PromptTemplate(input_variables=["context", "question"], template=prompt_template)
|
38 |
|
|
|
39 |
qa_chain = RetrievalQA.from_chain_type(llm=None, retriever=retriever, chain_type_kwargs={"prompt": prompt})
|
40 |
response = qa_chain.run(input_documents=documents, question=question)
|
|
|
41 |
return response
|
42 |
|
|
|
43 |
def chatbot_response(pdf, question):
|
44 |
-
#
|
45 |
pdf_path = "/tmp/uploaded_pdf.pdf"
|
|
|
46 |
|
47 |
-
#
|
48 |
-
pdf_content = pdf.read() # Hier holen wir den Inhalt der PDF als Byte-Stream
|
49 |
-
|
50 |
-
# Speichern des Byte-Streams von der Datei
|
51 |
with open(pdf_path, "wb") as f:
|
52 |
f.write(pdf_content)
|
53 |
-
|
54 |
-
#
|
55 |
-
extracted_text = ocr_tool.extract_text(pdf_path)
|
56 |
-
|
57 |
-
# Frage beantworten basierend auf der PDF und OCR-Inhalten
|
58 |
answer = process_pdf_and_query(pdf_path, question)
|
59 |
|
60 |
# Temporäre Datei löschen
|
@@ -62,11 +60,12 @@ def chatbot_response(pdf, question):
|
|
62 |
|
63 |
return answer
|
64 |
|
65 |
-
# Gradio Interface
|
66 |
pdf_input = gr.File(label="PDF-Datei hochladen")
|
67 |
question_input = gr.Textbox(label="Frage eingeben")
|
68 |
response_output = gr.Textbox(label="Antwort")
|
69 |
|
|
|
70 |
interface = gr.Interface(
|
71 |
fn=chatbot_response,
|
72 |
inputs=[pdf_input, question_input],
|
|
|
5 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
6 |
from langchain.chains import RetrievalQA
|
7 |
from langchain.prompts import PromptTemplate
|
8 |
+
import fitz # PyMuPDF für das Extrahieren von Text aus PDFs
|
|
|
9 |
|
10 |
+
# Funktion zum Extrahieren von Text aus einer PDF
|
11 |
+
def extract_text_from_pdf(pdf_path):
|
12 |
+
doc = fitz.open(pdf_path) # Öffnen der PDF-Datei
|
13 |
+
text_pages = []
|
14 |
+
|
15 |
+
# Durch alle Seiten der PDF iterieren und Text extrahieren
|
16 |
+
for page_num in range(doc.page_count):
|
17 |
+
page = doc.load_page(page_num)
|
18 |
+
text = page.get_text("text") # Extrahiert den Text als "plain text"
|
19 |
+
text_pages.append(text)
|
20 |
+
|
21 |
+
return text_pages
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
# Frage-Antwort-Funktion mit Langchain und Chroma
|
24 |
def process_pdf_and_query(pdf_path, question):
|
25 |
+
# Extrahiere Text aus der PDF
|
26 |
+
extracted_text = extract_text_from_pdf(pdf_path)
|
27 |
+
|
28 |
+
# Dokumente für Langchain laden
|
29 |
+
documents = [{"text": page_text} for page_text in extracted_text]
|
30 |
|
31 |
+
# Embedding und Vektorstore vorbereiten
|
32 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
33 |
vectordb = Chroma.from_documents(documents, embeddings)
|
34 |
|
|
|
36 |
prompt_template = "Beantworte die folgende Frage basierend auf dem Dokument: {context}\nFrage: {question}\nAntwort:"
|
37 |
prompt = PromptTemplate(input_variables=["context", "question"], template=prompt_template)
|
38 |
|
39 |
+
# Erstellung der RetrievalQA-Kette
|
40 |
qa_chain = RetrievalQA.from_chain_type(llm=None, retriever=retriever, chain_type_kwargs={"prompt": prompt})
|
41 |
response = qa_chain.run(input_documents=documents, question=question)
|
42 |
+
|
43 |
return response
|
44 |
|
45 |
+
# Gradio Antwortfunktion
|
46 |
def chatbot_response(pdf, question):
|
47 |
+
# Speichern der hochgeladenen PDF-Datei
|
48 |
pdf_path = "/tmp/uploaded_pdf.pdf"
|
49 |
+
pdf_content = pdf.read() # Hole den Inhalt der PDF als Byte-Stream
|
50 |
|
51 |
+
# Speichern des Byte-Streams in einer Datei
|
|
|
|
|
|
|
52 |
with open(pdf_path, "wb") as f:
|
53 |
f.write(pdf_content)
|
54 |
+
|
55 |
+
# Frage beantworten basierend auf der extrahierten PDF und der Frage
|
|
|
|
|
|
|
56 |
answer = process_pdf_and_query(pdf_path, question)
|
57 |
|
58 |
# Temporäre Datei löschen
|
|
|
60 |
|
61 |
return answer
|
62 |
|
63 |
+
# Gradio Interface erstellen
|
64 |
pdf_input = gr.File(label="PDF-Datei hochladen")
|
65 |
question_input = gr.Textbox(label="Frage eingeben")
|
66 |
response_output = gr.Textbox(label="Antwort")
|
67 |
|
68 |
+
# Gradio Interface starten
|
69 |
interface = gr.Interface(
|
70 |
fn=chatbot_response,
|
71 |
inputs=[pdf_input, question_input],
|