Spaces:
Sleeping
Sleeping
Download src/streamlit_app.py from fknuette/application: direct link, hf CLI and curl.
- Browser
- Download file 13.7 kB
-
https://huggingface.co/spaces/fknuette/application/resolve/main/src/streamlit_app.py
- Command line
-
hf download hf://spaces/fknuette/application/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/fknuette/application/resolve/main/src/streamlit_app.py
13.7 kB
| 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}) | |