Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,55 +1,64 @@
|
|
1 |
-
import
|
2 |
-
from langchain_community.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 |
-
|
|
|
12 |
def __init__(self):
|
13 |
-
#
|
14 |
-
self.processor =
|
15 |
-
self.model =
|
16 |
|
17 |
def extract_text(self, pdf_path):
|
|
|
18 |
images = convert_from_path(pdf_path)
|
19 |
text_pages = []
|
|
|
20 |
for image in images:
|
21 |
-
#
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
# Extrahiere den Text aus den Vorhersagen (falls dies vorgesehen ist)
|
26 |
-
text = self.processor.batch_decode(outputs.logits, skip_special_tokens=True)[0]
|
27 |
text_pages.append(text)
|
|
|
28 |
return text_pages
|
29 |
|
30 |
-
|
|
|
31 |
|
|
|
32 |
def process_pdf_and_query(pdf_path, question):
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
retriever = vectordb.as_retriever()
|
40 |
-
prompt_template = "Beantworte die folgende Frage basierend auf dem Dokument: {context}\nFrage: {question}\nAntwort:"
|
41 |
-
prompt = PromptTemplate(input_variables=["context", "question"], template=prompt_template)
|
42 |
-
|
43 |
-
qa_chain = RetrievalQA.from_chain_type(llm=None, retriever=retriever, chain_type_kwargs={"prompt": prompt})
|
44 |
-
response = qa_chain.run(input_documents=documents, question=question)
|
45 |
-
return response
|
46 |
|
|
|
47 |
def chatbot_response(pdf, question):
|
48 |
-
# Speichern der hochgeladenen Datei auf dem lokalen Dateisystem
|
49 |
pdf_path = "uploaded_pdf.pdf"
|
50 |
-
|
51 |
-
# Schreibe die PDF-Datei in eine lokale Datei
|
52 |
with open(pdf_path, "wb") as f:
|
53 |
f.write(pdf.read())
|
54 |
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import TrOCRProcessor, TrOCRForConditionalGeneration
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from pdf2image import convert_from_path
|
3 |
+
from PIL import Image
|
4 |
+
import torch
|
5 |
import os
|
6 |
|
7 |
+
# Klasse für die Textextraktion mit trOCR
|
8 |
+
class TrOCR:
|
9 |
def __init__(self):
|
10 |
+
# Laden des TrOCR Prozessors und Modells
|
11 |
+
self.processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-stage1")
|
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 |
+
# Prozessiere das Bild mit trOCR
|
21 |
+
pixel_values = self.processor(images=image, return_tensors="pt").pixel_values
|
22 |
+
generated_ids = self.model.generate(pixel_values)
|
23 |
+
text = self.processor.decode(generated_ids[0], skip_special_tokens=True)
|
|
|
|
|
24 |
text_pages.append(text)
|
25 |
+
|
26 |
return text_pages
|
27 |
|
28 |
+
# OCR-Tool initialisieren
|
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 |
+
# Extrahiere Text aus dem PDF
|
34 |
+
extracted_text = ocr_tool.extract_text(pdf_path)
|
35 |
+
# In diesem Beispiel geben wir den gesamten extrahierten Text zurück
|
36 |
+
# Hier kannst du Logik hinzufügen, um gezielt Fragen zu beantworten
|
37 |
+
return "\n".join(extracted_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# Speichern der hochgeladenen PDF-Datei
|
|
|
43 |
with open(pdf_path, "wb") as f:
|
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 |
+
# Beispiel für Gradio UI
|
55 |
+
import gradio as gr
|
56 |
+
|
57 |
+
with gr.Blocks() as demo:
|
58 |
+
pdf_input = gr.File(label="PDF-Datei hochladen")
|
59 |
+
question_input = gr.Textbox(label="Frage eingeben")
|
60 |
+
output = gr.Textbox(label="Antwort")
|
61 |
+
|
62 |
+
pdf_input.upload(chatbot_response, inputs=[pdf_input, question_input], outputs=output)
|
63 |
+
|
64 |
+
demo.launch()
|