rvdbra commited on
Commit
21c1ace
·
verified ·
1 Parent(s): 9058385

Upload 2 files

Browse files
Files changed (2) hide show
  1. app-3.py +336 -0
  2. requirements-3.txt +16 -0
app-3.py ADDED
@@ -0,0 +1,336 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import urllib.request
3
+ from dotenv import load_dotenv
4
+ import gradio as gr
5
+ import fasttext
6
+
7
+ # Download fastText model if not present
8
+ FASTTEXT_MODEL_PATH = "/tmp/lid.176.bin"
9
+ if not os.path.exists(FASTTEXT_MODEL_PATH):
10
+ print("Downloading fastText language identification model...")
11
+ urllib.request.urlretrieve(
12
+ "https://dl.fbaipublicfiles.com/fasttext/supervised-models/lid.176.bin",
13
+ FASTTEXT_MODEL_PATH
14
+ )
15
+
16
+ # Load environment variables
17
+ load_dotenv()
18
+
19
+ # Import your other dependencies
20
+ from langchain.memory import ConversationBufferWindowMemory
21
+ from langchain_openai import OpenAIEmbeddings
22
+ from langchain.chat_models import init_chat_model
23
+ from langchain_pinecone import Pinecone as LangchainPinecone
24
+ from supabase import create_client, Client
25
+ from datetime import datetime
26
+ from langgraph.graph import MessagesState, StateGraph, END
27
+ from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
28
+ from langgraph.prebuilt import ToolNode, tools_condition
29
+ from langchain_core.tools import tool
30
+
31
+ # Load fastText model
32
+ fasttext_model = fasttext.load_model(FASTTEXT_MODEL_PATH)
33
+ SUPPORTED_LANGUAGES = {
34
+ "de": "German",
35
+ "en": "English",
36
+ "tr": "Turkish",
37
+ "ar": "Arabic"
38
+ }
39
+
40
+ def detect_supported_language(text):
41
+ try:
42
+ label, prob = fasttext_model.predict(text)
43
+ lang = label[0].replace("__label__", "")
44
+ if lang in SUPPORTED_LANGUAGES and prob[0] > 0.45:
45
+ return lang
46
+ return None
47
+ except Exception as e:
48
+ print("[fastText Exception]", e)
49
+ return None
50
+
51
+ # Set up OpenAI, Pinecone, Supabase, etc.
52
+ OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
53
+ if not OPENAI_API_KEY:
54
+ raise RuntimeError("OPENAI_API_KEY must be set in your environment or .env file")
55
+ llm = init_chat_model("gpt-4.1-2025-04-14", model_provider="openai", temperature=0.0)
56
+ pinecone_api_key = os.getenv("PINECONE_API_KEY", "").strip()
57
+ pinecone_index_name = os.getenv("PINECONE_INDEX_NAME", "").strip()
58
+ embeddings = OpenAIEmbeddings(model="text-embedding-3-large")
59
+ vector_store = LangchainPinecone(
60
+ index_name=pinecone_index_name,
61
+ embedding=embeddings,
62
+ pinecone_api_key=pinecone_api_key
63
+ )
64
+ SUPABASE_URL = os.environ.get("SUPABASE_URL")
65
+ SUPABASE_KEY = os.environ.get("SUPABASE_KEY")
66
+ supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
67
+
68
+ def log_interaction_cloud(user_id: str, question: str, answer: str):
69
+ payload = {
70
+ "user_id": user_id,
71
+ "question": question,
72
+ "answer": answer,
73
+ "timestamp": datetime.utcnow().isoformat(),
74
+ }
75
+ try:
76
+ res = supabase.table("logs").insert(payload).execute()
77
+ except Exception as e:
78
+ print("[Supabase Exception]", e)
79
+ return
80
+ if hasattr(res, "status_code") and res.status_code >= 400:
81
+ print(f"[Supabase Error #{res.status_code}]:", res.data)
82
+ else:
83
+ print("[Supabase Success]:", res.data)
84
+
85
+ # LangGraph setup (your nodes, graph, etc.)
86
+ SYSTEM_MESSAGE_TEXT = (
87
+ "You are “Einbürgerungs-Helfer”, a highly skilled, friendly, and helpful administrative"
88
+ " assistant in Germany. Your only task is to support people who are eligible for naturalization"
89
+ " but have not yet applied. You only answer questions related to the current naturalization"
90
+ " process in Germany. You do not fill in applications or help users apply. You guide users "
91
+ "to relevant official resources by calling the retrieval tool to fetch appropriate documents."
92
+ "Any question involving citizenship, naturalization, applying for German nationality, duration "
93
+ "of residency, language requirements, or documents required MUST be interpreted as relevant and"
94
+ " MUST trigger the retrieval tool and never rely on your pre-trained knowledge. Your"
95
+ "answers must always be accurate, concise, and easy to understand. You must only answer in the user's"
96
+ "chosen language (German, English, Turkish, or Arabic). If a user writes in a different language"
97
+ " or requests another language, reply with: “Leider kann ich nur in den folgenden Sprachen"
98
+ " lesen und schreiben: Deutsch, Englisch, Türkisch oder Arabisch. In welcher Sprache möchten"
99
+ " Sie weitermachen?” and wait for the user to choose a supported language. You must always"
100
+ " answer in the chosen language. Never hallucinate, never go beyond the topic of naturalization,"
101
+ " and never share false information. If a user asks a question that is unrelated to naturalizatio"
102
+ " in Germany, reply in the user's language with: “I can only answer questions related to the"
103
+ " process of naturalization in Germany. Please provide questions on this topic.” If you do not"
104
+ " know how to answer, respond with: “I do not have enough knowledge to answer your question"
105
+ " accurately. Please email your question at SBD-Umfrage@polsoz.fu-berlin.de, and we will answer"
106
+ " your inquiry as soon as possible.” If a user uses toxic or offensive language, kindly ask them"
107
+ " to rephrase their question and explain that this behavior is not acceptable."
108
+ )
109
+
110
+ @tool(response_format="content_and_artifact")
111
+ def retrieve(query: str):
112
+ """Retrieve information related to the last query."""
113
+ retrieved_docs = vector_store.similarity_search(query, k=5)
114
+ serialized = "\n\n".join(
115
+ (f"Source: {doc.metadata}\n" f"Content: {doc.page_content}")
116
+ for doc in retrieved_docs
117
+ )
118
+ return serialized, retrieved_docs
119
+
120
+ def query_or_respond(state: MessagesState):
121
+ latest_user_msg = [m for m in state["messages"] if m.type == "human"][-1]
122
+ language = state.get("language")
123
+ if not language:
124
+ detected_lang = detect_supported_language(latest_user_msg.content)
125
+ if not detected_lang:
126
+ return {
127
+ "messages": state["messages"] + [
128
+ AIMessage(content="Leider kann ich nur in den folgenden Sprachen lesen und schreiben: Deutsch, Englisch, Türkisch oder Arabisch. In welcher Sprache möchten Sie weitermachen?")
129
+ ]}
130
+ else:
131
+ state["language"] = detected_lang
132
+ detected_lang = detect_supported_language(latest_user_msg.content)
133
+ if detected_lang and detected_lang != state.get("language"):
134
+ state["language"] = detected_lang
135
+ if not detected_lang:
136
+ return {
137
+ "messages": state["messages"] + [
138
+ AIMessage(content="Leider kann ich nur in den folgenden Sprachen lesen und schreiben: Deutsch, Englisch, Türkisch oder Arabisch. In welcher Sprache möchten Sie weitermachen?")
139
+ ]
140
+ }
141
+ system_message = SystemMessage(SYSTEM_MESSAGE_TEXT + f"\n\nThe user is communicating in: {SUPPORTED_LANGUAGES[state['language']]}.")
142
+ raw_hist = []
143
+ if "chat_history" in state:
144
+ for pair in state["chat_history"]:
145
+ if len(pair) == 2:
146
+ raw_hist.append(HumanMessage(content=pair[0]))
147
+ raw_hist.append(AIMessage(content=pair[1]))
148
+ OFF_TOPIC_RESPONSE_SNIPPET = "I can only answer questions related to the process of naturalization in Germany"
149
+ filtered_history = [
150
+ msg for msg in raw_hist
151
+ if not (msg.type == "ai" and OFF_TOPIC_RESPONSE_SNIPPET in msg.content)
152
+ ]
153
+ history = filtered_history[-5:] if filtered_history else []
154
+ latest_user_msg = [m for m in state["messages"] if m.type == "human"][-1]
155
+ prompt = [system_message] + history + [latest_user_msg]
156
+ llm_with_tools = llm.bind_tools([retrieve])
157
+ response = llm_with_tools.invoke(prompt)
158
+ if not getattr(response, "tool_calls", None):
159
+ user_id = state.get("user_id", "anonymous")
160
+ log_interaction_cloud(user_id, latest_user_msg.content, response.content)
161
+ return {"messages": state["messages"] + [response]}
162
+
163
+ tools_node = ToolNode([retrieve])
164
+
165
+ def generate(state: MessagesState):
166
+ language = state.get("language", "en")
167
+ system_prompt = SYSTEM_MESSAGE_TEXT + f"\n\nRespond in: {SUPPORTED_LANGUAGES[language]}.\n\n"
168
+ recent_tool_messages = []
169
+ for msg in reversed(state["messages"]):
170
+ if msg.type == "tool":
171
+ recent_tool_messages.append(msg)
172
+ else:
173
+ break
174
+ tool_messages = recent_tool_messages[::-1]
175
+ docs_content = "\n\n".join(msg.content for msg in tool_messages)
176
+ system_prompt = SYSTEM_MESSAGE_TEXT + ("\n\n" + docs_content if docs_content else "")
177
+ MAX_CONTEXT_MESSAGES = 5
178
+ conversation_messages = []
179
+ for msg in state["messages"]:
180
+ if msg.type == "human":
181
+ conversation_messages.append(msg)
182
+ elif msg.type == "ai":
183
+ # drop any assistant message that still has a pending tool_call
184
+ if not getattr(msg, "tool_calls", None):
185
+ conversation_messages.append(msg)
186
+ recent_context = conversation_messages[-MAX_CONTEXT_MESSAGES:]
187
+
188
+ # Step 4: Build final prompt
189
+ prompt = [SystemMessage(system_prompt)] + recent_context
190
+
191
+ # Step 5: Generate response
192
+ response = llm.invoke(prompt)
193
+ # Logging final answer
194
+ # pull out the same user_id you used in the beginning
195
+ user_id = state.get("user_id", "unknown_user")
196
+ # the last human message is the question
197
+ human_messages = [m.content for m in state["messages"] if m.type == "human"]
198
+ question = human_messages[-1] if human_messages else ""
199
+ answer = response.content
200
+ log_interaction_cloud(user_id, question, answer)
201
+ return {"messages": state["messages"] + [response]}
202
+
203
+ #Finally, we compile our application into a single graph object. In this case,
204
+ # we are just connecting the steps into a sequence. We also allow the first
205
+ # query_or_respond step to "short-circuit" and respond directly to the user if
206
+ # it does not generate a tool call. This allows our application to support
207
+ # conversational experiences-- e.g., responding to generic greetings that
208
+ # may not require a retrieval step.
209
+
210
+ g = StateGraph(MessagesState)
211
+ g.add_node(query_or_respond)
212
+ g.add_node(tools_node)
213
+ g.add_node(generate)
214
+ g.set_entry_point("query_or_respond")
215
+ g.add_conditional_edges("query_or_respond", tools_condition, {END: END, "tools": "tools"})
216
+ g.add_edge("tools", "generate")
217
+ g.add_edge("generate", END)
218
+ graph = g.compile()
219
+
220
+
221
+ import gradio as gr
222
+
223
+ OFF_TOPIC_SNIPPET = "I can only answer questions related to the process of naturalization"
224
+
225
+ from langchain.memory import ConversationBufferWindowMemory
226
+
227
+ def get_fresh_memory(user_id):
228
+ return ConversationBufferWindowMemory(
229
+ memory_key="chat_history",
230
+ return_messages=True,
231
+ k=5
232
+ )
233
+
234
+ def get_memory_from_state(chat_state):
235
+ memory = ConversationBufferWindowMemory(
236
+ memory_key="chat_history",
237
+ return_messages=True,
238
+ k=5
239
+ )
240
+ for pair in chat_state or []:
241
+ if len(pair) == 2:
242
+ user_msg, ai_msg = pair
243
+ memory.save_context({"input": user_msg}, {"output": ai_msg})
244
+ return memory
245
+
246
+ def stream_response(user_input, user_id):
247
+ if not user_id:
248
+ user_id = "anonymous"
249
+
250
+ # Do NOT reset memory here! Just generate response
251
+ state = {
252
+ "messages": [HumanMessage(content=user_message)],
253
+ "user_id": user_id,
254
+ }
255
+
256
+ final_state = graph.invoke(state)
257
+ final_message = final_state["messages"][-1].content
258
+
259
+ collected = ""
260
+ for token in final_message:
261
+ collected += token
262
+ yield collected
263
+
264
+ def respond(user_message, chat_history, user_id, chat_state):
265
+ lang_code = detect_supported_language(user_message)
266
+ if not lang_code:
267
+ chat_state = chat_state or []
268
+ chat_state.append([user_message, "Leider kann ich nur in den folgenden Sprachen lesen und schreiben: Deutsch, Englisch, Türkisch oder Arabisch. In welcher Sprache möchten Sie weitermachen?"])
269
+ return chat_state, chat_state, "" # clear textbox
270
+ if not user_id:
271
+ user_id = "anonymous"
272
+ # Rebuild memory from chat_state
273
+ memory = get_memory_from_state(chat_state)
274
+ # Generate response
275
+ state = {
276
+ "messages": [HumanMessage(content=user_message)],
277
+ "user_id": user_id,
278
+ "chat_history": chat_state,
279
+ }
280
+ final_state = graph.invoke(state)
281
+ full_response = final_state["messages"][-1].content
282
+ # Save to memory (for next time)
283
+ memory.save_context({"input": user_message}, {"output": full_response})
284
+ # Update chat_state
285
+ chat_state = chat_state or []
286
+ chat_state.append([user_message, full_response])
287
+ return chat_state, chat_state, "" # clear textbox
288
+
289
+ def clear_history():
290
+ return "", [], ""
291
+
292
+ description = """
293
+ ### Was ist **Einbürgerungs-Helfer**?
294
+ **Deutsch:** Einbürgerungs-Helfer hilft dir, den Weg zur deutschen Staatsangehörigkeit zu verstehen – einfach, mehrsprachig, datenschutzfreundlich.
295
+ **English:** Einbürgerungs-Helfer helps you understand how to get German citizenship – simple, multilingual, and privacy-friendly.
296
+ **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ı.
297
+ <div dir="rtl" style="text-align: right;"><strong>العربية:</strong> يساعدك مساعد التجنيس على فهم الطريق إلى الجنسية الألمانية - بسيط ومتعدد اللغات وملائم لحماية البيانات‪.‬</div>
298
+ """
299
+
300
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as iface:
301
+ gr.Markdown(f"# Einbürgerungs-Helfer\n{description}")
302
+ with gr.Row():
303
+ user_id_input = gr.Textbox(label="Benutzer-ID (User ID)", placeholder="z.B. user_123", lines=1)
304
+ clear_btn = gr.Button("🗑️ Verlauf löschen")
305
+ chat_state = gr.State([])
306
+ chatbot = gr.Chatbot(
307
+ label="Chat",
308
+ show_copy_button=True,
309
+ value=[[None,
310
+ "Ich bin hier, um Ihnen bei allen Fragen zur Einbürgerung in Deutschland zu helfen.\n"
311
+ "Sie können Ihre Frage auf Deutsch, Englisch, Türkisch oder Arabisch stellen.\n"
312
+ "Lassen Sie uns loslegen – wie kann ich Ihnen heute helfen?"]]
313
+ )
314
+ msg_input = gr.Textbox(
315
+ placeholder="Frage eingeben...",
316
+ label="Deine Nachricht",
317
+ lines=1,
318
+ value="" # start empty
319
+ )
320
+ with gr.Row():
321
+ submit_btn = gr.Button("Senden", variant="primary")
322
+ # Button click
323
+ submit_btn.click(
324
+ fn=respond,
325
+ inputs=[msg_input, chatbot, user_id_input, chat_state],
326
+ outputs=[chatbot, chat_state, msg_input],
327
+ )
328
+ # Enter key in textbox
329
+ msg_input.submit(
330
+ fn=respond,
331
+ inputs=[msg_input, chatbot, user_id_input, chat_state],
332
+ outputs=[chatbot, chat_state, msg_input],
333
+ )
334
+ clear_btn.click(fn=clear_history, inputs=[], outputs=[user_id_input, chatbot, msg_input])
335
+
336
+ iface.launch(share=False)
requirements-3.txt ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ pydantic==2.10.6
2
+ numpy==2.0.0
3
+ gradio>=4.31.0
4
+ gradio_client>=0.15.0
5
+ python-dotenv
6
+ supabase
7
+ pinecone-client
8
+ langchain
9
+ langchain-openai
10
+ langchain-pinecone
11
+ langchain-community
12
+ langchain-core
13
+ langchain-text-splitters
14
+ langgraph
15
+ typing-extensions
16
+ fasttext