gtani commited on
Commit
0ffc7ed
Β·
1 Parent(s): adec707

Refactor chat function for improved message handling and integrate Bedrock chat model; update CSS for enhanced styling and layout

Browse files
Files changed (3) hide show
  1. app.py +51 -34
  2. bedrock_client.py +20 -30
  3. static/deval.css +39 -12
app.py CHANGED
@@ -1,51 +1,68 @@
1
  import gradio as gr
2
- from bedrock_client import claude_llm
3
  from utils import load_users
 
 
 
4
 
5
- AUTHS = load_users('user.csv')
6
 
 
7
 
8
- # somewhere near the top of app.py:
9
- SYSTEM_PROMPT = (
10
- "Du bist DevalBot, ein konversationeller Assistent des Deutschen Evaluierungsinstituts "
11
- "fΓΌr Entwicklungsbewertung (DEval). DEval bietet staatlichen und zivilgesellschaftlichen "
12
- "Organisationen in der Entwicklungszusammenarbeit unabhΓ€ngige und wissenschaftlich fundierte "
13
- "Evaluierungen. Deine Hauptsprache ist Deutsch; antworte daher standardmÀßig auf Deutsch. "
14
- "Du kannst zudem bei statistischen Analysen und Programmierung in Stata und R unterstΓΌtzen."
15
- )
16
-
17
- def chat(user_message, history):
18
- if not user_message.strip():
19
- return
20
 
21
- ui_history = history + [{"role":"user","content":user_message}]
 
22
 
23
- # build a proper messages array instead of a raw prompt string
24
- llm_messages = [{"role":"system","content":SYSTEM_PROMPT}] \
25
- + history \
26
- + [{"role":"user","content":user_message}]
 
 
27
 
28
- full = ""
29
- for token in claude_llm.stream(llm_messages):
30
- full += token
31
- yield [{"role":"assistant","content":full}]
32
 
33
- ui_history.append({"role":"assistant","content":full})
34
- yield ui_history
 
 
 
35
 
36
 
37
  with gr.Blocks(css_paths=["static/deval.css"],theme = gr.themes.Default(primary_hue="blue", secondary_hue="yellow"),) as demo:
38
  # ── Logo + Header + Logout ────────────────────────────────
39
 
40
- gr.Image(
41
- value="static/logo.png",
42
- show_label=False,
43
- interactive=False,
44
- show_download_button=False,
45
- show_fullscreen_button=False,
46
- elem_id="logo-primary", # matches the CSS above
47
- )
48
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  #logout_btn = gr.Button("Logout", elem_id="logout-btn")
50
  # inject auto-reload script
