Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,65 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from pdf2image import convert_from_path
|
3 |
-
from PIL import Image
|
4 |
-
import torch
|
5 |
import os
|
6 |
|
7 |
-
|
8 |
-
class TrOCR:
|
9 |
def __init__(self):
|
10 |
-
|
11 |
-
self.
|
12 |
-
self.model = TrOCRForConditionalGeneration.from_pretrained("microsoft/trocr-base-stage1")
|
13 |
|
14 |
def extract_text(self, pdf_path):
|
15 |
-
# Konvertiere PDF-Seiten in Bilder
|
16 |
images = convert_from_path(pdf_path)
|
17 |
text_pages = []
|
18 |
-
|
19 |
for image in images:
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
text = self.processor.decode(generated_ids[0], skip_special_tokens=True)
|
24 |
text_pages.append(text)
|
25 |
-
|
26 |
return text_pages
|
27 |
|
28 |
-
|
29 |
-
ocr_tool = TrOCR()
|
30 |
|
31 |
-
# Beispiel: Funktion zum Verarbeiten von PDFs und Beantworten von Fragen
|
32 |
def process_pdf_and_query(pdf_path, question):
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
# Gradio Funktion zum Einlesen der Datei und Beantworten von Fragen
|
40 |
def chatbot_response(pdf, question):
|
41 |
pdf_path = "uploaded_pdf.pdf"
|
42 |
-
|
43 |
-
|
44 |
-
f.write(pdf.read())
|
45 |
-
|
46 |
-
# Extrahiere Text und beantworte die Frage
|
47 |
answer = process_pdf_and_query(pdf_path, question)
|
48 |
-
|
49 |
-
# Nach der Verarbeitung die Datei löschen
|
50 |
os.remove(pdf_path)
|
51 |
-
|
52 |
return answer
|
53 |
|
54 |
-
#
|
55 |
-
|
|
|
|
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
63 |
|
64 |
-
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from langchain.vectorstores import Chroma
|
3 |
+
from langchain_community.document_loaders import PyPDFLoader
|
4 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
5 |
+
from transformers import LayoutLMv3Processor, AutoModelForTokenClassification
|
6 |
+
from langchain.chains import RetrievalQA
|
7 |
+
from langchain.prompts import PromptTemplate
|
8 |
from pdf2image import convert_from_path
|
|
|
|
|
9 |
import os
|
10 |
|
11 |
+
class LayoutLMv3OCR:
|
|
|
12 |
def __init__(self):
|
13 |
+
self.processor = LayoutLMv3Processor.from_pretrained("microsoft/layoutlmv3-base")
|
14 |
+
self.model = AutoModelForTokenClassification.from_pretrained("microsoft/layoutlmv3-base")
|
|
|
15 |
|
16 |
def extract_text(self, pdf_path):
|
|
|
17 |
images = convert_from_path(pdf_path)
|
18 |
text_pages = []
|
|
|
19 |
for image in images:
|
20 |
+
inputs = self.processor(images=image, return_tensors="pt")
|
21 |
+
outputs = self.model(**inputs)
|
22 |
+
text = self.processor.batch_decode(outputs.logits, skip_special_tokens=True)[0]
|
|
|
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 |
+
loader = PyPDFLoader(pdf_path)
|
30 |
+
documents = loader.load()
|
31 |
+
|
32 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
33 |
+
vectordb = Chroma.from_documents(documents, embeddings)
|
34 |
+
|
35 |
+
retriever = vectordb.as_retriever()
|
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 |
pdf_path = "uploaded_pdf.pdf"
|
45 |
+
pdf.save(pdf_path)
|
46 |
+
extracted_text = ocr_tool.extract_text(pdf_path)
|
|
|
|
|
|
|
47 |
answer = process_pdf_and_query(pdf_path, question)
|
|
|
|
|
48 |
os.remove(pdf_path)
|
|
|
49 |
return answer
|
50 |
|
51 |
+
# Ändere 'inputs' und 'outputs' zur neuen Gradio API
|
52 |
+
pdf_input = gr.File(label="PDF-Datei hochladen")
|
53 |
+
question_input = gr.Textbox(label="Frage eingeben")
|
54 |
+
response_output = gr.Textbox(label="Antwort")
|
55 |
|
56 |
+
interface = gr.Interface(
|
57 |
+
fn=chatbot_response,
|
58 |
+
inputs=[pdf_input, question_input],
|
59 |
+
outputs=response_output,
|
60 |
+
title="RAG Chatbot mit PDF-Unterstützung",
|
61 |
+
description="Lade eine PDF-Datei hoch und stelle Fragen zu ihrem Inhalt."
|
62 |
+
)
|
63 |
|
64 |
+
if __name__ == "__main__":
|
65 |
+
interface.launch()
|