Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,138 +1,138 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
|
7 |
|
8 |
-
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
#
|
15 |
-
|
16 |
|
17 |
-
|
18 |
|
19 |
-
|
20 |
|
21 |
|
22 |
-
|
23 |
|
24 |
-
#
|
25 |
-
|
26 |
|
27 |
-
#
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
|
34 |
-
#
|
35 |
-
|
36 |
|
37 |
-
|
38 |
|
39 |
-
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
|
45 |
|
46 |
-
|
47 |
-
#
|
48 |
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
|
71 |
-
#
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
|
91 |
|
92 |
|
93 |
-
|
94 |
|
95 |
-
import gradio as gr
|
96 |
|
97 |
-
# Replace this with your target URL
|
98 |
-
NEW_PAGE_URL = "https://huggingface.co/spaces/evaluatorhub42/DEvalbot"
|
99 |
|
100 |
-
with gr.Blocks(css_paths=["static/deval.css"], theme=gr.themes.Default(primary_hue="blue", secondary_hue="yellow")) as demo:
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
demo.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from bedrock_client import bedrock_llm
|
3 |
+
from langchain.schema import SystemMessage, HumanMessage, AIMessage
|
4 |
+
import os
|
5 |
+
from distutils.util import strtobool
|
6 |
|
7 |
|
8 |
+
MULTIMODAL = os.environ.get("MULTIMODAL", "false")
|
9 |
|
10 |
+
# 1) convert common truthy/falsy strings to bool
|
11 |
+
try:
|
12 |
+
MULTIMODAL = bool(strtobool(MULTIMODAL))
|
13 |
+
except ValueError:
|
14 |
+
# catch unrecognized values
|
15 |
+
print(f"Invalid MULTIMODAL value: Use true/false, 1/0, yes/no.")
|
16 |
|
17 |
+
AUTHS = [(os.environ.get('USER'), os.environ.get('PW'))]
|
18 |
|
19 |
+
SYSTEM_PROMPT = os.environ.get('SYSTEM_PROMPT', '')
|
20 |
|
21 |
|
22 |
+
def chat(message, history):
|
23 |
|
24 |
+
# 1) start with the system prompt
|
25 |
+
history_langchain_format: list = [SystemMessage(content=SYSTEM_PROMPT)]
|
26 |
|
27 |
+
# 2) replay the user/assistant turns
|
28 |
+
for msg in history:
|
29 |
+
if msg["role"] == "user":
|
30 |
+
history_langchain_format.append(HumanMessage(content=msg["content"]))
|
31 |
+
elif msg["role"] == "assistant":
|
32 |
+
history_langchain_format.append(AIMessage(content=msg["content"]))
|
33 |
|
34 |
+
# 3) append the new user message
|
35 |
+
history_langchain_format.append(HumanMessage(content=message))
|
36 |
|
37 |
+
stream =bedrock_llm.stream(history_langchain_format)
|
38 |
|
39 |
+
full = next(stream)
|
40 |
|
41 |
+
for chunk in stream:
|
42 |
+
full +=chunk
|
43 |
+
yield full.content
|
44 |
|
45 |
|
46 |
+
with gr.Blocks(css_paths=["static/deval.css"],theme = gr.themes.Default(primary_hue="blue", secondary_hue="yellow"),) as demo:
|
47 |
+
# ββ Logo + Header + Logout ββββββββββββββββββββββββββββββββ
|
48 |
|
49 |
|
50 |
+
with gr.Row():
|
51 |
+
with gr.Column(scale=1):
|
52 |
+
gr.Image(
|
53 |
+
value="static/logo.png",
|
54 |
+
height=50,
|
55 |
+
show_label=False,
|
56 |
+
interactive=False,
|
57 |
+
show_download_button=False,
|
58 |
+
show_fullscreen_button=False,
|
59 |
+
elem_id="logo-primary", # matches the CSS above
|
60 |
+
)
|
61 |
+
with gr.Column(scale=10):
|
62 |
+
gr.Markdown(
|
63 |
+
"# DEvalBot\n\n"
|
64 |
+
"**Hinweis:** Bitte gebe keine vertraulichen Informationen ein. "
|
65 |
+
"Dazu zΓ€hlen u.a. sensible personenbezogene Daten, institutsinterne "
|
66 |
+
"Informationen oder Dokumente, unverΓΆffentlichte Berichtsinhalte, "
|
67 |
+
"vertrauliche Informationen oder Dokumente externer Organisationen "
|
68 |
+
"sowie sensible erhobene Daten (wie etwa Interviewtranskripte).", elem_id="header-text"
|
69 |
+
)
|
70 |
|
71 |
+
# inject auto-reload script
|
72 |
+
gr.HTML(
|
73 |
+
"""
|
74 |
+
<script>
|
75 |
+
// Reload the page after 1 minutes (300β000 ms)
|
76 |
+
setTimeout(() => {
|
77 |
+
window.location.reload();
|
78 |
+
}, 1000);
|
79 |
+
</script>
|
80 |
+
"""
|
81 |
+
)
|
82 |
+
gr.ChatInterface(
|
83 |
+
chat,
|
84 |
+
type="messages",
|
85 |
+
multimodal=MULTIMODAL,
|
86 |
+
editable=True,
|
87 |
+
concurrency_limit=20,
|
88 |
+
save_history=True,
|
89 |
+
)
|
90 |
|
91 |
|
92 |
|
93 |
+
demo.queue().launch(auth=AUTHS, share=True, ssr_mode=False)
|
94 |
|
95 |
+
# import gradio as gr
|
96 |
|
97 |
+
# # Replace this with your target URL
|
98 |
+
# NEW_PAGE_URL = "https://huggingface.co/spaces/evaluatorhub42/DEvalbot"
|
99 |
|
100 |
+
# with gr.Blocks(css_paths=["static/deval.css"], theme=gr.themes.Default(primary_hue="blue", secondary_hue="yellow")) as demo:
|
101 |
+
# with gr.Row():
|
102 |
+
# with gr.Column(scale=1):
|
103 |
+
# gr.Image(
|
104 |
+
# value="static/logo.png",
|
105 |
+
# height=50,
|
106 |
+
# show_label=False,
|
107 |
+
# interactive=False,
|
108 |
+
# show_download_button=False,
|
109 |
+
# show_fullscreen_button=False,
|
110 |
+
# elem_id="logo-primary",
|
111 |
+
# )
|
112 |
+
# with gr.Column(scale=10):
|
113 |
+
# pass # Empty space or title if needed
|
114 |
|
115 |
+
# with gr.Row():
|
116 |
+
# gr.HTML(
|
117 |
+
# f"""
|
118 |
+
# <div style="
|
119 |
+
# display: flex;
|
120 |
+
# justify-content: center;
|
121 |
+
# align-items: center;
|
122 |
+
# flex-direction: column;
|
123 |
+
# height: 60vh;
|
124 |
+
# text-align: center;
|
125 |
+
# color: white;
|
126 |
+
# font-size: 2rem;
|
127 |
+
# ">
|
128 |
+
# <p style="margin: 0.5em 0; color: white; font-weight: bold;">β οΈ DEvalBot has moved!</p>
|
129 |
+
# <p style="margin: 0.5em 0; color: white;">Please visit the new page:</p>
|
130 |
+
# <p style="margin: 0.5em 0;">
|
131 |
+
# <a href="{NEW_PAGE_URL}" style="color: white; font-weight: bold; text-decoration: underline;">
|
132 |
+
# {NEW_PAGE_URL}
|
133 |
+
# </a>
|
134 |
+
# </p>
|
135 |
+
# </div>
|
136 |
+
# """
|
137 |
+
# )
|
138 |
+
# demo.launch()
|