Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,80 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
-
load_dotenv() # loads variables from a local .env for local testing
|
| 4 |
|
| 5 |
-
|
| 6 |
-
from
|
| 7 |
-
from
|
| 8 |
from pinecone import Pinecone
|
| 9 |
-
from
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
model="gpt-4.1-2025-04-14",
|
| 16 |
-
temperature=0.0,
|
| 17 |
-
)
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
pinecone_api_key = os.getenv("PINECONE_API_KEY")
|
| 22 |
-
pinecone_index_name = os.getenv("PINECONE_INDEX_NAME")
|
| 23 |
-
pc = Pinecone(api_key=pinecone_api_key)
|
| 24 |
-
index = pc.Index(pinecone_index_name)
|
| 25 |
-
vector_store = PineconeVectorStore(embedding=embeddings, index=index)
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
def log_interaction_cloud(user_id: str, question: str, answer: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
payload = {
|
| 38 |
"user_id": user_id,
|
| 39 |
"question": question,
|
|
@@ -42,135 +83,215 @@ def log_interaction_cloud(user_id: str, question: str, answer: str):
|
|
| 42 |
}
|
| 43 |
try:
|
| 44 |
res = supabase.table("logs").insert(payload).execute()
|
| 45 |
-
|
| 46 |
-
print(f"[Supabase Error #{res.status_code}]:", res.data)
|
| 47 |
-
else:
|
| 48 |
-
print("[Supabase Success]:", res.data)
|
| 49 |
except Exception as e:
|
| 50 |
-
print("[Supabase Exception]
|
| 51 |
|
| 52 |
-
# 3) Conversation memory & user IDs
|
| 53 |
-
import uuid
|
| 54 |
-
from langchain.memory import ConversationBufferWindowMemory
|
| 55 |
def generate_user_id():
|
| 56 |
return "user_" + uuid.uuid4().hex[:6]
|
| 57 |
|
|
|
|
| 58 |
memory = ConversationBufferWindowMemory(
|
| 59 |
memory_key="chat_history",
|
| 60 |
return_messages=True,
|
| 61 |
k=5
|
| 62 |
)
|
| 63 |
|
| 64 |
-
# 4) Retrieval tool
|
| 65 |
-
from langchain_core.tools import tool
|
| 66 |
-
|
| 67 |
@tool(response_format="content_and_artifact")
|
| 68 |
def retrieve(query: str):
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
| 80 |
|
| 81 |
SYSTEM_MESSAGE_TEXT = (
|
| 82 |
-
"You are
|
|
|
|
|
|
|
|
|
|
| 83 |
)
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
# filter off-topic if needed…
|
| 89 |
-
prompt = [sys_msg] + raw_hist[-5:]
|
| 90 |
-
llm_with_tools = llm.bind_tools([retrieve])
|
| 91 |
-
resp = llm_with_tools.invoke(prompt)
|
| 92 |
-
# log if direct
|
| 93 |
-
user_q = [m.content for m in state["messages"] if m.type=="human"][-1]
|
| 94 |
-
memory.save_context({"input": user_q}, {"output": resp.content})
|
| 95 |
-
if not getattr(resp, "tool_calls", None):
|
| 96 |
-
log_interaction_cloud(state.get("user_id",""), user_q, resp.content)
|
| 97 |
-
return {"messages": state["messages"] + [resp]}
|
| 98 |
-
|
| 99 |
-
tools_node = ToolNode([retrieve])
|
| 100 |
-
|
| 101 |
-
def generate(state: MessagesState):
|
| 102 |
-
# collect any tool outputs
|
| 103 |
-
tool_msgs = []
|
| 104 |
-
for m in reversed(state["messages"]):
|
| 105 |
-
if m.type=="tool": tool_msgs.append(m)
|
| 106 |
-
else: break
|
| 107 |
-
docs = "\n\n".join(m.content for m in tool_msgs[::-1])
|
| 108 |
-
prompt = [SystemMessage(SYSTEM_MESSAGE_TEXT + ("\n\n"+docs if docs else ""))]
|
| 109 |
-
# build recent convo
|
| 110 |
-
convo = []
|
| 111 |
-
for m in state["messages"]:
|
| 112 |
-
if m.type=="human" or (m.type=="ai" and not getattr(m,"tool_calls",None)):
|
| 113 |
-
convo.append(m)
|
| 114 |
-
prompt += convo[-6:]
|
| 115 |
-
resp = llm.invoke(prompt)
|
| 116 |
-
# log final
|
| 117 |
-
user_q = [m.content for m in state["messages"] if m.type=="human"][-1]
|
| 118 |
-
log_interaction_cloud(state.get("user_id",""), user_q, resp.content)
|
| 119 |
-
return {"messages": state["messages"] + [resp]}
|
| 120 |
-
|
| 121 |
-
g = StateGraph(MessagesState)
|
| 122 |
-
g.add_node(query_or_respond)
|
| 123 |
-
g.add_node(tools_node)
|
| 124 |
-
g.add_node(generate)
|
| 125 |
-
g.set_entry_point("query_or_respond")
|
| 126 |
-
g.add_conditional_edges("query_or_respond", tools_condition, {END: END, "tools": "tools"})
|
| 127 |
-
g.add_edge("tools", "generate")
|
| 128 |
-
g.add_edge("generate", END)
|
| 129 |
-
graph = g.compile()
|
| 130 |
-
|
| 131 |
-
# 6) Gradio UI
|
| 132 |
-
import gradio as gr
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
chat_histories = {}
|
| 135 |
|
| 136 |
-
def
|
| 137 |
-
if not
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
def clear_history():
|
| 156 |
return "", []
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
-
with gr.Blocks() as iface:
|
| 162 |
-
gr.Markdown("# Einbürgerungs-Helfer\n" + description)
|
| 163 |
with gr.Row():
|
| 164 |
-
user_id_input = gr.Textbox(
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
| 9 |
+
from langchain_community.vectorstores import Pinecone as PineconeVectorStore
|
| 10 |
from pinecone import Pinecone
|
| 11 |
+
from supabase import create_client, Client
|
| 12 |
+
from langchain.memory import ConversationBufferWindowMemory
|
| 13 |
+
from langchain_core.messages import SystemMessage, HumanMessage
|
| 14 |
+
from langchain_core.tools import tool
|
| 15 |
+
from langgraph.graph import StateGraph, END
|
| 16 |
+
from langgraph.prebuilt import ToolNode, tools_condition
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Load environment variables
|
| 19 |
+
load_dotenv()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Environment variables with better error handling
|
| 22 |
+
SUPABASE_URL = os.environ.get("SUPABASE_URL")
|
| 23 |
+
SUPABASE_KEY = os.environ.get("SUPABASE_KEY")
|
| 24 |
+
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
| 25 |
+
LANGSMITH_API_KEY = os.environ.get("LANGSMITH_API_KEY")
|
| 26 |
+
PINECONE_API_KEY = os.environ.get("PINECONE_API_KEY")
|
| 27 |
+
PINECONE_INDEX = os.environ.get("PINECONE_INDEX", "natstudy")
|
| 28 |
|
| 29 |
+
# Check for required environment variables
|
| 30 |
+
required_vars = {
|
| 31 |
+
"SUPABASE_URL": SUPABASE_URL,
|
| 32 |
+
"SUPABASE_KEY": SUPABASE_KEY,
|
| 33 |
+
"OPENAI_API_KEY": OPENAI_API_KEY,
|
| 34 |
+
"PINECONE_API_KEY": PINECONE_API_KEY
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
missing_vars = [var for var, value in required_vars.items() if not value]
|
| 38 |
+
if missing_vars:
|
| 39 |
+
raise ValueError(f"Missing required environment variables: {', '.join(missing_vars)}")
|
| 40 |
+
|
| 41 |
+
# Model and vector store setup
|
| 42 |
+
try:
|
| 43 |
+
llm = ChatOpenAI(
|
| 44 |
+
model="gpt-4o-mini", # Use a more reliable model
|
| 45 |
+
temperature=0.0,
|
| 46 |
+
api_key=OPENAI_API_KEY
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
embeddings = OpenAIEmbeddings(
|
| 50 |
+
model="text-embedding-3-small", # Smaller model for cost efficiency
|
| 51 |
+
api_key=OPENAI_API_KEY
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# Pinecone setup
|
| 55 |
+
pc = Pinecone(api_key=PINECONE_API_KEY)
|
| 56 |
+
|
| 57 |
+
vector_store = PineconeVectorStore(
|
| 58 |
+
embedding=embeddings,
|
| 59 |
+
index_name=PINECONE_INDEX,
|
| 60 |
+
pinecone_api_key=PINECONE_API_KEY
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# Supabase
|
| 64 |
+
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 65 |
+
|
| 66 |
+
except Exception as e:
|
| 67 |
+
print(f"Setup error: {e}")
|
| 68 |
+
# Create dummy implementations for development
|
| 69 |
+
llm = None
|
| 70 |
+
vector_store = None
|
| 71 |
+
supabase = None
|
| 72 |
|
| 73 |
def log_interaction_cloud(user_id: str, question: str, answer: str):
|
| 74 |
+
if not supabase:
|
| 75 |
+
print("Supabase not configured")
|
| 76 |
+
return
|
| 77 |
+
|
| 78 |
payload = {
|
| 79 |
"user_id": user_id,
|
| 80 |
"question": question,
|
|
|
|
| 83 |
}
|
| 84 |
try:
|
| 85 |
res = supabase.table("logs").insert(payload).execute()
|
| 86 |
+
print("[Supabase Success]")
|
|
|
|
|
|
|
|
|
|
| 87 |
except Exception as e:
|
| 88 |
+
print(f"[Supabase Exception]: {e}")
|
| 89 |
|
|
|
|
|
|
|
|
|
|
| 90 |
def generate_user_id():
|
| 91 |
return "user_" + uuid.uuid4().hex[:6]
|
| 92 |
|
| 93 |
+
# Initialize memory
|
| 94 |
memory = ConversationBufferWindowMemory(
|
| 95 |
memory_key="chat_history",
|
| 96 |
return_messages=True,
|
| 97 |
k=5
|
| 98 |
)
|
| 99 |
|
|
|
|
|
|
|
|
|
|
| 100 |
@tool(response_format="content_and_artifact")
|
| 101 |
def retrieve(query: str):
|
| 102 |
+
"""Retrieve information related to the query."""
|
| 103 |
+
if not vector_store:
|
| 104 |
+
return "Vector store not available", []
|
| 105 |
+
|
| 106 |
+
try:
|
| 107 |
+
retrieved_docs = vector_store.similarity_search(query, k=5)
|
| 108 |
+
serialized = "\n\n".join(
|
| 109 |
+
f"Source: {doc.metadata}\nContent: {doc.page_content}"
|
| 110 |
+
for doc in retrieved_docs
|
| 111 |
+
)
|
| 112 |
+
return serialized, retrieved_docs
|
| 113 |
+
except Exception as e:
|
| 114 |
+
return f"Retrieval error: {e}", []
|
| 115 |
|
| 116 |
SYSTEM_MESSAGE_TEXT = (
|
| 117 |
+
"You are 'Einbürgerungs-Helfer', a highly skilled, friendly, and helpful administrative "
|
| 118 |
+
"assistant in Germany. Your only task is to support people who are eligible for naturalization "
|
| 119 |
+
"but have not yet applied. You provide accurate information about German citizenship requirements, "
|
| 120 |
+
"application processes, and related topics. You can respond in German, English, Turkish, or Arabic."
|
| 121 |
)
|
| 122 |
|
| 123 |
+
class MyState(TypedDict):
|
| 124 |
+
messages: list
|
| 125 |
+
user_id: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
+
def query_or_respond(state: MyState):
|
| 128 |
+
if not llm:
|
| 129 |
+
return {"messages": state["messages"] + [{"role": "assistant", "content": "Service temporarily unavailable"}], "user_id": state.get("user_id", "unknown")}
|
| 130 |
+
|
| 131 |
+
try:
|
| 132 |
+
system_message = SystemMessage(SYSTEM_MESSAGE_TEXT)
|
| 133 |
+
raw_history = memory.load_memory_variables({})["chat_history"]
|
| 134 |
+
|
| 135 |
+
# Filter out repeated messages
|
| 136 |
+
filtered_history = [msg for msg in raw_history if not (
|
| 137 |
+
hasattr(msg, 'content') and msg.content and "I can only answer questions related" in msg.content
|
| 138 |
+
)]
|
| 139 |
+
|
| 140 |
+
prompt = [system_message] + filtered_history[-5:]
|
| 141 |
+
llm_with_tools = llm.bind_tools([retrieve])
|
| 142 |
+
response = llm_with_tools.invoke(prompt)
|
| 143 |
+
|
| 144 |
+
user_id = state.get("user_id", "unknown_user")
|
| 145 |
+
user_question = [m["content"] for m in state["messages"] if m.get("role") == "user"][-1]
|
| 146 |
+
|
| 147 |
+
memory.save_context({"input": user_question}, {"output": response.content})
|
| 148 |
+
|
| 149 |
+
if not getattr(response, "tool_calls", None):
|
| 150 |
+
log_interaction_cloud(user_id, user_question, response.content)
|
| 151 |
+
|
| 152 |
+
return {"messages": state["messages"] + [response], "user_id": user_id}
|
| 153 |
+
except Exception as e:
|
| 154 |
+
error_msg = f"Error processing request: {str(e)}"
|
| 155 |
+
return {"messages": state["messages"] + [{"role": "assistant", "content": error_msg}], "user_id": state.get("user_id", "unknown")}
|
| 156 |
+
|
| 157 |
+
def generate(state: MyState):
|
| 158 |
+
try:
|
| 159 |
+
last_message = state["messages"][-1]
|
| 160 |
+
user_id = state.get("user_id", "unknown_user")
|
| 161 |
+
user_question = [m["content"] for m in state["messages"] if m.get("role") == "user"][-1]
|
| 162 |
+
answer_text = getattr(last_message, "content", str(last_message))
|
| 163 |
+
log_interaction_cloud(user_id, user_question, answer_text)
|
| 164 |
+
return {"messages": state["messages"], "user_id": user_id}
|
| 165 |
+
except Exception as e:
|
| 166 |
+
print(f"Generate error: {e}")
|
| 167 |
+
return {"messages": state["messages"], "user_id": state.get("user_id", "unknown")}
|
| 168 |
+
|
| 169 |
+
# LangGraph setup
|
| 170 |
+
workflow = StateGraph(MyState)
|
| 171 |
+
workflow.add_node("query_or_respond", query_or_respond)
|
| 172 |
+
workflow.add_node("tool", ToolNode([retrieve]))
|
| 173 |
+
workflow.add_node("generate", generate)
|
| 174 |
+
workflow.add_conditional_edges("query_or_respond", tools_condition, {
|
| 175 |
+
"tool": "tool",
|
| 176 |
+
"generate": "generate"
|
| 177 |
+
})
|
| 178 |
+
workflow.add_edge("tool", "generate")
|
| 179 |
+
workflow.add_edge("generate", END)
|
| 180 |
+
workflow.set_entry_point("query_or_respond")
|
| 181 |
+
graph = workflow.compile()
|
| 182 |
+
|
| 183 |
+
# Gradio UI
|
| 184 |
chat_histories = {}
|
| 185 |
|
| 186 |
+
def respond(user_message, chat_history, user_id):
|
| 187 |
+
if not user_message.strip():
|
| 188 |
+
return chat_history
|
| 189 |
+
|
| 190 |
+
if not user_id:
|
| 191 |
+
user_id = generate_user_id()
|
| 192 |
+
|
| 193 |
+
if user_id not in chat_histories:
|
| 194 |
+
chat_histories[user_id] = []
|
| 195 |
+
|
| 196 |
+
try:
|
| 197 |
+
state = {
|
| 198 |
+
"messages": [{"role": "user", "content": user_message}],
|
| 199 |
+
"user_id": user_id,
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
# Get response from graph
|
| 203 |
+
result = graph.invoke(state)
|
| 204 |
+
bot_response = result["messages"][-1].content if result["messages"] else "Sorry, I couldn't process your request."
|
| 205 |
+
|
| 206 |
+
new_history = chat_history + [[user_message, bot_response]]
|
| 207 |
+
chat_histories[user_id] = new_history
|
| 208 |
+
|
| 209 |
+
return new_history
|
| 210 |
+
except Exception as e:
|
| 211 |
+
error_response = f"I'm sorry, I encountered an error: {str(e)}"
|
| 212 |
+
return chat_history + [[user_message, error_response]]
|
| 213 |
|
| 214 |
def clear_history():
|
| 215 |
return "", []
|
| 216 |
|
| 217 |
+
# Multilingual description
|
| 218 |
+
description = """
|
| 219 |
+
### Was ist **Einbürgerungs-Helfer**?
|
| 220 |
+
**Deutsch:** Einbürgerungs-Helfer hilft dir, den Weg zur deutschen Staatsangehörigkeit zu verstehen – einfach, mehrsprachig, datenschutzfreundlich.
|
| 221 |
+
|
| 222 |
+
**English:** Einbürgerungs-Helfer helps you understand how to get German citizenship – simple, multilingual, and privacy-friendly.
|
| 223 |
+
|
| 224 |
+
**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ı.
|
| 225 |
+
|
| 226 |
+
**العربية:** يساعدك "Einbürgerungs-Helfer" على فهم كيفية الحصول على الجنسية الألمانية - بطريقة بسيطة، متعددة اللغات وتحترم الخصوصية.
|
| 227 |
+
"""
|
| 228 |
+
|
| 229 |
+
# Create interface
|
| 230 |
+
with gr.Blocks(
|
| 231 |
+
theme=gr.themes.Soft(primary_hue="blue"),
|
| 232 |
+
css="""
|
| 233 |
+
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans&display=swap');
|
| 234 |
+
* {
|
| 235 |
+
font-family: 'Noto Sans', sans-serif !important;
|
| 236 |
+
}
|
| 237 |
+
"""
|
| 238 |
+
) as iface:
|
| 239 |
+
gr.Markdown(f"# 🇩 Einbürgerungs-Helfer\n{description}")
|
| 240 |
|
|
|
|
|
|
|
| 241 |
with gr.Row():
|
| 242 |
+
user_id_input = gr.Textbox(
|
| 243 |
+
label="Benutzer-ID (User ID)",
|
| 244 |
+
placeholder="z.B. user_123",
|
| 245 |
+
lines=1,
|
| 246 |
+
value=generate_user_id()
|
| 247 |
+
)
|
| 248 |
+
clear_btn = gr.Button("🗑️ Verlauf löschen")
|
| 249 |
+
|
| 250 |
+
chatbot = gr.Chatbot(
|
| 251 |
+
label="Chat",
|
| 252 |
+
show_copy_button=True,
|
| 253 |
+
value=[[None,
|
| 254 |
+
"Hallo! Ich bin hier, um Ihnen bei allen Fragen zur Einbürgerung in Deutschland zu helfen.\n\n"
|
| 255 |
+
"Sie können Ihre Frage auf Deutsch, Englisch, Türkisch oder Arabisch stellen.\n\n"
|
| 256 |
+
"Wie kann ich Ihnen heute helfen?"]]
|
| 257 |
+
)
|
| 258 |
+
|
| 259 |
+
msg_input = gr.Textbox(
|
| 260 |
+
placeholder="Frage eingeben...",
|
| 261 |
+
label="Deine Nachricht",
|
| 262 |
+
lines=1
|
| 263 |
+
)
|
| 264 |
+
|
| 265 |
+
submit_btn = gr.Button("Senden", variant="primary")
|
| 266 |
+
|
| 267 |
+
# Event handlers
|
| 268 |
+
submit_btn.click(
|
| 269 |
+
fn=respond,
|
| 270 |
+
inputs=[msg_input, chatbot, user_id_input],
|
| 271 |
+
outputs=[chatbot]
|
| 272 |
+
).then(
|
| 273 |
+
lambda: "",
|
| 274 |
+
outputs=[msg_input]
|
| 275 |
+
)
|
| 276 |
+
|
| 277 |
+
msg_input.submit(
|
| 278 |
+
fn=respond,
|
| 279 |
+
inputs=[msg_input, chatbot, user_id_input],
|
| 280 |
+
outputs=[chatbot]
|
| 281 |
+
).then(
|
| 282 |
+
lambda: "",
|
| 283 |
+
outputs=[msg_input]
|
| 284 |
+
)
|
| 285 |
+
|
| 286 |
+
clear_btn.click(
|
| 287 |
+
fn=clear_history,
|
| 288 |
+
outputs=[user_id_input, chatbot]
|
| 289 |
+
)
|
| 290 |
+
|
| 291 |
+
# Launch configuration for Hugging Face Spaces
|
| 292 |
+
if __name__ == "__main__":
|
| 293 |
+
iface.launch(
|
| 294 |
+
server_name="0.0.0.0",
|
| 295 |
+
server_port=7860,
|
| 296 |
+
share=False # Set to False for Spaces
|
| 297 |
+
)
|