Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,13 @@
|
|
1 |
-
from transformers import pipeline,
|
2 |
import gradio as gr
|
3 |
from PyPDF2 import PdfReader
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
|
|
9 |
|
10 |
# Funktion zum Extrahieren von Text aus der PDF
|
11 |
def extract_text_from_pdf(pdf_path):
|
@@ -15,26 +17,17 @@ def extract_text_from_pdf(pdf_path):
|
|
15 |
text += page.extract_text()
|
16 |
return text
|
17 |
|
18 |
-
# Funktion für
|
19 |
def chatbot_response(pdf_path, question):
|
20 |
# PDF-Text extrahieren
|
21 |
context = extract_text_from_pdf(pdf_path)
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
for part in context_parts:
|
30 |
-
input_text = f"question: {question} context: {part}"
|
31 |
-
input_ids = tokenizer.encode(input_text, return_tensors="pt", truncation=True, max_length=512)
|
32 |
-
output = model.generate(input_ids, max_length=150, num_beams=4, early_stopping=True)
|
33 |
-
answer = tokenizer.decode(output[0], skip_special_tokens=True)
|
34 |
-
answers.append(answer.strip())
|
35 |
-
|
36 |
-
# Gib die letzte Antwort zurück
|
37 |
-
return answers[-1] if answers else "Keine Antwort gefunden"
|
38 |
|
39 |
# Gradio-Interface erstellen
|
40 |
pdf_input = gr.File(label="PDF-Datei hochladen", type="filepath")
|
@@ -46,8 +39,8 @@ interface = gr.Interface(
|
|
46 |
fn=chatbot_response,
|
47 |
inputs=[pdf_input, question_input],
|
48 |
outputs=response_output,
|
49 |
-
title="PDF-Fragebeantwortung
|
50 |
-
description="Lade eine PDF-Datei hoch und stelle Fragen zu ihrem Inhalt.
|
51 |
)
|
52 |
|
53 |
if __name__ == "__main__":
|
|
|
1 |
+
from transformers import pipeline, AutoModelForQuestionAnswering, AutoTokenizer
|
2 |
import gradio as gr
|
3 |
from PyPDF2 import PdfReader
|
4 |
|
5 |
+
# Modell und Tokenizer für Fragebeantwortung laden
|
6 |
+
model_name = "deepset/roberta-base-squad2"
|
7 |
+
model = AutoModelForQuestionAnswering.from_pretrained(model_name)
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
|
10 |
+
qa_pipeline = pipeline("question-answering", model=model, tokenizer=tokenizer)
|
11 |
|
12 |
# Funktion zum Extrahieren von Text aus der PDF
|
13 |
def extract_text_from_pdf(pdf_path):
|
|
|
17 |
text += page.extract_text()
|
18 |
return text
|
19 |
|
20 |
+
# Funktion für die Fragebeantwortung
|
21 |
def chatbot_response(pdf_path, question):
|
22 |
# PDF-Text extrahieren
|
23 |
context = extract_text_from_pdf(pdf_path)
|
24 |
|
25 |
+
# Frage beantworten
|
26 |
+
try:
|
27 |
+
result = qa_pipeline(question=question, context=context)
|
28 |
+
return result['answer']
|
29 |
+
except Exception as e:
|
30 |
+
return f"Fehler bei der Beantwortung: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
# Gradio-Interface erstellen
|
33 |
pdf_input = gr.File(label="PDF-Datei hochladen", type="filepath")
|
|
|
39 |
fn=chatbot_response,
|
40 |
inputs=[pdf_input, question_input],
|
41 |
outputs=response_output,
|
42 |
+
title="PDF-Fragebeantwortung auf Deutsch",
|
43 |
+
description="Lade eine PDF-Datei hoch und stelle Fragen zu ihrem Inhalt. Antworten basieren nur auf den PDF-Inhalten."
|
44 |
)
|
45 |
|
46 |
if __name__ == "__main__":
|