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 = """ """ st.markdown(removing_header, unsafe_allow_html=True) if check_password(): # CSS einfügen page_bg_img = f""" """ 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 = """

A different kind of application

""" 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( """

Welcome 👋

Hello, my name is Fernando. This app gives you an interactive way to get to know me better as an applicant.

Here’s a quick guide to get the most out of it:

Examples of good vs. bad questions:

Ready? Click the button below to start your first question.

PS: If my reply takes a moment, don’t worry — I’m just getting ready for you.

""", 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(""" """, 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(""" """, 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})