Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,70 @@
|
|
1 |
import faiss
|
2 |
import numpy as np
|
3 |
-
from sentence_transformers import SentenceTransformer
|
4 |
import gradio as gr
|
|
|
|
|
5 |
|
6 |
# Schritt 1: Lade das Modell für die Embeddings
|
7 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
8 |
|
9 |
-
#
|
10 |
-
documents
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
document_embeddings = model.encode(documents)
|
18 |
|
19 |
-
#
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
24 |
|
25 |
# Schritt 3: Suche nach einer ähnlichen Antwort auf eine Eingabeabfrage
|
26 |
-
def search_documents(query):
|
27 |
query_embedding = model.encode([query])[0].astype('float32')
|
28 |
D, I = index.search(np.array([query_embedding]), k=1) # Suche nach den Top 1 Treffern
|
29 |
return documents[I[0][0]] # Gibt das am besten passende Dokument zurück
|
30 |
|
31 |
-
#
|
32 |
-
def chatbot_response(
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
|
|
35 |
interface = gr.Interface(
|
36 |
fn=chatbot_response,
|
37 |
-
inputs=
|
38 |
-
outputs=
|
39 |
-
title="
|
40 |
-
description="
|
41 |
)
|
42 |
|
43 |
-
|
|
|
|
1 |
import faiss
|
2 |
import numpy as np
|
|
|
3 |
import gradio as gr
|
4 |
+
from sentence_transformers import SentenceTransformer
|
5 |
+
import fitz # PyMuPDF für die Textextraktion aus PDFs
|
6 |
|
7 |
# Schritt 1: Lade das Modell für die Embeddings
|
8 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
9 |
|
10 |
+
# FAISS-Index erstellen
|
11 |
+
def create_faiss_index(documents):
|
12 |
+
document_embeddings = model.encode(documents)
|
13 |
+
dimension = len(document_embeddings[0])
|
14 |
+
index = faiss.IndexFlatL2(dimension)
|
15 |
+
document_embeddings = np.array(document_embeddings).astype('float32')
|
16 |
+
index.add(document_embeddings) # Füge Dokumente zum Index hinzu
|
17 |
+
return index, documents
|
|
|
18 |
|
19 |
+
# Schritt 2: Extrahiere Text aus einem PDF
|
20 |
+
def extract_text_from_pdf(pdf_path):
|
21 |
+
doc = fitz.open(pdf_path)
|
22 |
+
text_pages = []
|
23 |
+
for page_num in range(len(doc)):
|
24 |
+
page = doc.load_page(page_num)
|
25 |
+
text = page.get_text("text") # Extrahiert den Text als normalen Text
|
26 |
+
text_pages.append(text)
|
27 |
+
return text_pages
|
28 |
|
29 |
# Schritt 3: Suche nach einer ähnlichen Antwort auf eine Eingabeabfrage
|
30 |
+
def search_documents(query, index, documents):
|
31 |
query_embedding = model.encode([query])[0].astype('float32')
|
32 |
D, I = index.search(np.array([query_embedding]), k=1) # Suche nach den Top 1 Treffern
|
33 |
return documents[I[0][0]] # Gibt das am besten passende Dokument zurück
|
34 |
|
35 |
+
# Schritt 4: Gesamtprozess (Fragebeantwortung)
|
36 |
+
def chatbot_response(pdf, question):
|
37 |
+
# Speichern der hochgeladenen PDF
|
38 |
+
pdf_path = "uploaded_pdf.pdf"
|
39 |
+
pdf.save(pdf_path)
|
40 |
+
|
41 |
+
# Textextraktion aus der PDF
|
42 |
+
text_pages = extract_text_from_pdf(pdf_path)
|
43 |
+
|
44 |
+
# FAISS-Index erstellen
|
45 |
+
index, documents = create_faiss_index(text_pages)
|
46 |
+
|
47 |
+
# Suche nach Antwort
|
48 |
+
answer = search_documents(question, index, documents)
|
49 |
+
|
50 |
+
# Lösche die hochgeladene PDF-Datei
|
51 |
+
os.remove(pdf_path)
|
52 |
+
|
53 |
+
return answer
|
54 |
+
|
55 |
+
# Gradio-Interface
|
56 |
+
pdf_input = gr.File(label="PDF-Datei hochladen", type="file")
|
57 |
+
question_input = gr.Textbox(label="Frage eingeben", placeholder="Stelle eine Frage zu dem PDF-Dokument")
|
58 |
+
response_output = gr.Textbox(label="Antwort")
|
59 |
|
60 |
+
# Gradio-Interface erstellen
|
61 |
interface = gr.Interface(
|
62 |
fn=chatbot_response,
|
63 |
+
inputs=[pdf_input, question_input],
|
64 |
+
outputs=response_output,
|
65 |
+
title="PDF-Fragebeantwortung mit FAISS und Transformers",
|
66 |
+
description="Lade eine PDF-Datei hoch und stelle Fragen zu ihrem Inhalt. Das System verwendet FAISS und Transformers, um die passende Antwort zu finden."
|
67 |
)
|
68 |
|
69 |
+
if __name__ == "__main__":
|
70 |
+
interface.launch()
|