Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,7 @@ from langchain.chains import ConversationalRetrievalChain
|
|
| 12 |
from langchain.llms import HuggingFaceHub
|
| 13 |
from dotenv import load_dotenv
|
| 14 |
from transformers import pipeline
|
|
|
|
| 15 |
###########
|
| 16 |
#pip install faiss-cpu
|
| 17 |
#pip install langchain
|
|
@@ -25,6 +26,8 @@ def check_question(user_question):
|
|
| 25 |
if len(user_question) < 10: # Beispielkriterium für minimale Länge
|
| 26 |
return False
|
| 27 |
return True
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# PDF in String umwandeln
|
| 30 |
def get_pdf_text(folder_path):
|
|
@@ -78,7 +81,16 @@ def get_vectorstore():
|
|
| 78 |
vectorstoreDB = FAISS.load_local(save_directory, embeddings)
|
| 79 |
return vectorstoreDB
|
| 80 |
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
|
| 84 |
def main():
|
|
@@ -106,6 +118,21 @@ def main():
|
|
| 106 |
#print(get_vectorstore().similarity_search_with_score("stelle")) # zeigt an ob Vektordatenbank gefüllt ist
|
| 107 |
|
| 108 |
#print(get_conversation_chain(get_vectorstore()))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
|
| 111 |
|
|
|
|
| 12 |
from langchain.llms import HuggingFaceHub
|
| 13 |
from dotenv import load_dotenv
|
| 14 |
from transformers import pipeline
|
| 15 |
+
from sentence_transformers import SentenceTransformer, util
|
| 16 |
###########
|
| 17 |
#pip install faiss-cpu
|
| 18 |
#pip install langchain
|
|
|
|
| 26 |
if len(user_question) < 10: # Beispielkriterium für minimale Länge
|
| 27 |
return False
|
| 28 |
return True
|
| 29 |
+
|
| 30 |
+
|
| 31 |
|
| 32 |
# PDF in String umwandeln
|
| 33 |
def get_pdf_text(folder_path):
|
|
|
|
| 81 |
vectorstoreDB = FAISS.load_local(save_directory, embeddings)
|
| 82 |
return vectorstoreDB
|
| 83 |
|
| 84 |
+
def calculate_similarity(user_question, pdf_text):
|
| 85 |
+
model = SentenceTransformer('paraphrase-distilroberta-base-v1') # Verwende ein vortrainiertes Modell
|
| 86 |
+
encoded_pdf = model.encode(pdf_text, convert_to_tensor=True)
|
| 87 |
+
encoded_question = model.encode(user_question, convert_to_tensor=True)
|
| 88 |
+
|
| 89 |
+
# Berechne die Ähnlichkeit zwischen der Frage und den PDF-Inhalten
|
| 90 |
+
similarity_scores = util.pytorch_cos_sim(encoded_question, encoded_pdf)
|
| 91 |
+
max_similarity = max(similarity_scores[0])
|
| 92 |
+
|
| 93 |
+
return max_similarity.item()
|
| 94 |
|
| 95 |
|
| 96 |
def main():
|
|
|
|
| 118 |
#print(get_vectorstore().similarity_search_with_score("stelle")) # zeigt an ob Vektordatenbank gefüllt ist
|
| 119 |
|
| 120 |
#print(get_conversation_chain(get_vectorstore()))
|
| 121 |
+
similarity_score = calculate_similarity(user_question, pdf_text)
|
| 122 |
+
|
| 123 |
+
# Nutze similarity_score zur Bewertung der Relevanz der Frage für die PDF-Inhalte
|
| 124 |
+
relevance_threshold = 0.6 # Beispielwert, anpassen nach Bedarf
|
| 125 |
+
|
| 126 |
+
if similarity_score >= relevance_threshold:
|
| 127 |
+
st.success("Die Frage ist relevant für die PDF-Inhalte.")
|
| 128 |
+
# Führe die weitere Verarbeitung durch
|
| 129 |
+
retriever = get_vectorstore().as_retriever()
|
| 130 |
+
retrieved_docs = retriever.invoke(user_question)
|
| 131 |
+
if user_question:
|
| 132 |
+
st.text(retrieved_docs[0].page_content)
|
| 133 |
+
# bei eingehendem PDF
|
| 134 |
+
else:
|
| 135 |
+
st.error("Die Frage ist nicht ausreichend relevant für die PDF-Inhalte. Bitte eine präzisere Frage stellen.")
|
| 136 |
|
| 137 |
|
| 138 |
|