Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,102 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
#
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def chat_with_bot(message, history):
|
| 10 |
-
""
|
| 11 |
-
|
| 12 |
-
""
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
if sources:
|
| 22 |
-
refs = "\n\n**Quellen:**\n" + "\n".join([
|
| 23 |
-
f"- {src.get('metadata', {}).get('source', 'Unbekannt')}"
|
| 24 |
-
for src in sources
|
| 25 |
-
])
|
| 26 |
-
else:
|
| 27 |
-
refs = ""
|
| 28 |
-
|
| 29 |
-
return answer + refs
|
| 30 |
-
|
| 31 |
-
except Exception as e:
|
| 32 |
-
return f"Fehler: {e}"
|
| 33 |
|
|
|
|
|
|
|
|
|
|
| 34 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 35 |
-
gr.Markdown("
|
| 36 |
-
gr.Markdown(
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
chat = gr.ChatInterface(
|
| 40 |
fn=chat_with_bot,
|
| 41 |
-
title="Prüfungsrecht RAG-Chatbot",
|
| 42 |
examples=[
|
| 43 |
"Was steht in §10 über Wiederholungsprüfungen?",
|
| 44 |
-
"Welche Regel gilt laut Hochschulgesetz für Prüfungsanspruch?"
|
| 45 |
]
|
| 46 |
)
|
| 47 |
|
| 48 |
-
gr.HTML("""
|
| 49 |
-
<div style='display:flex;gap:10px;'>
|
| 50 |
-
<iframe src="https://recht.nrw.de/lmi/owa/br_text_anzeigen?v_id=10000000000000000654"
|
| 51 |
-
width="49%" height="400"></iframe>
|
| 52 |
-
<iframe src="https://huggingface.co/spaces/<your-space-name>/resolve/main/PO_BWI_THK_2022.pdf"
|
| 53 |
-
width="49%" height="400"></iframe>
|
| 54 |
-
</div>
|
| 55 |
-
""")
|
| 56 |
-
|
| 57 |
-
|
| 58 |
if __name__ == "__main__":
|
| 59 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 4 |
+
from langchain_community.document_loaders import WebBaseLoader
|
| 5 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 6 |
+
from langchain_community.vectorstores import FAISS
|
| 7 |
+
from langchain.memory import ConversationBufferMemory
|
| 8 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 9 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
| 10 |
|
| 11 |
+
# -----------------------------
|
| 12 |
+
# 1️⃣ Load pipeline JSON (Flowise export)
|
| 13 |
+
# -----------------------------
|
| 14 |
+
PIPELINE_PATH = "Chat1 Chatflow.json"
|
| 15 |
+
with open(PIPELINE_PATH, "r") as f:
|
| 16 |
+
pipeline = json.load(f)
|
| 17 |
|
| 18 |
+
# Lấy URL trong pipeline (nếu có)
|
| 19 |
+
web_url = None
|
| 20 |
+
for node in pipeline.get("nodes", []):
|
| 21 |
+
if "url" in node.get("data", {}):
|
| 22 |
+
web_url = node["data"]["url"]
|
| 23 |
+
|
| 24 |
+
if not web_url:
|
| 25 |
+
web_url = "https://recht.nrw.de/lmi/owa/br_text_anzeigen?v_id=10000000000000000654"
|
| 26 |
+
|
| 27 |
+
# -----------------------------
|
| 28 |
+
# 2️⃣ Load documents (from website)
|
| 29 |
+
# -----------------------------
|
| 30 |
+
print(f"🌐 Lade Text von: {web_url}")
|
| 31 |
+
loader = WebBaseLoader(web_url)
|
| 32 |
+
docs = loader.load()
|
| 33 |
+
|
| 34 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150)
|
| 35 |
+
docs_split = splitter.split_documents(docs)
|
| 36 |
+
|
| 37 |
+
# -----------------------------
|
| 38 |
+
# 3️⃣ Embeddings + Vectorstore
|
| 39 |
+
# -----------------------------
|
| 40 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 41 |
+
vectorstore = FAISS.from_documents(docs_split, embedding=embeddings)
|
| 42 |
+
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
|
| 43 |
+
|
| 44 |
+
# -----------------------------
|
| 45 |
+
# 4️⃣ LLM + Memory
|
| 46 |
+
# -----------------------------
|
| 47 |
+
llm = HuggingFaceEndpoint(
|
| 48 |
+
repo_id="mistralai/Mistral-7B-Instruct-v0.2",
|
| 49 |
+
temperature=0.0,
|
| 50 |
+
huggingfacehub_api_token=None # Hugging Face Space đã tự có quyền
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 54 |
+
|
| 55 |
+
# -----------------------------
|
| 56 |
+
# 5️⃣ RAG Chain
|
| 57 |
+
# -----------------------------
|
| 58 |
+
qa_chain = ConversationalRetrievalChain.from_llm(
|
| 59 |
+
llm=llm,
|
| 60 |
+
retriever=retriever,
|
| 61 |
+
memory=memory,
|
| 62 |
+
return_source_documents=True
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
# -----------------------------
|
| 66 |
+
# 6️⃣ Chat function
|
| 67 |
+
# -----------------------------
|
| 68 |
def chat_with_bot(message, history):
|
| 69 |
+
result = qa_chain({"question": message})
|
| 70 |
+
answer = result.get("answer", "Keine Antwort gefunden.")
|
| 71 |
+
sources = result.get("source_documents", [])
|
| 72 |
+
if sources:
|
| 73 |
+
refs = "\n\n**Quellen:**\n" + "\n".join([
|
| 74 |
+
f"- {src.metadata.get('source', 'Unbekannt')}"
|
| 75 |
+
for src in sources
|
| 76 |
+
])
|
| 77 |
+
else:
|
| 78 |
+
refs = ""
|
| 79 |
+
return answer + refs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
# -----------------------------
|
| 82 |
+
# 7️⃣ UI
|
| 83 |
+
# -----------------------------
|
| 84 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 85 |
+
gr.Markdown("## 🤖 Chatbot für Prüfungsrecht")
|
| 86 |
+
gr.Markdown(
|
| 87 |
+
"Fragen Sie zur Prüfungsordnung oder zum Hochschulgesetz NRW. "
|
| 88 |
+
"Der Chatbot antwortet auf Basis der aus Flowise exportierten Pipeline "
|
| 89 |
+
"und zitiert direkt aus der Quelle (recht.nrw.de)."
|
| 90 |
+
)
|
| 91 |
|
| 92 |
chat = gr.ChatInterface(
|
| 93 |
fn=chat_with_bot,
|
| 94 |
+
title="Prüfungsrecht RAG-Chatbot (Serverless)",
|
| 95 |
examples=[
|
| 96 |
"Was steht in §10 über Wiederholungsprüfungen?",
|
| 97 |
+
"Welche Regel gilt laut Hochschulgesetz für Prüfungsanspruch?"
|
| 98 |
]
|
| 99 |
)
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
if __name__ == "__main__":
|
| 102 |
demo.launch()
|