devalbot / app.py
gtani's picture
Refactor chat function to streamline message handling and improve clarity
adec707
raw
history blame
2.3 kB
import gradio as gr
from bedrock_client import claude_llm
from utils import load_users
AUTHS = load_users('user.csv')
# somewhere near the top of app.py:
SYSTEM_PROMPT = (
"Du bist DevalBot, ein konversationeller Assistent des Deutschen Evaluierungsinstituts "
"für Entwicklungsbewertung (DEval). DEval bietet staatlichen und zivilgesellschaftlichen "
"Organisationen in der Entwicklungszusammenarbeit unabhängige und wissenschaftlich fundierte "
"Evaluierungen. Deine Hauptsprache ist Deutsch; antworte daher standardmäßig auf Deutsch. "
"Du kannst zudem bei statistischen Analysen und Programmierung in Stata und R unterstützen."
)
def chat(user_message, history):
if not user_message.strip():
return
ui_history = history + [{"role":"user","content":user_message}]
# build a proper messages array instead of a raw prompt string
llm_messages = [{"role":"system","content":SYSTEM_PROMPT}] \
+ history \
+ [{"role":"user","content":user_message}]
full = ""
for token in claude_llm.stream(llm_messages):
full += token
yield [{"role":"assistant","content":full}]
ui_history.append({"role":"assistant","content":full})
yield ui_history
with gr.Blocks(css_paths=["static/deval.css"],theme = gr.themes.Default(primary_hue="blue", secondary_hue="yellow"),) as demo:
# ── Logo + Header + Logout ────────────────────────────────
gr.Image(
value="static/logo.png",
show_label=False,
interactive=False,
show_download_button=False,
show_fullscreen_button=False,
elem_id="logo-primary", # matches the CSS above
)
#logout_btn = gr.Button("Logout", elem_id="logout-btn")
# inject auto-reload script
gr.HTML(
"""
<script>
// Reload the page after 1 minutes (300 000 ms)
setTimeout(() => {
window.location.reload();
}, 1000);
</script>
"""
)
gr.ChatInterface(
chat,
type="messages",
editable=True,
concurrency_limit=200,
save_history=True,
)
demo.queue().launch(auth=AUTHS,share=True, ssr_mode=False)