51
  gr.HTML(
 
1
  import gradio as gr
 
2
  from utils import load_users
3
+ from bedrock_client import bedrock_llm
4
+ from langchain.schema import SystemMessage, HumanMessage, AIMessage
5
+ import os
6
 
 
7
 
8
+ AUTHS = os.environ.get('USERS')
9
 
10
+ def chat(message, history):
11
+ # 3a) Build Bedrock input (with system prompt + raw dict‐history)
12
+ system_prompt = (
13
+ "Du bist DevalBot, ein konversationeller Assistent des Deutschen Evaluierungsinstituts "
14
+ "fΓΌr Entwicklungsbewertung (DEval). DEval bietet staatlichen und zivilgesellschaftlichen "
15
+ "Organisationen in der Entwicklungszusammenarbeit unabhΓ€ngige und wissenschaftlich fundierte "
16
+ "Evaluierungen. Deine Hauptsprache ist Deutsch; antworte daher standardmÀßig auf Deutsch. "
17
+ "Du kannst zudem bei statistischen Analysen und Programmierung in Stata und R unterstΓΌtzen."
18
+ )
 
 
 
19
 
20
+ # 1) start with the system prompt
21
+ history_langchain_format: list = [SystemMessage(content=system_prompt)]
22
 
23
+ # 2) replay the user/assistant turns
24
+ for msg in history:
25
+ if msg["role"] == "user":
26
+ history_langchain_format.append(HumanMessage(content=msg["content"]))
27
+ elif msg["role"] == "assistant":
28
+ history_langchain_format.append(AIMessage(content=msg["content"]))
29
 
30
+ # 3) append the new user message
31
+ history_langchain_format.append(HumanMessage(content=message))
 
 
32
 
33
+ stream =bedrock_llm.stream(history_langchain_format)
34
+ full = next(stream)
35
+ for chunk in stream:
36
+ full +=chunk
37
+ yield full.content
38
 
39
 
40
  with gr.Blocks(css_paths=["static/deval.css"],theme = gr.themes.Default(primary_hue="blue", secondary_hue="yellow"),) as demo:
41
  # ── Logo + Header + Logout ────────────────────────────────
42
 
43
+
44
+ with gr.Row():
45
+ with gr.Column(scale=1):
46
+ gr.Image(
47
+ value="static/logo.png",
48
+ height=50,
49
+ show_label=False,
50
+ interactive=False,
51
+ show_download_button=False,
52
+ show_fullscreen_button=False,
53
+ elem_id="logo-primary", # matches the CSS above
54
+ )
55
+ with gr.Column(scale=10):
56
+ gr.Markdown(
57
+ "# DEvalBot\n\n"
58
+ "**Hinweis:** Bitte gebe keine vertraulichen Informationen ein. "
59
+ "Dazu zΓ€hlen u.a. sensible personenbezogene Daten, institutsinterne "
60
+ "Informationen oder Dokumente, unverΓΆffentlichte Berichtsinhalte, "
61
+ "vertrauliche Informationen oder Dokumente externer Organisationen "
62
+ "sowie sensible erhobene Daten (wie etwa Interviewtranskripte).", elem_id="header-text"
63
+ )
64
+
65
+ #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).
66
  #logout_btn = gr.Button("Logout", elem_id="logout-btn")
67
  # inject auto-reload script
68
  gr.HTML(
bedrock_client.py CHANGED
@@ -1,38 +1,28 @@
 
1
  from anthropic import AnthropicBedrock
 
2
  from langchain_aws.llms.bedrock import BedrockLLM
3
- import os
4
 
5
- def get_anthropic_client():
6
- return AnthropicBedrock(
7
- aws_access_key=os.environ["AWS_ACCESS_KEY_ID"],
8
- aws_secret_key=os.environ["AWS_SECRET_ACCESS_KEY"],
9
- aws_region=os.environ.get("AWS_DEFAULT_REGION", "eu-central-1")
10
- )
11
 
12
- def claude_stream_response(messages, client):
13
- stream = client.messages.create(
14
- model="anthropic.claude-v2:1",
15
- max_tokens=1024,
16
- temperature=0.7,
17
- messages=messages,
18
- stream=True
19
- )
 
 
 
 
 
 
 
 
 
 
20
 
21
- for event in stream:
22
- # Only yield parts that have actual text deltas
23
- if event.type == "content_block_delta":
24
- text = getattr(event.delta, "text", None)
25
- if text:
26
- yield text
27
 
28
 
29
 
30
- claude_llm = BedrockLLM(
31
- aws_access_key_id = os.environ["AWS_ACCESS_KEY_ID"],
32
- aws_secret_access_key = os.environ["AWS_SECRET_ACCESS_KEY"],
33
- region_name = "eu-central-1",
34
- provider = "anthropic",
35
- model_id = "anthropic.claude-v2:1",
36
- streaming = True,
37
- model_kwargs = {"temperature": 0.7},
38
- )
 
1
+ import os
2
  from anthropic import AnthropicBedrock
3
+ from langchain_aws.chat_models import ChatBedrockConverse
4
  from langchain_aws.llms.bedrock import BedrockLLM
 
5
 
 
 
 
 
 
 
6
 
7
+ # bedrock_llm1 = BedrockLLM(
8
+ # aws_access_key_id = os.environ["AWS_ACCESS_KEY_ID"],
9
+ # aws_secret_access_key = os.environ["AWS_SECRET_ACCESS_KEY"],
10
+ # region_name = "eu-west-1",
11
+ # provider = "mistral",
12
+ # model_id = "mistral.mistral-large-2402-v1:0",
13
+ # streaming = True,
14
+ # model_kwargs = {"temperature": 0.7},
15
+ # )
16
+
17
+ # Initialize the streaming Bedrock chat model
18
+ bedrock_llm = ChatBedrockConverse(
19
+ aws_access_key_id =os.environ.get("AWS_ACCESS_KEY_ID"),
20
+ aws_secret_access_key =os.environ.get("AWS_SECRET_ACCESS_KEY"),
21
+ region_name =os.environ.get("AWS_DEFAULT_REGION", "eu-west-1"),
22
+ provider = "mistral",
23
+ model_id =os.environ.get("MODEL_ID", "mistral.mistral-large-2402-v1:0"), # or your preferred Bedrock model
24
+ temperature= 0.7)
25
 
 
 
 
 
 
 
26
 
27
 
28
 
 
 
 
 
 
 
 
 
 
static/deval.css CHANGED
@@ -4,25 +4,52 @@
4
  --color-brand-secondary: #0D456C;
5
  }
6
 
7
-
8
- /* ── Page background ───────────────────────────── */
9
  body, .gradio-container {
10
- background-color: #F2F6F8 !important; /* your desired page bg */
11
  }
12
 
13
- /* ── All buttons ───────────────────────────────── */
14
  button.gr-button {
15
- background-color: #0D456C !important; /* your desired button bg */
16
- color: #F2F6F8 !important; /* button text color */
17
  }
18
-
19
- /* optional: hover state */
20
  button.gr-button:hover {
21
  background-color: #0056b3 !important;
22
  }
23
 
24
- /* ── Global button look ──────────────────────────────────────── */
25
- button.gr-button {
26
- background-color: #0D456C !important;
27
- color: #E7AB12 !important;
28
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  --color-brand-secondary: #0D456C;
5
  }
6
 
7
+ /* ── Page background ────────────────────────────────────────── */
 
8
  body, .gradio-container {
9
+ background-color: #003C66 !important;
10
  }
11
 
12
+ /* ── Global button look ─────────────────────────────────────── */
13
  button.gr-button {
14
+ background-color: #003C66 !important;
15
+ color: #E7AB12 !important;
16
  }
 
 
17
  button.gr-button:hover {
18
  background-color: #0056b3 !important;
19
  }
20
 
21
+ /* force the logo container to left‐align its child */
22
+ #logo-primary {
23
+ display: block; /* ensure it’s a block container */
24
+ text-align: left !important;
25
  }
26
+
27
+ /* still keep your size constraints */
28
+ #logo-primary img {
29
+ max-width: 80%;
30
+ max-height: 90%;
31
+ height: auto;
32
+ object-fit: contain;
33
+ margin: 2 !important;
34
+
35
+ /* nix any auto‐margins that might center it */
36
+ }
37
+
38
+ /* ── Header Markdown styling ───────────────────────────────── */
39
+ #header-text {
40
+ /* overall container */
41
+ font-size: 1.75rem; /* ~23px */
42
+ line-height: 1.6;
43
+ font-weight: bold;
44
+ color: #C1CDDF !important;
45
+ }
46
+
47
+ /* ensure all child text elements inherit that color */
48
+ #header-text,
49
+ #header-text h1,
50
+ #header-text p,
51
+ #header-text strong,
52
+ #header-text b,
53
+ #header-text em {
54
+ color: #C1CDDF !important;
55
+ }