Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,14 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
-
from
|
4 |
-
from
|
5 |
-
from langchain.
|
6 |
-
from langchain_community.embeddings import HuggingFaceEmbeddings
|
7 |
from langchain.chains import ConversationalRetrievalChain
|
8 |
-
from langchain_community.llms import HuggingFaceEndpoint
|
9 |
from langchain.memory import ConversationBufferMemory
|
|
|
10 |
|
11 |
# Liste der Modelle
|
12 |
-
list_llm = ["google/flan-t5-small", "distilbert-base-uncased"] #
|
13 |
list_llm_simple = [os.path.basename(llm) for llm in list_llm]
|
14 |
|
15 |
# PDF-Dokument laden und aufteilen
|
@@ -19,7 +18,7 @@ def load_doc(list_file_path):
|
|
19 |
for loader in loaders:
|
20 |
pages.extend(loader.load())
|
21 |
text_splitter = RecursiveCharacterTextSplitter(
|
22 |
-
chunk_size=512, # Kleinere Chunks für
|
23 |
chunk_overlap=32
|
24 |
)
|
25 |
doc_splits = text_splitter.split_documents(pages)
|
@@ -33,16 +32,17 @@ def create_db(splits):
|
|
33 |
|
34 |
# Initialisierung des LLM Chains
|
35 |
def initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db):
|
36 |
-
llm =
|
37 |
repo_id=llm_model,
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
41 |
)
|
42 |
|
43 |
memory = ConversationBufferMemory(
|
44 |
memory_key="chat_history",
|
45 |
-
output_key='answer',
|
46 |
return_messages=True
|
47 |
)
|
48 |
|
@@ -80,10 +80,8 @@ def format_chat_history(message, chat_history):
|
|
80 |
# Chat-Funktion
|
81 |
def conversation(qa_chain, message, history):
|
82 |
formatted_chat_history = format_chat_history(message, history)
|
83 |
-
response = qa_chain
|
84 |
response_answer = response["answer"]
|
85 |
-
if "Helpful Answer:" in response_answer:
|
86 |
-
response_answer = response_answer.split("Helpful Answer:")[-1]
|
87 |
new_history = history + [(message, response_answer)]
|
88 |
return qa_chain, gr.update(value=""), new_history
|
89 |
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
+
from langchain.vectorstores import FAISS
|
4 |
+
from langchain.document_loaders import PyPDFLoader
|
5 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
|
|
6 |
from langchain.chains import ConversationalRetrievalChain
|
|
|
7 |
from langchain.memory import ConversationBufferMemory
|
8 |
+
from langchain.llms import HuggingFaceHub
|
9 |
|
10 |
# Liste der Modelle
|
11 |
+
list_llm = ["google/flan-t5-small", "distilbert-base-uncased"] # Leichte Modelle für CPU
|
12 |
list_llm_simple = [os.path.basename(llm) for llm in list_llm]
|
13 |
|
14 |
# PDF-Dokument laden und aufteilen
|
|
|
18 |
for loader in loaders:
|
19 |
pages.extend(loader.load())
|
20 |
text_splitter = RecursiveCharacterTextSplitter(
|
21 |
+
chunk_size=512, # Kleinere Chunks für schnelleres Verarbeiten auf CPU
|
22 |
chunk_overlap=32
|
23 |
)
|
24 |
doc_splits = text_splitter.split_documents(pages)
|
|
|
32 |
|
33 |
# Initialisierung des LLM Chains
|
34 |
def initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db):
|
35 |
+
llm = HuggingFaceHub(
|
36 |
repo_id=llm_model,
|
37 |
+
model_kwargs={
|
38 |
+
"temperature": temperature,
|
39 |
+
"max_length": max_tokens,
|
40 |
+
"top_k": top_k,
|
41 |
+
}
|
42 |
)
|
43 |
|
44 |
memory = ConversationBufferMemory(
|
45 |
memory_key="chat_history",
|
|
|
46 |
return_messages=True
|
47 |
)
|
48 |
|
|
|
80 |
# Chat-Funktion
|
81 |
def conversation(qa_chain, message, history):
|
82 |
formatted_chat_history = format_chat_history(message, history)
|
83 |
+
response = qa_chain({"question": message, "chat_history": formatted_chat_history})
|
84 |
response_answer = response["answer"]
|
|
|
|
|
85 |
new_history = history + [(message, response_answer)]
|
86 |
return qa_chain, gr.update(value=""), new_history
|
87 |
|