Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
-
from transformers import pipeline
|
2 |
import gradio as gr
|
3 |
-
import torch
|
4 |
from PyPDF2 import PdfReader
|
5 |
-
import os
|
6 |
|
7 |
-
# T5-Modell laden
|
8 |
-
|
|
|
|
|
9 |
|
10 |
# Funktion zum Extrahieren von Text aus der PDF
|
11 |
def extract_text_from_pdf(pdf_path):
|
@@ -20,18 +20,20 @@ def chatbot_response(pdf_path, question):
|
|
20 |
# PDF-Text extrahieren
|
21 |
context = extract_text_from_pdf(pdf_path)
|
22 |
|
23 |
-
#
|
24 |
max_input_length = 512
|
25 |
context_parts = [context[i:i + max_input_length] for i in range(0, len(context), max_input_length)]
|
26 |
|
|
|
27 |
answers = []
|
28 |
-
|
29 |
-
# Iteriere über alle Textabschnitte und frage das Modell
|
30 |
for part in context_parts:
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
33 |
|
34 |
-
#
|
35 |
return answers[-1] if answers else "Keine Antwort gefunden"
|
36 |
|
37 |
# Gradio-Interface erstellen
|
|
|
1 |
+
from transformers import pipeline, T5ForConditionalGeneration, T5Tokenizer
|
2 |
import gradio as gr
|
|
|
3 |
from PyPDF2 import PdfReader
|
|
|
4 |
|
5 |
+
# T5-Modell und Tokenizer laden
|
6 |
+
model = T5ForConditionalGeneration.from_pretrained('t5-small')
|
7 |
+
tokenizer = T5Tokenizer.from_pretrained('t5-small')
|
8 |
+
qa_model = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
|
9 |
|
10 |
# Funktion zum Extrahieren von Text aus der PDF
|
11 |
def extract_text_from_pdf(pdf_path):
|
|
|
20 |
# PDF-Text extrahieren
|
21 |
context = extract_text_from_pdf(pdf_path)
|
22 |
|
23 |
+
# Text aufteilen, falls er zu lang ist
|
24 |
max_input_length = 512
|
25 |
context_parts = [context[i:i + max_input_length] for i in range(0, len(context), max_input_length)]
|
26 |
|
27 |
+
# Die Frage als Prompt an T5 schicken
|
28 |
answers = []
|
|
|
|
|
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
|