Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,26 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
import os
|
3 |
from langchain.vectorstores import Chroma
|
4 |
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 |
-
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 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
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 |
-
|
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 |
-
#
|
|
|
|
|
32 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
33 |
vectordb = Chroma.from_documents(documents, embeddings)
|
34 |
|
@@ -36,23 +28,22 @@ 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 |
-
# 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 |
-
#
|
48 |
pdf_path = "/tmp/uploaded_pdf.pdf"
|
49 |
-
pdf_content = pdf.read() # Hole den Inhalt der PDF als Byte-Stream
|
50 |
|
51 |
-
#
|
|
|
|
|
|
|
52 |
with open(pdf_path, "wb") as f:
|
53 |
f.write(pdf_content)
|
54 |
-
|
55 |
-
# Frage beantworten basierend auf der
|
56 |
answer = process_pdf_and_query(pdf_path, question)
|
57 |
|
58 |
# Temporäre Datei löschen
|
@@ -60,12 +51,11 @@ def chatbot_response(pdf, question):
|
|
60 |
|
61 |
return answer
|
62 |
|
63 |
-
# Gradio Interface
|
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],
|
|
|
1 |
import gradio as gr
|
2 |
+
import pdfplumber
|
3 |
import os
|
4 |
from langchain.vectorstores import Chroma
|
5 |
from langchain_community.document_loaders import PyPDFLoader
|
6 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
7 |
from langchain.chains import RetrievalQA
|
8 |
from langchain.prompts import PromptTemplate
|
|
|
9 |
|
|
|
10 |
def extract_text_from_pdf(pdf_path):
|
11 |
+
# Verwende pdfplumber, um den Text aus der PDF zu extrahieren
|
12 |
+
with pdfplumber.open(pdf_path) as pdf:
|
13 |
+
full_text = ""
|
14 |
+
for page in pdf.pages:
|
15 |
+
full_text += page.extract_text()
|
16 |
+
return full_text
|
|
|
|
|
|
|
|
|
17 |
|
|
|
18 |
def process_pdf_and_query(pdf_path, question):
|
19 |
+
text = extract_text_from_pdf(pdf_path)
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
# Extrahiere die Dokumente und erstelle einen Vektorstore
|
22 |
+
documents = [{"text": text}]
|
23 |
+
|
24 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
25 |
vectordb = Chroma.from_documents(documents, embeddings)
|
26 |
|
|
|
28 |
prompt_template = "Beantworte die folgende Frage basierend auf dem Dokument: {context}\nFrage: {question}\nAntwort:"
|
29 |
prompt = PromptTemplate(input_variables=["context", "question"], template=prompt_template)
|
30 |
|
|
|
31 |
qa_chain = RetrievalQA.from_chain_type(llm=None, retriever=retriever, chain_type_kwargs={"prompt": prompt})
|
32 |
response = qa_chain.run(input_documents=documents, question=question)
|
|
|
33 |
return response
|
34 |
|
|
|
35 |
def chatbot_response(pdf, question):
|
36 |
+
# Gradio gibt uns die PDF als NamedString, wir extrahieren den Inhalt als Byte-Stream
|
37 |
pdf_path = "/tmp/uploaded_pdf.pdf"
|
|
|
38 |
|
39 |
+
# Extrahiere den Inhalt der Datei als Bytes
|
40 |
+
pdf_content = pdf.read() # Hier holen wir den Inhalt der PDF als Byte-Stream
|
41 |
+
|
42 |
+
# Speichern des Byte-Streams von der Datei
|
43 |
with open(pdf_path, "wb") as f:
|
44 |
f.write(pdf_content)
|
45 |
+
|
46 |
+
# Frage beantworten basierend auf der PDF und extrahiertem Text
|
47 |
answer = process_pdf_and_query(pdf_path, question)
|
48 |
|
49 |
# Temporäre Datei löschen
|
|
|
51 |
|
52 |
return answer
|
53 |
|
54 |
+
# Gradio Interface
|
55 |
pdf_input = gr.File(label="PDF-Datei hochladen")
|
56 |
question_input = gr.Textbox(label="Frage eingeben")
|
57 |
response_output = gr.Textbox(label="Antwort")
|
58 |
|
|
|
59 |
interface = gr.Interface(
|
60 |
fn=chatbot_response,
|
61 |
inputs=[pdf_input, question_input],
|