Spaces:
Sleeping
Sleeping
File size: 2,863 Bytes
fbe3ac4 c106446 692eafb fbe3ac4 6378bf1 c106446 6378bf1 fbe3ac4 6378bf1 fbe3ac4 6378bf1 bd2041d fbe3ac4 bd2041d 6378bf1 fbe3ac4 6378bf1 bd2041d fbe3ac4 6378bf1 bd2041d c106446 692eafb c106446 24d5864 c106446 692eafb fbe3ac4 24d5864 692eafb ee9ba92 24d5864 692eafb ee9ba92 692eafb ee9ba92 c106446 fbe3ac4 ee9ba92 fbe3ac4 ee9ba92 fbe3ac4 692eafb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
import gradio as gr
import os
from langchain.vectorstores import Chroma
from langchain_community.document_loaders import PyPDFLoader
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain.chains import RetrievalQA
from langchain.prompts import PromptTemplate
from pdf2image import convert_from_path
from transformers import LayoutLMv3Processor, AutoModelForTokenClassification
class LayoutLMv3OCR:
def __init__(self):
self.processor = LayoutLMv3Processor.from_pretrained("microsoft/layoutlmv3-base")
self.model = AutoModelForTokenClassification.from_pretrained("microsoft/layoutlmv3-base")
def extract_text(self, pdf_path):
images = convert_from_path(pdf_path)
text_pages = []
for image in images:
inputs = self.processor(images=image, return_tensors="pt")
outputs = self.model(**inputs)
text = self.processor.batch_decode(outputs.logits, skip_special_tokens=True)[0]
text_pages.append(text)
return text_pages
ocr_tool = LayoutLMv3OCR()
def process_pdf_and_query(pdf_path, question):
loader = PyPDFLoader(pdf_path)
documents = loader.load()
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
vectordb = Chroma.from_documents(documents, embeddings)
retriever = vectordb.as_retriever()
prompt_template = "Beantworte die folgende Frage basierend auf dem Dokument: {context}\nFrage: {question}\nAntwort:"
prompt = PromptTemplate(input_variables=["context", "question"], template=prompt_template)
qa_chain = RetrievalQA.from_chain_type(llm=None, retriever=retriever, chain_type_kwargs={"prompt": prompt})
response = qa_chain.run(input_documents=documents, question=question)
return response
def chatbot_response(pdf, question):
# Gradio gibt uns das PDF als NamedString, wir speichern es als temporäre Datei
pdf_path = "/tmp/uploaded_pdf.pdf"
# Speichern des Byte-Streams von der Datei
with open(pdf_path, "wb") as f:
f.write(pdf) # pdf ist bereits als Byte-Stream verfügbar
# OCR-Text extrahieren
extracted_text = ocr_tool.extract_text(pdf_path)
# Frage beantworten basierend auf der PDF und OCR-Inhalten
answer = process_pdf_and_query(pdf_path, question)
# Temporäre Datei löschen
os.remove(pdf_path)
return answer
# Gradio Interface
pdf_input = gr.File(label="PDF-Datei hochladen")
question_input = gr.Textbox(label="Frage eingeben")
response_output = gr.Textbox(label="Antwort")
interface = gr.Interface(
fn=chatbot_response,
inputs=[pdf_input, question_input],
outputs=response_output,
title="RAG Chatbot mit PDF-Unterstützung",
description="Lade eine PDF-Datei hoch und stelle Fragen zu ihrem Inhalt."
)
if __name__ == "__main__":
interface.launch()
|