Spaces:
Running
Running
import gradio as gr | |
from utils import load_users | |
from bedrock_client import bedrock_llm | |
from langchain.schema import SystemMessage, HumanMessage, AIMessage | |
import os | |
AUTHS = os.environ.get('USERS') | |
def chat(message, history): | |
# 3a) Build Bedrock input (with system prompt + raw dict‐history) | |
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." | |
) | |
# 1) start with the system prompt | |
history_langchain_format: list = [SystemMessage(content=system_prompt)] | |
# 2) replay the user/assistant turns | |
for msg in history: | |
if msg["role"] == "user": | |
history_langchain_format.append(HumanMessage(content=msg["content"])) | |
elif msg["role"] == "assistant": | |
history_langchain_format.append(AIMessage(content=msg["content"])) | |
# 3) append the new user message | |
history_langchain_format.append(HumanMessage(content=message)) | |
stream =bedrock_llm.stream(history_langchain_format) | |
full = next(stream) | |
for chunk in stream: | |
full +=chunk | |
yield full.content | |
with gr.Blocks(css_paths=["static/deval.css"],theme = gr.themes.Default(primary_hue="blue", secondary_hue="yellow"),) as demo: | |
# ── Logo + Header + Logout ──────────────────────────────── | |
with gr.Row(): | |
with gr.Column(scale=1): | |
gr.Image( | |
value="static/logo.png", | |
height=50, | |
show_label=False, | |
interactive=False, | |
show_download_button=False, | |
show_fullscreen_button=False, | |
elem_id="logo-primary", # matches the CSS above | |
) | |
with gr.Column(scale=10): | |
gr.Markdown( | |
"# DEvalBot\n\n" | |
"**Hinweis:** Bitte gebe keine vertraulichen Informationen ein. " | |
"Dazu zählen u.a. sensible personenbezogene Daten, institutsinterne " | |
"Informationen oder Dokumente, unveröffentlichte Berichtsinhalte, " | |
"vertrauliche Informationen oder Dokumente externer Organisationen " | |
"sowie sensible erhobene Daten (wie etwa Interviewtranskripte).", elem_id="header-text" | |
) | |
#Hinweis: Bitte gebe keine vertraulichen Informationen ein. Dazu zählen u.a. sensible personenbezogene Daten, institutsinterne Informationen oder Dokumente, unveröffentlichte Berichtsinhalte, vertrauliche Informationen oder Dokumente externer Organisationen sowie sensible erhobene Daten (wie etwa Interviewtranskripte). | |
#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) |