Nguyen5 commited on
Commit
ac264f6
·
verified ·
1 Parent(s): 6acefbc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -25
app.py CHANGED
@@ -1,26 +1,54 @@
1
  import gradio as gr
2
- import json
3
- from langchain_community.vectorstores import FAISS
4
- from langchain_community.embeddings import HuggingFaceEmbeddings
5
- from langchain.chains import ConversationalRetrievalChain
6
- from langchain.memory import ConversationBufferMemory
7
- from langchain_community.llms import HuggingFaceHub
8
-
9
- # --- Load local RAG pipeline (Flowise JSON) ---
10
- PIPELINE_PATH = "Chat1 Chatflow.json"
11
- with open(PIPELINE_PATH, "r") as f:
12
- pipeline = json.load(f)
13
-
14
- # Dummy — you can later parse nodes from JSON to build chain dynamically
15
- embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
16
- memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
17
- llm = HuggingFaceHub(repo_id="mistralai/Mistral-7B-Instruct-v0.2", model_kwargs={"temperature": 0.0})
18
- vectorstore = FAISS.from_texts(["Prüfungsordnung PDF", "Hochschulgesetz NRW"], embedding=embeddings)
19
- qa_chain = ConversationalRetrievalChain.from_llm(llm, vectorstore.as_retriever(), memory=memory)
20
-
21
- def chat(message, history):
22
- result = qa_chain({"question": message})
23
- return result["answer"]
24
-
25
- demo = gr.ChatInterface(fn=chat, title="Prüfungsrecht Chatbot", description="Offline RAG pipeline ohne Flowise")
26
- 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()