Spaces:
Sleeping

rvdbra commited on
Commit
5af9ca3
·
verified ·
1 Parent(s): 81ab8cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -34
app.py CHANGED
@@ -163,45 +163,96 @@ workflow.add_edge("generate", END)
163
  workflow.set_entry_point("query_or_respond")
164
  graph = workflow.compile()
165
 
166
- # Gradio UI
167
- def gradio_chatbot(user_message, chat_history, user_id):
 
 
 
 
168
  if not user_id:
169
- user_id = generate_user_id()
170
- # Convert chat_history to MessagesState format
171
- messages = []
172
- for turn in chat_history:
173
- messages.append({"type": "human", "content": turn[0]})
174
- if turn[1]:
175
- messages.append({"type": "ai", "content": turn[1]})
176
- messages.append({"type": "human", "content": user_message})
177
- state = {"messages": messages, "user_id": user_id}
178
- result = graph.invoke(state)
179
- # Extract the last AI message
180
- ai_message = [m.content for m in result["messages"] if m.type == "ai"][-1]
181
- chat_history.append((user_message, ai_message))
182
- return chat_history, user_id
183
-
184
- with gr.Blocks() as iface:
185
- gr.Markdown("# 🇩🇪 Einbürgerungs-Helfer\nA chatbot to help with questions about naturalization in Germany.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
  with gr.Row():
187
- chatbot = gr.Chatbot()
 
 
 
 
 
 
 
 
 
 
 
 
 
188
  with gr.Row():
189
- user_input = gr.Textbox(placeholder="Stellen Sie Ihre Frage hier / Ask your question here")
190
- user_id_state = gr.State("")
191
- send_btn = gr.Button("Send")
192
- clear_btn = gr.Button("Clear")
193
- send_btn.click(
194
- gradio_chatbot,
195
- inputs=[user_input, chatbot, user_id_state],
196
- outputs=[chatbot, user_id_state]
197
- )
198
- clear_btn.click(lambda: ([], generate_user_id()), None, [chatbot, user_id_state])
199
- user_input.submit(
200
- gradio_chatbot,
201
- inputs=[user_input, chatbot, user_id_state],
202
- outputs=[chatbot, user_id_state]
203
  )
204
 
 
 
 
 
205
  if __name__ == "__main__":
206
  iface.launch()
207
 
 
163
  workflow.set_entry_point("query_or_respond")
164
  graph = workflow.compile()
165
 
166
+ #Gradio UI
167
+ import gradio as gr
168
+ # Global chat history (for demo purposes)
169
+ chat_histories = {}
170
+
171
+ def stream_response(user_input, user_id):
172
  if not user_id:
173
+ user_id = "anonymous"
174
+ state = {
175
+ "messages": [{"role": "user", "content": user_input}],
176
+ "user_id": user_id,
177
+ }
178
+
179
+ collected = ""
180
+ for step in graph.stream(state, stream_mode="values"):
181
+ message = step["messages"][-1].content
182
+ # Stream response token by token
183
+ for token in message:
184
+ collected += token
185
+ yield collected
186
+
187
+
188
+
189
+ def respond(user_message, chat_history, user_id):
190
+ if user_id not in chat_histories:
191
+ chat_histories[user_id] = []
192
+
193
+ response_generator = stream_response(user_message, user_id)
194
+ full_response = ""
195
+ for partial in response_generator:
196
+ full_response = partial
197
+ yield chat_history + [[user_message, full_response]]
198
+
199
+ chat_histories[user_id].append([user_message, full_response])
200
+
201
+ def clear_history():
202
+ return "", []
203
+
204
+ # Multilingual description
205
+ description = """
206
+ ### Was ist **Einbürgerungs-Helfer**?
207
+ **Deutsch:** Einbürgerungs-Helfer hilft dir, den Weg zur deutschen Staatsangehörigkeit zu verstehen – einfach, mehrsprachig, datenschutzfreundlich.
208
+
209
+ **English:** Einbürgerungs-Helfer helps you understand how to get German citizenship – simple, multilingual, and privacy-friendly.
210
+
211
+ **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ı.
212
+
213
+ **العربية:** يساعدك "Einbürgerungs-Helfer" على فهم كيفية الحصول على الجنسية الألمانية - بطريقة بسيطة، متعددة اللغات وتحترم الخصوصية.
214
+ """
215
+
216
+ with gr.Blocks(
217
+ theme=gr.themes.Soft(primary_hue="blue"),
218
+ css="""
219
+ @import url('https://fonts.googleapis.com/css2?family=Noto+Sans&display=swap');
220
+
221
+ * {
222
+ font-family: 'Noto Sans', sans-serif !important;
223
+ }
224
+ """
225
+ ) as iface:
226
+ gr.Markdown(f"# Einbürgerungs-Helfer\n{description}")
227
+
228
  with gr.Row():
229
+ user_id_input = gr.Textbox(label="Benutzer-ID (User ID)", placeholder="z.B. user_123", lines=1)
230
+ clear_btn = gr.Button("🗑️ Verlauf löschen")
231
+
232
+ chatbot = gr.Chatbot(
233
+ label="Chat",
234
+ show_copy_button=True,
235
+ value=[[None,
236
+ "Ich bin hier, um Ihnen bei allen Fragen zur Einbürgerung in Deutschland zu helfen.\n"
237
+ "Sie können Ihre Frage auf Deutsch, Englisch, Türkisch oder Arabisch stellen.\n"
238
+ "Lassen Sie uns loslegen – wie kann ich Ihnen heute helfen?"]]
239
+ )
240
+
241
+ msg_input = gr.Textbox(placeholder="Frage eingeben...", label="Deine Nachricht")
242
+
243
  with gr.Row():
244
+ submit_btn = gr.Button("Senden", variant="primary")
245
+
246
+ submit_btn.click(
247
+ fn=respond,
248
+ inputs=[msg_input, chatbot, user_id_input],
249
+ outputs=chatbot,
 
 
 
 
 
 
 
 
250
  )
251
 
252
+ clear_btn.click(fn=clear_history, inputs=[], outputs=[user_id_input, chatbot])
253
+
254
+ iface.launch(share=True)
255
+
256
  if __name__ == "__main__":
257
  iface.launch()
258