Spaces:
Sleeping
Sleeping
File size: 13,726 Bytes
2426387 ad862a3 63af1c9 6a5a7d1 ecd844d ad862a3 0c1c0de ad862a3 63af1c9 f196612 63af1c9 478a1cf e557b47 478a1cf 1fe55bc 0c1c0de 1fe55bc c38c7c0 1fe55bc c38c7c0 1fe55bc c38c7c0 1fe55bc 43fb1a9 1fe55bc e557b47 f636a68 478a1cf 7454a06 daaeaa4 478a1cf e557b47 1fe55bc e557b47 1fe55bc 268a066 e557b47 7813c11 f996faf 7813c11 e557b47 f996faf 1fe55bc 43fb1a9 e557b47 1fe55bc 14a332c 1fe55bc acc3794 14a332c 1fe55bc 14a332c 1fe55bc 14a332c 1fe55bc 14a332c 1fe55bc e557b47 1fe55bc 101cff0 acc3794 101cff0 7454a06 7813c11 101cff0 f996faf 7813c11 f996faf 7813c11 6294913 e316288 6294913 7813c11 6294913 7813c11 6294913 7813c11 6294913 979b9d2 7813c11 6294913 7813c11 933330c 7813c11 1fe55bc 14a332c 1fe55bc e7571d3 1fe55bc ad862a3 1fe55bc 43fb1a9 e7571d3 43fb1a9 e7571d3 1fe55bc 43fb1a9 e7571d3 7454a06 e7571d3 7454a06 e7571d3 43fb1a9 7454a06 1fe55bc e7571d3 1fe55bc | 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 96 97 98 99 100 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | import os
import requests
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
API_URL = os.environ['MODAL_URL']
import streamlit as st
import time
import base64
# Function for converting the image
def get_base64_of_bin_file(path):
with open(path, "rb") as f:
return base64.b64encode(f.read()).decode()
# Little Password check
def check_password():
HF_PASSWORD = os.environ['PASSWORD']
def password_entered():
if st.session_state["password"] == HF_PASSWORD:
st.session_state["password_correct"] = True
del st.session_state["password"]
else:
st.session_state["password_correct"] = False
# When password is not entered
if "password_correct" not in st.session_state:
st.text_input(
"🔑 Please enter the password", type="password",
on_change=password_entered, key="password"
)
return False
# When password is wrong
elif not st.session_state["password_correct"]:
st.text_input(
"🔑 Please enter the password", type="password",
on_change=password_entered, key="password"
)
st.error("❌ Wrong password, please try again")
return False
# When password is correkt
else:
return True
# Path to Image
image_path = os.path.join(BASE_DIR, "img", "background.png")
bg_base64 = get_base64_of_bin_file(image_path)
# API-Key for Modal
headers = {
"x-api-key": os.environ['MODAL_TOKEN']
}
removing_header = """
<style>
[data-testid="stHeader"] {
display: none;
}
</style>
"""
st.markdown(removing_header, unsafe_allow_html=True)
if check_password():
# CSS einfügen
page_bg_img = f"""
<style>
[data-testid="stAppViewContainer"] {{
background-image: url("data:image/png;base64,{bg_base64}");
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}}
</style>
"""
st.markdown(page_bg_img, unsafe_allow_html=True)
if "confirmed" not in st.session_state:
st.session_state.splash = False
st.session_state.confirmed = False
if not st.session_state.splash:
st.set_page_config(page_title="A different kind of application", layout="wide")
splash_html = """
<div id="splash">
<div class="center">
<h1 style="color:white; font-size: 40px;">A different kind of application</h1>
<div class="loader"></div>
</div>
</div>
<style>
/* Splash Hintergrund */
#splash {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: black;
display: flex;
justify-content: center;
align-items: center;
z-index: 9999;
animation: fadeout 2s ease 3s forwards;
}
/* Text & Animation */
.center {
text-align: center;
}
.loader {
border: 8px solid #f3f3f3;
border-top: 8px solid #444;
border-radius: 50%;
width: 50px;
height: 50px;
margin: 20px auto;
animation: spin 2s linear infinite;
}
/* Animationen */
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
@keyframes fadeout {
to { opacity: 0; visibility: hidden; }
}
</style>
"""
st.markdown(splash_html, unsafe_allow_html=True)
min_loader_time = 5
start = time.time()
if not requests.get(API_URL + "/model_status", headers=headers).json()['status'] == "ready":
requests.post(API_URL + "/load_model", headers=headers)
elapsed = time.time() - start
if elapsed < min_loader_time:
time.sleep(min_loader_time - elapsed)
st.session_state.splash = True
st.rerun()
elif not st.session_state.confirmed:
st.markdown(
"""
<style>
.overlay {
position: relative;
top: 0; left: 0;
width: 100%; height: 100%;
display: flex;
justify-content: center;
align-items: center;
backdrop-filter: blur(6px);
z-index: 9998;
margin-top: -60px;
}
.overlay-box {
display: flex;
flex-direction: column;
justify-content: space-between;
max-width: 600px;
min-height: 300px;
padding: 30px;
color: #333;
border-radius: 20px;
box-shadow: 0 0 20px rgba(0,0,0,0.3);
text-align: center;
font-family: "Segoe UI", Roboto, sans-serif;
line-height: 1.6;
font-size: 16px;
background: rgba(255, 255, 255, 0.5);
backdrop-filter: blur(10px);
border: 1px solid rgba(255,255,255,0.6);
}
.overlay-box h2 {
font-size: 28px;
margin-bottom: 15px;
color: #222;
}
.overlay-box p {
margin: 10px 0;
color: #555;
}
ul {
list-style-type: none;
padding-left: 0;
}
ul li {
margin-bottom: 8px;
font-size: 15px;
}
[data-testid="stVerticalBlock"] {
-webkit-box-align: center;
align-items: center;
}
[data-testid="stButton"] button {
margin-top: 20px;
background: linear-gradient(90deg, #4facfe 0%, #00f2fe 100%);
color: white;
font-weight: bold;
border-radius: 30px;
padding: 10px 25px;
border: none;
box-shadow: 0 4px 10px rgba(0,0,0,0.2);
transition: transform 0.1s ease-in-out;
}
[data-testid="stButton"] button:hover {
transform: scale(1.05);
transform: scale(1.05);
box-shadow: 0 0 12px rgba(79,172,254,0.6);
}
</style>
<div class="overlay">
<div class="overlay-box">
<h2>Welcome 👋</h2>
<p>Hello, my name is Fernando. This app gives you an interactive way to get to know me better as an applicant.</p>
<p>Here’s a quick guide to get the most out of it:</p>
<ul style="text-align:center;">
<li>💡 Ask clear and specific questions about my personality</li>
<li>🎯 Provide context in your questions</li>
<li>🧪 Use my resume to ask relevant questions</li>
</ul>
<p>Examples of good vs. bad questions:</p>
<ul style="text-align:left;">
<li>✅ <i>“What is your name?”</i> → Clear and useful</li>
<li>❌ <i>“Tell name”</i> → Too vague</li>
</ul>
<p>Ready? Click the button below to start your first question.</p>
<p>PS: If my reply takes a moment, don’t worry — I’m just getting ready for you.</p>
</div>
</div>
""",
unsafe_allow_html=True
)
# Button bleibt jetzt sicher im Container
if st.button("Got it, let’s start 🚀", key="confirm_btn"):
st.session_state.confirmed = True
st.rerun()
else:
st.set_page_config(page_title="A different kind of application", layout="centered")
avatar_path = os.path.join(BASE_DIR, "img", "fernando.jpg")
if "history_render" not in st.session_state:
# two histories: one for rendering on client side, one for submitting to server
st.session_state.history_render = []
st.session_state.history_submit = []
if "fallback_counter" not in st.session_state:
st.session_state.fallback_counter = 0
if "last_response_time" not in st.session_state:
st.session_state.last_response_time = 0
st.markdown("""
<style>
[data-testid="stChatMessage"]:has([data-testid="stChatMessageAvatarUser"]) {
background-color: #e6f0ff;
color: #003366;
border-radius: 14px;
padding: 10px 14px;
margin: 8px 0;
max-width: 70%;
align-self: flex-end;
box-shadow: 0 2px 6px rgba(0,0,0,0.1);
}
[data-testid="stChatMessage"] {
background-color: #f9f9f9;
color: #222;
border-radius: 14px;
padding: 10px 14px;
margin: 8px 0;
max-width: 70%;
align-self: flex-start;
box-shadow: 0 2px 6px rgba(0,0,0,0.08);
}
[data-testid="stMarkdownContainer"] {
color: #444;
font-family: "Segoe UI", "Roboto", sans-serif;
}
</style>
""", unsafe_allow_html=True)
# Render Chat
for dic in st.session_state.history_render:
with st.chat_message("user"):
st.markdown(dic["q"])
with st.chat_message("assistant", avatar=avatar_path):
st.markdown(dic["a"])
st.markdown("""
<style>
[data-testid="stBottom"] > div {
background-color: white;
max-width: 70%;
min-width: 70%;
margin: 0 auto 20px;
border-radius: 40px 40px 40px 40px;
padding: 0px 0px -10px 0px;
box-shadow: 0 4px 12px rgba(0,0,0,0.08);
}
[data-testid="stBottomBlockContainer"] {
padding: 1rem 1rem 3rem;
}
[data-testid="stAppViewContainer"] {
color: black; /* Textfarbe ändern */
}
/* Chat Input Box */
[data-testid="stChatInput"] {
background-color: #f9f9f9;
border: 2px solid #888;
border-radius: 20px;
color: black;
font-size: 16px;
}
/* Placeholder Text */
[data-testid="stChatInput"] textarea::placeholder {
color: gray;
font-style: italic;
}
</style>
""", unsafe_allow_html=True)
# Get the Answer
user_input = st.chat_input("Write someting...")
if user_input:
answer = ""
with st.chat_message("user"):
st.markdown(user_input)
with st.chat_message("assistant", avatar=avatar_path):
messages = [
"⏳ Checking model status…",
"🔄 Initializing application…",
"📚 Loading memory…",
"🔐 Check security…",
"🌐 Connecting…",
"✨ Optimizing responses…",
"🧑🎓 Adapting to personality of Fernando…",
"🚀 Almost ready…"
]
try:
# Request to backend
i = 0
now = time.time()
elapsed = now - st.session_state.last_response_time
placeholder = st.empty()
# look after model status if
if elapsed > 45:
while True:
# some interactive placeholder
placeholder.markdown(messages[i % len(messages)])
status = requests.get(API_URL + "/model_status", headers=headers).json()['status']
if status == "not_initialized":
requests.post(API_URL + "/load_model", headers=headers)
elif status == "ready":
i = 0
break
i += 1
time.sleep(2)
placeholder.markdown("💬 Typing…")
response = requests.post(API_URL +"/generate", headers=headers, json={"message": user_input, "history": st.session_state.history_submit, "fallback_counter": st.session_state.fallback_counter}, timeout=120)
response.raise_for_status()
st.session_state.last_response_time = time.time()
# Get reply and show it
st.session_state.history_submit = response.json().get("history", [])
answer = response.json().get("reply", "")
placeholder.markdown(answer)
except Exception as e:
placeholder.error(f"Fehler: {e}")
st.session_state.history_render.append({"q": user_input, "a": answer})
|