Update app.py
Browse files
app.py
CHANGED
|
@@ -1,216 +1,146 @@
|
|
| 1 |
import os
|
| 2 |
import uuid
|
| 3 |
from datetime import datetime
|
| 4 |
-
from typing_extensions import TypedDict
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
import gradio as gr
|
| 8 |
-
|
| 9 |
-
from langchain.chat_models import init_chat_model
|
| 10 |
-
from langchain_community.vectorstores import Pinecone
|
| 11 |
-
from pinecone import Pinecone
|
| 12 |
from supabase import create_client, Client
|
| 13 |
-
from langchain.
|
| 14 |
-
from
|
| 15 |
-
from
|
| 16 |
-
from
|
| 17 |
-
from
|
| 18 |
|
| 19 |
-
# Load environment variables
|
| 20 |
load_dotenv()
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
#
|
| 29 |
-
llm =
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
index_name=PINECONE_INDEX,
|
| 36 |
-
|
| 37 |
-
environment=os.environ.get("PINECONE_ENVIRONMENT", "us-east1-gcp")
|
| 38 |
)
|
| 39 |
|
| 40 |
-
# Supabase
|
| 41 |
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 42 |
|
| 43 |
def log_interaction_cloud(user_id: str, question: str, answer: str):
|
|
|
|
| 44 |
payload = {
|
| 45 |
-
"user_id":
|
| 46 |
"question": question,
|
| 47 |
-
"answer":
|
| 48 |
"timestamp": datetime.utcnow().isoformat(),
|
| 49 |
}
|
| 50 |
try:
|
| 51 |
-
res = supabase.
|
|
|
|
| 52 |
except Exception as e:
|
| 53 |
-
print("
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
print(f"[Supabase Error #{res.status_code}]:", res.data)
|
| 57 |
-
else:
|
| 58 |
-
print("[Supabase Success]:", res.data)
|
| 59 |
-
|
| 60 |
-
def generate_user_id():
|
| 61 |
return "user_" + uuid.uuid4().hex[:6]
|
| 62 |
|
| 63 |
-
memory
|
| 64 |
-
|
| 65 |
-
return_messages=True,
|
| 66 |
-
k=5
|
| 67 |
-
)
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
return serialized, retrieved_docs
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
"
|
| 82 |
-
"
|
|
|
|
| 83 |
)
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
user_id
|
| 88 |
-
|
| 89 |
-
def query_or_respond(state: MyState):
|
| 90 |
-
system_message = SystemMessage(SYSTEM_MESSAGE_TEXT)
|
| 91 |
-
raw_history = memory.load_memory_variables({})["chat_history"]
|
| 92 |
-
filtered_history = [msg for msg in raw_history if not (
|
| 93 |
-
msg.type == "ai" and "I can only answer questions related" in msg.content)]
|
| 94 |
-
prompt = [system_message] + filtered_history[-5:]
|
| 95 |
-
llm_with_tools = llm.bind_tools([retrieve])
|
| 96 |
-
response = llm_with_tools.invoke(prompt)
|
| 97 |
-
user_id = state.get("user_id", "unknown_user")
|
| 98 |
-
user_question = [m["content"] for m in state["messages"] if m["type"] == "human"][-1]
|
| 99 |
-
memory.save_context({"input": user_question}, {"output": response.content})
|
| 100 |
-
if not getattr(response, "tool_calls", None):
|
| 101 |
-
log_interaction_cloud(user_id, user_question, response.content)
|
| 102 |
-
return {"messages": state["messages"] + [response], "user_id": user_id}
|
| 103 |
-
|
| 104 |
-
def generate(state: MyState):
|
| 105 |
-
last_message = state["messages"][-1]
|
| 106 |
-
user_id = state.get("user_id", "unknown_user")
|
| 107 |
-
user_question = [m["content"] for m in state["messages"] if m["type"] == "human"][-1]
|
| 108 |
-
answer_text = getattr(last_message, "content", str(last_message))
|
| 109 |
-
log_interaction_cloud(user_id, user_question, answer_text)
|
| 110 |
-
return {"messages": state["messages"], "user_id": user_id}
|
| 111 |
-
|
| 112 |
-
# LangGraph setup
|
| 113 |
-
workflow = StateGraph(MyState)
|
| 114 |
-
workflow.add_node("query_or_respond", query_or_respond)
|
| 115 |
-
workflow.add_node("tool", ToolNode([retrieve]))
|
| 116 |
-
workflow.add_node("generate", generate)
|
| 117 |
-
workflow.add_conditional_edges("query_or_respond", tools_condition, {
|
| 118 |
-
"tool": "tool",
|
| 119 |
-
"generate": "generate"
|
| 120 |
-
})
|
| 121 |
-
workflow.add_edge("tool", "generate")
|
| 122 |
-
workflow.add_edge("generate", END)
|
| 123 |
-
workflow.set_entry_point("query_or_respond")
|
| 124 |
-
graph = workflow.compile()
|
| 125 |
-
|
| 126 |
-
# Gradio UI
|
| 127 |
-
import gradio as gr
|
| 128 |
-
|
| 129 |
-
# Global chat history (for demo purposes)
|
| 130 |
-
chat_histories = {}
|
| 131 |
-
|
| 132 |
-
def stream_response(user_input, user_id):
|
| 133 |
if not user_id:
|
| 134 |
-
user_id =
|
| 135 |
-
state = {
|
| 136 |
-
"messages": [{"role": "user", "content": user_input}],
|
| 137 |
-
"user_id": user_id,
|
| 138 |
-
}
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
# Stream response token by token
|
| 144 |
-
for token in message:
|
| 145 |
-
collected += token
|
| 146 |
-
yield collected
|
| 147 |
|
|
|
|
|
|
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
full_response = partial
|
| 158 |
-
yield chat_history + [[user_message, full_response]]
|
| 159 |
|
| 160 |
-
|
|
|
|
| 161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
def clear_history():
|
| 163 |
return "", []
|
| 164 |
|
| 165 |
-
#
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
**Türkçe:** Einbürgerungs-Helfer, Almanya vatandaşlığına giden yolu anlamanıza yardımcı olur – basit, çok dilli ve gizliliğe saygılı.
|
| 173 |
-
|
| 174 |
-
**العربية:** يساعدك "Einbürgerungs-Helfer" على فهم كيفية الحصول على الجنسية الألمانية - بطريقة بسيطة، متعددة اللغات وتحترم الخصوصية.
|
| 175 |
-
"""
|
| 176 |
-
|
| 177 |
-
with gr.Blocks(
|
| 178 |
-
theme=gr.themes.Soft(primary_hue="blue"),
|
| 179 |
-
css="""
|
| 180 |
-
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans&display=swap');
|
| 181 |
-
|
| 182 |
-
* {
|
| 183 |
-
font-family: 'Noto Sans', sans-serif !important;
|
| 184 |
-
}
|
| 185 |
-
"""
|
| 186 |
-
) as iface:
|
| 187 |
-
gr.Markdown(f"# Einbürgerungs-Helfer\n{description}")
|
| 188 |
|
| 189 |
with gr.Row():
|
| 190 |
-
user_id_input = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
clear_btn = gr.Button("🗑️ Verlauf löschen")
|
| 192 |
|
| 193 |
-
chatbot
|
| 194 |
-
label="Chat",
|
| 195 |
-
show_copy_button=True,
|
| 196 |
-
value=[[None,
|
| 197 |
-
"Ich bin hier, um Ihnen bei allen Fragen zur Einbürgerung in Deutschland zu helfen.\n"
|
| 198 |
-
"Sie können Ihre Frage auf Deutsch, Englisch, Türkisch oder Arabisch stellen.\n"
|
| 199 |
-
"Lassen Sie uns loslegen – wie kann ich Ihnen heute helfen?"]]
|
| 200 |
-
)
|
| 201 |
-
|
| 202 |
msg_input = gr.Textbox(placeholder="Frage eingeben...", label="Deine Nachricht")
|
| 203 |
-
|
| 204 |
-
with gr.Row():
|
| 205 |
-
submit_btn = gr.Button("Senden", variant="primary")
|
| 206 |
|
| 207 |
submit_btn.click(
|
| 208 |
fn=respond,
|
| 209 |
inputs=[msg_input, chatbot, user_id_input],
|
| 210 |
-
outputs=chatbot,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
)
|
| 212 |
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
iface.launch(server_name="0.0.0.0", server_port=7860)
|
| 216 |
-
|
|
|
|
| 1 |
import os
|
| 2 |
import uuid
|
| 3 |
from datetime import datetime
|
|
|
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
+
import pinecone
|
|
|
|
|
|
|
|
|
|
| 8 |
from supabase import create_client, Client
|
| 9 |
+
from langchain.chat_models import ChatOpenAI
|
| 10 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 11 |
+
from langchain.vectorstores import Pinecone as LangchainPinecone
|
| 12 |
+
from langchain.memory import ConversationBufferMemory
|
| 13 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 14 |
|
| 15 |
+
# ─── Load environment variables ────────────────────────────────────────────────
|
| 16 |
load_dotenv()
|
| 17 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 18 |
+
PINECONE_API_KEY = os.getenv("PINECONE_API_KEY")
|
| 19 |
+
PINECONE_ENV = os.getenv("PINECONE_ENVIRONMENT", "us-east1-gcp")
|
| 20 |
+
PINECONE_INDEX = os.getenv("PINECONE_INDEX", "natstudy")
|
| 21 |
+
SUPABASE_URL = os.getenv("SUPABASE_URL")
|
| 22 |
+
SUPABASE_KEY = os.getenv("SUPABASE_KEY")
|
| 23 |
+
|
| 24 |
+
# ─── Initialize LLM ────────────────────────────────────────────────────────────
|
| 25 |
+
llm = ChatOpenAI(
|
| 26 |
+
model_name="gpt-4",
|
| 27 |
+
temperature=0.0,
|
| 28 |
+
openai_api_key=OPENAI_API_KEY
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# ─── Initialize Pinecone + VectorStore ────────────────────────────────────────
|
| 32 |
+
pinecone.init(api_key=PINECONE_API_KEY, environment=PINECONE_ENV)
|
| 33 |
+
embeddings = OpenAIEmbeddings(openai_api_key=OPENAI_API_KEY)
|
| 34 |
+
vectorstore = LangchainPinecone.from_existing_index(
|
| 35 |
index_name=PINECONE_INDEX,
|
| 36 |
+
embedding=embeddings
|
|
|
|
| 37 |
)
|
| 38 |
|
| 39 |
+
# ─── Initialize Supabase client ───────────────────────────────────────────────
|
| 40 |
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 41 |
|
| 42 |
def log_interaction_cloud(user_id: str, question: str, answer: str):
|
| 43 |
+
"""Log each Q&A to your Supabase `logs` table."""
|
| 44 |
payload = {
|
| 45 |
+
"user_id": user_id,
|
| 46 |
"question": question,
|
| 47 |
+
"answer": answer,
|
| 48 |
"timestamp": datetime.utcnow().isoformat(),
|
| 49 |
}
|
| 50 |
try:
|
| 51 |
+
res = supabase.from_("logs").insert(payload).execute()
|
| 52 |
+
print("Logged to Supabase:", res)
|
| 53 |
except Exception as e:
|
| 54 |
+
print("Supabase logging error:", e)
|
| 55 |
+
|
| 56 |
+
def generate_user_id() -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
return "user_" + uuid.uuid4().hex[:6]
|
| 58 |
|
| 59 |
+
# ─── Per-user memory store ─────────────────────────────────────────────────────
|
| 60 |
+
user_memories = {}
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
def get_memory(user_id: str) -> ConversationBufferMemory:
|
| 63 |
+
if user_id not in user_memories:
|
| 64 |
+
user_memories[user_id] = ConversationBufferMemory(
|
| 65 |
+
memory_key="chat_history",
|
| 66 |
+
return_messages=True,
|
| 67 |
+
k=5
|
| 68 |
+
)
|
| 69 |
+
return user_memories[user_id]
|
|
|
|
| 70 |
|
| 71 |
+
# ─── A friendly greeting to start the conversation ─────────────────────────────
|
| 72 |
+
GREETING = (
|
| 73 |
+
"Ich bin hier, um Ihnen bei allen Fragen zur Einbürgerung in Deutschland zu helfen.\n"
|
| 74 |
+
"Sie können Ihre Frage auf Deutsch, Englisch, Türkisch oder Arabisch stellen.\n"
|
| 75 |
+
"Lassen Sie uns loslegen – wie kann ich Ihnen heute helfen?"
|
| 76 |
)
|
| 77 |
|
| 78 |
+
# ─── The main Gradio callback ───────────────────────────────────────────────────
|
| 79 |
+
def respond(user_message, chat_history, user_id):
|
| 80 |
+
# assign a new user_id if none provided
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
if not user_id:
|
| 82 |
+
user_id = generate_user_id()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
# initialize chat history with greeting if this is the first turn
|
| 85 |
+
if not chat_history:
|
| 86 |
+
chat_history = [[None, GREETING]]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
# fetch (or create) the user's memory buffer
|
| 89 |
+
memory = get_memory(user_id)
|
| 90 |
|
| 91 |
+
# build a conversational retrieval chain
|
| 92 |
+
chain = ConversationalRetrievalChain.from_llm(
|
| 93 |
+
llm=llm,
|
| 94 |
+
retriever=vectorstore.as_retriever(),
|
| 95 |
+
memory=memory,
|
| 96 |
+
return_source_documents=False,
|
| 97 |
+
)
|
| 98 |
|
| 99 |
+
# run the chain
|
| 100 |
+
result = chain({"question": user_message})
|
| 101 |
+
answer = result["answer"]
|
|
|
|
|
|
|
| 102 |
|
| 103 |
+
# log to Supabase
|
| 104 |
+
log_interaction_cloud(user_id, user_message, answer)
|
| 105 |
|
| 106 |
+
# append to our Gradio history
|
| 107 |
+
chat_history.append([user_message, answer])
|
| 108 |
+
return chat_history, user_id
|
| 109 |
+
|
| 110 |
+
# ─── Clear button resets user_id & chat history ────────────────────────────────
|
| 111 |
def clear_history():
|
| 112 |
return "", []
|
| 113 |
|
| 114 |
+
# ─── Gradio UI ─────────────────────────────────────────────────────────────────
|
| 115 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as iface:
|
| 116 |
+
gr.Markdown(
|
| 117 |
+
"# Einbürgerungs-Helfer\n"
|
| 118 |
+
"Einbürgerungs-Helfer hilft dir, den Weg zur deutschen Staatsangehörigkeit zu verstehen – "
|
| 119 |
+
"einfach, mehrsprachig und datenschutzfreundlich."
|
| 120 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
with gr.Row():
|
| 123 |
+
user_id_input = gr.Textbox(
|
| 124 |
+
label="Benutzer-ID (User ID)",
|
| 125 |
+
placeholder="z.B. user_123",
|
| 126 |
+
lines=1
|
| 127 |
+
)
|
| 128 |
clear_btn = gr.Button("🗑️ Verlauf löschen")
|
| 129 |
|
| 130 |
+
chatbot = gr.Chatbot(label="Chat", show_copy_button=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
msg_input = gr.Textbox(placeholder="Frage eingeben...", label="Deine Nachricht")
|
| 132 |
+
submit_btn = gr.Button("Senden", variant="primary")
|
|
|
|
|
|
|
| 133 |
|
| 134 |
submit_btn.click(
|
| 135 |
fn=respond,
|
| 136 |
inputs=[msg_input, chatbot, user_id_input],
|
| 137 |
+
outputs=[chatbot, user_id_input]
|
| 138 |
+
)
|
| 139 |
+
clear_btn.click(
|
| 140 |
+
fn=clear_history,
|
| 141 |
+
inputs=[],
|
| 142 |
+
outputs=[user_id_input, chatbot]
|
| 143 |
)
|
| 144 |
|
| 145 |
+
if __name__ == "__main__":
|
| 146 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|