Update app.py
Browse files
app.py
CHANGED
|
@@ -1,102 +1,54 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
|
| 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 |
-
#
|
| 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 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
# -----------------------------
|
| 82 |
-
# 7️⃣ UI
|
| 83 |
-
# -----------------------------
|
| 84 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 85 |
-
gr.Markdown("
|
| 86 |
-
gr.Markdown(
|
| 87 |
-
|
| 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
|
| 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()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# --- Flowise endpoint ---
|
| 6 |
+
# Nếu bạn chạy Flowise cục bộ: ngrok http 3000
|
| 7 |
+
FLOWISE_API = os.getenv("FLOWISE_API", "https://<your-ngrok-or-render-url>/api/v1/prediction")
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def chat_with_bot(message, history):
|
| 10 |
+
"""
|
| 11 |
+
Gửi câu hỏi người dùng đến Flowise API và nhận câu trả lời.
|
| 12 |
+
"""
|
| 13 |
+
payload = {"question": message}
|
| 14 |
+
try:
|
| 15 |
+
response = requests.post(FLOWISE_API, json=payload, timeout=60)
|
| 16 |
+
data = response.json()
|
| 17 |
+
|
| 18 |
+
answer = data.get("text", "Keine Antwort gefunden.")
|
| 19 |
+
sources = data.get("sourceDocuments", [])
|
| 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("# 🤖 Chatbot für Prüfungsrecht")
|
| 36 |
+
gr.Markdown("Fragen Sie zur Prüfungsordnung oder zum Hochschulgesetz NRW. "
|
| 37 |
+
"Der Chatbot zitiert direkt aus den Quellen (PDF & Webseite).")
|
|
|
|
|
|
|
|
|
|
| 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 |
+
<h3>📄 Quellen anzeigen</h3>
|
| 50 |
+
<iframe src="https://recht.nrw.de/lmi/owa/br_text_anzeigen?v_id=10000000000000000654" width="100%" height="400"></iframe>
|
| 51 |
+
""")
|
| 52 |
+
|
| 53 |
if __name__ == "__main__":
|
| 54 |
demo.launch()
|