commit
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
# app.py
|
| 2 |
import os
|
| 3 |
-
from typing import List,
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
from langchain_core.documents import Document
|
|
@@ -12,9 +12,7 @@ from load_documents import load_documents
|
|
| 12 |
from speech_io import transcribe_audio, synthesize_speech
|
| 13 |
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
# 1. Documents Laden
|
| 17 |
-
# ===============================
|
| 18 |
print("🔹 Lade Dokumente aus Supabase …")
|
| 19 |
docs: List[Document] = load_documents()
|
| 20 |
print("✔ DOCUMENTS LOADED:", len(docs))
|
|
@@ -40,9 +38,7 @@ llm = ChatOpenAI(
|
|
| 40 |
)
|
| 41 |
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
# 2. RAG Engine
|
| 45 |
-
# ===============================
|
| 46 |
def build_context(docs: List[Document]) -> str:
|
| 47 |
parts = []
|
| 48 |
for i, d in enumerate(docs, 1):
|
|
@@ -61,60 +57,53 @@ def build_context(docs: List[Document]) -> str:
|
|
| 61 |
return "\n\n".join(parts)
|
| 62 |
|
| 63 |
|
| 64 |
-
def rag_answer(query: str, mode: str)
|
| 65 |
retrieved = retriever.invoke(query)
|
| 66 |
ctx = build_context(retrieved)
|
| 67 |
|
| 68 |
modes = {
|
| 69 |
"Kurz": "Antworte sehr kurz (max. 3 Sätze).",
|
| 70 |
-
"Standard": "Antworte ausführlich und
|
| 71 |
-
"Juristisch Präzise": "
|
| 72 |
}
|
| 73 |
|
| 74 |
messages = [
|
| 75 |
-
{
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
]
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
return answer, retrieved
|
| 84 |
|
| 85 |
|
| 86 |
-
#
|
| 87 |
-
|
| 88 |
-
# ===============================
|
| 89 |
-
def chatbot_text(user_input: str, history: List[Dict], mode: str):
|
| 90 |
answer, _ = rag_answer(user_input, mode)
|
| 91 |
-
history = history + [
|
| 92 |
-
{"role": "user", "content": user_input},
|
| 93 |
-
{"role": "assistant", "content": answer},
|
| 94 |
-
]
|
| 95 |
return history, history
|
| 96 |
|
| 97 |
|
| 98 |
-
def chatbot_voice(audio_file: str, history: List[
|
| 99 |
-
user_text = transcribe_audio(audio_file,
|
| 100 |
answer, _ = rag_answer(user_text, mode)
|
| 101 |
audio_out = synthesize_speech(answer)
|
| 102 |
|
| 103 |
-
history = history + [
|
| 104 |
-
{"role": "user", "content": user_text},
|
| 105 |
-
{"role": "assistant", "content": answer},
|
| 106 |
-
]
|
| 107 |
return history, audio_out, user_text, history
|
| 108 |
|
| 109 |
|
| 110 |
-
#
|
| 111 |
-
# 4. UI
|
| 112 |
-
# ===============================
|
| 113 |
with gr.Blocks(title="Prüfungsrechts-Chatbot") as demo:
|
| 114 |
|
| 115 |
with gr.Tab("💬 Text-Chat"):
|
| 116 |
mode = gr.Radio(["Kurz", "Standard", "Juristisch Präzise"], value="Standard")
|
| 117 |
-
chat = gr.Chatbot(
|
| 118 |
state = gr.State([])
|
| 119 |
inp = gr.Textbox(label="Frage eingeben")
|
| 120 |
send = gr.Button("Senden")
|
|
@@ -123,19 +112,19 @@ with gr.Blocks(title="Prüfungsrechts-Chatbot") as demo:
|
|
| 123 |
|
| 124 |
with gr.Tab("🎙️ Sprach-Chat"):
|
| 125 |
mode_v = gr.Radio(["Kurz", "Standard", "Juristisch Präzise"], value="Standard")
|
| 126 |
-
chat_v = gr.Chatbot(
|
| 127 |
state_v = gr.State([])
|
| 128 |
|
| 129 |
mic = gr.Audio(sources=["microphone"], type="filepath")
|
| 130 |
-
|
| 131 |
-
out_audio = gr.Audio(
|
| 132 |
-
|
| 133 |
|
| 134 |
btn = gr.Button("Sprechen")
|
| 135 |
btn.click(
|
| 136 |
chatbot_voice,
|
| 137 |
-
[mic, state_v, mode_v,
|
| 138 |
-
[chat_v, out_audio,
|
| 139 |
)
|
| 140 |
|
| 141 |
if __name__ == "__main__":
|
|
|
|
| 1 |
# app.py
|
| 2 |
import os
|
| 3 |
+
from typing import List, Tuple
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
from langchain_core.documents import Document
|
|
|
|
| 12 |
from speech_io import transcribe_audio, synthesize_speech
|
| 13 |
|
| 14 |
|
| 15 |
+
# ========== 1. Lade Dokumente ==========
|
|
|
|
|
|
|
| 16 |
print("🔹 Lade Dokumente aus Supabase …")
|
| 17 |
docs: List[Document] = load_documents()
|
| 18 |
print("✔ DOCUMENTS LOADED:", len(docs))
|
|
|
|
| 38 |
)
|
| 39 |
|
| 40 |
|
| 41 |
+
# ========== 2. RAG ==========
|
|
|
|
|
|
|
| 42 |
def build_context(docs: List[Document]) -> str:
|
| 43 |
parts = []
|
| 44 |
for i, d in enumerate(docs, 1):
|
|
|
|
| 57 |
return "\n\n".join(parts)
|
| 58 |
|
| 59 |
|
| 60 |
+
def rag_answer(query: str, mode: str):
|
| 61 |
retrieved = retriever.invoke(query)
|
| 62 |
ctx = build_context(retrieved)
|
| 63 |
|
| 64 |
modes = {
|
| 65 |
"Kurz": "Antworte sehr kurz (max. 3 Sätze).",
|
| 66 |
+
"Standard": "Antworte ausführlich und verständlich.",
|
| 67 |
+
"Juristisch Präzise": "Formuliere juristisch präzise.",
|
| 68 |
}
|
| 69 |
|
| 70 |
messages = [
|
| 71 |
+
{
|
| 72 |
+
"role": "system",
|
| 73 |
+
"content": "Du bist ein Chatbot für Prüfungsrecht. Antworte nur auf Deutsch."
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"role": "user",
|
| 77 |
+
"content": f"FRAGE:\n{query}\n\nKONTEXT:\n{ctx}\n\n{modes[mode]}"
|
| 78 |
+
}
|
| 79 |
]
|
| 80 |
|
| 81 |
+
resp = llm.invoke(messages)
|
| 82 |
+
return resp.content, retrieved
|
|
|
|
| 83 |
|
| 84 |
|
| 85 |
+
# ========== 3. Chatbot Funktionen (GRADIO 4.x – TUPLES) ==========
|
| 86 |
+
def chatbot_text(user_input: str, history: List[Tuple[str, str]], mode: str):
|
|
|
|
|
|
|
| 87 |
answer, _ = rag_answer(user_input, mode)
|
| 88 |
+
history = history + [(user_input, answer)]
|
|
|
|
|
|
|
|
|
|
| 89 |
return history, history
|
| 90 |
|
| 91 |
|
| 92 |
+
def chatbot_voice(audio_file: str, history: List[Tuple[str, str]], mode: str, language_hint: str):
|
| 93 |
+
user_text = transcribe_audio(audio_file, language_hint or None)
|
| 94 |
answer, _ = rag_answer(user_text, mode)
|
| 95 |
audio_out = synthesize_speech(answer)
|
| 96 |
|
| 97 |
+
history = history + [(user_text, answer)]
|
|
|
|
|
|
|
|
|
|
| 98 |
return history, audio_out, user_text, history
|
| 99 |
|
| 100 |
|
| 101 |
+
# ========== 4. UI ==========
|
|
|
|
|
|
|
| 102 |
with gr.Blocks(title="Prüfungsrechts-Chatbot") as demo:
|
| 103 |
|
| 104 |
with gr.Tab("💬 Text-Chat"):
|
| 105 |
mode = gr.Radio(["Kurz", "Standard", "Juristisch Präzise"], value="Standard")
|
| 106 |
+
chat = gr.Chatbot()
|
| 107 |
state = gr.State([])
|
| 108 |
inp = gr.Textbox(label="Frage eingeben")
|
| 109 |
send = gr.Button("Senden")
|
|
|
|
| 112 |
|
| 113 |
with gr.Tab("🎙️ Sprach-Chat"):
|
| 114 |
mode_v = gr.Radio(["Kurz", "Standard", "Juristisch Präzise"], value="Standard")
|
| 115 |
+
chat_v = gr.Chatbot()
|
| 116 |
state_v = gr.State([])
|
| 117 |
|
| 118 |
mic = gr.Audio(sources=["microphone"], type="filepath")
|
| 119 |
+
lang = gr.Textbox(label="Sprache (optional: de/en/vi)")
|
| 120 |
+
out_audio = gr.Audio()
|
| 121 |
+
transcript = gr.Textbox(label="Transkript")
|
| 122 |
|
| 123 |
btn = gr.Button("Sprechen")
|
| 124 |
btn.click(
|
| 125 |
chatbot_voice,
|
| 126 |
+
[mic, state_v, mode_v, lang],
|
| 127 |
+
[chat_v, out_audio, transcript, state_v]
|
| 128 |
)
|
| 129 |
|
| 130 |
if __name__ == "__main__":
|